%% importing data
clc  
clear all
close all

data = readtable('FF_daily.xlsx');
data.Properties.VariableNames{'Column1'} = 'date';
data.Properties.VariableNames{'Mkt_RF'} = 'MKT';
data.date = datetime(data.date, 'ConvertFrom', 'yyyymmdd', 'Format','dd/MM/yyyy');
mom_daily_us = xlsread('mom_us_daily.xlsx');
mom_daily_us = array2table(mom_daily_us);
mom_daily_us.Properties.VariableNames{'mom_daily_us1'} = 'Date';
mom_daily_us.Properties.VariableNames{'mom_daily_us2'} = 'MOM';
mom_daily_us.Date = datetime(mom_daily_us.Date, 'ConvertFrom', 'yyyymmdd', 'Format','dd/MM/yyyy');

%% Monthly data
FFMonth = xlsread('FF_Month.xlsx');
FFMonth = array2table(FFMonth);
FFMonth.Properties.VariableNames{'FFMonth1'} = 'Date';
FFMonth.Properties.VariableNames{'FFMonth2'} = 'Mkt';
FFMonth.Properties.VariableNames{'FFMonth3'} = 'SMB';
FFMonth.Properties.VariableNames{'FFMonth4'} = 'HML';
FFMonth.Properties.VariableNames{'FFMonth5'} = 'RF';
FFMonth.Date = datetime(fix(FFMonth.Date/100),rem(FFMonth.Date,100), ones(size(FFMonth.Date)), 'Format','MM/yyyy');

mom_month_us = xlsread('mom_us_month.xlsx');
mom_month_us = array2table(mom_month_us);
mom_month_us.Properties.VariableNames{'mom_month_us1'} = 'date';
mom_month_us.Properties.VariableNames{'mom_month_us2'} = 'MOM';
mom_month_us.date = datetime(fix(mom_month_us.date/100),rem(mom_month_us.date,100), ones(size(mom_month_us.date)), 'Format','MM/yyyy');

%%
 data.month = month(data.date);
 data.year = year(data.date);

 FFMonth.month = month(FFMonth.Date);
 FFMonth.year = year(FFMonth.Date);
 
 mom_month_us.month = month(mom_month_us.date);
 mom_month_us.year = year(mom_month_us.date);
 
 mom_daily_us.month = month(mom_daily_us.Date);
 mom_daily_us.year = year(mom_daily_us.Date);

%% Removal of data not used

toDelete = FFMonth.year > 2015;
FFMonth(toDelete,:) = [];
toDelete=  data.year > 2015;
data(toDelete,:) = [];

toDelete = mom_daily_us.year > 2015;
mom_daily_us(toDelete,:) = [];
toDelete=  mom_month_us.year > 2015;
mom_month_us(toDelete,:) = [];


%% Realized variance

[a,~,q] = unique([data.year,data.month],'rows');
volMKT = [a, accumarray(q,data.MKT,[],@(x)stat(x))];
RVmkt = volMKT(:,3);

[a,~,q] = unique([data.year,data.month],'rows');
volSMB = [a, accumarray(q,data.SMB,[],@(x)stat(x))];
RVsmb = volSMB(:,3);

[a,~,q] = unique([data.year,data.month],'rows');
volHML = [a, accumarray(q,data.HML,[],@(x)stat(x))];
RVhml = volHML(:,3);

[a,~,q] = unique([mom_daily_us.year,mom_daily_us.month],'rows');
volMOM = [a, accumarray(q,mom_daily_us.MOM,[],@(x)stat(x))];
RVmom = volMOM(:,3);

%% F_sigma with out C

for i = 2:length(RVmkt)
    F_sigma_uc_mkt((i),1) = (1/RVmkt(i-1))*(FFMonth.Mkt(i));
end

for i = 2:length(RVsmb)
    F_sigma_ucsmb((i-1),1) = (1/RVsmb(i-1))*(FFMonth.SMB(i));
end

for i = 2:length(RVhml)
    F_sigma_uchml((i-1),1) = (1/RVhml(i-1))*(FFMonth.HML(i));
end

for i = 2:length(RVmom)
    F_sigma_uc_mom((i),1) = (1/RVmom(i-1))*(mom_month_us.MOM(i));
end
%% Creation of C
stdmkt= std(FFMonth.Mkt);
stdmktusigma = std(F_sigma_uc_mkt);
cmkt= stdmkt/stdmktusigma

stdsmb= std(FFMonth.SMB);
stdsmbusigma = std(F_sigma_ucsmb);
csmb= stdsmb/stdsmbusigma;

stdhml = std(FFMonth.HML);
stdhmlusigma = std(F_sigma_uchml);
chml= stdhml/stdhmlusigma;

stdmom= std(mom_month_us.MOM);
stdmomusigma = std(F_sigma_uc_mom);
cmom= stdmom/stdmomusigma;
%% creation of portifolio 


for i = 2:length(FFMonth.Mkt)
    F_sigma_mkt(i-1,1) = (cmkt/RVmkt((i-1)))*FFMonth.Mkt(i);
end

for i = 2:length(FFMonth.Mkt)
    F_sigma_smb(i-1,1) = (csmb/RVsmb((i-1)))*FFMonth.SMB(i);
end

for i = 2:length(FFMonth.Mkt)
    F_sigma_hml(i-1,1) = (chml/RVhml((i-1)))*FFMonth.HML(i);
end

for i = 2:length(mom_month_us.MOM)
    F_sigma_mom(i-1,1) = (cmom/RVmom((i-1)))*mom_month_us.MOM(i);
end
%% RV plot
hold on
plot(FFMonth.Date,RVmkt)
plot(FFMonth.Date,RVsmb)
plot(FFMonth.Date,RVhml)
plot(mom_month_us.date,RVmom)
title('Realized Variance')
legend({'MKT','SMB','HML','MOM'},'Location','north')
hold off
print -depsc replication_rv

%% regression 
close all
rettable = table(F_sigma_mkt, F_sigma_hml, F_sigma_smb,FFMonth.Mkt(2:end));
rettable.Properties.VariableNames{'Var4'} = 'FFmkt';
rettable.FFsmb = FFMonth.SMB(2:end);
rettable.FFhml = FFMonth.HML(2:end);


%%

regmkt = fitlm(rettable, 'F_sigma_mkt ~FFmkt')
regsmb = fitlm(rettable, 'F_sigma_smb ~FFsmb')
reghml = fitlm(rettable, 'F_sigma_hml ~FFhml')
regmom = fitlm(mom_month_us.MOM(2:end),F_sigma_mom)


%% Alphas
alphaMKT = regmkt.Coefficients.Estimate(1);
yearlyalphaMKT = alphaMKT*12;
alphaSMB = regsmb.Coefficients.Estimate(1);
yearlalphaSMB = alphaSMB*12;
alphaHML = reghml.Coefficients.Estimate(1);
yearlyalphaHML = alphaHML*12;
alphaMOM = regmom.Coefficients.Estimate(1);
yearlyalphaMOM = alphaMOM*12;

%% Standard errors
[estcov SEh_mkt coeff] = hac(regmkt);
SEh_mkt = SEh_mkt(2);
[estcov SEh_smb coeff] = hac(regsmb);
SEh_smb = SEh_smb(2);
[estcov SEh_hml coeff] = hac(reghml);
SEh_hml = SEh_hml(2);
[estcov SEh_mom coeff] = hac(regmom);
SEh_mom = SEh_mom(2)


%% Controlling for fama french three factor
clc
Fama_sigma_mkt = fitlm(rettable,'F_sigma_mkt ~ FFmkt+FFsmb+FFhml')
Fama_sigma_hml = fitlm(rettable,'F_sigma_hml ~ FFmkt+FFsmb+FFhml')
Fama_sigma_smb = fitlm(rettable,'F_sigma_smb ~ FFmkt+FFsmb+FFhml')

%% 
FFmom = table(F_sigma_mom);
FFmom.MKT = FFMonth.Mkt(7:end);
FFmom.SMB = FFMonth.SMB(7:end);
FFmom.HML = FFMonth.HML(7:end);
FFmom.MOM = mom_month_us.MOM(2:end);

Fama_sigma_mom = fitlm(FFmom, 'F_sigma_mom~MKT+SMB+HML+MOM')

%% Yearly Fama Alpha
FamaAlphaMKT = Fama_sigma_mkt.Coefficients.Estimate(1)*12;
FamaAlphaHML = Fama_sigma_hml.Coefficients.Estimate(1)*12;
FamaAlphaSMB = Fama_sigma_smb.Coefficients.Estimate(1)*12;
FamaAlphaMOM = Fama_sigma_mom.Coefficients.Estimate(1)*12;
%% cumlogret
cumretmkt = zeros(length(F_sigma_mkt),1);
cumretmkt(1,1) = 1;
cumretf_sigma = zeros(length(F_sigma_mkt),1);
cumretf_sigma(1,1) = 1;



for l = 2: length(F_sigma_mkt)
    cumretmkt(l,1) = cumretmkt(l-1,1) * (1+(FFMonth.Mkt(l)/100));
    cumretf_sigma(l,1)= cumretf_sigma(l-1,1) * (1+(F_sigma_mkt(l-1)/100));
end
%% rolling avg
rollingavg_managed = movmean(F_sigma_mkt,12);
rollingavg_mkt = movmean(FFMonth.Mkt(2:end),12);



%% plots
subplot(2,2,[1 2])
hold on
set(gca, 'Yscale','log')
plot(FFMonth.Date(2:end),cumretmkt)
plot(FFMonth.Date(2:end),cumretf_sigma)
title('Cumulative Returns')
legend({'Buy and hold','Volatility managed'},'Location','north')
hold off

subplot(2,2,3)
hold on
plot(FFMonth.Date(2:end),rollingavg_mkt)
plot(FFMonth.Date(2:end),rollingavg_managed)
legend({'Buy and hold','Volatility managed'},'Location','north')
title('One-Year Rolling Average Return')
hold off

subplot(2,2,4)
hold on
ylim([-10 0])
plot(FFMonth.Date(2:end),rollingavg_mkt)
plot(FFMonth.Date(2:end),rollingavg_managed)
legend({'Buy and hold','Volatility managed'},'Location','south')
title('Draw Downs')
hold off
print -depsc replication

%% Summary stats US
close all

