This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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); |
NewerOlder