Skip to content

Instantly share code, notes, and snippets.

View drbenvincent's full-sized avatar

Benjamin T. Vincent drbenvincent

View GitHub Profile
@drbenvincent
drbenvincent / importanceSamplingDemo1.m
Created December 10, 2015 20:52
quick example of importance sampling
%% true probability distribution
true_func = @(x) betapdf(x,1+1,1+10);
%% Do importance sampling
N = 10^6;
% uniform proposal distribution
x_samples = rand(N,1);
proposal = 1/N;
% evaluate for each sample
target = true_func(x_samples);
@drbenvincent
drbenvincent / rejectionSamplingDemo2.m
Last active December 10, 2015 15:24
rejection sampling demo - Matlab - parameter estimation
true_mean = 0;
true_sigma = 1;
% likelihood_func = @(x, mean, sigma) normpdf(x, mean, sigma);
% the above function to calcalate in matrix form, for speed
likelihood_func = @(x, mean, sigma)...
prod(normpdf(repmat(x,[1 numel(mean)]),...
repmat(mean, [1 numel(x)])',...
repmat(sigma,[1 numel(x)])' ), 1);
%% generate data
@drbenvincent
drbenvincent / rejectionSamplingDemo.m
Last active May 26, 2018 11:41
rejection sampling demo - Matlab
%% true probability distribution
true_func = @(x) betapdf(x,1+1,1+10);
%% Do rejection sampling
% create many samples on interval 0-1
x_samples = rand(10^6,1);
% evaluate for each sample
sample_value = true_func(x_samples);
% accept in proportion to highest
max_value = max(sample_value);