1.已知对数似然函数,利用fminunc函数搜索极值。【1】
function [ll] = neg_loglike(theta,Y,X) %定义neg_loglike.m方程
mu = X*theta(1:2);
ll = -1*sum(log(normpdf(Y,mu,theta(3)))); %fminunc默认求最小值,调整
end
%% 假设每次观测独立,代入f(观测|参数)数据。
data = csvread('/tmp/data.csv');
X = data(:,[3 1]);
Y = data(:,2);
% Optimization设置,详见help
opt = optimset('GradObj','off',...
'Hessian','off',...
'LargeScale','off',...
'Display','off',...
'MaxIter', 100000,...
'MaxFunEvals', 1000000, ...
'TolFun', 1.000e-006,...
'TolX', 1.000e-006,...
'FunValCheck','off',...
'DerivativeCheck','off',...
'Diagnostics','off',...
'Algorithm','trust-region-reflective');
% 设置初始值,求解
theta_start=.5*ones(size(X,2)+1,1);
[theta,fval,flag,out,grad,hess] = fminunc(@(theta) neg_loglike(theta,Y,X),theta_start, opt);
se = sqrt(diag(inv(hess)));
ans = [theta se]
2.已知对数似然函数,利用fminsearch函数搜索极值。【2】
相似度,matlab代码见参考文献【2】.
function ml = t_var(param)
rng default;
data = random('norm',0,1,[400,1]);
z = (data - param(1)) ./ param(2);
L = -.5.*z.*z - log(sqrt(2.*pi).*param(2));
ml = -sum(numel(data).*L);
end
x0 =[0 0]
[x,fval] = fminsearch(@t_var,x0,optimset('TolX',1e-19,'MaxFunEvals',1000,'MaxIter',1000))
3利用遗传算法求取似然函数的极值
matlab自带的遗传算法。
待续,方法发生了变化。
参考文献
【1】https://rlhick.people.wm.edu/posts/estimating-custom-mle.html
【2】https://www.656463.com/wenda/yMATLABbxzdsrgjsf_187