diff options
| -rw-r--r-- | numpy/random/_generator.pyx | 23 | ||||
| -rw-r--r-- | numpy/random/mtrand.pyx | 23 |
2 files changed, 40 insertions, 6 deletions
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 8af9ca048..d1deeac05 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -3572,13 +3572,30 @@ cdef class Generator: >>> y.shape (3, 3, 2) - Here we generate a scatter plot of 800 samples from the bivariate - normal distribution with mean (0, 0) and covariance matrix - [[6, -3], [-3, 3.5]]. + 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') diff --git a/numpy/random/mtrand.pyx b/numpy/random/mtrand.pyx index d3724fcb5..fcae0a5ef 100644 --- a/numpy/random/mtrand.pyx +++ b/numpy/random/mtrand.pyx @@ -4079,13 +4079,30 @@ cdef class RandomState: >>> x.shape (3, 3, 2) - Here we generate a scatter plot of 800 samples from the bivariate - normal distribution with mean (0, 0) and covariance matrix - [[6, -3], [-3, 3.5]]. + 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') |
