# QuantFinanceToGo, module 16: Monte Carlo Simulation. Exercise 1 (easy).
# Runs on its own with Python 3 and numpy. Fill in the lines marked TODO.
#
# Throw two dice M times and estimate the chance that their sum is at least
# 10. The exact chance is 6/36 = 1/6: (4,6), (5,5), (6,4), (5,6), (6,5), (6,6).
# a) Write share_and_se(flags): the share of True values and its standard
#    error sqrt(p (1 - p) / M), where M is the number of flags.
# b) Run it for M = 100, 10,000 and 1,000,000 throws.
# c) How far is each estimate from 1/6, counted in standard errors?
import math
import numpy as np

rng = np.random.default_rng(1)                  # fixed seed

def share_and_se(flags):
    # TODO: return (share, standard error) as two floats
    return None

if share_and_se(np.array([True, False])) is None:
    print("Exercise 1 is not solved yet. Fill in the lines marked TODO.")
else:
    for m in (100, 10000, 1000000):
        dice = rng.integers(1, 7, size=(m, 2))      # whole numbers 1 to 6
        share, se = share_and_se(dice.sum(axis=1) >= 10)
        print(f"M = {m:>9,}:  estimate {share:.4%}   SE {se:.4%}   "
              f"off by {(share - 1 / 6) / se:+.2f} SE")
