using a function that is similar to polyfit but with two linear terms

Hi,
I am looking for a matlab function that is working similar to polyfit, but where I can use two different input function but instead of having just one linear term, I need two. At the moment the regression looks as follows:
y=b0+b1*x+error
and the code to compute R^2 is the following:
x= changePriceWithoutNaN;
y=changeFWithoutNaN;
p = polyfit(x,y,1);
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
yfit = polyval(p,x);
yfit = p(1) * x + p(2);
yresid = y - yfit;
SSresid = sum(yresid.^2);
SStotal = (length(y)-1) * var(y);
rsq_full = 1 - SSresid/SStotal
Instead of having just one linear term, the term I am looking for is the following:
y=b0+b1*x+b2*z+error
Is there anybody how knows a function that is solving a least squared optimazation in the way to coe above does? Importat is that I do not look for a quadratic solution and therefore from what I can see polyfit(x,y,2) is not an option

 Accepted Answer

Locks, it seems like you are interested in multiple linear regression. If you have the stats toolbox you can use the REGRESS function to do that. If you don't then you can use a simple '\' as follows:
x = dataT(:,2);
%is the implied volatility
y = dataT(:,10);
z = dataT(:,15);
p = [x z ones(length(dataT))]\y
p will have the 3 coeff you desire. We are essentially solving a linear system in a least square sense.

11 Comments

thanks!
I have the Financial Toolbox, but it seems to me that I have to regress function as well, although I have never bought the stats toolbox.
However, I tried to program it, but I always get an error, here is the code:
Y= changePriceWithoutNaN; %change option price
x1=changeFWithoutNaN;
x2=changeLastAdjustmentWithoutNaN
X= [ones(size(x1)) x1 x2];
p2=regress(X,Y)
X is 1804x3 double
Y is 1804x1 double
and the error message I get is:
Error using regress (line 62)
Y must be a vector and must have the same number of rows as X.
Error in regressionAnalysisSAD_fullsample (line 83)
p2=regress(X,Y)
I do not understand that, from what I can see they have the same numbers of rows
gives me an error message:
EDU>> X/Y
Error using /
Matrix dimensions must agree.
Locks,
1) Regress takes the response first and then the predictors. Here is the documentation for regress:
>> p=regress(Y,X)
2) Statistics Toolbox is required for Financial Toolbox, therefore you will have it already installed. You can check the output of 'VER' to verify it.
3) Use backslash '\' not forwardslash '/', they are different in MATLAB in what they do. http://www.mathworks.com/help/matlab/ref/mldivide.html
>> X\Y
4) You can also use LinearModel.fit function (recommended over regress) It does the same thing but has the added advantage of obtaining the fit statistics automatically: http://www.mathworks.com/help/stats/linearmodel.fit.html
>> X= [x1 x2];
>> LinearModel.fit(x,y) %Ones not required, automatically added.
thanks, this is working, I get the following result:
Number of observations: 1804, Error degrees of freedom: 1801 Root Mean Squared Error: 1.69 R-squared: 0.97, Adjusted R-Squared 0.97 F-statistic vs. constant model: 2.9e+04, p-value = 0
how can I show the r^2 and r^2 adjusted numbers with 4 fractional digits?
IS there a way to see the coefficients b0, b1 and b2 I am also looking for?
mdl = LinearModel.fit(x,y)
mdl.Coefficients.Estimate
If you are using the last approach. I recommend you read the documentation. This is all covered along with examples. If you are using earlier methods the estimates are the outputs.
mdl.Rsquared
I try to do it based on the dataset and for some reason I get this error message below, the code I am using is the following:
Y = changePriceWithoutNaN; %change option price
x1=changeFWithoutNaN;
x2=changeSADadjustmentWithoutNaN;
X= [x1 x2];
LinearModel.fit(X,Y)
mdl = LinearModel.fit(X,Y);
mdl.Coefficients.Estimate;
mdl;
mdl.Rsquared
the error message is the following:
Out of memory. Type HELP MEMORY for your options.
Error in classreg.regr.modelutils.model2terms (line 112)
linear = eye(nvars); linear = linear(whichVars,:);
Error in classreg.regr.LinearFormula (line 383)
f.terms = classreg.regr.modelutils.model2terms(alias,predVars,hasIntercept);
Error in classreg.regr.TermsRegression/createFormula (line 815)
formula =
classreg.regr.LinearFormula(modelDef,varNames,responseVar,intercept,link);
Error in LinearModel/createFormula (line 932)
formula = classreg.regr.TermsRegression.createFormula(supplied,modelDef, ...
Error in LinearModel.fit (line 908)
model.Formula = LinearModel.createFormula(supplied,modelDef,X, ...
Error in regressionAnalysisSAD_fullsample (line 98)
LinearModel.fit(X,Y)
I have no idea what I need to change in order to save the problem, do you see where the problem is?
I think we both see where the problem is. You are running out of memory and we have no idea how big X and Y are how much memory you have on the system, if its 32 bit or 64 bit etc. Are you able to use the regress approach?
>> p=regress(Y,X)
?
sorry, I made a mistake due to the fact that this comand is only working for columnvectors and the polyfoit worked also for rowvectors.
the only thing that is missing at the moment is that I would like to save the estimates, standarderror, tStat and pValue in seperate vectors or structures.
for the Rsquared it has worked, but for the other elements I haven't found anything in the documentation, do you have an idea?
the code I am using now is:
Y= changePriceWithoutNaN'; %change option price
x1=changeFWithoutNaN'; %change Futures times delta
x2=changeSADadjustmentWithoutNaN'; %change adjusted term
X= [x1 x2];
LinearModel.fit(X,Y)
mdl = LinearModel.fit(X,Y);
mdl.Coefficients.Estimate;
mdl;
RSQ=mdl.Rsquared
It is indeed in the documentation, please go through it closely. This will help you find things later yourself :
and the answer is already in your code, I encourage you to take a closer look.
Hint:
tstat = mdl.Coefficients.tStat
Once again, please go through the documentation, fundamentally, all properties can be accessed with the '.' dot notation from your model.
I had some strange structure because I only used mdl_SAD.Coefficients.Estimate;
and I was not able to get the values saved in there, but no it's clear, thanks

Sign in to comment.

More Answers (1)

4 Comments

thanks but from what I can see both are still quadratic, but I am looking only for a linear regression incl. least squared optimization in a way that the parameters b0, b1 and b2 are optimized, the linear equation is the following:
y=b0+x*b1+z*b2
I don't see the problem. Both FEX files handle two-dimensional polynomials of order N. There's no reason why can't choose N=1.
I am not fully sure if I get the description there right but there problem that I have is that there are two linear components which I need to cover. in addition I am not sure how excatly I need to do that. can I just download one of them in instead this code:
x= dataT(:,2);
%is the implied volatility
y=dataT(:,10);
p = polyfit(x,y,2)
using something like that:
x= dataT(:,2);
%is the implied volatility
y=dataT(:,10);
z=dataT(:,15);
p = polyfitn(x,y,z,3)
is that way the the z not quadratic? As said, the regression must stay linear, I do not want any quadratic components
As said, the regression must stay linear, I do not want any quadratic components
And as I keep telling you, if you want to have only linear terms, then tell that to polyfitn:
p = polyfitn([x,y],z,1);

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!