Ask Your Question
1

Covariance

asked 2020-04-02 21:06:16 +0100

Cyrille gravatar image

I do not find if there is a command to evaluate the covariance between two vectors. Should I construct it by myself ?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-04-02 21:40:35 +0100

Sébastien gravatar image

updated 2020-04-02 21:41:07 +0100

You can use numpy to compute the covariance of two list of numbers:

sage: x = [random() for _ in range(10)]
sage: y = [random() for _ in range(10)]
sage: import numpy as np
sage: np.cov(x,y)
array([[ 0.09306006, -0.04053014],
       [-0.04053014,  0.10647864]])

The numpy array can be converted back into a SageMath matrix:

sage: matrix(_)
[ 0.09306006427535807 -0.04053014267978353]
[-0.04053014267978353  0.10647864456311205]

More usage examples can be found in the numpy doc.

edit flag offensive delete link more

Comments

Thanks a lot. But that is to say that I must also learn Numpy in using Sagemath.

Cyrille gravatar imageCyrille ( 2020-04-03 15:54:44 +0100 )edit

Indeed, doing search_src('covariance') seems to confirm there is nothing in the sage library to do that. I think as SageMath users, it is not a bad thing that we learn how to use python libraries such as numpy, pandas, matplotlib, etc.

Sébastien gravatar imageSébastien ( 2020-04-04 12:37:51 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-04-02 21:06:16 +0100

Seen: 943 times

Last updated: Apr 02 '20