2d arrayと1d arrayの掛け算

a=np.arange(9).reshape([3,3])
b=np.array([10,10,10])


a
Out[16]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
b
Out[18]: array([10, 10, 10])
np.dot(a,b)
Out[22]: array([ 30, 120, 210])

np.dot(b,a)
Out[23]: array([ 90, 120, 150])
c=np.arange(6).reshape([3,2])
d=np.array([10,10,10])

c
Out[25]: 
array([[0, 1],
       [2, 3],
       [4, 5]])

d
Out[27]: array([10, 10, 10])

np.dot(c,d)
Traceback (most recent call last):

  File "<ipython-input-28-e4e1007e7d01>", line 1, in <module>
    np.dot(c,d)

ValueError: shapes (3,2) and (3,) not aligned: 2 (dim 1) != 3 (dim 0)


np.dot(d,c)
Out[29]: array([60, 90])


csvを読むよりnumpy arrayをロードした方がだいぶ速い

大きなファイルだと、np.genfromtxtとnp.loadでスピードにかなり差が出る。

import numpy as np
import time
a=np.arange(10000).reshape([1000,10])

np.save('data_to_read_np',a)
np.savetxt('data_to_read_csv.csv',a,delimiter=',')

1000回ずつ読み込む。

rep = 1000

start = time.time()
for i in xrange(rep):
    b = np.genfromtxt('data_to_read_csv.csv', delimiter=',')
elapsed_time_csv =time.time() - start

start = time.time()
for i in xrange(rep):
    c = np.load('data_to_read_np.npy')
elapsed_time_np =time.time() - start

print('Read csv:'+str(elapsed_time_csv)+'sec')
print('Read np: '+str(elapsed_time_np)+'sec')

結果。

Read csv:10.2129998207sec
Read np: 0.53200006485sec

小さなファイルならそれほど差は出ない。

a=np.arange(1000).reshape([100,10])

結果

Read csv:1.47600007057sec
Read np: 0.424000024796sec

np.loadの方が、ファイルサイズから影響を受けづらくなっている。

Bonnet, C{\'e}line and Dubois, Pierre (2010) Inference on vertical contracts between manufacturers and retailers allowing for nonlinear pricing and resale price maintenance., The RAND Journal of Economics, 41(1), 139-164.

フランスのミネラルウォーター市場を例に、manufacturerとretailerの契約を構造推定した論文。manufacturerとretailerがそれぞれ競争的か談合か、vertical relationshipがdouble marginalizationかtwo-part tariffか、再販価格維持が行われているかなどでモデルを分けてそれぞれ推定、最後にRivers and Vuong (2002)でモデル選択をしている。

社会学と心理学の関係について触れられている論文二つ。著者は"The new institutionalism in organizational analysis"のDimaggioとPowell。

Dimaggio, Paul (1997) Culture and Cognition. Annual review of sociology, 23, 263-287.

心理学のスキーマ概念と社会学における文化との対応について。

Powell, Walter W and Colyvas, Jeannette A (2008) Microfoundations of Institutional Theory. The Sage handbook of organizational institutionalism

institutional theoryのmicrofoundationを明確にすることの必要性は二十年近く認識されてきたが、不思議と大きな進捗がないのが現状。有力なbuilding blockとして、Goffmanのinteraction rituals、Weickのsensemaking、社会心理学におけるlegitimation、ethnomethodologyなどが挙げられている。

Fan, Ying (2013) Ownership Consolidation and Product Characteristics: A Study of the US Daily Newspaper Market. American Economic Review, 103(5), 1598-1628

countyレベルの新聞のデータを使って、競争環境が紙面に与える影響を推定した論文。Steven Berryの学生である著者の博士論文を基にしたもの。

需要サイドは基本的にはよくあるBLPだが、特徴として -各消費者が二つまで商品(新聞)を購入できる -outside goodの効用が0でなくてタイムトレンド(tの一次関数)。 複数商品の購入に関しては、二つ目の商品の効用がκだけ割り引かれるという仮定が置かれている。紙面の内容によるsubstitutabilityやcomplimentarity(似通った内容の新聞を二つとっても仕方ない、など)は考慮されていない。

供給サイドは2ステージで、 -first stageで紙面の内容 -second stageで価格 を選択する。

counterfactualとして、ミネアポリスで法務省から許可が下りなかったために実現しなかった合併をシミュレートしている。合併が実現していた場合、地方記事の割合が10%低下し、およびconsumer surplusが328万ドル減少するとしている。

arrayの中から要素の位置をみつける

np.in1dを使うと、arrayの中から特定した要素と一致するindexを探せる。

a=np.array([0,0,0,1,1,2,2])
b=np.array([1,2])

np.in1d(a,b)
Out[51]: array([False, False, False,  True,  True,  True,  True], dtype=bool)