
clc;
clear;
clear all;


%% Alternative Approach
%% Import Data
%%

Data         = readtable('RawDataYearly.xlsx');
Dates        = Data(:,1);
Data         = table2array(Data(1:end,3:11));
Dates        = table2array(Dates);
Dates        = datenum(Dates);
StateNetCF   = readtable('StateNetCF.xlsx');
StateNetCF   = table2array(StateNetCF);

%% Parameter Estimation and Simulation
%%

Prices                 = Data(:,1);
Returns                = tick2ret(Prices);
mean_return            = mean(Returns);
mu                     = 0.1211;                
sigma                  = std(Returns);
dt                     = 1;
nPeriods               = 21;
nSimulations           = 50000;
nCashInflow            = 21;
PortfolioValue         = zeros(nSimulations,nPeriods);
PortfolioValue(:,1)    = StateNetCF(1,1);
epsilon                = normrnd(0,1,nSimulations,nPeriods);


for i = 1:nSimulations
    PortfolioValue(i,2) = StateNetCF(2,1) + PortfolioValue(i,1).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,2));
end

for i = 1:nSimulations
    PortfolioValue(i,3) = StateNetCF(3,1) + PortfolioValue(i,2).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,3));
end

for i = 1:nSimulations
    PortfolioValue(i,4) = StateNetCF(4,1) + PortfolioValue(i,3).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,4));
end

for i = 1:nSimulations
    PortfolioValue(i,5) = StateNetCF(5,1) + PortfolioValue(i,4).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,5));
end

for i = 1:nSimulations
    PortfolioValue(i,6) = StateNetCF(6,1) + PortfolioValue(i,5).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,6));
end

for i = 1:nSimulations
    PortfolioValue(i,7) = StateNetCF(7,1) + PortfolioValue(i,6).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,7));
end

for i = 1:nSimulations
    PortfolioValue(i,8) = StateNetCF(8,1) + PortfolioValue(i,7).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,8));
end

for i = 1:nSimulations
    PortfolioValue(i,9) = StateNetCF(9,1) + PortfolioValue(i,8).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,9));
end

for i = 1:nSimulations
    PortfolioValue(i,10) = StateNetCF(10,1) + PortfolioValue(i,9).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,10));
end

for i = 1:nSimulations
    PortfolioValue(i,11) = StateNetCF(11,1) + PortfolioValue(i,10).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,11));
end

for i = 1:nSimulations
    PortfolioValue(i,12) = StateNetCF(12,1) + PortfolioValue(i,11).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,12));
end

for i = 1:nSimulations
    PortfolioValue(i,13) = StateNetCF(13,1) + PortfolioValue(i,12).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,13));
end

for i = 1:nSimulations
    PortfolioValue(i,14) = StateNetCF(14,1) + PortfolioValue(i,13).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,14));
end

for i = 1:nSimulations
    PortfolioValue(i,15) = StateNetCF(15,1) + PortfolioValue(i,14).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,15));
end

for i = 1:nSimulations
    PortfolioValue(i,16) = StateNetCF(16,1) + PortfolioValue(i,15).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,16));
end

for i = 1:nSimulations
    PortfolioValue(i,17) = StateNetCF(17,1) + PortfolioValue(i,16).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,17));
end

for i = 1:nSimulations
    PortfolioValue(i,18) = StateNetCF(18,1) + PortfolioValue(i,17).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,18));
end

for i = 1:nSimulations
    PortfolioValue(i,19) = StateNetCF(19,1) + PortfolioValue(i,18).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,19));
end

for i = 1:nSimulations
    PortfolioValue(i,20) = StateNetCF(20,1) + PortfolioValue(i,19).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,20));
end

for i = 1:nSimulations
    PortfolioValue(i,21) = StateNetCF(21,1) + PortfolioValue(i,20).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,21));
end

rng('default');

meanpriceendyear       = mean(PortfolioValue(:,21));
SimDates               = readtable('SimDatesYearly.xlsx');
SimDates               = table2array(SimDates);
SimDates               = datenum(SimDates(1:21,1));
SimPrices              = transpose(PortfolioValue);

%% Plot Simulation Paths
%%

figure(9);

plot(SimDates(1:end), PortfolioValue(1:end,:));
datetick();
title('Alternative Approach Simulated Values');
xlabel('Date');
ylabel('Value ($)');
legend('Simulation','location','northwest');

%% Oil Fund
%% Import Data
%%

Data         = readtable('RawDataYearly.xlsx');
Dates        = Data(:,1);
Data         = table2array(Data(1:end,3:11));
Dates        = table2array(Dates);
Dates        = datenum(Dates);
StateNetCF   = readtable('StateNetCF.xlsx');
StateNetCF   = table2array(StateNetCF);

%% Parameter Estimation and Simulation
%%