sumstatus_month = [mean(FFMonth.Mkt),mean(FFMonth.SMB),mean(FFMonth.HML),mean(mom_month_us.MOM),mean(FFMonth.RF);...
    max(FFMonth.Mkt),max(FFMonth.SMB),max(FFMonth.HML),max(mom_month_us.MOM),max(FFMonth.RF);...
    min(FFMonth.Mkt),min(FFMonth.SMB),min(FFMonth.HML),min(mom_month_us.MOM),min(FFMonth.RF);...
    length(FFMonth.Mkt),length(FFMonth.SMB),length(FFMonth.HML),length(mom_month_us.MOM),length(FFMonth.RF)];

sumstatus_daily = [mean(data.MKT),mean(data.SMB),mean(data.HML),mean(mom_daily_us.MOM),mean(data.RF);...
    max(data.MKT),max(data.SMB),max(data.HML),max(mom_daily_us.MOM),max(data.RF);...
    min(data.MKT),min(data.SMB),min(data.HML),min(mom_daily_us.MOM),min(data.RF);...
    length(data.MKT),length(data.SMB),length(data.HML),length(mom_daily_us.MOM),length(data.RF)];

corrmonth_us = corr(table2array((FFMonth(:,(2:6))),mom_month_us.MOM));

latex_cormonth_us =latex(vpa(sym(corrmonth_us),4))






%% Norwegian data
close all
clc
%% daily data
FFnordaily = readtable('FF_daily_nor.xlsx');
FFnordaily.date = datetime(FFnordaily.date, 'ConvertFrom', 'yyyymmdd', 'Format','dd/MM/yyyy');
FFnordaily.MKT_nor = FFnordaily.MKT_nor * 100;
FFnordaily.SMB_nor = FFnordaily.SMB_nor * 100;
FFnordaily.HML_nor = FFnordaily.HML_nor * 100;
FFnordaily.PR1YR_nor = FFnordaily.PR1YR_nor * 100;
FFnordaily.MOM_nor = FFnordaily.MOM_nor * 100;
FFnordaily.LIQ_nor = FFnordaily.LIQ_nor * 100;
FFnordaily.RF_nor = FFnordaily.RF_nor * 100;

FFnormonth = xlsread('FF_Month_nor.xlsx');
FFnormonth = array2table(FFnormonth);
FFnormonth.Properties.VariableNames{'FFnormonth1'} = 'date';
FFnormonth.Properties.VariableNames{'FFnormonth2'} = 'MKT_nor';
FFnormonth.Properties.VariableNames{'FFnormonth3'} = 'SMB_nor';
FFnormonth.Properties.VariableNames{'FFnormonth4'} = 'HML_nor';
FFnormonth.Properties.VariableNames{'FFnormonth5'} = 'PR1YR_nor';
FFnormonth.Properties.VariableNames{'FFnormonth6'} = 'MOM_nor';
FFnormonth.Properties.VariableNames{'FFnormonth7'} = 'LIQ_nor';
FFnormonth.Properties.VariableNames{'FFnormonth8'} = 'RF_nor';
FFnormonth.date = datetime(FFnormonth.date, 'ConvertFrom', 'yyyymmdd', 'Format','dd/MM/yyyy');
FFnormonth.MKT_nor = FFnormonth.MKT_nor*100;
FFnormonth.SMB_nor = FFnormonth.SMB_nor*100;
FFnormonth.HML_nor = FFnormonth.HML_nor*100;
FFnormonth.PR1YR_nor = FFnormonth.PR1YR_nor*100;
FFnormonth.MOM_nor = FFnormonth.MOM_nor*100;
FFnormonth.LIQ_nor = FFnormonth.LIQ_nor*100;
FFnormonth.RF_nor = FFnormonth.RF_nor*100;
FFnormonth.month = month(FFnormonth.date);
FFnormonth.year = year(FFnormonth.date);

FFnordaily.month = month(FFnordaily.date);
FFnordaily.year = year(FFnordaily.date);

%% creation of subperiods
firstsub = FFnormonth.year <=1990 ;
firstsub_month=FFnormonth(firstsub,:);
firstsub = FFnordaily.year >= 1990;
firstsub_daily = FFnordaily(firstsub,:);
%%
secondsub_month = FFnormonth((FFnormonth.year<=2000),:);
fjern = secondsub_month.year <= 1990;
secondsub_month(fjern,:) = [];
%%
secondsub_daily = FFnordaily((FFnordaily.year<=2000),:);
fjern = secondsub_daily.year<= 1990;
secondsub_daily(fjern,:)=[];
%%
thirdsub_month = FFnormonth((FFnormonth.year<=2010),:);
fjern = thirdsub_month.year <= 2000;
thirdsub_month(fjern,:) = [];

thirdsub_daily = FFnordaily((FFnordaily.year<=2010),:);
fjern = FFnordaily.year <= 2000;
thirdsub_daily(fjern,:)=[];
%%
forthsub_month = FFnormonth((FFnormonth.year<=2020),:);
fjern = forthsub_month.year <= 2010;
forthsub_month(fjern,:) = [];

forthsub_daily = FFnordaily((FFnordaily.year<=2020),:);
fjern = FFnordaily.year<= 2010;
forthsub_daily(fjern,:)=[];

%% RV Nor

[a,~,q] = unique([FFnordaily.year,FFnordaily.month],'rows');
volMKT_nor = [a, accumarray(q,FFnordaily.MKT_nor,[],@(x)stat(x))];
RVmkt_nor = volMKT_nor(:,3);

[a,~,q] = unique([FFnordaily.year,FFnordaily.month],'rows');
volSMB_nor = [a, accumarray(q,FFnordaily.SMB_nor,[],@(x)stat(x))];
RVsmb_nor = volSMB_nor(:,3);

[a,~,q] = unique([FFnordaily.year,FFnordaily.month],'rows');
volHML_nor = [a, accumarray(q,FFnordaily.HML_nor,[],@(x)stat(x))];
RVhml_nor = volHML_nor(:,3);

[a,~,q] = unique([FFnordaily.year,FFnordaily.month],'rows');
volPR1YR_nor = [a, accumarray(q,FFnordaily.PR1YR_nor,[],@(x)stat(x))];
RVpr1yr_nor = volPR1YR_nor(:,3);

[a,~,q] = unique([FFnordaily.year,FFnordaily.month],'rows');
volMOM_nor = [a, accumarray(q,FFnordaily.MOM_nor,[],@(x)stat(x))];
RVmom_nor = volMOM_nor(:,3);

[a,~,q] = unique([FFnordaily.year,FFnordaily.month],'rows');
volLIQ_nor = [a, accumarray(q,FFnordaily.LIQ_nor,[],@(x)stat(x))];
RVliq_nor = volLIQ_nor(:,3);

%% plot RV nor

hold on
plot(FFnormonth.date,RVmkt_nor)
plot(FFnormonth.date,RVsmb_nor)
plot(FFnormonth.date,RVhml_nor)
plot(FFnormonth.date,RVmom_nor)
plot(FFnormonth.date,RVpr1yr_nor)
plot(FFnormonth.date,RVliq_nor)
title('Realized Variance')
legend({'MKT','SMB','HML','MOM','PR1YR','LIQ'},'Location','north')
hold off
print -depsc rv


%% RV sub 1
[a,~,q] = unique([firstsub_daily.year,firstsub_daily.month],'rows');
volsub1MKT_nor = [a, accumarray(q,firstsub_daily.MKT_nor,[],@(x)stat(x))];
RVsub1mkt_nor = volsub1MKT_nor(:,3);

[a,~,q] = unique([firstsub_daily.year,firstsub_daily.month],'rows');
volsub1SMB_nor = [a, accumarray(q,firstsub_daily.SMB_nor,[],@(x)stat(x))];
RVsub1smb_nor = volsub1SMB_nor(:,3);

[a,~,q] = unique([firstsub_daily.year,firstsub_daily.month],'rows');
volsub1HML_nor = [a, accumarray(q,firstsub_daily.HML_nor,[],@(x)stat(x))];
RVsub1hml_nor = volsub1HML_nor(:,3);

[a,~,q] = unique([firstsub_daily.year,firstsub_daily.month],'rows');
volsub1PR1YR_nor = [a, accumarray(q,firstsub_daily.PR1YR_nor,[],@(x)stat(x))];
RVsub1pr1yr_nor = volsub1PR1YR_nor(:,3);

[a,~,q] = unique([firstsub_daily.year,firstsub_daily.month],'rows');
volsub1MOM_nor = [a, accumarray(q,firstsub_daily.MOM_nor,[],@(x)stat(x))];
RVsub1mom_nor = volsub1MOM_nor(:,3);

[a,~,q] = unique([firstsub_daily.year,firstsub_daily.month],'rows');
volsub1LIQ_nor = [a, accumarray(q,firstsub_daily.LIQ_nor,[],@(x)stat(x))];
RVsub1liq_nor = volsub1LIQ_nor(:,3);

%% RV sub 2
[a,~,q] = unique([secondsub_daily.year,secondsub_daily.month],'rows');
volsub2MKT_nor = [a, accumarray(q,secondsub_daily.MKT_nor,[],@(x)stat(x))];
RVsub2mkt_nor = volsub2MKT_nor(:,3);

[a,~,q] = unique([secondsub_daily.year,secondsub_daily.month],'rows');
volsub2SMB_nor = [a, accumarray(q,secondsub_daily.SMB_nor,[],@(x)stat(x))];
RVsub2smb_nor = volsub2SMB_nor(:,3);

[a,~,q] = unique([secondsub_daily.year,secondsub_daily.month],'rows');
volsub2HML_nor = [a, accumarray(q,secondsub_daily.HML_nor,[],@(x)stat(x))];
RVsub2hml_nor = volsub2HML_nor(:,3);

[a,~,q] = unique([secondsub_daily.year,secondsub_daily.month],'rows');
volsub2PR1YR_nor = [a, accumarray(q,secondsub_daily.PR1YR_nor,[],@(x)stat(x))];
RVsub2pr1yr_nor = volsub2PR1YR_nor(:,3);

[a,~,q] = unique([secondsub_daily.year,secondsub_daily.month],'rows');
volsub2MOM_nor = [a, accumarray(q,secondsub_daily.MOM_nor,[],@(x)stat(x))];
RVsub2mom_nor = volsub2MOM_nor(:,3);

[a,~,q] = unique([secondsub_daily.year,secondsub_daily.month],'rows');
volsub2LIQ_nor = [a, accumarray(q,secondsub_daily.LIQ_nor,[],@(x)stat(x))];
RVsub2liq_nor = volsub2LIQ_nor(:,3);

%% RV sub 3
[a,~,q] = unique([thirdsub_daily.year,thirdsub_daily.month],'rows');
volsub3MKT_nor = [a, accumarray(q,thirdsub_daily.MKT_nor,[],@(x)stat(x))];
RVsub3mkt_nor = volsub3MKT_nor(:,3);

[a,~,q] = unique([thirdsub_daily.year,thirdsub_daily.month],'rows');
volsub3SMB_nor = [a, accumarray(q,thirdsub_daily.SMB_nor,[],@(x)stat(x))];
RVsub3smb_nor = volsub3SMB_nor(:,3);

[a,~,q] = unique([thirdsub_daily.year,thirdsub_daily.month],'rows');
volsub3HML_nor = [a, accumarray(q,thirdsub_daily.HML_nor,[],@(x)stat(x))];
RVsub3hml_nor = volsub3HML_nor(:,3);

[a,~,q] = unique([thirdsub_daily.year,thirdsub_daily.month],'rows');
volsub3PR1YR_nor = [a, accumarray(q,thirdsub_daily.PR1YR_nor,[],@(x)stat(x))];
RVsub3pr1yr_nor = volsub3PR1YR_nor(:,3);

[a,~,q] = unique([thirdsub_daily.year,thirdsub_daily.month],'rows');
volsub3MOM_nor = [a, accumarray(q,thirdsub_daily.MOM_nor,[],@(x)stat(x))];
RVsub3mom_nor = volsub3MOM_nor(:,3);

[a,~,q] = unique([thirdsub_daily.year,thirdsub_daily.month],'rows');
volsub3LIQ_nor = [a, accumarray(q,thirdsub_daily.LIQ_nor,[],@(x)stat(x))];
RVsub3liq_nor = volsub3LIQ_nor(:,3);

%% RV sub 4

[a,~,q] = unique([forthsub_daily.year,forthsub_daily.month],'rows');
volsub4MKT_nor = [a, accumarray(q,forthsub_daily.MKT_nor,[],@(x)stat(x))];
RVsub4mkt_nor = volsub4MKT_nor(:,3);

[a,~,q] = unique([forthsub_daily.year,forthsub_daily.month],'rows');
volsub4SMB_nor = [a, accumarray(q,forthsub_daily.SMB_nor,[],@(x)stat(x))];
RVsub4smb_nor = volsub4SMB_nor(:,3);

[a,~,q] = unique([forthsub_daily.year,forthsub_daily.month],'rows');
volsub4HML_nor = [a, accumarray(q,forthsub_daily.HML_nor,[],@(x)stat(x))];
RVsub4hml_nor = volsub4HML_nor(:,3);

[a,~,q] = unique([forthsub_daily.year,forthsub_daily.month],'rows');
volsub4PR1YR_nor = [a, accumarray(q,forthsub_daily.PR1YR_nor,[],@(x)stat(x))];
RVsub4pr1yr_nor = volsub4PR1YR_nor(:,3);

[a,~,q] = unique([forthsub_daily.year,forthsub_daily.month],'rows');
volsub4MOM_nor = [a, accumarray(q,forthsub_daily.MOM_nor,[],@(x)stat(x))];
RVsub4mom_nor = volsub4MOM_nor(:,3);

[a,~,q] = unique([forthsub_daily.year,forthsub_daily.month],'rows');
volsub4LIQ_nor = [a, accumarray(q,forthsub_daily.LIQ_nor,[],@(x)stat(x))];
RVsub4liq_nor = volsub4LIQ_nor(:,3);

%% creating excess return
FFnormonth.MKT_nor= FFnormonth.MKT_nor - FFnormonth.RF_nor;
FFnormonth.SMB_nor= FFnormonth.SMB_nor - FFnormonth.RF_nor;
FFnormonth.HML_nor= FFnormonth.HML_nor - FFnormonth.RF_nor;
FFnormonth.PR1YR_nor= FFnormonth.PR1YR_nor - FFnormonth.RF_nor;
FFnormonth.MOM_nor= FFnormonth.MOM_nor - FFnormonth.RF_nor;
FFnormonth.LIQ_nor= FFnormonth.LIQ_nor - FFnormonth.RF_nor;



%% portfolio cration without c

for i = 2:length(FFnormonth.MKT_nor)
    F_sigma_mkt_nor_uc(i-1,1) = (1/RVmkt_nor((i-1)))*FFnormonth.MKT_nor(i);
end

for i = 2:length(FFnormonth.SMB_nor)
    F_sigma_SMB_nor_uc(i-1,1) = (1/RVsmb_nor((i-1)))*FFnormonth.SMB_nor(i);
end

for i = 2:length(FFnormonth.HML_nor)
    F_sigma_HML_nor_uc(i-1,1) = (1/RVhml_nor((i-1)))*FFnormonth.HML_nor(i);
end

for i = 2:length(FFnormonth.PR1YR_nor)
    F_sigma_PR1YR_nor_uc(i-1,1) = (1/RVpr1yr_nor((i-1)))*FFnormonth.PR1YR_nor(i);
end

for i = 2:length(FFnormonth.MOM_nor)
    F_sigma_MOM_nor_uc(i-1,1) = (1/RVmom_nor((i-1)))*FFnormonth.MOM_nor(i);
end

for i = 2:length(FFnormonth.LIQ_nor)
    F_sigma_LIQ_nor_uc(i-1,1) = (1/RVliq_nor((i-1)))*FFnormonth.LIQ_nor(i);
end

%% creation of C nor
stdmkt_nor= std(FFnormonth.MKT_nor(1:end));
stdmktusigma_nor = std(F_sigma_mkt_nor_uc);
cmkt_nor= stdmkt_nor/stdmktusigma_nor;

stdsmb_nor= std(FFnormonth.SMB_nor);
stdsmbusigma_nor = std(F_sigma_SMB_nor_uc);
csmb_nor= stdsmb_nor/stdsmbusigma_nor;

stdhml_nor = std(FFnormonth.HML_nor);
stdhmlusigma_nor = std(F_sigma_HML_nor_uc);
chml_nor= stdhml_nor/stdhmlusigma_nor;

stdpr1yr_nor = std(FFnormonth.PR1YR_nor);
stdpr1yrusigma_nor = std(F_sigma_PR1YR_nor_uc);
cpr1yr_nor= stdpr1yr_nor/stdpr1yrusigma_nor;

stdmom_nor = std(FFnormonth.MOM_nor);
stdmomusigma_nor = std(F_sigma_MOM_nor_uc);
cmom_nor= stdmom_nor/stdmomusigma_nor;

stdliq_nor = std(FFnormonth.LIQ_nor);
stdliqusigma_nor = std(F_sigma_LIQ_nor_uc);
cliq_nor= stdliq_nor/stdliqusigma_nor;



%%


for i = 2:length(FFnormonth.MKT_nor)
    F_sigma_mkt_nor(i-1,1) = (cmkt_nor/RVmkt_nor((i-1)))*FFnormonth.MKT_nor(i);
end

for i = 2:length(FFnormonth.SMB_nor)
    F_sigma_smb_nor(i-1,1) = (csmb_nor/RVsmb_nor((i-1)))*FFnormonth.SMB_nor(i);
end

for i = 2:length(FFnormonth.HML_nor)
    F_sigma_hml_nor(i-1,1) = (chml_nor/RVhml_nor((i-1)))*FFnormonth.HML_nor(i);
end

for i = 2:length(FFnormonth.PR1YR_nor)
    F_sigma_pr1yr_nor(i-1,1) = (cpr1yr_nor/RVpr1yr_nor((i-1)))*FFnormonth.PR1YR_nor(i);
end

for i = 2:length(FFnormonth.MOM_nor)
    F_sigma_mom_nor(i-1,1) = (cmom_nor/RVmom_nor((i-1)))*FFnormonth.MOM_nor(i);
end

for i = 2:length(FFnormonth.LIQ_nor)
    F_sigma_liq_nor(i-1,1) = (cliq_nor/RVliq_nor((i-1)))*FFnormonth.LIQ_nor(i);
end


%%
rettable_nor = table(F_sigma_mkt_nor, F_sigma_hml_nor, F_sigma_smb_nor,...
    F_sigma_pr1yr_nor,F_sigma_mom_nor,F_sigma_liq_nor, FFnormonth.MKT_nor(2:end));
rettable_nor.Properties.VariableNames{'Var7'} = 'FFmkt_nor';
rettable_nor.FFsmb_nor = FFnormonth.SMB_nor(2:end);
rettable_nor.FFhml_nor= FFnormonth.HML_nor(2:end);
rettable_nor.pr1yr_nor = FFnormonth.PR1YR_nor(2:end);
rettable_nor.mom_nor = FFnormonth.MOM_nor(2:end);
rettable_nor.liq_nor = FFnormonth.LIQ_nor(2:end);

%% Regression nor

regmkt_nor = fitlm(rettable_nor, 'F_sigma_mkt_nor ~FFmkt_nor')
regsmb_nor = fitlm(rettable_nor, 'F_sigma_smb_nor ~FFsmb_nor')
reghml_nor = fitlm(rettable_nor, 'F_sigma_hml_nor ~FFhml_nor')
regpr1yr_nor = fitlm(rettable_nor, 'F_sigma_pr1yr_nor ~ pr1yr_nor')
regmom_nor = fitlm(rettable_nor, 'F_sigma_mom_nor ~ mom_nor')
regliq_nor = fitlm(rettable_nor, 'F_sigma_liq_nor ~ liq_nor')
 %% Single factor Alpha
alphaMKT_nor = regmkt_nor.Coefficients.Estimate(1);
yearlyalphaMKT_nor = alphaMKT_nor*12;
alphaSMB_nor = regsmb_nor.Coefficients.Estimate(1);
yearlyalphaSMB_nor = alphaSMB_nor*12;
alphaHML_nor = reghml_nor.Coefficients.Estimate(1);
yearlyalphaHML_nor = alphaHML_nor*12;
alphaPR1YR_nor = regpr1yr_nor.Coefficients.Estimate(1);
yearlyalphaPR1YR_nor = alphaPR1YR_nor*12;
alphaMOM_nor = regmom_nor.Coefficients.Estimate(1);
yearlyalphaMOM_nor = alphaMOM_nor*12;
alphaLIQ_nor = regliq_nor.Coefficients.Estimate(1);
yearlyalphaLIQ_nor = alphaLIQ_nor*12;
    
