don't understand matrix-multiplication should be reversed in python?
Hi all, I've come around a webpage with python-tutorial/description for obtaining something and I'll solve this: R = p^T w where R is a vector and p^T is the transpose of another vector. ... p is a Nx1 column vector, so p^T turns into a 1xN row vector which can be multiplied with the Nx1 weight (column) vector w to give a scalar result. This is equivalent to the dot product used in the code. Keep in mind that Python has a reversed definition of rows and columns and the accurate NumPy version of the previous equation would be R = w * p.T ... (source: http://blog.quantopian.com/markowitz-portfolio-optimization-2/ ) I don't understand this: "Keep in mind that Python has a reversed definition of rows and columns and the accurate NumPy version of the previous equation would be R = w * p.T" Not true for numpy, is it? This page: http://mathesaurus.sourceforge.net/matlab-numpy.html says it python and matlab looks quite similar... Anyone could please explain or elaborate on exactly this (quote): "Keep in mind that Python has a reversed definition of rows and columns"??? That I don't understand - thank you for any hints/guidance/explanations/ideas/suggestions etc! -- https://mail.python.org/mailman/listinfo/python-list
Re: don't understand matrix-multiplication should be reversed in python?
On Thursday, 12 November 2015 17:35:39 UTC+1, Ian wrote: > On Thu, Nov 12, 2015 at 8:57 AM, PythonDude wrote: > > Hi all, > > Anyone could please explain or elaborate on exactly this (quote): "Keep in > > mind that Python has a reversed definition of rows and columns"??? > > > > That I don't understand - thank you for any > > hints/guidance/explanations/ideas/suggestions etc! > > py> import numpy > py> p = numpy.reshape(range(5), (5,1)) > py> p > array([[0], >[1], >[2], >[3], >[4]]) > py> p.T > array([[0, 1, 2, 3, 4]]) > py> p.dot(p.T) > array([[ 0, 0, 0, 0, 0], >[ 0, 1, 2, 3, 4], >[ 0, 2, 4, 6, 8], >[ 0, 3, 6, 9, 12], >[ 0, 4, 8, 12, 16]]) > py> p.T.dot(p) > array([[30]]) > py> m = numpy.asmatrix(p) > py> m > matrix([[0], > [1], > [2], > [3], > [4]]) > py> m.T > matrix([[0, 1, 2, 3, 4]]) > py> m * m.T > matrix([[ 0, 0, 0, 0, 0], > [ 0, 1, 2, 3, 4], > [ 0, 2, 4, 6, 8], > [ 0, 3, 6, 9, 12], > [ 0, 4, 8, 12, 16]]) > py> m.T * m > matrix([[30]]) > > Yeah, I don't know what that person is talking about either. It looks > correct to me. Thank you very much, Ian - just had to be sure about this - I would also be very disappointed in Python, if this author was right about this non-intuitive interpretation of how to do matrix multiplication :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: don't understand matrix-multiplication should be reversed in python?
On Thursday, 12 November 2015 22:57:21 UTC+1, Robert Kern wrote: > On 2015-11-12 15:57, PythonDude wrote: > > Hi all, > > > > I've come around a webpage with python-tutorial/description for obtaining > > something and I'll solve this: > > > > R = p^T w > > > > where R is a vector and p^T is the transpose of another vector. > > > > ... > > p is a Nx1 column vector, so p^T turns into a 1xN row vector which can be > > multiplied with the > > Nx1 weight (column) vector w to give a scalar result. This is equivalent to > > the dot > > product used in the code. Keep in mind that Python has a reversed > > definition of > > rows and columns and the accurate NumPy version of the previous equation > > would > > be R = w * p.T > > ... > > > > (source: http://blog.quantopian.com/markowitz-portfolio-optimization-2/ ) > > > > I don't understand this: "Keep in mind that Python has a reversed > > definition of > > rows and columns and the accurate NumPy version of the previous equation > > would > > be R = w * p.T" > > > > Not true for numpy, is it? This page: > > http://mathesaurus.sourceforge.net/matlab-numpy.html says it python and > > matlab looks quite similar... > > > > Anyone could please explain or elaborate on exactly this (quote): "Keep in > > mind that Python has a reversed definition of rows and columns"??? > > He's wrong, simply put. There is no "reversed definition of rows and > columns". Great, thank... > He simply instantiated the two vectors as row-vectors instead of > column-vectors, > which he could have easily done, so he had to flip the matrix expression. Thank you very much Robert - I just had to be sure about it :-) -- https://mail.python.org/mailman/listinfo/python-list
numpy column_stack - why does this work?
Hi all, Just a quick question about this code-piece (it works, I've tested it): means, stds = np.column_stack([ getMuSigma_from_PF(return_vec) for _ in xrange(n_portfolios) ]) 1) I understand column_stack does this (assembles vectors vertically, side-by-side): >>> a = np.array((1,2,3)) # NB: a is row-vector: {1 2 3} >>> b = np.array((2,3,4)) # NB: b is also a row-vector... >>> np.column_stack((a,b)) array([[1, 2], [2, 3], [3, 4]]) 2) I understand the underscore is just a "dummy variable" in the last line "for _ in xrange(n_portfolios)" - this also looked a bit confusing to me, at first... 3) I DON'T understand why the code doesn't look like this: means, stds = np.column_stack([ for _ in xrange(n_portfolios): getMuSigma_from_PF(return_vec) ]) ??? Any comments/advice/hints, I would appreciate from you, thank you! -- https://mail.python.org/mailman/listinfo/python-list
Re: numpy column_stack - why does this work?
On Friday, 13 November 2015 18:17:59 UTC+1, Ian wrote: > On Fri, Nov 13, 2015 at 8:37 AM, PythonDude wrote: > > 3) I DON'T understand why the code doesn't look like this: > > > > means, stds = np.column_stack([ > > for _ in xrange(n_portfolios): > > getMuSigma_from_PF(return_vec) ]) > > Because that would be invalid syntax; you can't put a for loop inside > an expression like that. Your question is not about numpy.column_stack > at all, but about list comprehensions. I suggest you start by reading > this: > > https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions > > Then if you're still confused, come back and ask further questions. Thank you very much, I'll look careful into that before asking again :-) -- https://mail.python.org/mailman/listinfo/python-list