How to use Monte Carlo Simulation in a linear regression?

5 views (last 30 days)
I have a linear regression model with a lagged dependet variable: y_new=beta1+beta2*x+u
n=4; %sample size
u=normrnd(0,1,[1,n]); %iid errors
y_0=1; %start values
beta1=1;
beta2=0.5;
y = zeros(1,n);
yt=y_0;
%real value of y
for i=1:n;
y_real=beta1+beta2*yt;
y(i) = y_real;
yt=y_real1;
end
%estimated y including the error term
y_new=y+u
x=[y_0,y_new(1:end-1)] %lagged explanatory variable
[beta1_hat, beta2_hat]=regression(y_new,x) %regression to estimate beta1 and beta2
solution1=beta1_hat-beta1 %estimation errors
solution2=beta2_hat-beta2
I have to do 1000 Monte Carlo replications (for n=4). How do I code that?

Answers (1)

Prajit T R
Prajit T R on 5 Feb 2018
Edited: Prajit T R on 5 Feb 2018
Hey try this approach. I'm not sure if this is the result you were looking for, but I think it can help you get an idea on how to proceed. Attaching the code below:
n=4; %sample size
no_of_simulations=1000; %For 1000 simulations
BETA2=zeros(1,no_of_simulations);
BETA1=zeros(1,no_of_simulations);
for j=1:no_of_simulations
u=normrnd(0,1,[1,n]) %iid errors
y_0=1; %start values
beta1=1;
beta2=0.5;
y = zeros(1,n);
yt=y_0;
%real value of y
for i=1:n
y_real=beta1+beta2*yt;
y(i) = y_real;
yt=y_real;
end
%estimated y including the error term
y;
y_new=y+u;
x=[y_0,y_new(1:end-1)]; %lagged explanatory variable
[beta1_hat, beta2_hat]=regression(y_new,x); %regression to estimate beta1 and beta2
solution1=beta1_hat-beta1; %estimation errors
solution2=beta2_hat-beta2;
BETA2(j)=beta2_hat;
BETA1(j)=beta1_hat;
end
plot(BETA1)
avg_beta1=mean(BETA1)
figure
plot(BETA2)
avg_beta1=mean(BETA2)
You can obtain the values of beta1 and beta2 over all 1000 simulations. Feel free to make modifications and explore. I hope you find a solution to the question, and if you do I'd recommend you to share it here for the benefit of the entire MATLAB community :)

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!