Sunday, January 27, 2013

MCMC

Doing MCMC is fun! Thanks to KnuthClass
aa
% probability step
if x(n) < 0
    px = 3/5;
else
    px = 2/5;
end


% Metropolis Hastings MCMC

p = min([1, p(y)/p(x(n))]);
r = rand;

n = n+1;
if r < p 
    x(n) = y;
else
    x(n) = x(n-1);
end


%%

% do this 100 times

x(1) = 1;   % start in the low prob region
px(1) = 2/5;

for n = 1:10000
    
    % take a step
    y = (-1)*x(n);
    
    % probability step
    if y < 0
        py = 3/5;
    else
     py = 2/5;
    end
    
    % Metropolis Hastings MCMC
    p = min([1, py/px(n)]);
    r = rand;

    %n = n+1;
    if r < p 
        x(n+1) = y;
        px(n+1) = py;
    else
        x(n+1) = x(n);
        px(n+1) = px(n);
    end
    
    x(n)
    
end
    

%%
clear

% Gaussian probability density with zero mean and unit variance
prob = @(z)((sqrt(2*pi))^(-1)*exp(-z^2/2));

x(1) = 5*(2*rand-1);
px(1) = prob(x);

for n = 1:100000
    step = 0.01*randn;
    y = x(n)+step;
    
    py = prob(y);
    
    p = min([1,py/px(n)]);
    r = rand;

    if r < p 
        x(n+1) = y;
        px(n+1) = py;
    else
        x(n+1) = x(n);
        px(n+1) = px(n);
    end
end

 aa

Wednesday, January 23, 2013

Blogger Trick hide the post/widget in main page

Well, I wanted to replace some of the widgets to invisible mode in nabinkm.com. Actually, wanted for long time...
<b:if cond='data:blog.pageType != "index"'>

the widget goes here...

</b:if>

Another trick is: replacing
]]></b:skin>


 by


]]></b:skin>
<b:if cond="data:blog.url == data:blog.homepageUrl">
<style type="text/css">
.post,   #blog-pager {display:none;}
</style>
</b:if>
<b:if cond="data:blog.url == data:blog.homepageUrl">
<style type="text/css">
body#layout .sidebar {display:inline;margin-top:200px;}
</style>
</b:if>
-->