I am trying to calculate the first and second order moments for a portfolio of stocks (i.e. expected return and standard deviation).
expected_returns_annual
The shape of weights.T should be (,5) and not (5,),
suggests some confusion over the shape
attribute. shape
is an ordinary Python tuple, i.e. just a set of numbers, one for each dimension of the array. That's analogous to the size
of a MATLAB matrix.
(5,)
is just the way of displaying a 1 element tuple. The ,
is required because of older Python history of using ()
as a simple grouping.
In [22]: tuple([5])
Out[22]: (5,)
Thus the ,
in (5,)
does not have a special numpy
meaning, and
In [23]: (,5)
File "", line 1
(,5)
^
SyntaxError: invalid syntax
A key difference between numpy
and MATLAB is that arrays can have any number of dimensions (upto 32). MATLAB has a lower boundary of 2.
The result is that a 5 element numpy
array can have shapes (5,)
, (1,5)
, (5,1)
, (1,5,1)`, etc.
The handling of a 1d weight
array in your example is best explained the np.dot
documentation. Describing it as inner product
seems clear enough to me. But I'm also happy with the
sum product over the last axis of
a
and the second-to-last axis ofb
description, adjusted for the case where b
has only one axis.
(5,) with (5,n) => (n,) # 5 is the common dimension
(n,5) with (5,) => (n,)
(n,5) with (5,1) => (n,1)
In:
(x1,...,xn' * (R1,...,Rn)
are you missing a )
?
(x1,...,xn)' * (R1,...,Rn)
And the *
means matrix product? Not elementwise product (.*
in MATLAB)? (R1,...,Rn)
would have size (n,1). (x1,...,xn)'
size (1,n). The product (1,1)
.
By the way, that raises another difference. MATLAB expands dimensions to the right (n,1,1...). numpy
expands them to the left (1,1,n) (if needed by broadcasting). The initial dimensions are the outermost ones. That's not as critical a difference as the lower size 2 boundary, but shouldn't be ignored.