%% standard errors NOR
[estcov SEh_mkt_nor coeff] = hac(regmkt_nor);
SEh_mkt_nor = SEh_mkt_nor(2);
[estcov SEh_smb_nor coeff] = hac(regsmb_nor);
SEh_smb_nor = SEh_smb_nor(2);
[estcov SEh_hml_nor coeff] = hac(reghml_nor);
SEh_hml_nor = SEh_hml_nor(2);
[estcov SEh_mom_nor coeff] = hac(regmom_nor);
SEh_mom_nor = SEh_mom_nor(2);
[estcov SEh_pr1yr_nor coeff] = hac(regpr1yr_nor);
SEh_pr1yr_nor = SEh_pr1yr_nor(2);
[estcov SEh_liq_nor coeff] = hac(regliq_nor);
SEh_liq_nor = SEh_liq_nor(2);



%%

Fama_sigma_mkt_nor = fitlm(rettable_nor,'F_sigma_mkt_nor ~ FFmkt_nor+FFsmb_nor+FFhml_nor')
Fama_sigma_hml_nor = fitlm(rettable_nor,'F_sigma_hml_nor ~ FFmkt_nor+FFsmb_nor+FFhml_nor')
Fama_sigma_smb_nor = fitlm(rettable_nor,'F_sigma_smb_nor ~ FFmkt_nor+FFsmb_nor+FFhml_nor')
Fama_sigma_pr1yr_nor = fitlm(rettable_nor, 'F_sigma_pr1yr_nor~FFmkt_nor+FFsmb_nor+FFhml_nor+pr1yr_nor')
Fama_sigma_mom_nor = fitlm(rettable_nor, 'F_sigma_mom_nor~FFmkt_nor+FFsmb_nor+FFhml_nor+mom_nor')
Fama_sigma_liq_nor = fitlm(rettable_nor, 'F_sigma_liq_nor~FFmkt_nor+FFsmb_nor+FFhml_nor+liq_nor')

%% Fama French alphas

FFalphaMKT_nor = Fama_sigma_mkt_nor.Coefficients.Estimate(1);
yearlyFFalphaMKT_nor = FFalphaMKT_nor*12;
FFalphaHML_nor = Fama_sigma_hml_nor.Coefficients.Estimate(1);
yearlyFFalphaHML_nor = FFalphaHML_nor*12;
FFalphaSMB_nor = Fama_sigma_smb_nor.Coefficients.Estimate(1);
yearlyFFalphaSMB_nor = FFalphaSMB_nor*12;
FFalphaPR1YR_nor = Fama_sigma_pr1yr_nor.Coefficients.Estimate(1);
yearlyFFalphaPR1YR_nor = FFalphaPR1YR_nor*12;
FFalphaMOM_nor = Fama_sigma_mom_nor.Coefficients.Estimate(1);
yearlyFFalphaMOM_nor = FFalphaMOM_nor*12;
FFalphaLIQ_nor = Fama_sigma_liq_nor.Coefficients.Estimate(1);
yearlyFFalphaLIQ_nor = FFalphaLIQ_nor*12;







 %% cumret nor 

cumretmkt_nor = zeros(length(F_sigma_mkt_nor),1);
cumretmkt_nor(1,1) = 1;
cumretf_sigma_nor = zeros(length(F_sigma_mkt_nor),1);
cumretf_sigma_nor(1,1) = 1;



for l = 2: length(F_sigma_mkt_nor)
    cumretmkt_nor(l,1) = cumretmkt_nor(l-1,1) * (1+(FFnormonth.MKT_nor(l)/100));
    cumretf_sigma_nor(l,1)= cumretf_sigma_nor(l-1,1) * (1+(F_sigma_mkt_nor(l-1)/100));
end

%% Cumret plot

plot(FFnormonth.date(2:end),cumretmkt_nor)
hold on
set(gca, 'Yscale', 'log')
plot(FFnormonth.date(2:end),cumretf_sigma_nor)
title('Cumulative Returns')
hold off
legend({'Buy and hold','Volatility managed'},'Location','north')
print -depsc cumret

%% MVE 

MVE_month_nor = readtable('MVE_nor.xlsx','Sheet','Monthly');
MVE_month_nor.date = datetime(MVE_month_nor.date, 'ConvertFrom', 'yyyymmdd', 'Format','dd/MM/yyyy');
MVE_month_nor.month = month(MVE_month_nor.date);
MVE_month_nor.year = year(MVE_month_nor.date);
MVE_month_nor.MKT = MVE_month_nor.MKT*100;
MVE_month_nor.FF3 = MVE_month_nor.FF3*100;
MVE_month_nor.FF3MOM = MVE_month_nor.FF3MOM*100;
MVE_month_nor.FF3LIQ = MVE_month_nor.FF3LIQ*100;
MVE_month_nor.FF3MOMLIQ = MVE_month_nor.FF3MOMLIQ*100;

MVE_daily_nor = readtable('MVE_nor.xlsx','Sheet','daily');
MVE_daily_nor.date = datetime(MVE_daily_nor.date, 'ConvertFrom', 'yyyymmdd', 'Format','dd/MM/yyyy');
MVE_daily_nor.month = month(MVE_daily_nor.date);
MVE_daily_nor.year = year(MVE_daily_nor.date);
MVE_daily_nor.MKT = MVE_daily_nor.MKT*100;
MVE_daily_nor.FF3 = MVE_daily_nor.FF3*100;
MVE_daily_nor.FF3MOM = MVE_daily_nor.FF3MOM*100;
MVE_daily_nor.FF3LIQ = MVE_daily_nor.FF3LIQ*100;
MVE_daily_nor.FF3MOMLIQ = MVE_daily_nor.FF3MOMLIQ*100;


%%
[a,~,q] = unique([MVE_daily_nor.year,MVE_daily_nor.month],'rows');
volMKT_nor = [a, accumarray(q,MVE_daily_nor.MKT,[],@(x)stat(x))];
MVE_RVmkt_nor = volMKT_nor(:,3);

[a,~,q] = unique([MVE_daily_nor.year,MVE_daily_nor.month],'rows');
volFF3_nor = [a, accumarray(q,MVE_daily_nor.FF3,[],@(x)stat(x))];
MVE_RVff3_nor = volFF3_nor(:,3);

[a,~,q] = unique([MVE_daily_nor.year,MVE_daily_nor.month],'rows');
volFF3MOM_nor = [a, accumarray(q,MVE_daily_nor.FF3MOM,[],@(x)stat(x))];
MVE_RVff3mom_nor = volFF3MOM_nor(:,3);

[a,~,q] = unique([MVE_daily_nor.year,MVE_daily_nor.month],'rows');
volFF3LIQ_nor = [a, accumarray(q,MVE_daily_nor.FF3LIQ,[],@(x)stat(x))];
MVE_RVff3liq_nor = volFF3LIQ_nor(:,3);

[a,~,q] = unique([MVE_daily_nor.year,MVE_daily_nor.month],'rows');
volFF3MOMLIQ_nor = [a, accumarray(q,MVE_daily_nor.FF3MOMLIQ,[],@(x)stat(x))];
MVE_RVff3momliq_nor = volFF3MOMLIQ_nor(:,3);

%% MVE F_sigma w/o C 

for i = 2:length(MVE_month_nor.FF3)
    F_sigma_FF3_nor(i-1,1) = (1/MVE_RVff3_nor((i-1)))*MVE_month_nor.FF3(i);
end

for i = 2:length(MVE_month_nor.FF3MOM)
    F_sigma_FF3MOM_nor(i-1,1) = (1/MVE_RVff3mom_nor((i-1)))*MVE_month_nor.FF3MOM(i);
end

for i = 2:length(MVE_month_nor.FF3LIQ)
    F_sigma_FF3LIQ_nor(i-1,1) = (1/MVE_RVff3liq_nor((i-1)))*MVE_month_nor.FF3LIQ(i);
end

for i = 2:length(MVE_month_nor.FF3MOMLIQ)
    F_sigma_FF3MOMLIQ_nor(i-1,1) = (1/MVE_RVff3momliq_nor((i-1)))*MVE_month_nor.FF3MOMLIQ(i);
end

%% Creation of C

std_mve_ff3= std(MVE_month_nor.FF3);
std_mve_ff3_usigma = std(F_sigma_FF3_nor);
cMVEFF3_nor= std_mve_ff3/std_mve_ff3_usigma;

std_mve_ff3mom= std(MVE_month_nor.FF3MOM);
std_mve_ff3mom_usigma = std(F_sigma_FF3MOM_nor);
cMVEFF3MOM_nor= std_mve_ff3mom/std_mve_ff3mom_usigma;

std_mve_ff3liq= std(MVE_month_nor.FF3LIQ);
std_mve_ff3liq_usigma = std(F_sigma_FF3LIQ_nor);
cMVEFF3LIQ_nor= std_mve_ff3liq/std_mve_ff3liq_usigma;

std_mve_ff3momliq= std(MVE_month_nor.FF3MOMLIQ);
std_mve_ff3momliq_usigma = std(F_sigma_FF3MOMLIQ_nor);
cMVEFF3MOMLIQ_nor= std_mve_ff3momliq/std_mve_ff3momliq_usigma;

%% MVE port. with C


for i = 2:length(MVE_month_nor.FF3)
    F_sigma_FF3_nor(i-1,1) = (cMVEFF3_nor/MVE_RVff3_nor((i-1)))*MVE_month_nor.FF3(i);
end

for i = 2:length(MVE_month_nor.FF3MOM)
    F_sigma_FF3MOM_nor(i-1,1) = (cMVEFF3MOM_nor/MVE_RVff3mom_nor((i-1)))*MVE_month_nor.FF3MOM(i);
end

for i = 2:length(MVE_month_nor.FF3LIQ)
    F_sigma_FF3LIQ_nor(i-1,1) = (cMVEFF3LIQ_nor/MVE_RVff3liq_nor((i-1)))*MVE_month_nor.FF3LIQ(i);
