One factor model calibration
2 views (last 30 days)
Show older comments
How can I add more common factors in a One factor model?
I have implemented the code from here (https://it.mathworks.com/help/risk/examples/one-factor-model-calibration.html) . I tried to modify the "fitlm" arguments in the for loop adding another time series for a second common factor:
for i = 1:4
lm = fitlm(commonfactor1,commonfactor2,dependentVar(:,i));
w_regress_std(i) = lm.Coefficients{'x1','x2','Estimate'};
end
I get this error.
"Error using classreg.regr.TermsRegression.createFormula (line 758) The model definition must be a formula character vector, a terms matrix, a model alias, or a LINEARFORMULA."
Thank you
0 Comments
Answers (1)
Max Wyse
on 19 Dec 2019
Hi Giacomo,
It looks like you are trying to fit a linear regression model using two independent variables. I can adjust your for loop to make this work like so:
rng(1,'twister')
commonfactor1 = rand(200,1);
commonfactor2 = rand(200,1);
dependentVar = rand(200,4);
%this should work
for i=1:4
lm = fitlm([commonfactor1, commonfactor2],dependentVar(:,i));
Coeffs(i,:) = lm.Coefficients{{'x1','x2'},'Estimate'};
end
The changes I made to your code are as follows:
1.) I initialzed your variables (I don't have your data)
2.) I passed in all the dependent variables as a matrix, rather than each variable as a separate vector. This is the format expected by fitlm.
3.) I changed 'x1','x2' to {'x1','x2'}. The idea is that the first input in lm.Coefficients are the rows that are to be selected, the second input are the columns. As such, {'x1','x2'} both need to be given in the first input.
4.) I changed w_regress_std(i) to Coeffs(i,:). I did this for two reasons. The first is that you are now pulling out two coeffiecents instead of one, so I want the Coeffs output to be 2 dimensional. The second is that I am not sure what assumptions are going into the calculation of w_regress_std. All I have done is change this code to succesfully compute a multilinear regression, the mathematics in the example you link to may not hold in the 2 dimensional case (I haven't checked).
0 Comments
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!