%Bash commands
ls
pwd
mkdir new_dir
cd new_dir
pwd
ls
%%Vectors and Matrices
a=[1;2;3] %column vector
b=[1 2 3] %row vector
b' %transposed vector
a+b %Dimension error
a+b'
c=1:10
d=linspace(1,5, 24)
A=[1 2 3; 4 5 6]
A(1,2)=3
A=[1 2 3; 4 5 6]
A=[A; 7 8 9] %new row
A=[1 2 3; 4 5 6]
A=[A [1;1]] %new column
A=ones(3,2)
A=zeros(3,2)
A=rand(3,2)
B=randn(3,2)
C=randi([-100 100], 3,2)
D=floor(rand(3,2)*201-100)
A=rand(5,2)
sum(A) %sum of columns
sum(A') %sum of rows
mean(A) %mean of columns
mean(A') %mean of rows - 1st option
mean(A,2) %mean of rows - 2nd option
max(A)
corrcoef(A)
%Non-standard normal distribution
X=randn(2, 1e5); %NB. semicolon!
mean(X'); %why do we transpose?
std(X');
d=[.75 .1; .1 .25];
S=chol(d, 'lower');
Y=d*X+repmat([2;1], 1, 1e5);
cov(Y') %remember about transposing!!!
%Conditional statements
x=2
if x==2
disp('IF condition is met')
end
if x==2
disp('IF condition is met')
elseif x>1
disp('ELSEIF condition is met and IF condition is NOT met')
else
disp('None of conditions are met')
end;
%Random Economist Generator
x=rand(1)
if x<.5
x='Debreu'
elseif x<.8 % why don't we have to check if x>.5?
x='Sargent'
else
x='Lange'
end;
%Loops
%%For
for i = 1:100
disp([num2str(i), ' I will hand in macro problem sets on time...'])
end
N=1e7;
x=randn(N,1);
counter=0
for i=1:length(x)
if abs(x(i))<1
counter=counter+1;
end
end;
frac1=counter/N;
%%Probability of Range (example)
N=1e7;
x=randn(N,1);
counter=0
for i=1:length(x)
if abs(x(i))<1
counter=counter+1;
end
end;
frac1=counter/N;
frac1
%%%
counter=0
for i=1:length(x)
if x(i)<1 && x(i)>-.5
counter=counter+1;
end
end;
frac2=counter/N;
frac2
%%While
count=1;
x=0
while x>=0
['x has been non-negative for ', num2str(count), ' iterations']
x=randn(1)
count=count+1;
end;
frac1
%%%
counter=0
for i=1:length(x)
if x(i)<1 && x(i)>-.5
counter=counter+1;
end
end;
frac2=counter/N;
frac2
%Plots
X=randn(30,1)
y=rand(1)*X+randn(30,1);
plot(X,y, 'p')
xlabel('X')
ylabel('y')
title('Title')
legend('X')
grid()
X=randn(30, 6);
alpha=[1 2.5 -10 100 -100 0]';
y=X*alpha+randn(30,1);
for i=1:6
subplot(3,2,i)
plot(X(:,i), y, 'p')
grid()
end;
X=randn(30,1);
alpha=[10 -10];
y=X*alpha+randn(30,2);
plot(X, y, 'x')
legend('positive', 'negative')
grid()
%%Histograms
X=randn(300,1)
hist(X)
grid()
hist(X,100)