【发布时间】:2018-01-23 03:28:37
【问题描述】:
我正在尝试编写一个非常基本的平均似然估计函数,该函数依赖于优化工具箱的 fminsearch() 函数。我尝试将函数句柄作为输入传递并优化否定的总和。函数的日志。我的代码是:
function [ params, max ] = routine( fun )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
[filename,path] = uigetfile('*.'); #To grab the csv file location
name = strcat(path,filename);
data = csvread(name);
lh = @(x) sum(-log( fun(x) )); <-------- ERROR LINE
options = optimset('Display', 'off', 'MaxIter', 100000, 'TolX', 10^-20, 'TolFun', 10^-20);
[theta, max1] = fminsearch(lh, [0,1], options);
params = theta
max = max1
end
中间的行给了我这个错误:
Undefined function or variable 'data'.
Error in @(x)(1/(sqrt(2*pi)*x(2)))*exp((-(data-x(1)).^2)/(2*x(2)^2))
Error in @(x)sum(-log(fun(x)),data)
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
现在,在这段代码之外,它工作得非常好,足够令人恼火。
pan = @(x)sum(-log((1/(sqrt(2*pi)*x(2)))*exp((-(data-x(1)).^2)/(2*x(2)^2))));
options = optimset('Display', 'off', 'MaxIter', 100000, 'TolX', 10^-20, 'TolFun', 10^-20);
[theta, max1] = fminsearch(pan, [0,1], options);
出于某种原因,按照我在中间的方式转换等式会生成data var。无形的。我应该如何正确地将转换应用于我的方程,以便此代码有效?
【问题讨论】:
标签: matlab function optimization anonymous-function mle