Skip to content

Instantly share code, notes, and snippets.

@sromano
Forked from sgromano/codigo.m
Last active August 29, 2015 14:21
Show Gist options
  • Save sromano/0f0715b6966415540fcd to your computer and use it in GitHub Desktop.
Save sromano/0f0715b6966415540fcd to your computer and use it in GitHub Desktop.
%% Prior and Posterior Predictive, Second Example
clear;
%% Data
k = zeros(4,3);
k(1,:) = [0 0 0];
k(2,:) = [1 0 1];
k(3,:) = [1 4 6];
k(4,:) = [2 3 6];
n = [2 4 6]
%% Sampling
% MCMC Parameters
nchains = 2; % How Many Chains?
nburnin = 1e2; % How Many Burn-in Samples?
nsamples = 1e4; %How Many Recorded Samples?
nthin = 1; % How Often is a Sample Recorded?
doparallel = 0; % Parallel Option
% Assign Matlab Variables to the Observed Nodes
datastruct = struct('k',k,'n',n);
% Initialize Unobserved Variables
for i=1:nchains
S.theta = [0.5 0.5 0.5 0.5]; % Intial Value
S.alpha = 1;
init0(i) = S;
end
% Use JAGS to Sample
tic
fprintf( 'Running JAGS ...\n' );
[samples, stats] = matjags( ...
datastruct, ...
fullfile(pwd, 'modelo.txt'), ...
init0, ...
'doparallel' , doparallel, ...
'nchains', nchains,...
'nburnin', nburnin,...
'nsamples', nsamples, ...
'thin', nthin, ...
'monitorparams', {'theta','alpha'}, ...
'savejagsoutput' , 1 , ...
'verbosity' , 1 , ...
'cleanup' , 0 , ...
'workingdir' , 'tmpjags' );
toc
% Plot Posterior
stats.mean.theta
stats.mean.alpha
model {
alpha ~ dunif(0.01,3)
for (i in 1:4){
theta[i] ~ dbeta(alpha,alpha)
for (j in 1:3){
k[i,j] ~ dbin(theta[i],n[j])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment