ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 04 Apr 2020 12:37:51 +0200Covariancehttps://ask.sagemath.org/question/50472/covariance/I do not find if there is a command to evaluate the covariance between two vectors. Should I construct it by myself ?Thu, 02 Apr 2020 21:06:16 +0200https://ask.sagemath.org/question/50472/covariance/Answer by Sébastien for <p>I do not find if there is a command to evaluate the covariance between two vectors. Should I construct it by myself ?</p>
https://ask.sagemath.org/question/50472/covariance/?answer=50473#post-id-50473You 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](https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html).Thu, 02 Apr 2020 21:40:35 +0200https://ask.sagemath.org/question/50472/covariance/?answer=50473#post-id-50473Comment by Cyrille for <p>You can use numpy to compute the covariance of two list of numbers:</p>
<pre><code>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]])
</code></pre>
<p>The numpy array can be converted back into a SageMath matrix:</p>
<pre><code>sage: matrix(_)
[ 0.09306006427535807 -0.04053014267978353]
[-0.04053014267978353 0.10647864456311205]
</code></pre>
<p>More usage examples can be found in the <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html">numpy doc</a>.</p>
https://ask.sagemath.org/question/50472/covariance/?comment=50489#post-id-50489Thanks a lot. But that is to say that I must also learn Numpy in using Sagemath.Fri, 03 Apr 2020 15:54:44 +0200https://ask.sagemath.org/question/50472/covariance/?comment=50489#post-id-50489Comment by Sébastien for <p>You can use numpy to compute the covariance of two list of numbers:</p>
<pre><code>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]])
</code></pre>
<p>The numpy array can be converted back into a SageMath matrix:</p>
<pre><code>sage: matrix(_)
[ 0.09306006427535807 -0.04053014267978353]
[-0.04053014267978353 0.10647864456311205]
</code></pre>
<p>More usage examples can be found in the <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.cov.html">numpy doc</a>.</p>
https://ask.sagemath.org/question/50472/covariance/?comment=50506#post-id-50506Indeed, 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](https://numpy.org/), [pandas](https://pandas.pydata.org/), [matplotlib](https://matplotlib.org/), etc.Sat, 04 Apr 2020 12:37:51 +0200https://ask.sagemath.org/question/50472/covariance/?comment=50506#post-id-50506