Skip to content

Instantly share code, notes, and snippets.

@drbenvincent
Created December 10, 2015 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbenvincent/745adfa06cc96925fc10 to your computer and use it in GitHub Desktop.
Save drbenvincent/745adfa06cc96925fc10 to your computer and use it in GitHub Desktop.
importance 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
N=20;
observed_data = normrnd(true_mean, true_sigma, [N 1]);
%% Do importance sampling
% create many samples for mean and sigma
N = 10^6;
mean_samples = (rand(N,1)-0.5)*5;
sigma_samples = rand(N, 1) * 10;
proposal = 1/N;
% evaluate likelihood for each (mean, sigma) sample
target = likelihood_func(observed_data, mean_samples, sigma_samples);
% calculate importance weight
w = target ./ proposal;
w = w ./ sum(w);
% resample, with replacement, according to importance weight
sample_ind = randsample([1:N],N,true,w);
mean_samples = mean_samples(sample_ind);
sigma_samples = sigma_samples(sample_ind);
%% plot
plot(mean_samples,sigma_samples,'.')
xlabel('mean')
ylabel('sigma')
hold on
plot(true_mean, true_sigma, 'r.','MarkerSize',5^2)
axis square
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment