Search code examples
python-3.xpandasbroadcasting

pandas: how to broadcast multiply by vector * matrix * vector, columns-wise


python 3.5.2; pandas 0.23.4; numpy 1.15.4; on windows

I am trying to find an efficient way to do pandas multiplication vector by matrix by vector, for example:

np.random.seed(43)    
w_ = np.random.uniform(size=(3,5))
# the vector w
w = pd.DataFrame(w_/w_.sum(axis=0), index=['a', 'b', 'c'])
# the matrix cov
cov = pd.DataFrame(np.cov(np.random.randn(3,100)), index=r.index, columns=r.index)

Calculate: eq for each columns of w, I use:

r = [w.iloc[:,i].T.dot(cov.dot(w.iloc[:, i])) for i in range(w.shape[1])] 

gives:

[0.5073635209626383, 0.3262776109704286, 0.45469128089985883, 0.5226072271864488, 0.35602577932396257]

This is fine, however I am looking for a more efficient and elegant way of doing this other than by list comprehension or by a lambda function.


Solution

  • You can use np.diag:

    In [11]: np.diag(w.T.dot(cov.dot(w)))
    Out[11]: array([0.50736352, 0.32627761, 0.45469128, 0.52260723, 0.35602578])
    
    In [12]: r
    Out[12]:
    [0.5073635209626383, 0.32627761097042857, 0.45469128089985883,
     0.5226072271864487, 0.3560257793239626]