end

for i = 2:length(MVE_month_nor.FF3MOMLIQ)
    F_sigma_FF3MOMLIQ_nor(i-1,1) = (cMVEFF3MOMLIQ_nor/MVE_RVff3momliq_nor((i-1)))*MVE_month_nor.FF3MOMLIQ(i);
end

%% MVE Regtable
regtable_MVE = table(F_sigma_FF3_nor,F_sigma_FF3LIQ_nor,F_sigma_FF3MOM_nor...
    ,F_sigma_FF3MOMLIQ_nor);
regtable_MVE.FF3= MVE_month_nor.FF3(2:end);
regtable_MVE.FF3MOM = MVE_month_nor.FF3MOM(2:end);
regtable_MVE.FF3LIQ = MVE_month_nor.FF3LIQ(2:end);
regtable_MVE.FF3MOMLIQ = MVE_month_nor.FF3MOMLIQ(2:end);
regtable_MVE.FFmkt = FFnormonth.MKT_nor(2:end);
regtable_MVE.FFsmb = FFnormonth.SMB_nor(2:end);
regtable_MVE.FFhml = FFnormonth.HML_nor(2:end);

%% Regression MVE

reg_MVE_FF3 = fitlm(regtable_MVE, 'F_sigma_FF3_nor ~FF3')
reg_MVE_FF3LIQ = fitlm(regtable_MVE, 'F_sigma_FF3LIQ_nor ~ FF3LIQ')
reg_MVE_FF3MOM = fitlm(regtable_MVE, 'F_sigma_FF3MOM_nor ~ FF3MOM')
reg_MVE_FF3MOMLIQ = fitlm(regtable_MVE, 'F_sigma_FF3MOMLIQ_nor ~ FF3MOMLIQ')

%% MVE alpha 

MVE_alpha_FF3 = reg_MVE_FF3.Coefficients.Estimate(1)*12;
MVE_alpha_FF3LIQ = reg_MVE_FF3LIQ.Coefficients.Estimate(1)*12;
MVE_alpha_FF3MOM = reg_MVE_FF3MOM.Coefficients.Estimate(1)*12;
MVE_alpha_FF3MOMLIQ = reg_MVE_FF3MOMLIQ.Coefficients.Estimate(1)*12;

%% SE

[estcov SEh_mveff3 coeff] = hac(reg_MVE_FF3);
SEh_mveff3 = SEh_mveff3(2);
[estcov SEh_mveff3liq coeff] = hac(reg_MVE_FF3LIQ);
SEh_mveff3liq = SEh_mveff3liq(2);
[estcov SEh_mveff3mom coeff] = hac(reg_MVE_FF3MOM);
SEh_mveff3mom = SEh_mveff3mom(2);
[estcov SEh_mveff3momliq coeff] = hac(reg_MVE_FF3MOMLIQ);
SEh_mveff3momliq = SEh_mveff3momliq(2);



%% Fama French controll MVE
FF_MVE_FF3 = fitlm(regtable_MVE, 'F_sigma_FF3_nor ~ FFmkt+FFsmb+FFhml+FF3')
FF_MVE_FF3LIQ = fitlm(regtable_MVE, 'F_sigma_FF3LIQ_nor ~ FFmkt+FFsmb+FFhml+FF3LIQ')
FF_MVE_FF3MOM = fitlm(regtable_MVE, 'F_sigma_FF3MOM_nor ~FFmkt+FFsmb+FFhml+FF3MOM')
FF_MVE_FF3MOMLIQ = fitlm(regtable_MVE,'F_sigma_FF3MOMLIQ_nor ~ FFmkt+FFsmb+FFhml+FF3MOMLIQ')

%% Fama-French alpha
MVE_FFalpha_FF3 = FF_MVE_FF3.Coefficients.Estimate(1)*12;
MVE_FFalpha_FF3LIQ = FF_MVE_FF3LIQ.Coefficients.Estimate(1)*12;
MVE_FFalpha_FF3MOM = FF_MVE_FF3MOM.Coefficients.Estimate(1)*12;
MVE_FFalpha_FF3MOMLIQ =FF_MVE_FF3MOMLIQ.Coefficients.Estimate(1)*12;


%% Appraisal Ratio

AR_mkt_nor = sqrt(12)*((regmkt_nor.Coefficients.Estimate(1)/regmkt_nor.RMSE));
AR_hml_nor = sqrt(12)*((reghml_nor.Coefficients.Estimate(1)/reghml_nor.RMSE));
AR_smb_nor = sqrt(12)*((regsmb_nor.Coefficients.Estimate(1)/regsmb_nor.RMSE));
AR_mom_nor = sqrt(12)*((regmom_nor.Coefficients.Estimate(1)/regmom_nor.RMSE));
AR_pr1yr_nor = sqrt(12)*((regpr1yr_nor.Coefficients.Estimate(1)/regpr1yr_nor.RMSE))
AR_lig_nor = sqrt(12)*((regliq_nor.Coefficients.Estimate(1)/regliq_nor.RMSE));
AR_MVE_FF3 = sqrt(12)*((reg_MVE_FF3.Coefficients.Estimate(1)/reg_MVE_FF3.RMSE));
AR_MVE_FF3MOM = sqrt(12)*((reg_MVE_FF3MOM.Coefficients.Estimate(1)/reg_MVE_FF3MOM.RMSE));
AR_MVE_FF3LIQ = sqrt(12)*((reg_MVE_FF3LIQ.Coefficients.Estimate(1)/reg_MVE_FF3LIQ.RMSE));
AR_MVE_FF3MOMLIQ = sqrt(12)*((reg_MVE_FF3MOMLIQ.Coefficients.Estimate(1)/reg_MVE_FF3MOMLIQ.RMSE));

%% Summary statistic
MKT_mean_nor = mean(FFnormonth.MKT_nor);
MKT_max_nor = max(FFnormonth.MKT_nor);
MKT_min_nor = min(FFnormonth.MKT_nor);
MKT_N_nor = numel(FFnormonth.MKT_nor);
MKT_nor = [MKT_mean_nor;MKT_max_nor;MKT_min_nor;MKT_N_nor]

%%
SMB_mean_nor = mean(FFnormonth.SMB_nor);
SMB_max_nor = max(FFnormonth.SMB_nor);
SMB_min_nor = min(FFnormonth.SMB_nor);
SMB_N_nor = numel(FFnormonth.SMB_nor);
SMB_nor = [SMB_mean_nor;SMB_max_nor;SMB_min_nor;SMB_N_nor]
%%


HML_mean_nor = mean(FFnormonth.HML_nor);
HML_max_nor = max(FFnormonth.HML_nor); 
HML_min_nor = min(FFnormonth.HML_nor);
HML_N_nor = numel(FFnormonth.HML_nor);
HML_nor = [HML_mean_nor;HML_max_nor;HML_min_nor;HML_N_nor]
%%
PR1YR_mean_nor= mean(FFnormonth.PR1YR_nor);
PR1YR_max_nor = max(FFnormonth.PR1YR_nor);
PR1YR_min_nor = min(FFnormonth.PR1YR_nor);
PR1YR_N_nor = numel(FFnormonth.PR1YR_nor);
PR1YR_nor = [PR1YR_mean_nor;PR1YR_max_nor;PR1YR_min_nor;PR1YR_N_nor]
%%
MOM_mean_nor = mean(FFnormonth.MOM_nor);
MOM_max_nor = max(FFnormonth.MOM_nor);
MOM_min_nor = min(FFnormonth.MOM_nor);
MOM_N_nor = numel(FFnormonth.MOM_nor);
MOM_nor = [MOM_mean_nor;MOM_max_nor;MOM_min_nor;MOM_N_nor]
%%
LIQ_mean_nor = mean(FFnormonth.LIQ_nor);
LIQ_max_nor = max(FFnormonth.LIQ_nor);
LIQ_min_nor = min(FFnormonth.LIQ_nor);
LIQ_N_nor = numel(FFnormonth.LIQ_nor);
LIQ_nor = [LIQ_mean_nor;LIQ_max_nor;LIQ_min_nor;LIQ_N_nor]

%%

RF_mean_nor = mean(FFnormonth.RF_nor);
RF_max_nor = max(FFnormonth.RF_nor); 
RF_min_nor = min(FFnormonth.RF_nor);
RF_N_nor = numel(FFnormonth.RF_nor);
RF_nor = [RF_mean_nor;RF_max_nor;RF_min_nor;RF_N_nor]
%%
MKT_mean_nor_daily = mean(FFnordaily.MKT_nor);
MKT_max_nor_daily = max(FFnordaily.MKT_nor);
MKT_min_nor_daily = min(FFnordaily.MKT_nor);
MKT_N_nor_daily = numel(FFnordaily.MKT_nor);


%%
SMB_mean_nor_daily = mean(FFnordaily.SMB_nor);
SMB_max_nor_daily = max(FFnordaily.SMB_nor);
SMB_min_nor_daily = min(FFnordaily.SMB_nor);
SMB_N_nor_daily = numel(FFnordaily.SMB_nor);

%%


HML_mean_nor_daily = mean(FFnordaily.HML_nor);
HML_max_nor_daily = max(FFnordaily.HML_nor); 
HML_min_nor_daily = min(FFnordaily.HML_nor);
HML_N_nor_daily = numel(FFnordaily.HML_nor);

%%
PR1YR_mean_nor_daily= mean(FFnordaily.PR1YR_nor);
PR1YR_max_nor_daily = max(FFnordaily.PR1YR_nor);
PR1YR_min_nor_daily = min(FFnordaily.PR1YR_nor);
PR1YR_N_nor_daily = numel(FFnordaily.PR1YR_nor);

%%
MOM_mean_nor_daily = mean(FFnordaily.MOM_nor);
MOM_max_nor_daily = max(FFnordaily.MOM_nor);
MOM_min_nor_daily = min(FFnordaily.MOM_nor);
MOM_N_nor_daily = numel(FFnordaily.MOM_nor);

%%
LIQ_mean_nor_daily = mean(FFnordaily.LIQ_nor);
LIQ_max_nor_daily = max(FFnordaily.LIQ_nor);
LIQ_min_nor_daily = min(FFnordaily.LIQ_nor);
LIQ_N_nor_daily = numel(FFnordaily.LIQ_nor);


%%

