<jupyter_start><jupyter_text>Chapter 4 - Inferences with Gaussians 4.1 Inferring a mean and standard deviationInferring the mean and variance of a Gaussian distribution. $$ \mu \sim \text{Gaussian}(0, .001)  $$$$ \sigma \sim \text{Uniform} (0, 10)  $$$$ x_{i} \sim \text{Gaussian} (\mu, \frac{1}{\sigma^2})  $$<jupyter_code># Data
x = np.array([1.1, 1.9, 2.3, 1.8])
n = len(x)
    
with pm.Model() as model1:
    # prior
    mu = pm.Normal('mu', mu=0, tau=.001)
    sigma = pm.Uniform('sigma', lower=0, upper=10)
    # observed
    xi = pm.Normal('xi',mu=mu, tau=1/(sigma**2), observed=x)
    # inference
    trace = pm.sample(1e3, cores=2)

pm.traceplot(trace[50:]);
y = trace['mu']
x = trace['sigma']
sns.jointplot(x, y, kind='scatter', stat_func=None, alpha=.5);

print('The mu estimation is: %.3f' % y.mean())
print('The sigma estimation is: %.3f' % x.mean())<jupyter_output>The mu estimation is: 1.739
The sigma estimation is: 0.935<jupyter_text>Note from <NAME>There are might be divergence warning (Uniform prior on sigma is not a good idea in general), which you can further visualize below<jupyter_code># display the total number and percentage of divergent
divergent = trace['diverging']
print('Number of Divergent %d' % divergent.nonzero()[0].size)
divperc = divergent.nonzero()[0].size/len(trace)
print('Percentage of Divergent %.5f' % divperc)

# scatter plot for the identifcation of the problematic neighborhoods in parameter space
plt.figure(figsize=(6, 6))
plt.scatter(x[divergent == 0], y[divergent == 0], color='r', alpha=.05)
plt.scatter(x[divergent == 1], y[divergent == 1], color='g', alpha=.5);<jupyter_output>Number of Divergent 0
Percentage of Divergent 0.00000<jupyter_text>4.2 The seven scientistsThe model:$$ \mu \sim \text{Gaussian}(0, .001)  $$$$ \lambda_{i} \sim \text{Gamma} (.001, .001)  $$$$ \sigma = 1/{\sqrt\lambda_{i}} $$  $$ x_{i} \sim \text{Gaussian} (\mu, \lambda_{i})  $$The mean is the same for all seven scientists, while the standard deviations are different<jupyter_code># data
x = np.array([-27.020, 3.570, 8.191, 9.898, 9.603, 9.945, 10.056])
n = len(x)

with pm.Model() as model2: 
    # prior
    mu = pm.Normal('mu', mu=0, tau=.001)
    lambda1 = pm.Gamma('lambda1', alpha=.01, beta=.01, shape=n)
    # sigma = pm.Deterministic('sigma',1 / sqrt(lambda1))
    # observed
    xi = pm.Normal('xi', mu=mu, tau=lambda1, observed=x)

    # inference
    trace2 = pm.sample(1000, cores=2)

pm.traceplot(trace2);
pm.summary(trace2)<jupyter_output>Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [lambda1_log__, mu]
100%|██████████| 1500/1500 [00:05<00:00, 495.41it/s]<jupyter_text>4.3 Repeated measurement of IQThe model:$$ \mu_{i} \sim \text{Uniform}(0, 300)  $$$$ \sigma \sim \text{Uniform} (0, 100)  $$$$ x_{ij} \sim \text{Gaussian} (\mu_{i}, \frac{1}{\sigma^2})  $$Data Come From Gaussians With Different Means But Common Precision<jupyter_code># Data
y = np.array([[90, 95, 100], [105, 110, 115], [150, 155, 160]])
ntest = 3
nsbj = 3

import sys
eps = sys.float_info.epsilon

with pm.Model() as model3:
    # mu_i ~ Uniform(0, 300)
    # notices the shape here need to be properly 
    # initualized to have the right repeated measure
    mui = pm.Uniform('mui', 0, 300, shape=(nsbj, 1))

    # sg ~ Uniform(0, 100)
    # sg = pm.Uniform('sg', .0, 100)
    
    # It is more stable to use a Gamma prior
    lambda1 = pm.Gamma('lambda1', alpha=.01, beta=.01)
    sg = pm.Deterministic('sg',1 / np.sqrt(lambda1))
    
    # y ~ Normal(mu_i, sg)
    yd = pm.Normal('y', mu=mui, sd=sg, observed=y)
    
    trace3 = pm.sample(1000, cores=2)
pm.traceplot(trace3);
pm.summary(trace3)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17421602787456447, 'fraction_numerical': 0.061645671401768964, 'mean_word_length': 4.161825726141079, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '647329', 'license': 'MIT', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1377, 'n_words': 382, 'path': 'BCM/ParameterEstimation/Gaussian.ipynb', 'repo_name': 'vbartle/resources', 'size': '3736'}