Min Kim
Rutgers
September 2, 2022
H = 40;
L = 4;
irf_i = zeros(H,1);
irf_i_u = zeros(H,1);
irf_i_l = zeros(H,1);
for h=0:H
YY = Y(L+1+h:end,1);
XX = X(L+1:end-h,:);
length(XX);
[bet, ~, se] = olshac(YY,XX,h+1);
irf_i(h+1) = bet(2);
irf_i_u(h+1) = bet(2) + 1.65*se(2);
irf_i_l(h+1) = bet(2) - 1.65*se(2);
end
figure
plot(0:H,irf_i*100,'k','linewidth',2)
hold on
plot(0:H,irf_i_u*100,'k','LineStyle','--')
plot(0:H,irf_i_l*100,'k','LineStyle','--')
1 - 1
2 * 2;
ans
ans / ans
help log % try also "doc log" and "which log"
a = log(ans);
a = a + 1
b = a^2
0 / 0
1 / 0
s = 'this is a string'
x = [2 ; 1]
y = [0 1]
A = [2 0; 0, 1]
x = [2 ; 1] % see length(x) or size(x)
y = [0 1] % y = [0, 1]
A = [2 0; 0, 1] % see det(A) and rank(A)
A = [2 0; y]
A = [A(:,1) y']
A = [x(1) 0; 0 1]
A = diag(x)
A = eye(2) + [1 0;0 0]
import numpy as np
x = np.array([2, 1]).reshape(2, 1)
A1 = np.array([2, 0])
A2 = np.array([0, 1])
A = np.hstack((A1, A2))
which does not look as simple as MATLAB
save data x y A
clear
load data
5*A
inv(A)
max(A)
exp(A)
A(1) % see also A(3), A(1,2) and A(:)
A'
A.^2
A + ones(2,2)
ans^2
log(ans)
diag(ans)
1:8
1:2:8
linspace(1,8,4)
B = reshape(ans,2,2);
A*B
C = magic(5)
C = C(1:end,1) - C(:,3);
C = reshape(C(1:end-1),2,2);
index = find(C > 5)
C_1col = C(index)
size(C_1col)
size(A)
C_1col'*A
rand
randn(2,2)
n = 100
mean(rand(n,1))
C = {'cell array', A; B, x}
C{1,1}
student(1).name = 'Min Kim';
student(1).nationality = 'South Korea';
student(1).info = [1; 6]; % credits and year
student
2 >= 1
ones(4,1) >= 1
A(:) > 0
sum(ans)
true
false
true == false
true ~= false
true | false
true & false
$$ V_t = \frac{M}{(1+r)^{T-t}}$$
x = 5;
if (x > 3)
disp('x is greater than 3')
else
disp('x is lower than or equal to 3')
end
i = 0; S = 0;
while i <= 10
S = S + i;
i = i + 1;
end
i = 0; S = 0;
while i <= 10
if mod(i,2) == 0
S = S + i;
end
i = i + 1;
end
S = 0;
for i = 0:10
S = S + i;
end
S_bar = S/i;
$$V_{t+1}-V_t$$
function V = ex1(M,r,T)
tau = T:-1:0;
V = M./(1+r).^tau;
end
function answer = ex2(M,r,T)
V = ex1(M,r,T);
Vdiff = diff(V);
Vdiffsum = cumsum(Vdiff);
tau = find(Vdiffsum>30,1);
answer = strcat("Period ",num2str(tau));
% or using a loop
% for i = 1:T
% if Vdiffsum(i) > 30
% answer = strcat("Period ",num2str(i));
% break
% end
% end
end
function V = compute_factorial(n)
if n<0 || ~isint(n)
error('n must be a positive integer')
elseif n == 0
V = 1
else
n * compute_factorial(n-1)
end
end
\(n! = n*(n-1)*(n-2)*\cdots*1\)
\(0! = 1\)
\(V_t = \frac{V_{t+1}}{1+r}\)
function V = ex1(M,r,T)
tau = T:-1:0;
V = M./(1+r).^tau;
end
T = 200;
epsilon = randn(T,1);
figure;
plot(epsilon)
x = zeros(T,1); % pre-allocation of memory
rho = 0.9;
for t = 2:T
x(t) = rho * x(t-1) + randn;
end
figure
plot(1:T,x)
x = linspace(-10,10,100)';
y = x;
[X,Y] = meshgrid(x,y);
g = @(x,y) (4*x.^2+2*y.^2+4*x.*y+2*y+1);
figure;
surf(X,Y,g(X,Y)) % looks like g is convex
function sequence=collatz(n)
% Collatz problem. Generate a sequence of integers resolving to 1
% by Ian M. Brooks
% For any positive integer, n:
% Divide n by 2 if n is even
% Multiply n by 3 and add 1 if n is odd
% Repeat for the result
% Continue until the result is 1
sequence = n;
next_value = n;
while next_value >= 1
if rem(next_value,2)=0
next_value = next_value/2;
else
next_value = 3*nextvalue+1;
end
sequence = [sequence, next_value];
end
T = 1000;
x = zeros(T,1);
rho = 0.7;
c = 5;
for t = 2:T
x(t) = c + rho * x(t-1) + 2*randn;
end
X = x(1:end-1); % one period lag
y = x(2:end);
model = fitlm(X,y); % linear model fitting
model.Coefficients
f = @(x) (x.^2-2); % anonymous function
x_val = linspace(-5,5,100); % domain or grids
figure;
plot(x_val,f(x_val))
yline(0,'k--')
int_val = 1; % initial value
[sol, fvalue] = fzero(@f,int_val); % find_root is the function
g = @(x) (4*x(1).^2+2*x(2).^2+4*x(1).*x(2)+2*x(2)+1);
int_val = [1; 1]; % initial value
[sol, gvalue] = fminunc(@g,int_val); % g is the function