RF_mean_nor_daily = mean(FFnordaily.RF_nor);
RF_max_nor_daily = max(FFnordaily.RF_nor); 
RF_min_nor_daily = min(FFnordaily.RF_nor);
RF_N_nor_daily = numel(FFnordaily.RF_nor);

%%

MKT_nor_daily = [MKT_mean_nor_daily;MKT_max_nor_daily;MKT_min_nor_daily;MKT_N_nor_daily]
SMB_nor_daily = [SMB_mean_nor_daily;SMB_max_nor_daily;SMB_min_nor_daily;SMB_N_nor_daily]
HML_nor_daily = [HML_mean_nor_daily;HML_max_nor_daily;HML_min_nor_daily;HML_N_nor_daily]
PR1YR_nor_daily = [PR1YR_mean_nor_daily;PR1YR_max_nor_daily;PR1YR_min_nor_daily;PR1YR_N_nor_daily]
MOM_nor_daily = [MOM_mean_nor_daily;MOM_max_nor_daily;MOM_min_nor_daily;MOM_N_nor_daily]
LIQ_nor_daily = [LIQ_mean_nor_daily;LIQ_max_nor_daily;LIQ_min_nor_daily;LIQ_N_nor_daily]
RF_nor_daily = [RF_mean_nor_daily;RF_max_nor_daily;RF_min_nor_daily;RF_N_nor_daily]

sumdaily = [MKT_nor_daily,SMB_nor_daily,HML_nor_daily,MOM_nor_daily,PR1YR_nor_daily...
    ,LIQ_nor_daily,RF_nor_daily]

latex_sumdaily = latex(vpa(sym(sumdaily),3))

%% correlation matrix
corrmonth = corr(table2array(FFnormonth(:,(2:8))))

latex_cormonth =latex(vpa(sym(corrmonth),3))

%% Subperiode analysis 

firstsub = FFnormonth.year <=1990 ;
firstsub_month=FFnormonth(firstsub,:);
firstsub = FFnordaily.year <= 1990;
firstsub_daily = FFnordaily(firstsub,:);

secondsub_month = FFnormonth((FFnormonth.year<=2000),:);
fjern = secondsub_month.year <= 1990;
secondsub_month(fjern,:) = [];
secondsub_daily = FFnordaily((FFnormonth.year<=2000),:);
fjern = secondsub_daily.year<= 1990;
secondsub_daily(fjern,:)=[];

thirdsub_month = FFnormonth((FFnormonth.year<=2010),:);
fjern = thirdsub_month.year <= 2000;
thirdsub_month(fjern,:) = [];

thirdsub_daily = FFnordaily((FFnormonth.year<=2010),:);
fjern = thirdsub_daily.year<= 2000;
thirdsub_daily(fjern,:)=[];

forthsub_month = FFnormonth((FFnormonth.year<=2020),:);
fjern = forthsub_month.year <= 2010;
forthsub_month(fjern,:) = [];

forthsub_daily = FFnordaily((FFnormonth.year<=2020),:);
fjern = forthsub_daily.year<= 2010;
forthsub_daily(fjern,:)=[];

%% Creation of portfolio without C


for i = 2:length(firstsub_month.MKT_nor)
    F_sigma_sub1_mkt_nor_uc(i-1,1) = (1/RVsub1mkt_nor((i-1)))*firstsub_month.MKT_nor(i);
end

for i = 2:length(firstsub_month.SMB_nor)
    F_sigma_sub1_SMB_nor_uc(i-1,1) = (1/RVsub1smb_nor((i-1)))*firstsub_month.SMB_nor(i);
end

for i = 2:length(firstsub_month.HML_nor)
    F_sigma_sub1_HML_nor_uc(i-1,1) = (1/RVsub1hml_nor((i-1)))*firstsub_month.HML_nor(i);
end

for i = 2:length(firstsub_month.PR1YR_nor)
    F_sigma_sub1_PR1YR_nor_uc(i-1,1) = (1/RVsub1pr1yr_nor((i-1)))*firstsub_month.PR1YR_nor(i);
end

for i = 2:length(firstsub_month.MOM_nor)
    F_sigma_sub1_MOM_nor_uc(i-1,1) = (1/RVsub1mom_nor((i-1)))*firstsub_month.MOM_nor(i);
end

for i = 2:length(firstsub_month.LIQ_nor)
    F_sigma_sub1_LIQ_nor_uc(i-1,1) = (1/RVsub1liq_nor((i-1)))*firstsub_month.LIQ_nor(i);
end

%% Creation of C sub 1 

stdmkt_sub1_nor= std(firstsub_month.MKT_nor(1:end));
stdmktusigma_nor = std(F_sigma_sub1_mkt_nor_uc);
cmkt_sub1_nor= stdmkt_sub1_nor/stdmktusigma_nor;

stdsmb_sub1_nor= std(firstsub_month.SMB_nor);
stdsmbusigma_sub1_nor = std(F_sigma_sub1_SMB_nor_uc);
csmb_sub1_nor= stdsmb_sub1_nor/stdsmbusigma_sub1_nor;

stdhml_sub1_nor = std(firstsub_month.HML_nor);
stdhmlusigma_sub1_nor = std(F_sigma_sub1_HML_nor_uc);
chml_sub1_nor= stdhml_sub1_nor/stdhmlusigma_sub1_nor;

stdpr1yr_sub1_nor = std(firstsub_month.PR1YR_nor);
stdpr1yrusigma_sub1_nor = std(F_sigma_sub1_PR1YR_nor_uc);
cpr1yr_sub1_nor= stdpr1yr_sub1_nor/stdpr1yrusigma_sub1_nor;

stdmom_sub1_nor = std(firstsub_month.MOM_nor);
stdmomusigma_sub1_nor = std(F_sigma_sub1_MOM_nor_uc);
cmom_sub1_nor= stdmom_sub1_nor/stdmomusigma_sub1_nor;

stdliq_sub1_nor = std(firstsub_month.LIQ_nor);
stdliqusigma_sub1_nor = std(F_sigma_sub1_LIQ_nor_uc);
cliq_sub1_nor= stdliq_sub1_nor/stdliqusigma_sub1_nor;

%% 

for i = 2:length(firstsub_month.MKT_nor)
    F_sigma_sub1_mkt_nor(i-1,1) = (cmkt_sub1_nor/RVsub1mkt_nor((i-1)))*firstsub_month.MKT_nor(i);
end

for i = 2:length(firstsub_month.SMB_nor)
    F_sigma_sub1_smb_nor(i-1,1) = (csmb_nor/RVsub1smb_nor((i-1)))*firstsub_month.SMB_nor(i);
end

for i = 2:length(firstsub_month.HML_nor)
    F_sigma_sub1_hml_nor(i-1,1) = (chml_sub1_nor/RVsub1hml_nor((i-1)))*firstsub_month.HML_nor(i);
end

for i = 2:length(firstsub_month.PR1YR_nor)
    F_sigma_sub1_pr1yr_nor(i-1,1) = (cpr1yr_sub1_nor/RVsub1pr1yr_nor((i-1)))*firstsub_month.PR1YR_nor(i);
end

for i = 2:length(firstsub_month.MOM_nor)
    F_sigma_sub1_mom_nor(i-1,1) = (cmom_sub1_nor/RVsub1mom_nor((i-1)))*firstsub_month.MOM_nor(i);
end

for i = 2:length(firstsub_month.LIQ_nor)
    F_sigma_sub1_liq_nor(i-1,1) = (cliq_sub1_nor/RVsub1liq_nor((i-1)))*firstsub_month.LIQ_nor(i);
end



regsub1 = array2table([firstsub_month.MKT_nor(2:end),firstsub_month.SMB_nor(2:end),...
    firstsub_month.HML_nor(2:end),firstsub_month.PR1YR_nor(2:end),...
    firstsub_month.MOM_nor(2:end), firstsub_month.LIQ_nor(2:end)]);
regsub1.F_sigma_sub1_hml_nor = F_sigma_sub1_hml_nor;
regsub1.F_sigma_sub1_mkt_nor = F_sigma_sub1_mkt_nor;
regsub1.F_sigma_sub1_smb_nor = F_sigma_sub1_smb_nor;
regsub1.F_sigma_sub1_mom_nor = F_sigma_sub1_mom_nor;
regsub1.F_sigma_sub1_pr1yr_nor = F_sigma_sub1_pr1yr_nor;
regsub1.F_sigma_sub1_liq_nor = F_sigma_sub1_hml_nor;
regsub1.Properties.VariableNames{'Var1'} = 'FFmkt_nor';
regsub1.Properties.VariableNames{'Var2'} = 'FFsmb_nor';
regsub1.Properties.VariableNames{'Var3'} = 'FFhml_nor';
regsub1.Properties.VariableNames{'Var4'} = 'pr1yr_nor';
regsub1.Properties.VariableNames{'Var5'} = 'mom_nor';
regsub1.Properties.VariableNames{'Var6'} = 'liq_nor';

regsub1_mkt = fitlm(regsub1,'F_sigma_sub1_mkt_nor ~FFmkt_nor')
regsub1_smb = fitlm(regsub1,'F_sigma_sub1_smb_nor ~FFsmb_nor')
regsub1_hml = fitlm(regsub1,'F_sigma_sub1_hml_nor ~FFhml_nor')
regsub1_mom = fitlm(regsub1,'F_sigma_sub1_mom_nor ~mom_nor')
regsub1_pr1yr = fitlm(regsub1,'F_sigma_sub1_pr1yr_nor ~pr1yr_nor')
regsub1_liq = fitlm(regsub1,'F_sigma_sub1_liq_nor ~liq_nor')

%% Secondsub analysis 

for i = 2:length(secondsub_month.MKT_nor)
    F_sigma_sub2_mkt_nor_uc(i-1,1) = (1/RVsub2mkt_nor((i-1)))*secondsub_month.MKT_nor(i);
end

for i = 2:length(secondsub_month.SMB_nor)
    F_sigma_sub2_SMB_nor_uc(i-1,1) = (1/RVsub2smb_nor((i-1)))*secondsub_month.SMB_nor(i);
end

for i = 2:length(secondsub_month.HML_nor)
    F_sigma_sub2_HML_nor_uc(i-1,1) = (1/RVsub2hml_nor((i-1)))*secondsub_month.HML_nor(i);
end

