% MONTE CARLO EXPERIMENT FOR THE GENERALIZED REGRESSION MODEL clc; % Parameters n = 50; loops = 5000; beta = [100;0.8]; HD = 0; AC = 1; if HD == 1; sigma2 = 0.05; elseif AC == 1; sigma2 = 25000; rho = 0.5; end; % RHS Variable y = 300+2000*rand(n,1); xmat = [ones(n,1),y]; % Storage Matrices b = zeros(2,loops); betahat = zeros(2,loops); rhomat = zeros(1,loops); % Create Variance-Covariance Matrix of Errors if HD == 1; omega = zeros(n) + diag(sigma2*(y.^2)); elseif AC == 1; omega = zeros(n); for row = [1:1:n]; for col = [1:1:n]; omega(row,col) = sigma2*(rho^abs(row-col))/(1-rho^2); end; end; end; % Simulate Data and Perform Estimation for i = (1:1:loops) if HD == 1; c = beta(1) + beta(2)*y + sqrt(sigma2)*(y.*randn(n,1)); elseif AC == 1; c = zeros(n,1); eps = zeros(n,1); eps(1) = sqrt(sigma2)*randn(1,1); c(1) = beta(1) + beta(2)*y(1) + eps(1); for t = (2:1:n); eps(t) = rho*eps(t-1) + sqrt(sigma2)*randn(1); c(t) = beta(1) + beta(2)*y(t) + eps(t); end; end; b(:,i) = inv(xmat'*xmat)*(xmat'*c); e = c - xmat*b(:,i); e_t = e(2:n); e_tless1 = e(1:n-1); %rhohat = (e_t'*e_tless1)/(e_tless1'*e_tless1); rhohat = -0.5; rhomat(1,i) = rhohat; if HD == 1; omegahat = zeros(n) + diag(sigma2*(y.^2)); elseif AC == 1; omegahat = zeros(n); for row2 = [1:1:n]; for col2 = [1:1:n]; omegahat(row2,col2) = sigma2*(rhohat^abs(row2-col2))/(1-rhohat^2); end; end; end; betahat(:,i) = inv(xmat'*inv(omegahat)*xmat)*(xmat'*inv(omegahat)*c); end; % Sampling Distribution of Estimated MPC subplot(2,1,1); hist(b(2,:)',20); set(gca,'XLim',[0.6,1]); title('OLS Sampling Distribution'); subplot(2,1,2); hist(betahat(2,:)',20); set(gca,'XLim',[0.6,1]); title('GLS Sampling Distribution'); %Scatterplot of Data figure(2) plot(y,c,'bo');