Example of numpy cov() not correct?
Hi, all,
I am trying out the example here (http://www.scipy.org/Numpy_Example_List_With_Doc#cov)
from numpy import *
...
T = array([1.3, 4.5, 2.8, 3.9]) P = array([2.7, 8.7, 4.7, 8.2]) cov(T,P)
The answer is supposed to be 3.9541666666666657
The result I got is instead a cov matrix array([[ 1.97583333, 3.95416667], [ 3.95416667, 8.22916667]]) So, I just wanna confirm this particular example may be no longer correct.
I am using python 2.4.3 with numpy 1.1.0 on MS win
Cheers, Anthony
NOTICE This email and any attachments are confidential and may contain copyright material of Macquarie Group Limited or third parties. If you are not the intended recipient of this email you should not read, print, retransmit, store or act in reliance on this email or any attachments, and should destroy all copies of them. Macquarie Group Limited does not guarantee the integrity of any emails or any attached files. The views or opinions expressed are the author's own and may not reflect the views or opinions of Macquarie Group Limited.
On Tue, Jul 29, 2008 at 9:10 PM, Anthony Kong Anthony.Kong@macquarie.com wrote:
I am trying out the example here (http://www.scipy.org/Numpy_Example_List_With_Doc#cov)
from numpy import *
...
T = array([1.3, 4.5, 2.8, 3.9]) P = array([2.7, 8.7, 4.7, 8.2]) cov(T,P)
The answer is supposed to be 3.9541666666666657
The result I got is instead a cov matrix array([[ 1.97583333, 3.95416667], [ 3.95416667, 8.22916667]]) So, I just wanna confirm this particular example may be no longer correct.
I am using python 2.4.3 with numpy 1.1.0 on MS win
It works for me (1.1 on GNU/Linux):
import numpy as np T = np.array([1.3, 4.5, 2.8, 3.9]) P = np.array([2.7, 8.7, 4.7, 8.2]) np.cov(T,P)
array([[ 1.97583333, 3.95416667], [ 3.95416667, 8.22916667]])'
If you read the cov function documentation you'll see that if a second vector is given, it joins the 2 into one matrix and calculate the covariance of it. In your case, you are looking for the offdiagonal elements.
Nadav.
הודעה מקורית מאת: numpydiscussionbounces@scipy.org בשם Keith Goodman נשלח: ד 30יולי08 17:04 אל: Discussion of Numerical Python נושא: Re: [Numpydiscussion] Example of numpy cov() not correct?
On Tue, Jul 29, 2008 at 9:10 PM, Anthony Kong Anthony.Kong@macquarie.com wrote:
I am trying out the example here (http://www.scipy.org/Numpy_Example_List_With_Doc#cov)
from numpy import *
...
T = array([1.3, 4.5, 2.8, 3.9]) P = array([2.7, 8.7, 4.7, 8.2]) cov(T,P)
The answer is supposed to be 3.9541666666666657
The result I got is instead a cov matrix array([[ 1.97583333, 3.95416667], [ 3.95416667, 8.22916667]]) So, I just wanna confirm this particular example may be no longer correct.
I am using python 2.4.3 with numpy 1.1.0 on MS win
It works for me (1.1 on GNU/Linux):
import numpy as np T = np.array([1.3, 4.5, 2.8, 3.9]) P = np.array([2.7, 8.7, 4.7, 8.2]) np.cov(T,P)
array([[ 1.97583333, 3.95416667], [ 3.95416667, 8.22916667]])' _______________________________________________ Numpydiscussion mailing list Numpydiscussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpydiscussion
Wed, 30 Jul 2008 18:49:10 +0300, Nadav Horesh wrote:
If you read the cov function documentation you'll see that if a second vector is given, it joins the 2 into one matrix and calculate the covariance of it. In your case, you are looking for the offdiagonal elements.
So the final answer to the OP's question is:
Yes, the example on
http://www.scipy.org/Numpy_Example_List_With_Doc#cov
is wrong; cov(T,P) indeed returns a matrix. And it would be nice if someone fixed this, you can simply register a wiki account and fix the problem.
On Thu, Jul 31, 2008 at 1:14 AM, Pauli Virtanen pav@iki.fi wrote:
Yes, the example on
http://www.scipy.org/Numpy_Example_List_With_Doc#cov
is wrong; cov(T,P) indeed returns a matrix. And it would be nice if someone fixed this, you can simply register a wiki account and fix the problem.
Done.
participants (4)

Anthony Kong

Keith Goodman

Nadav Horesh

Pauli Virtanen