for i = 2:length(secondsub_month.PR1YR_nor)
    F_sigma_sub2_PR1YR_nor_uc(i-1,1) = (1/RVsub2pr1yr_nor((i-1)))*secondsub_month.PR1YR_nor(i);
end

for i = 2:length(secondsub_month.MOM_nor)
    F_sigma_sub2_MOM_nor_uc(i-1,1) = (1/RVsub2mom_nor((i-1)))*secondsub_month.MOM_nor(i);
end

for i = 2:length(secondsub_month.LIQ_nor)
    F_sigma_sub2_LIQ_nor_uc(i-1,1) = (1/RVsub2liq_nor((i-1)))*secondsub_month.LIQ_nor(i);
end

stdmkt_sub2_nor= std(secondsub_month.MKT_nor(1:end));
stdmktusigma_nor = std(F_sigma_sub2_mkt_nor_uc);
cmkt_sub2_nor= stdmkt_sub2_nor/stdmktusigma_nor;

stdsmb_sub2_nor= std(secondsub_month.SMB_nor);
stdsmbusigma_sub2_nor = std(F_sigma_sub2_SMB_nor_uc);
csmb_sub2_nor= stdsmb_sub2_nor/stdsmbusigma_sub2_nor;

stdhml_sub2_nor = std(secondsub_month.HML_nor);
stdhmlusigma_sub2_nor = std(F_sigma_sub2_HML_nor_uc);
chml_sub2_nor= stdhml_sub2_nor/stdhmlusigma_sub2_nor;

stdpr1yr_sub2_nor = std(secondsub_month.PR1YR_nor);
stdpr1yrusigma_sub2_nor = std(F_sigma_sub2_PR1YR_nor_uc);
cpr1yr_sub2_nor= stdpr1yr_sub2_nor/stdpr1yrusigma_sub2_nor;

stdmom_sub2_nor = std(secondsub_month.MOM_nor);
stdmomusigma_sub2_nor = std(F_sigma_sub2_MOM_nor_uc);
cmom_sub2_nor= stdmom_sub2_nor/stdmomusigma_sub2_nor;

stdliq_sub2_nor = std(secondsub_month.LIQ_nor);
stdliqusigma_sub2_nor = std(F_sigma_sub2_LIQ_nor_uc);
cliq_sub2_nor= stdliq_sub2_nor/stdliqusigma_sub2_nor;


for i = 2:length(secondsub_month.MKT_nor)
    F_sigma_sub2_mkt_nor(i-1,1) = (cmkt_sub2_nor/RVsub2mkt_nor((i-1)))*secondsub_month.MKT_nor(i);
end

for i = 2:length(secondsub_month.SMB_nor)
    F_sigma_sub2_smb_nor(i-1,1) = (csmb_nor/RVsub2smb_nor((i-1)))*secondsub_month.SMB_nor(i);
end

for i = 2:length(secondsub_month.HML_nor)
    F_sigma_sub2_hml_nor(i-1,1) = (chml_sub2_nor/RVsub2hml_nor((i-1)))*secondsub_month.HML_nor(i);
end

for i = 2:length(secondsub_month.PR1YR_nor)
    F_sigma_sub2_pr1yr_nor(i-1,1) = (cpr1yr_sub2_nor/RVsub2pr1yr_nor((i-1)))*secondsub_month.PR1YR_nor(i);
end

for i = 2:length(secondsub_month.MOM_nor)
    F_sigma_sub2_mom_nor(i-1,1) = (cmom_sub2_nor/RVsub2mom_nor((i-1)))*secondsub_month.MOM_nor(i);
end

for i = 2:length(secondsub_month.LIQ_nor)
    F_sigma_sub2_liq_nor(i-1,1) = (cliq_sub2_nor/RVsub2liq_nor((i-1)))*secondsub_month.LIQ_nor(i);
end


regsub2 = array2table([secondsub_month.MKT_nor(2:end),secondsub_month.SMB_nor(2:end),...
    secondsub_month.HML_nor(2:end),secondsub_month.PR1YR_nor(2:end),...
    secondsub_month.MOM_nor(2:end), secondsub_month.LIQ_nor(2:end)]);
regsub2.F_sigma_sub2_hml_nor = F_sigma_sub2_hml_nor;
regsub2.F_sigma_sub2_mkt_nor = F_sigma_sub2_mkt_nor;
regsub2.F_sigma_sub2_smb_nor = F_sigma_sub2_smb_nor;
regsub2.F_sigma_sub2_mom_nor = F_sigma_sub2_mom_nor;
regsub2.F_sigma_sub2_pr1yr_nor = F_sigma_sub2_pr1yr_nor;
regsub2.F_sigma_sub2_liq_nor = F_sigma_sub2_hml_nor;
regsub2.Properties.VariableNames{'Var1'} = 'FFmkt_nor';
regsub2.Properties.VariableNames{'Var2'} = 'FFsmb_nor';
regsub2.Properties.VariableNames{'Var3'} = 'FFhml_nor';
regsub2.Properties.VariableNames{'Var4'} = 'pr1yr_nor';
regsub2.Properties.VariableNames{'Var5'} = 'mom_nor';
regsub2.Properties.VariableNames{'Var6'} = 'liq_nor';

regsub2_mkt = fitlm(regsub2,'F_sigma_sub2_mkt_nor ~FFmkt_nor')
regsub2_smb = fitlm(regsub2,'F_sigma_sub2_smb_nor ~FFsmb_nor')
regsub2_hml = fitlm(regsub2,'F_sigma_sub2_hml_nor ~FFhml_nor')
regsub2_mom = fitlm(regsub2,'F_sigma_sub2_mom_nor ~mom_nor')
regsub2_pr1yr = fitlm(regsub2,'F_sigma_sub2_pr1yr_nor ~pr1yr_nor')
regsub2_liq = fitlm(regsub2,'F_sigma_sub2_liq_nor ~liq_nor')

%% Thirds sub analysis


for i = 2:length(thirdsub_month.MKT_nor)
    F_sigma_sub3_mkt_nor_uc(i-1,1) = (1/RVsub3mkt_nor((i-1)))*thirdsub_month.MKT_nor(i);
end

for i = 2:length(thirdsub_month.SMB_nor)
    F_sigma_sub3_SMB_nor_uc(i-1,1) = (1/RVsub3smb_nor((i-1)))*thirdsub_month.SMB_nor(i);
end

for i = 2:length(thirdsub_month.HML_nor)
    F_sigma_sub3_HML_nor_uc(i-1,1) = (1/RVsub3hml_nor((i-1)))*thirdsub_month.HML_nor(i);
end

for i = 2:length(thirdsub_month.PR1YR_nor)
    F_sigma_sub3_PR1YR_nor_uc(i-1,1) = (1/RVsub3pr1yr_nor((i-1)))*thirdsub_month.PR1YR_nor(i);
end

for i = 2:length(thirdsub_month.MOM_nor)
    F_sigma_sub3_MOM_nor_uc(i-1,1) = (1/RVsub3mom_nor((i-1)))*thirdsub_month.MOM_nor(i);
end

for i = 2:length(thirdsub_month.LIQ_nor)
    F_sigma_sub3_LIQ_nor_uc(i-1,1) = (1/RVsub3liq_nor((i-1)))*thirdsub_month.LIQ_nor(i);
end

stdmkt_sub3_nor= std(thirdsub_month.MKT_nor(1:end));
stdmktusigma_nor = std(F_sigma_sub3_mkt_nor_uc);
cmkt_sub3_nor= stdmkt_sub3_nor/stdmktusigma_nor;

stdsmb_sub3_nor= std(thirdsub_month.SMB_nor);
stdsmbusigma_sub3_nor = std(F_sigma_sub3_SMB_nor_uc);
csmb_sub3_nor= stdsmb_sub3_nor/stdsmbusigma_sub3_nor;

stdhml_sub3_nor = std(thirdsub_month.HML_nor);
stdhmlusigma_sub3_nor = std(F_sigma_sub3_HML_nor_uc);
chml_sub3_nor= stdhml_sub3_nor/stdhmlusigma_sub3_nor;

stdpr1yr_sub3_nor = std(thirdsub_month.PR1YR_nor);
stdpr1yrusigma_sub3_nor = std(F_sigma_sub3_PR1YR_nor_uc);
cpr1yr_sub3_nor= stdpr1yr_sub3_nor/stdpr1yrusigma_sub3_nor;

stdmom_sub3_nor = std(thirdsub_month.MOM_nor);
stdmomusigma_sub3_nor = std(F_sigma_sub3_MOM_nor_uc);
cmom_sub3_nor= stdmom_sub3_nor/stdmomusigma_sub3_nor;

stdliq_sub3_nor = std(thirdsub_month.LIQ_nor);
stdliqusigma_sub3_nor = std(F_sigma_sub3_LIQ_nor_uc);
cliq_sub3_nor= stdliq_sub3_nor/stdliqusigma_sub3_nor;


for i = 2:length(thirdsub_month.MKT_nor)
    F_sigma_sub3_mkt_nor(i-1,1) = (cmkt_sub3_nor/RVsub3mkt_nor((i-1)))*thirdsub_month.MKT_nor(i);
end

for i = 2:length(thirdsub_month.SMB_nor)
    F_sigma_sub3_smb_nor(i-1,1) = (csmb_nor/RVsub3smb_nor((i-1)))*thirdsub_month.SMB_nor(i);
end

for i = 2:length(thirdsub_month.HML_nor)
    F_sigma_sub3_hml_nor(i-1,1) = (chml_sub3_nor/RVsub3hml_nor((i-1)))*thirdsub_month.HML_nor(i);
end

for i = 2:length(thirdsub_month.PR1YR_nor)
    F_sigma_sub3_pr1yr_nor(i-1,1) = (cpr1yr_sub3_nor/RVsub3pr1yr_nor((i-1)))*thirdsub_month.PR1YR_nor(i);
end

for i = 2:length(thirdsub_month.MOM_nor)
    F_sigma_sub3_mom_nor(i-1,1) = (cmom_sub3_nor/RVsub3mom_nor((i-1)))*thirdsub_month.MOM_nor(i);
end

for i = 2:length(thirdsub_month.LIQ_nor)
    F_sigma_sub3_liq_nor(i-1,1) = (cliq_sub3_nor/RVsub3liq_nor((i-1)))*thirdsub_month.LIQ_nor(i);
