Skip to content

Instantly share code, notes, and snippets.

@starovoitovs
Created April 30, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starovoitovs/f40e389a819cb3187bacd8eda0397406 to your computer and use it in GitHub Desktop.
Save starovoitovs/f40e389a819cb3187bacd8eda0397406 to your computer and use it in GitHub Desktop.
from random import random
from scipy import mean
from math import log, pow
import matplotlib.pyplot as plt
def simulate_single(n):
sample = [random() - 0.5 for _ in range(n)]
mx = mean(sample)
tx = 0.5 * (max(sample) + min(sample))
return pow(mx, 2), pow(tx, 2)
def simulate_estimator(n, K):
errors = [simulate_single(n) for _ in range(K)]
total_mx2 = total_tx2 = 0
for mx2, tx2 in errors:
total_mx2 += mx2 / K
total_tx2 += tx2 / K
return total_mx2, total_tx2
print("mean errors squared:")
print(simulate_estimator(100, 1000))
k = 6
errs = [simulate_estimator(10 ** k, 1000) for k in range(k)]
mxs = [log(x) / log(10) for x, _ in errs]
txs = [log(x) / log(10) for _, x in errs]
plt.plot(range(k), mxs)
plt.plot(range(k), txs)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment