% This symbol (%) indicates that the line is a comment. % To run this file : % 1- Press the "play" button in the matlab text editor OR ; % 2- In the command line, type the name of this file (without the ".m") % but make sure matlab looks for the right directory. % % basic_examples.m % runs basic examples (duh) clear %This clears everything in memory x = 10 %Affectation (x == 10) x = x + 10 %Now, x == 20 x = [1, 0.5; 0.5, 1] %x is now a matrix y = 2 * x %scalar multiplication y = x * y %(matrix multiplication) z = x.*y %element by element multiplication z = x.^y %element by element exponentiation %Note that the semi-colon ";" suppress the output. x = x / 2; %<-- No output, though the value of x changed... %So all this is fairly trivial. Here is a slightly more advanced example of what could be %done... clear %Clears all the previous stuff. %Let y = 10 + 12*x + eps be a regression model where : % x is distributed uniformly (and is independent of eps) % eps is normally distributed. % % What are the OLS estimators of (b_0 = 10, b_1 = 12) ? % % Lets make a sample of n = 100 : x = rand(100, 1); %Creates an 100x1 vector of uniform numbers. cons = ones(100,1); %Creates a 100x1 vector of ones. y = cons*10 + 12*x + randn(100,1); %randn(100,1) creates a nx1 vector of normally distributed numbers... %Now, we perform the regression : X = [cons, x]; %Full set of regressors. b = (X'*X)^(-1)*X'*y %OLS formula (see the output on display)