% Q1
% 10x4 matrix of random integers between 10 and 100
ri = randi([10, 100], 10, 4)
% 10x4 matrix of random numbers between 10 and 100
rn = 10 + (100-10)*rand(10, 4)
% Max. value in each column and its index
% mc = max(ri)
[mc, indc] = max(ri) 
% Max. value in each row
% mr = max(ri')
[mr, indr] = max(ri') 
% Max. value in the matrix
mm = max(max(ri))
% Matrix with max. value
maxm = mm * ones(10, 4)
% Diff
diff = maxm - ri
% Entries less than 40 and more than 20
(diff > 20) & (diff <40)
% What does this do?
(diff > 3).* diff
% ... and this?
diff.*diff > 5*diff

% Q2
% x, y
x = rand(100, 1);
y = 0.1 + rand(100, 1);
% Statistics
mx = mean(x)
stdx = std(x)
rx = range(x)
my = mean(y)
stdy = std(y)
ry = range(y)
% Test-statistics
ttest(x)
ttest(y)
ttest2(x, y)
% Counting members
z = sum((x > y))
% Greatest value
xm = max(x-y)

% Q3
X = rand(10, 1);
X1 = [NaN; X(1:end-1)];
X11 = [0; X(1:end-1)];
X2 = [NaN; X1(1:end-1)];
X3 = [NaN; X2(1:end-1)];
X4 = [NaN; X3(1:end-1)];
mat = [X X1 X2 X3 X4];
corr(mat, 'rows', 'pairwise')

% Q4
unifrv = rand(10, 1);
mean(unifrv)
var(unifrv)
normrv = randn(10, 1);
mean(normrv)
var(normrv)
chirv = randn(10, 1);
chirv = chirv .* chirv;
mean(chirv)
var(chirv)

% Q5
% See the other matlab file

% Q6
x0 = [1,1];
options = optimset('LargeScale','off');
objfunc = @(x) (sqrt(x(1)*x(2)));
[x,fval,exitflag,output] = fminunc(objfunc, x0, options)


% Q7
x0 = [10; 10; 10]
options = optimset('LargeScale', 'off');
A = [-1 -2 -2; 1 2 2];
b = [0; 72];
objfunc2 = @(x) (-x(1)*x(2)*x(3));
[xc,fvalc] = fmincon(objfunc2, x0, A, b)
% Check constraints are satisified at the solution:
A*xc -b


% Built-in Functions versus Vectorization versus Loops 1
% Find the optimum of sin(t) - t^2 over [0, 1]
tic
x0 = 0;
options = optimset('LargeScale','off');
objfunc = @(x) (-sin(x) + x^2);
[x,fval,exitflag,output] = fminunc(objfunc, x0, options);
toc

tic
t = 0:0.01:1;
f = @(t) (sin(t) - t.^2);
y = f(t);
maxs = max(y);
toc

tic
maxsl = 0;
n=100;
yy = zeros(n, 1);
for i=1:n
    yy(i) = f(i/n);
    if yy(i) > maxsl
        maxsl = yy(i);
    end
end
maxsl;
toc

% Vectorization II
% Find the optimum of f(x_1, x_2) = - sin(x_1) - sin(x_2)
tic
x0 = [0, 0];
options = optimset('LargeScale','off');
objfunc = @(x) (-sin(x(1)) - sin(x(2)));
[x,fval,exitflag,output] = fminunc(objfunc, x0, options)
toc

% Manual evaluation
x = 0:0.2:2;
y = 0:0.2:2;
f = @(x, y) (sin(x) + cos(y));
z = max(f(x, y))

% meshgrid
[X, Y] = meshgrid(x, y);
zz = max(max(f(X, Y)))



tic
% Optimal growth problem.
% Replicates parts of Table 12.2 in Judd.
% u(c) =  c^(1-sigma) / (1-sigma)
% f(k) = A k^alpha
% rc: c + k' = Ak^alpha + (1-delta) k
% bellman eq. : v(k) = max_k' u(c) + beta v(k')

% Parameters
a = 0.25;
b = 0.9;
d = 0.0;
g = -2;
A = (1-b) / (a*b)
tol = 0.0000001 % tolerance level
stop = 1;
m = 10; % number of value functions wanted
count = 0; % counts number of iterations

% State Space
k = 0.8:0.1:1.2
kp = k;
n = length(k);


% Initial, Tomorrow's and Temp Value Function
v = zeros(size(k));
vp = v;
vtemp = v;

% Utility
u = @(c)((c^(1+g)) / (1+g));

% Value function Iteration

% Currently doing m value functions iterations
% Uncomment while below to use tol to stop
% while(stop > 0)
for l=1:m
    for i=1:n
        for j=1:n
            c = A*k(i)^a + k(i)*(1-d) - kp(j);
            if(c < 0) 
                c=0;
            end
            vtemp(j) = u(c) + b*vp(j);
        end
        v(i) = max(vtemp);
        vtemp = zeros(size(k));
    end  
    if (norm((vp-v), inf) < tol)
        stop = 0;
    end
    count = count +1;
    vp = v
end
count
toc