怎样利用matlab 里的 fitsvensson fit nelsonsiegel ,的 图形做出参数估计 呢 , 我在我那个上搜到3 个某国程序人编的 对这个 有帮组吗,欢迎高人来指点 , 我是新手
下面是3 个 程序
function[y] = nelsonfun(x,par)
% NELSONFUN Evaluate Nelson-Siegel function
% Inputs x - n*1 or 1*n vector
% par - structure with fields 'beta' (3*1 or 1*3 vector), 'tau' (scalar)
% Outputs y - n*1 vector
% Example par.beta = [.05 .1 .5]
% par.tau = 1
% x = [.125 .25 .5 1 2 3 5 7 10 20 30]
% y = nelsonfun(x,par)
% Author Dimitri Shvorob, dimitri.shvorob@vanderbilt.edu, 12/30/07
checkfield('beta',3)
checkfield('tau' ,1)
i = x(:)/par.tau;
j = 1-exp(-i);
y = par.beta(1) + par.beta(2)*j./i + par.beta(3)*((j./i)+j-1);
function checkfield(name,n)
if ~isfield(par,name)
error('Field "%s" not found in parameter structure',name)
else
if ~isvector(par.(name)) || numel(par.(name)) ~= n
error('Field "%s" must have %d elements',name,n)
end
end
end
end
2
function[par] = nelsonfit(x,y)
% NELSONFIT Fit Nelson-Siegel function
% Inputs x,y - n*1 or 1*n vectors
% Outputs par - structure with fields 'beta' (3*1 vector), 'tau' (scalar)
% Notes No non-negativity constraints are imposed on betas; taus are
% searched over by FMINBND within range (0,10).
% Example x = [.125 .25 .5 1 2 3 5 7 10 20 30]
% y = [2.57 3.18 3.45 3.34 3.12 3.13 3.52 3.77 4.11 4.56 4.51]
% par = nelsonfit(x,y)
% Author Dimitri Shvorob, dimitri.shvorob@vanderbilt.edu, 12/30/07
par.tau = fminbnd(@(tau) nelsonsse(tau),0,10);
par.beta = lsbetas(par.tau);
function[f] = nelsonsse(tau)
[b,f] = lsbetas(tau); %#ok
end
function[b,varargout] = lsbetas(tau)
i = x(:)/tau;
j = 1-exp(-i);
n = length(x);
z = [ones(n,1) j./i (j./i)+j-1];
b = (z'*z)\(z'*y(:));
e = y(:) - z*b;
varargout(1) = {e'*e};
end
end
3
function[par] = nelsonfit(x,y)
% NELSONFIT Fit Nelson-Siegel function
% Inputs x,y - n*1 or 1*n vectors
% Outputs par - structure with fields 'beta' (3*1 vector), 'tau' (scalar)
% Notes No non-negativity constraints are imposed on betas; taus are
% searched over by FMINBND within range (0,10).
% Example x = [.125 .25 .5 1 2 3 5 7 10 20 30]
% y = [2.57 3.18 3.45 3.34 3.12 3.13 3.52 3.77 4.11 4.56 4.51]
% par = nelsonfit(x,y)
% Author Dimitri Shvorob, dimitri.shvorob@vanderbilt.edu, 12/30/07
par.tau = fminbnd(@(tau) nelsonsse(tau),0,10);
par.beta = lsbetas(par.tau);
function[f] = nelsonsse(tau)
[b,f] = lsbetas(tau); %#ok
end
function[b,varargout] = lsbetas(tau)
i = x(:)/tau;
j = 1-exp(-i);
n = length(x);
z = [ones(n,1) j./i (j./i)+j-1];
b = (z'*z)\(z'*y(:));
e = y(:) - z*b;
varargout(1) = {e'*e};
end