全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 MATLAB等数学软件专版
4787 8
2009-09-14
悬赏 5 个论坛币 未解决
function [re1,re2]=minvar(x,y)
% calculate the optimal coefficient by minimize the variance between x and
% c*y. That is var(x-c*y).
% Input parameters:
%       x: the raw data
%       y: the benchmark for optimization
% Output parameters:
%     re1: the R^2 (first column)
%          the parameters c (rest of every row)
%     re2: the ESS
% x should be a array or a matrix
% y should be a matrix,the form is either(1,b1,b2...),or(b1,b2...)
% x and y should have the same dimension
[m,n]=size(y);
[a,b]=size(x);
lb = zeros(1,n);        % Set lower bounds
if y(:,1) == ones(m,1)  % if b is (1,b1,b2...), the corresponding lower bound is empty
    lb(1,1)=-inf;
end
ub = [ ];               % No upper bounds
Aeq= ones(1,n);         % Set equality constrain
if y(:,1) == ones(m,1)  % if b is (1,b1,b2...), the Aeq is (0,1,1...)
    Aeq(1,1)=0;
end
beq=1;
options = optimset('LargeScale','off');
mm1 = zeros(b,n+1);
mm2 = zeros(a,b);
for i =1:b
    k = x(:,i);
    [v,fval] =  lsqlin(y,k,[ ],[ ],Aeq,beq,lb,ub);
    mm1(i,2:n+1) = v';          % calculate the optimal coefficient c
    tssk=sum((k-mean(k)).^2);
    essk=sum((k-y*v).^2);  
    mm1(i,1)=1-essk/tssk;    % calculate the R^2 value
    mm2(:,i)=(k-y*v).^2;              % calculate the ESS
end
re1 = mm1;
re2 = mm2;



为什么运行后出现: Error: Function definitions are not permitted at the prompt or in scripts.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2009-9-15 17:09:25
不可以在脚本窗运行,要在editor里。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2009-9-17 22:09:36
真是太感谢楼主了 无私奉献
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2010-5-11 12:25:04
editor里具体怎么操作啊,请楼主说具体点啊 ?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2010-5-11 14:24:15
函数定义要单独放在一个文件里
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2010-5-11 17:54:17
if y(:,1) == ones(m,1)  % if b is (1,b1,b2...), the corresponding lower bound is empty
    lb(1,1)=-inf;
end

本文来自: 人大经济论坛 详细出处参考:http://www.pinggu.org/bbs/viewth ... &from^^uid=418684
循环里的 -inf 是什么?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群