diff options
| author | Matti Picus <matti.picus@gmail.com> | 2021-12-24 09:59:41 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-24 09:59:41 +0200 |
| commit | 138963bd61e4d6e82261ae8c8db98394b69fe3f5 (patch) | |
| tree | f28a33442711a7827198ef85225ba433c5815b47 | |
| parent | d0248e91fba772ab31ca092d841fd65ab628abca (diff) | |
| parent | aa8b117c81355b434c7666896c4c00f1d7b6432f (diff) | |
| download | numpy-138963bd61e4d6e82261ae8c8db98394b69fe3f5.tar.gz | |
Merge pull request #20380 from WarrenWeckesser/doc-fix-mvn-example
DOC: random: Fix a comment and example in the multivariate_normal docstring
| -rw-r--r-- | numpy/random/_generator.pyx | 33 | ||||
| -rw-r--r-- | numpy/random/mtrand.pyx | 33 |
2 files changed, 56 insertions, 10 deletions
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 5dc761af9..d7c1879e7 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -3574,12 +3574,35 @@ cdef class Generator: >>> y.shape (3, 3, 2) - The following is probably true, given that 0.6 is roughly twice the - standard deviation: - - >>> list((x[0,0,:] - mean) < 0.6) - [True, True] # random + Here we generate 800 samples from the bivariate normal distribution + with mean [0, 0] and covariance matrix [[6, -3], [-3, 3.5]]. The + expected variances of the first and second components of the sample + are 6 and 3.5, respectively, and the expected correlation + coefficient is -3/sqrt(6*3.5) ≈ -0.65465. + + >>> cov = np.array([[6, -3], [-3, 3.5]]) + >>> pts = rng.multivariate_normal([0, 0], cov, size=800) + + Check that the mean, covariance, and correlation coefficient of the + sample are close to the expected values: + + >>> pts.mean(axis=0) + array([ 0.0326911 , -0.01280782]) # may vary + >>> np.cov(pts.T) + array([[ 5.96202397, -2.85602287], + [-2.85602287, 3.47613949]]) # may vary + >>> np.corrcoef(pts.T)[0, 1] + -0.6273591314603949 # may vary + + We can visualize this data with a scatter plot. The orientation + of the point cloud illustrates the negative correlation of the + components of this sample. + >>> import matplotlib.pyplot as plt + >>> plt.plot(pts[:, 0], pts[:, 1], '.', alpha=0.5) + >>> plt.axis('equal') + >>> plt.grid() + >>> plt.show() """ if method not in {'eigh', 'svd', 'cholesky'}: raise ValueError( diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index d8f7c500f..ae3f37b07 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -4079,12 +4079,35 @@ cdef class RandomState: >>> x.shape (3, 3, 2) - The following is probably true, given that 0.6 is roughly twice the - standard deviation: - - >>> list((x[0,0,:] - mean) < 0.6) - [True, True] # random + Here we generate 800 samples from the bivariate normal distribution + with mean [0, 0] and covariance matrix [[6, -3], [-3, 3.5]]. The + expected variances of the first and second components of the sample + are 6 and 3.5, respectively, and the expected correlation + coefficient is -3/sqrt(6*3.5) ≈ -0.65465. + + >>> cov = np.array([[6, -3], [-3, 3.5]]) + >>> pts = np.random.multivariate_normal([0, 0], cov, size=800) + + Check that the mean, covariance, and correlation coefficient of the + sample are close to the expected values: + + >>> pts.mean(axis=0) + array([ 0.0326911 , -0.01280782]) # may vary + >>> np.cov(pts.T) + array([[ 5.96202397, -2.85602287], + [-2.85602287, 3.47613949]]) # may vary + >>> np.corrcoef(pts.T)[0, 1] + -0.6273591314603949 # may vary + + We can visualize this data with a scatter plot. The orientation + of the point cloud illustrates the negative correlation of the + components of this sample. + >>> import matplotlib.pyplot as plt + >>> plt.plot(pts[:, 0], pts[:, 1], '.', alpha=0.5) + >>> plt.axis('equal') + >>> plt.grid() + >>> plt.show() """ from numpy.linalg import svd |
