diff options
Diffstat (limited to 'numpy')
| -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 |