end


regsub3 = array2table([thirdsub_month.MKT_nor(2:end),thirdsub_month.SMB_nor(2:end),...
    thirdsub_month.HML_nor(2:end),thirdsub_month.PR1YR_nor(2:end),...
    thirdsub_month.MOM_nor(2:end), thirdsub_month.LIQ_nor(2:end)]);
regsub3.F_sigma_sub3_hml_nor = F_sigma_sub3_hml_nor;
regsub3.F_sigma_sub3_mkt_nor = F_sigma_sub3_mkt_nor;
regsub3.F_sigma_sub3_smb_nor = F_sigma_sub3_smb_nor;
regsub3.F_sigma_sub3_mom_nor = F_sigma_sub3_mom_nor;
regsub3.F_sigma_sub3_pr1yr_nor = F_sigma_sub3_pr1yr_nor;
regsub3.F_sigma_sub3_liq_nor = F_sigma_sub3_hml_nor;
regsub3.Properties.VariableNames{'Var1'} = 'FFmkt_nor';
regsub3.Properties.VariableNames{'Var2'} = 'FFsmb_nor';
regsub3.Properties.VariableNames{'Var3'} = 'FFhml_nor';
regsub3.Properties.VariableNames{'Var4'} = 'pr1yr_nor';
regsub3.Properties.VariableNames{'Var5'} = 'mom_nor';
regsub3.Properties.VariableNames{'Var6'} = 'liq_nor';

regsub3_mkt = fitlm(regsub3,'F_sigma_sub3_mkt_nor ~FFmkt_nor')
regsub3_smb = fitlm(regsub3,'F_sigma_sub3_smb_nor ~FFsmb_nor')
regsub3_hml = fitlm(regsub3,'F_sigma_sub3_hml_nor ~FFhml_nor')
regsub3_mom = fitlm(regsub3,'F_sigma_sub3_mom_nor ~mom_nor')
regsub3_pr1yr = fitlm(regsub3,'F_sigma_sub3_pr1yr_nor ~pr1yr_nor')
regsub3_liq = fitlm(regsub3,'F_sigma_sub3_liq_nor ~liq_nor')


%% Forthsub analysis


for i = 2:length(forthsub_month.MKT_nor)
    F_sigma_sub4_mkt_nor_uc(i-1,1) = (1/RVsub4mkt_nor((i-1)))*forthsub_month.MKT_nor(i);
end

for i = 2:length(forthsub_month.SMB_nor)
    F_sigma_sub4_SMB_nor_uc(i-1,1) = (1/RVsub4smb_nor((i-1)))*forthsub_month.SMB_nor(i);
end

for i = 2:length(forthsub_month.HML_nor)
    F_sigma_sub4_HML_nor_uc(i-1,1) = (1/RVsub4hml_nor((i-1)))*forthsub_month.HML_nor(i);
end

for i = 2:length(forthsub_month.PR1YR_nor)
    F_sigma_sub4_PR1YR_nor_uc(i-1,1) = (1/RVsub4pr1yr_nor((i-1)))*forthsub_month.PR1YR_nor(i);
end

for i = 2:length(forthsub_month.MOM_nor)
    F_sigma_sub4_MOM_nor_uc(i-1,1) = (1/RVsub4mom_nor((i-1)))*forthsub_month.MOM_nor(i);
end

for i = 2:length(forthsub_month.LIQ_nor)
    F_sigma_sub4_LIQ_nor_uc(i-1,1) = (1/RVsub4liq_nor((i-1)))*forthsub_month.LIQ_nor(i);
end

stdmkt_sub4_nor= std(forthsub_month.MKT_nor(1:end));
stdmktusigma_nor = std(F_sigma_sub4_mkt_nor_uc);
cmkt_sub4_nor= stdmkt_sub4_nor/stdmktusigma_nor;

stdsmb_sub4_nor= std(forthsub_month.SMB_nor);
stdsmbusigma_sub4_nor = std(F_sigma_sub4_SMB_nor_uc);
csmb_sub4_nor= stdsmb_sub4_nor/stdsmbusigma_sub4_nor;

stdhml_sub4_nor = std(forthsub_month.HML_nor);
stdhmlusigma_sub4_nor = std(F_sigma_sub4_HML_nor_uc);
chml_sub4_nor= stdhml_sub4_nor/stdhmlusigma_sub4_nor;

stdpr1yr_sub4_nor = std(forthsub_month.PR1YR_nor);
stdpr1yrusigma_sub4_nor = std(F_sigma_sub4_PR1YR_nor_uc);
cpr1yr_sub4_nor= stdpr1yr_sub4_nor/stdpr1yrusigma_sub4_nor;

stdmom_sub4_nor = std(forthsub_month.MOM_nor);
stdmomusigma_sub4_nor = std(F_sigma_sub4_MOM_nor_uc);
cmom_sub4_nor= stdmom_sub4_nor/stdmomusigma_sub4_nor;

stdliq_sub4_nor = std(forthsub_month.LIQ_nor);
stdliqusigma_sub4_nor = std(F_sigma_sub4_LIQ_nor_uc);
cliq_sub4_nor= stdliq_sub4_nor/stdliqusigma_sub4_nor;


for i = 2:length(forthsub_month.MKT_nor)
    F_sigma_sub4_mkt_nor(i-1,1) = (cmkt_sub4_nor/RVsub4mkt_nor((i-1)))*forthsub_month.MKT_nor(i);
end

for i = 2:length(forthsub_month.SMB_nor)
    F_sigma_sub4_smb_nor(i-1,1) = (csmb_nor/RVsub4smb_nor((i-1)))*forthsub_month.SMB_nor(i);
end

for i = 2:length(forthsub_month.HML_nor)
    F_sigma_sub4_hml_nor(i-1,1) = (chml_sub4_nor/RVsub4hml_nor((i-1)))*forthsub_month.HML_nor(i);
end

for i = 2:length(forthsub_month.PR1YR_nor)
    F_sigma_sub4_pr1yr_nor(i-1,1) = (cpr1yr_sub4_nor/RVsub4pr1yr_nor((i-1)))*forthsub_month.PR1YR_nor(i);
end

for i = 2:length(forthsub_month.MOM_nor)
    F_sigma_sub4_mom_nor(i-1,1) = (cmom_sub4_nor/RVsub4mom_nor((i-1)))*forthsub_month.MOM_nor(i);
end

for i = 2:length(forthsub_month.LIQ_nor)
    F_sigma_sub4_liq_nor(i-1,1) = (cliq_sub4_nor/RVsub4liq_nor((i-1)))*forthsub_month.LIQ_nor(i);
end


regsub4 = array2table([forthsub_month.MKT_nor(2:end),forthsub_month.SMB_nor(2:end),...
    forthsub_month.HML_nor(2:end),forthsub_month.PR1YR_nor(2:end),...
    forthsub_month.MOM_nor(2:end), forthsub_month.LIQ_nor(2:end)]);
regsub4.F_sigma_sub4_hml_nor = F_sigma_sub4_hml_nor;
regsub4.F_sigma_sub4_mkt_nor = F_sigma_sub4_mkt_nor;
regsub4.F_sigma_sub4_smb_nor = F_sigma_sub4_smb_nor;
regsub4.F_sigma_sub4_mom_nor = F_sigma_sub4_mom_nor;
regsub4.F_sigma_sub4_pr1yr_nor = F_sigma_sub4_pr1yr_nor;
regsub4.F_sigma_sub4_liq_nor = F_sigma_sub4_hml_nor;
regsub4.Properties.VariableNames{'Var1'} = 'FFmkt_nor';
regsub4.Properties.VariableNames{'Var2'} = 'FFsmb_nor';
regsub4.Properties.VariableNames{'Var3'} = 'FFhml_nor';
regsub4.Properties.VariableNames{'Var4'} = 'pr1yr_nor';
regsub4.Properties.VariableNames{'Var5'} = 'mom_nor';
regsub4.Properties.VariableNames{'Var6'} = 'liq_nor';

regsub4_mkt = fitlm(regsub4,'F_sigma_sub4_mkt_nor ~FFmkt_nor')
regsub4_smb = fitlm(regsub4,'F_sigma_sub4_smb_nor ~FFsmb_nor')
regsub4_hml = fitlm(regsub4,'F_sigma_sub4_hml_nor ~FFhml_nor')
regsub4_mom = fitlm(regsub4,'F_sigma_sub4_mom_nor ~mom_nor')
regsub4_pr1yr = fitlm(regsub4,'F_sigma_sub4_pr1yr_nor ~pr1yr_nor')
regsub4_liq = fitlm(regsub4,'F_sigma_sub4_liq_nor ~liq_nor')

%% Alpha table 

subanalysis= [regsub1_mkt.Coefficients.Estimate(1)*12,regsub1_smb.Coefficients.Estimate(1)*12,...
    regsub1_hml.Coefficients.Estimate(1)*12,regsub1_mom.Coefficients.Estimate(1)*12, regsub1_pr1yr.Coefficients.Estimate(1)*12,...
    regsub1_liq.Coefficients.Estimate(1)*12;regsub2_mkt.Coefficients.Estimate(1)*12,regsub2_smb.Coefficients.Estimate(1)*12,...
    regsub2_hml.Coefficients.Estimate(1)*12,regsub2_mom.Coefficients.Estimate(1)*12, regsub2_pr1yr.Coefficients.Estimate(1)*12,...
    regsub2_liq.Coefficients.Estimate(1)*12;regsub3_mkt.Coefficients.Estimate(1)*12,regsub3_smb.Coefficients.Estimate(1)*12,...
    regsub3_hml.Coefficients.Estimate(1)*12,regsub3_mom.Coefficients.Estimate(1)*12, regsub3_pr1yr.Coefficients.Estimate(1)*12,...
    regsub3_liq.Coefficients.Estimate(1)*12;regsub4_mkt.Coefficients.Estimate(1)*12,regsub4_smb.Coefficients.Estimate(1)*12,...
    regsub4_hml.Coefficients.Estimate(1)*12,regsub4_mom.Coefficients.Estimate(1)*12, regsub4_pr1yr.Coefficients.Estimate(1)*12,...
    regsub4_liq.Coefficients.Estimate(1)*12]
latex_subanalysis = latex(vpa(sym(subanalysis),3))








