amazon

1.10.14

Python ftest


Here is an Excel-like ftest of python. (two-sided test)
import numpy as np
import scipy.stats as stats
def ftest(x1, x2):
    dfn = len(x1)-1
    dfd = len(x2)-1
    F = np.var(x1) / np.var(x2)
    f_pf = stats.f.cdf(F, dfn, dfd)
    return min(f_pf, 1 - f_pf)*2

29.8.14

Python ScipyでExcelのftest


ScipyにはExcelのftestやRのvar.testのように単純に等分散かどうかのp値を返す関数がない。
python用ftestを作ってみた。因みに両側検定。

import numpy as np
import scipy.stats as stats
def ftest(x1, x2):
    dfn = len(x1)-1
    dfd = len(x2)-1
    F = np.var(x1) / np.var(x2)
    f_pf = stats.f.cdf(F, dfn, dfd)
    return min(f_pf, 1 - f_pf)*2

Simple Excel-like f-test in python using scipy


Python Scipy does not have Excel-like f-test of equality of variances, which simply returns p-value of it.
Here provides a simple f-test function.

import numpy as np
import scipy.stats as stats
def ftest(x1, x2):
    dfn = len(x1)-1
    dfd = len(x2)-1
    F = np.var(x1) / np.var(x2)
    f_pf = stats.f.cdf(F, dfn, dfd)
    return min(f_pf, 1 - f_pf)*2

Note this is two-sided model as implemented in Excel.

5.8.14

Macでanacondaのipythonを直接呼び出す

AnacondaのLaunhcher.appから起動したIPython notebookでは、matplotlibのpylab.plotがブラウザ内に表示されない。
plt.show()を呼べば表示されるが、別ウインドウに描画されるので美しくない。

そこで、Anaconda内のipythonバイナリを直接叩く。
/anaconda/bin/ipython notebook --pylab=inline
オプション--pylab=inlineを付けると、グラフがブラウザ内に表示される。

Plot a graph inline in IPython notebook of Anaconda on Mac

I could not show a graph of matplotlib pylab.plot inline with ipython notebook, unless using plt.show(), which however showed an separated "aqua" window.

Therefore, I typed on the terminal,
/anaconda/bin/ipython notebook --pylab=inline
, instead of launching notebook with anaconda Launcher.app.

8.5.14

バイナリファイルの呼び出し元を変更

ここここで書いたが、ターミナルでバイナリ名を叩いた時に呼び出されるファイルは、

$ type [バイナリ名]

$ which [バイナリ名]

で表示できる。

例えば、

$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

$ type python
python is /Library/Frameworks/Python.framework/Versions/2.7/bin/python

また、-aオプションを付けるとインストールされている一覧が表示される

$ which -a python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/Library/Frameworks/Python.framework/Versions/2.6/bin/python
/usr/local/bin/python
/usr/bin/python
/opt/local/bin/python

$ type -a python
python is /Library/Frameworks/Python.framework/Versions/2.7/bin/python
python is /Library/Frameworks/Python.framework/Versions/2.6/bin/python
python is /usr/local/bin/python
python is /usr/bin/python
python is /opt/local/bin/python

バイナリ名を叩いた時に呼び出されるファイルはこの一覧の一番上にくるファイルである。

この順序を変更すればひも付けを変更できる。


ところで、-aオプションで表示される順序はパスの順序であるので、パスの順序を変更すればひも付けも変更する

パスの一覧を表示するには、

$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin/python:/Library/Frameworks/Python.framework/Versions/2.6/bin/python/usr/local/bin:/usr/bin:/opt/local/bin

例えば、pythonと叩いてpython2.6を呼び出したいのならば、

$ export PATH= /Library/Frameworks/Python.framework/Versions/2.6/bin/python:/Library/Frameworks/Python.framework/Versions/2.7/bin/python/usr/local/bin:/usr/bin:/opt/local/bin

とすればよい。

パスの設定方法はググってをみてください。


上記の例ではpythonですが他のバイナリでも同じです。perlなんかも複数インストールしていて呼び出し元を変更したい人がいるのではないでしょうか。

5.5.14

AVD(Android Virtual Device) マネージャーで開始を押してもエミュレーターが起動しない

最近のADTはSDKが同梱されいていてすぐに開発ができ便利である。
最新のものをダウンロードしてAndroid Virutal Device Mangerでエミュレーターを起動しようとした。
ところが開始を押してシークバーが完了しても反応がない。
どうやら内部で呼ばるemulatorというバイナリが正しく選ばれていないようである。

/sdk/toolsにある"emulator"を"emulator.notused"等と適当にリネームして、"emulator64-(avdのCPU/ABI)"を"emulator"に変えたら動作した。

Not starting emulator in AVD (Android Virtual Device) Manager

After created an avd with Android Virutal Device Manger recently downloaded I couldn't start and launch an emulator. Nothing would be happened.
I figured out an solution as did it with a command line.
It said Bus error.

Here is a solution.
Rename a binary "emulator64-(avd's CPU/ABI)" found in /sdk/tools to "emulator", where old "emulator" may be renamed like "emulator.notused".


16.4.14

Set field values at once in MATLAB

When setting values in a field of a struct array, I met errors.

First of all using the for statement:

>> a=struct()
a =
1x1 struct array with no fields.

>> for ii = 1:3
a(ii).field1 = ii;
end
>> a.field1
ans =
   1
ans =
   2
ans =
   3

Ok so for

Then I tried it with one line like:

>> a.field2 = 1:3

??? Incorrect number of right hand side elements in dot name assignment.
Missing [] around left hand side is a likely cause.


>> [a.field2] = 1:3

??? Error using ==> colon
Too many output arguments.

The solution is here, to use deal function:
>> [a.field2] = deal(1,2,3)
a =
1x3 struct array with fields:
  field1
  field2
>> a.field2
ans =
   1

ans =
   2

ans =
   3

Just in case, make sure to use comma to separate values in right hand side otherwise whole values would be set to each struct.
>> [a.field2] = deal(1:3)
a =
1x3 struct array with fields:
  field1
  field2

>> a.field2
ans =
   1   2   3

ans =
   1   2   3

ans =
   1   2   3