# Chapter 14: Probability, Simulation, and Statistical Inference
# Fundamentals of Python for Financial Econometrics - Probability, Simulation, and Statistical Inference
# Dataset: Ceteris Lab teaching sample

# Probability, Simulation, and Statistical Inference
#
# **Opening question:** How can uncertainty be represented, simulated, and summarized without pretending that one sample is the population?

# %% Cell 2
import numpy as np

rng = np.random.default_rng(1401)
draws = rng.normal(0, 1, 100_000)
probability = np.mean(draws > 1.96)
print(round(probability, 4))

# **Interpretation check:** Interpretation. The estimate is close to the theoretical upper-tail probability of 0.025, with small Monte Carlo error.

# %% Cell 4
import numpy as np
from scipy import stats

sample = np.array([2.1, 2.4, 2.0, 2.7, 2.3, 2.5])
mean = sample.mean()
se = stats.sem(sample)
interval = stats.t.interval(0.95, df=len(sample)-1, loc=mean, scale=se)
print(round(mean, 3), tuple(round(v, 3) for v in interval))

# **Interpretation check:** Interpretation. The interval is wider than a normal-based interval because the small sample uses a Student-t critical value.

# %% Cell 6
import numpy as np
rng = np.random.default_rng(1401)
draws = rng.normal(0, 1, 100_000)
probability = np.mean(draws > 1.96)
print(round(probability, 4))
import numpy as np

# Verified source output
#
# ```text
# 0.0249
# ```
#
# ```text
# 2.333 (np.float64(2.062), np.float64(2.604))
# ```
