Jump to content

File:Sample mean and variance of IID samples from a standard Cauchy distribution..png

Page contents not supported in other languages.
This is a file from the Wikimedia Commons
From Wikipedia, the free encyclopedia

Original file (1,589 × 490 pixels, file size: 115 KB, MIME type: image/png)

Summary

Description
English: sample mean and variance of IID samples from a standard Cauchy distribution.

```python import numpy as np import matplotlib.pyplot as plt

def running_sample_variance(data):

   """
   Welford's online algorithm to calculate the variance.
   In each iteration, the mean and M2 (the sum of the squares of differences 
   from the mean) are updated. 
   For each column i, the difference delta between the current data point and 
   the previous mean is calculated, and the mean and M2 are updated accordingly.
   variance = M2 / np.arange(1, m + 1).
   """
   n, m = data.shape
   mean = np.zeros((n, m))
   M2 = np.zeros((n, m))
   
   for i in range(m):
       if i == 0:
           delta = data[:, i] - mean[:, i]
           mean[:, i] = mean[:, i] + delta / (i + 1)
           M2[:, i] = delta * (data[:, i] - mean[:, i])
       else:
           delta = data[:, i] - mean[:, i - 1]
           mean[:, i] = mean[:, i - 1] + delta / (i + 1)
           M2[:, i] = M2[:, i - 1] + delta * (data[:, i] - mean[:, i])
   variance_n = M2 / (np.arange(1, m + 1))
   return variance_n

def running_sample_average(data):

   cumsum = np.cumsum(data, axis=1)
   cum_n = np.arange(1, data.shape[1] + 1)
   return cumsum / cum_n
  1. Parameters

n = 10 # Number of rows m = 500000 # Number of columns

  1. Generate Cauchy-distributed variables

data = np.random.standard_cauchy(size=(n, m))

variances = running_sample_variance(data) mean = running_sample_average(data)

  1. Plot the variances

fig, (ax2, ax1) = plt.subplots(1, 2, figsize=(16, 5))

  1. Plot the variances in the first subplot

x = np.arange(1, variances.shape[1] + 1) ax1.semilogy(variances.T, linewidth=1) ax1.set_xlabel('Number of Samples') ax1.set_ylabel('Variance') ax1.set_title('Running Sample Variances')

  1. Plot the running sample averages in the second subplot

ax2.semilogy(np.abs(mean.T), linewidth=1) ax2.set_xlabel('Number of Samples') ax2.set_ylabel('Average (absolute value)') ax2.set_title('Running Sample Averages')

  1. Adjust the layout and display the plot

plt.tight_layout() plt.show()

```
Date
Source Own work
Author Cosmia Nebula

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

2 June 2023

image/png

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current21:17, 2 June 2023Thumbnail for version as of 21:17, 2 June 20231,589 × 490 (115 KB)Cosmia NebulaUploaded while editing "Cauchy distribution" on en.wikipedia.org

The following page uses this file:

Metadata