<jupyter_start><jupyter_text>Machine Learning Abductive Loop Goals of this Procedure1. Instantiate linear model.2. Attempt to update parameters.3. Recommend new experiments to run...<jupyter_code>import numpy as np

from scipy.stats import invgamma, norm, multivariate_normal
from matplotlib import pyplot as plt
%matplotlib inline
B = np.array([[2.,1.]]).T                  # Contrived
x = np.array([np.linspace(0,1,5000)]).T    # Inputs from data
x = np.c_[x,np.ones([len(x),1])]
y = np.matrix(norm.rvs(np.dot(x,B),.05))    # Outputs from data

plt.plot(x[:,0],y)
plt.plot(x[:,0],np.dot(x,B))
n = float(len(y)) ## import from data
p = float(x.shape[1])   ## import from data

inv = np.linalg.inv(np.dot(x.T,x)) 
P = np.dot(np.dot(x,inv),x.T)

a = (n-p)/2.
s2 = (1./(n-p))*np.dot(np.dot(y.T,np.eye(P.shape[0])-P), y)
scale = ((n-p) * s2 / 2.)

print s2
sigma2hat = invgamma.rvs(a,scale=scale,size=10000)
Bhat = np.dot(inv,np.dot(x.T,y))
print Bhat
_ = plt.hist(sigma2hat,500,normed=True)
phi = np.linspace(sigma2hat.min(),sigma2hat.max(),100)
_ = plt.plot(phi,invgamma.pdf(phi,a,scale=scale).T)
bsamples = []

for sigma2 in sigma2hat:
    bhat = multivariate_normal.rvs(mean = np.ravel(Bhat), cov = np.dot(inv,sigma2))
    bsamples.append(bhat)
    
bsamples = np.array(bsamples)
_ = plt.hist(bsamples[:,0], 1000, normed = True)
_ = plt.hist(bsamples[:,1], 1000, normed = True)
Y_hat = []
for b,s in zip(bsamples,sigma2hat):
    y_hat = norm.rvs(np.dot(x,b),s)
    Y_hat.append(y_hat)
Y_hat = np.array(Y_hat)
_ = plt.hist(Y_hat[:,450],100)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19044585987261148, 'fraction_numerical': 0.0375796178343949, 'mean_word_length': 4.454861111111111, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '385456', 'license': 'MIT', 'n_tokens_mistral': 713, 'n_tokens_neox': 677, 'n_words': 121, 'path': 'notebooks/scratch-dev-files/abductive-loop-simulation.ipynb', 'repo_name': 'NGS2Montage/montage-abloop', 'size': '1570'}