%%%%%%%%%%%%(c) Jan Kabatek, Universiteit van Tilburg %%%%%%%%%%%%%%%%%%%%%
% This is a resulting m-file for 3rd tutorial of Econometrics 1 for       %
% research masters.                                                       %
%                                                                         %
% Contents: basic functions, simulating data, regression output           %  
%                                                                         %
% IMPORTANT: before asking me about a specific command in this code(or any%
% code) consult a HELP FILE by parsing:	 help <that command into the      %     
% Matlab console.                                                         %	
%																		  %	
% Version 7.10.2011	, j.kabatek@uvt.nl, K606 (should you find any typos,  %
% let me know)                                                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%(1)% Playing around %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% you can write directly into the command window %%%%%%%%%%%%%%%%%%%%%%

'hello world'
1
7*6/3+1                             % for help with operators,type "help +"
log(0)

ourscalar = 1

%%%% Matlab is all about vectors & matrices %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ourrowvector = [ 1 2 3 ]
rv = [ 1 : 3 ]

ourcolumnvector = [ 3 ; 7 ; 8 ]
cv = ourcolumnvector

ourscalprod = rv* cv
ourmat = cv * rv
ourmat'                              % is transpose.
invmat = inv(ourmat)                 % inverse is impossible, as the matrix 
% is obviously singular (just a linear combination of vectors)

magic(3)

invmat = inv(magic(3))               % better than inv is to use / or \ 
% \ is usually preferred as it is working faster. See help file for how to
% use it.
magic(3)*invmat

ones(3,3)                            % a command for matrix/vector of ones   
zeros(3,3)                           % zeros           
eye(3)                               % This is an I-matrix

%%%% accessing points in the matrix  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ourmat
ourmat(1,1)
ourmat(1,end)
ourmat(1,:)                         % without specifying, it'll be the 
% first row, with 2:3 (or 2:end) instead of :, it'll be first two terms of 
% the first row, etc.

%%%% observing which points satisfy condition %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ourmat > 8
sum(ourmat > 8)
sum(sum(ourmat > 8))                % how many points satisfy the condition
censormat = ourmat.*(ourmat > 8)    % censoring the matrix according to our 
% condition. The dot operator stands for array multiplication. Only terms 
% at the same positions are multiplied.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(2)% DATA GENERATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all ;                         % Remember - the more variables you 
% store in the memory, the less of it can be used for actual computation

%%%% set ANR number as a seed ! %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
s = RandStream('mcg16807','Seed',123456)
RandStream.setDefaultStream(s)

%%%% data generating parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N = 100 ;                           %number of obs
K = 4 ;                             %number of regressors
beta = [1 2 3 4]' ;                 %true regressor values
sigma = .5 ;                        %sd of error terms
    
%%%% data generating process %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X = [ones(N,1) randn(N,K-1)] ;      %first col is constant term
E = sigma*randn(N,1) ; 
Y = X*beta+E ; 

%%%% basic statistical functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mean(X,1)                           %mean value over rows (2=cols)
var(X,1)
std(X,1)
cov(X(:,2),Y)

%%% OLS by command %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% help regress

regress(Y,X)
[B,BINT,R,RINT,STATS] = regress(Y,X) ;
                                    % X should include a column of ones so 
% that the model contains a constant term, otherwise it's a regression 
% through origin!!!
% one way how to include it is (for an X matrix without constat term) 
% X = [ones(size(<regressand name>,1),1) , X ]

% Or be creative: Y~=NaN will do the same job as "ones" command! 
% NaN = not a number


%%% small loop example %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% we're gonna examine how do the values of beta change as we run ols on new
% draws from the DGP

nloop = 1000
keepbeta = NaN(K,1000) ;                  % defining our storage matrix for
% Betas - it is useful to define it in advance, as it is more efficient in 
% terms of memory allocation. 
% parsing keepbeta=[keepbeta,newbeta] 1000 times is very memory-intensive. 

for i=1:nloop
    % generate new draw
    X = [ones(N,1) randn(N,K-1)] ;
    E = sigma*randn(N,1) ;
    Y = X*beta+E ;

    %run regression 
    newbeta=regress(Y,X) ;
    keepbeta(:,i) = newbeta ;  
end

% plotting trajectories of beta
figure(1), plot(keepbeta') ;

%this is what we would call Monte Carlo (MC) simulation. In fact, we could
%get an approximation of covariance matrix of betas by cov(keepbeta').


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(3)% IMPORTING DATA% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% most safe way is to use Import data wizard accessible through selection 
% File->Import data in the main Matlab window.

% a direct command is 
% A = importdata(filename)  check the help file for further info
