%% simple.m
%% Simple regression model Monte Carlo Simulation with normal errors
%% By Starz
%% Modified by Kyongwook Choi 2000.12.12
randn('seed',0); %%
n = 100; %% Data points are 100
sim = 1000; %% 1000 trials
betahat = zeros(sim,1);
% Generate empty vector, This is very important when you do
% Monte Carlo Simulations. Later, you will save your result into
% This file: Input = (sim, 1) where r=sim=# of rows and c=1= # of
% column
x = randn(n,1); % create "fixed" x vector. Each element of x is chosen from
% randn(r,c) function where r = # of row, c= # of column
sumxsqr = sum(x.^2) % Sum of squres to get the standard error. In here note that
% you should use '.' since sum of squrares can be generated by
% elements by elements
for i=1:sim % Starting the data generations. We use for.. end command
u = randn(n,1); % Generate error term with standard normal randome number
% generator with S^2=1
s2=1.0; % This will you use it later for creating the Graph.
y = 5.0*x + u; % Generate dependent variable using X and U which we already produce them
betahat(i) = x\y; % estimate the beta hat. Note that now, you have 1,000 beta hat.
end
disp(['Theory says mean is 5.0 and variance is ',num2str(s2/sumxsqr)]);
mean(betahat) % Mean of 1,000 simulated beta hat
cov(betahat) % Covariance of of 1,000 simulated beta hat
sd=sqrt(cov(betahat)*(inv(x'*x)))% Please remember the formular for the standard error.
hist(betahat,50); % Historgram for Betahat by 50 increments.