sum of coefficients less than 1

Can someone please modify the following code so that it has a constraint that the sum of ar1 and ar2 is less than one.
Thanks
clc;
clear;
m=2;
a0 = 0.1; a1 = 0.2; a2=0.15;
epsi=zeros(3000,1);
simsig=zeros(3000,1);
for i = 1:3000
if (i==1)
simsig(i) = a0/(1-a1-a2) ;
s=(simsig(i))^0.5;
epsi(i) = normrnd(0,1) * s;
elseif (i==2)
epsi(i)=epsi(1);
else
simsig(i) = a0+ a1*(epsi(i-1))^2+a2*(epsi(i-2))^2;
s=(simsig(i))^0.5;
epsi(i) = normrnd(0,1) * s;
end
end
yt1=epsi.^2;
ytlast=epsi(3000)^2;
ytlast2=epsi(2999)^2;
ytinitial=yt1(2001:3000);
yt = yt1(2003:3000);
ytlag1= yt1(2002:2999);
ytlag2= yt1(2001:2998);
a =ones(1000-m,1);
x = [a,ytlag1,ytlag2];
opts = optimset('lsqnonneg');
opts.LargeScale = 'off';
opts.Display = 'none';
coef = lsqnonneg(x,yt);
alpha0 =coef(1);
ar1=coef(2);
ar2= coef(3);

 Akzeptierte Antwort

Shashank Prasanna
Shashank Prasanna am 3 Mär. 2013

1 Stimme

Do you have the optimization toolbox? LSQNONNEG cannot handle constraints.
Also, please format your code when you post. It makes it easier to read code. You can select your code and use the '{}Code' button.

9 Kommentare

dav
dav am 3 Mär. 2013
Bearbeitet: dav am 3 Mär. 2013
I do have optimization tool box.
Do you mean I have to do the whole thing again without using lsqnonneg?
I need to fit a regression with the constraints that the parameters are positive and their sum is less than one.
Since I am new to matlab it took a long time to figure this out and do the code as given in the above code( without sum less than one part)
Is their an easier way to do this?
Thanks.
Shashank Prasanna
Shashank Prasanna am 3 Mär. 2013
Bearbeitet: Shashank Prasanna am 3 Mär. 2013
Use LSQLIN which allows for constraints.
x = lsqlin(x,yt,[],[],[],[],[0;0;0],[1;1;1])
dav
dav am 4 Mär. 2013
Bearbeitet: dav am 4 Mär. 2013
Thanks,
I used the your code. The program ran and i got comparatively good answers. However, It generated the following message. Can you help me correct it? and is it some thing to worry about?
Optimization terminated: relative function value changing by less than OPTIONS.TolFun.
Walter Roberson
Walter Roberson am 5 Mär. 2013
That is normal.
Matt J
Matt J am 5 Mär. 2013
Bearbeitet: Matt J am 5 Mär. 2013
It is nothing to worry about, as long as the solutions you're getting are good.
dav
dav am 5 Mär. 2013
Bearbeitet: dav am 5 Mär. 2013
thank you very much. is there a way to stop printing that message? because im running this in a simulation.
Also x = lsqlin(x,yt,[],[],[],[],[0;0;0],[1;1;1]) lets the coefficients to be between 0 and 1 right? but I have another constraint that ar1+ar2<1. Does this code take care of that too?
Walter Roberson
Walter Roberson am 5 Mär. 2013
Give a try to passing in an options structure with Display set 'off'
To make coeff(2) + coeff(3) < 1, you can call LSQLIN like this:
coeff = lsqlin(x,yt,[0 1 1],[1],[],[],[0;0;0],[1;1;1])
See the help for LSQLIN
dav
dav am 5 Mär. 2013
Thanks alot everyone!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by