Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Checking Bias of An Estimation Using Simulation | Estimation of Population Parameters
Advanced Probability Theory

book
Challenge: Checking Bias of An Estimation Using Simulation

In the last chapter, we covered the concepts of sample variance and adjusted sample variance. Now let's see how with the help of simulation, we can determine that the first estimation is biased and the second is unbiased.

We will use the Gaussian population: we will build an estimate of the sample variance and the adjusted sample variance on different subsets of the population. Next, using the law of large numbers, we will estimate the mean of the sample variance and the adjusted sample variance and compare it with the real variance of the population.

Uppgift

Swipe to start coding

Your task is to perform simulations to obtain the value of the sample variance, and the adjusted sample variance for 2000 different subsets of the population and compare the mean of the sample variance and the adjusted sample variance with the real value of the population mean:

  1. Use ddof=0 as an argument of np.var() method to calculate sample variance.
  2. Use ddof=1 as an argument of np.var() method to calculate the adjusted sample variance.
  3. Use .mean() method to estimate the expectation of sample variance.

Lösning

import numpy as np

# True population parameters
mu = 0
sigma = 2

# Sample size
n = 100

# Number of simulations
num_sims = 2000

# Initialize array to store sample variances
sample_variances = np.zeros(num_sims)
adjusted_variances = np.zeros(num_sims)

# Repeat simulation many times
for i in range(num_sims):
# Draw random samples from Gaussian population
samples = np.random.normal(mu, sigma, n)
# Calculate sample variances
# `ddof = 0` means that we calculate samples using standard formula
sample_variances[i] = np.var(samples, ddof=0)
# `ddof = 1` means that we use corrected sample variance
adjusted_variances[i]= np.var(samples, ddof=1)

# Calculate mean of sample variances and compare to population variance
# Due to the law of large numbers mean of sample variances estimation converges to real expectation value
mean_sample_variance = np.mean(sample_variances)
mean_adjusted_variance = np.mean(adjusted_variances)
population_variance = sigma**2
print('Abs difference between real variance and mean sample variance: ', np.abs(population_variance - mean_sample_variance))
print('Abs difference between real variance mean adjusted variance: ', np.abs(population_variance - mean_adjusted_variance))

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 5
single

single

import numpy as np

# True population parameters
mu = 0
sigma = 2

# Sample size
n = 100

# Number of simulations
num_sims = 2000

# Initialize array to store sample variances
sample_variances = np.zeros(num_sims)
adjusted_variances = np.zeros(num_sims)

# Repeat simulation many times
for i in range(num_sims):
# Draw random samples from Gaussian population
samples = np.random.normal(mu, sigma, n)
# Calculate sample variances
# `ddof = 0` means that we calculate samples using standard formula
sample_variances[i] = np.var(samples, ddof=___)
# `ddof = 1` means that we use corrected sample variance
adjusted_variances[i]= np.var(samples, ddof=___)

# Calculate mean of sample variances and compare to population variance
# Due to the law of large numbers mean of sample variances estimation converges to real expectation value
mean_sample_variance = np.___(sample_variances)
mean_adjusted_variance = np.mean(adjusted_variances)
population_variance = sigma**2
print('Abs difference between real variance and mean sample variance: ', np.abs(population_variance - mean_sample_variance))
print('Abs difference between real variance mean adjusted variance: ', np.abs(population_variance - mean_adjusted_variance))

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt