In [1]:
import sys
sys.path.append('../..')
import PyMB
from PyMB import magic # enable %%PyMB cell magic
import numpy as np
See the TMB tutorial for more information on writing custom models
In [2]:
%%PyMB LinearRegression
// DATA
DATA_VECTOR(Y);
DATA_VECTOR(x);
// PARAMETERS
PARAMETER(alpha);
PARAMETER(Beta);
PARAMETER(logSigma);
// MODEL
vector<Type> Y_hat = alpha + Beta*x;
REPORT(Y_hat);
Type nll = -sum(dnorm(Y, Y_hat, exp(logSigma), true));
return nll;
In [3]:
LinearRegression.data = {
'x': np.arange(10),
'Y': np.random.normal(np.arange(10))
}
In [4]:
LinearRegression.init = {
'alpha': 0.,
'Beta': 0.,
'logSigma': 0.
}
The model likelihood will be integrated wrt the random parameters. See here for more information.
In [5]:
LinearRegression.optimize(random=['alpha','Beta'])
In [6]:
print(LinearRegression.report('Y_hat'))
In [7]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.DataFrame({ k: v['draws'][0] for k,v in LinearRegression.parameters.iteritems() })
g = sns.PairGrid(df, diag_sharey=False)
g.map_lower(sns.kdeplot, cmap='Blues_d')
g.map_upper(plt.scatter)
g.map_diag(sns.kdeplot, lw=3)