Returns_equities       = tick2ret(Data(:,2));
Returns_bonds          = tick2ret(Data(17:35,3));
mean_ret_equities      = mean(Returns_equities);
mean_ret_bonds         = mean(Returns_bonds);
Weight_equities        = 0.4;
Weight_bonds           = 0.6;
mu                     = 0.1211;      
sigma_equities         = std(Returns_equities);
sigma_bonds            = std(Returns_bonds);
correlation            = corr(Returns_equities(17:34),Returns_bonds);
sigma                  = sqrt((Weight_equities^2)*(sigma_equities^2)+(Weight_bonds^2)*(sigma_bonds^2)+...
                          2*Weight_equities*Weight_bonds*correlation*sigma_equities*sigma_bonds);
dt                     = 1;
nPeriods               = 21;
nSimulations           = 50000;
nCashInflow            = 21;
PortfolioValue         = zeros(nSimulations,nPeriods);
PortfolioValue(:,1)    = StateNetCF(1,1);
epsilon                = normrnd(0,1,nSimulations,nPeriods);


for i = 1:nSimulations
    PortfolioValue(i,2) = StateNetCF(2,1) + PortfolioValue(i,1).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,2));
end

for i = 1:nSimulations
    PortfolioValue(i,3) = StateNetCF(3,1) + PortfolioValue(i,2).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,3));
end

for i = 1:nSimulations
    PortfolioValue(i,4) = StateNetCF(4,1) + PortfolioValue(i,3).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,4));
end

for i = 1:nSimulations
    PortfolioValue(i,5) = StateNetCF(5,1) + PortfolioValue(i,4).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,5));
end

for i = 1:nSimulations
    PortfolioValue(i,6) = StateNetCF(6,1) + PortfolioValue(i,5).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,6));
end

for i = 1:nSimulations
    PortfolioValue(i,7) = StateNetCF(7,1) + PortfolioValue(i,6).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,7));
end

for i = 1:nSimulations
    PortfolioValue(i,8) = StateNetCF(8,1) + PortfolioValue(i,7).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,8));
end

for i = 1:nSimulations
    PortfolioValue(i,9) = StateNetCF(9,1) + PortfolioValue(i,8).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,9));
end

for i = 1:nSimulations
    PortfolioValue(i,10) = StateNetCF(10,1) + PortfolioValue(i,9).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,10));
end

for i = 1:nSimulations
    PortfolioValue(i,11) = StateNetCF(11,1) + PortfolioValue(i,10).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,11));
end

for i = 1:nSimulations
    PortfolioValue(i,12) = StateNetCF(12,1) + PortfolioValue(i,11).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,12));
end

for i = 1:nSimulations
    PortfolioValue(i,13) = StateNetCF(13,1) + PortfolioValue(i,12).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,13));
end

for i = 1:nSimulations
    PortfolioValue(i,14) = StateNetCF(14,1) + PortfolioValue(i,13).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,14));
end

for i = 1:nSimulations
    PortfolioValue(i,15) = StateNetCF(15,1) + PortfolioValue(i,14).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,15));
end

for i = 1:nSimulations
    PortfolioValue(i,16) = StateNetCF(16,1) + PortfolioValue(i,15).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,16));
end

for i = 1:nSimulations
    PortfolioValue(i,17) = StateNetCF(17,1) + PortfolioValue(i,16).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,17));
end

for i = 1:nSimulations
    PortfolioValue(i,18) = StateNetCF(18,1) + PortfolioValue(i,17).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,18));
end

for i = 1:nSimulations
    PortfolioValue(i,19) = StateNetCF(19,1) + PortfolioValue(i,18).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,19));
end

for i = 1:nSimulations
    PortfolioValue(i,20) = StateNetCF(20,1) + PortfolioValue(i,19).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,20));
end

for i = 1:nSimulations
    PortfolioValue(i,21) = StateNetCF(21,1) + PortfolioValue(i,20).*exp((mu-0.5*sigma^2)*dt+sigma*sqrt(dt).*epsilon(i,21));
end

rng('default');

meanpriceendyear       = mean(PortfolioValue(:,21));
SimDates               = readtable('SimDatesYearly.xlsx');
SimDates               = table2array(SimDates);
SimDates               = datenum(SimDates(1:21,1));
SimPrices              = transpose(PortfolioValue);

%% Plot Simulation Paths
%%

figure(9);

plot(SimDates(1:end), PortfolioValue(1:end,:));
datetick();
title('Oil Fund Simulated Values');
xlabel('Date');
ylabel('Value ($)');
legend('Simulation','location','northwest');

%% Descriptive Statistics
%%

Mean_Return     = mean(Returns);
Mean_Price      = mean(Prices);
Median_Return   = median(Returns);
Median_Price    = median(Prices);
Min_Return      = min(Returns);
Min_Price       = min(Prices);
Max_Return      = max(Returns);
Max_Price       = max(Prices);
Std_Return      = std(Returns);
Std_Price       = std(Prices);
Skew_Return     = skewness(Returns);
Skew_Price      = skewness(Prices);
Kurt_Return     = kurtosis(Returns);
Kurt_Price      = kurtosis(Prices);

