Created
April 2, 2019 14:31
-
-
Save alejandrociatti/5f485233fca9e18fa75495db7900fd5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function prepare_returns_matrix(account, number_of_scenarios, scenarios_length, interval) | |
classes_returns = | |
[:gbp_uk_aggregate_bonds, :gbp_uk_equities] |> # replace with the classes you want to see the correlations | |
class_name -> DataLoader.get_returns_matrix( | |
class_name, | |
scenarios_length, | |
interval | |
), | |
std = | |
classes_returns -> collect( combinations(returns, 2) ) |> # make all combinations | |
combs -> map( combination_correlation(number_of_scenarios), combs ) |> # do correlation -> get median | |
medians -> weighted_std_deviation(classes, medians, interval) | |
DataLoader.get_monte_carlo_returns_matrix( | |
scenarios_length, | |
interval, | |
weighted_mean(classes, interval), | |
std | |
) | |
end | |
# returns a fn that calculates | |
# the correlations of each scenario in a combination of its returns | |
# then gets the median of each correlation | |
# once ran, the inner fn returns an array of medians of each combination's correlation | |
function combination_correlation(number_of_scenarios) | |
function (combination) | |
class1 = combination[1] # class 1 returns | |
class2 = combination[2] # class 2 returns | |
# for each row (scenarios) | |
correlations = | |
map( 1:number_of_scenarios ) do i | |
cor( view(class1, i, :), view(class2, i, :) ) | |
end | |
return quantile(correlations, 0.5) | |
end | |
end | |
function weighted_std_deviation(classes::Vector{AssetClass}, medians::Vector{Float64}, interval::Symbol)::Float64 | |
sqrt( | |
weighted_std_deviation_term_1(classes, interval) + | |
weighted_std_deviation_correlations(classes, medians, interval) | |
) # sum is the variance, sqrt(variance) gives us the std (volatility) | |
end | |
function weighted_std_deviation_term_1(classes::Vector{AssetClass}, interval::Symbol)::Float64 | |
sum = 0 | |
foreach(classes) do class | |
sum += | |
class.percentage[1, 1] ^ 2 * | |
Interval.yearly_std_to_interval( | |
class.returns_std_deviation, | |
interval | |
) ^ 2 | |
end | |
sum | |
end | |
# medians : 50th percentile of classes combination correlations | |
function weighted_std_deviation_correlations(classes::Vector{AssetClass}, medians::Vector{Float64}, interval::Symbol) | |
sum = 0 | |
# we zip class combinations with previously calculated medians | |
for combination_median in zip( collect(combinations(classes, 2)), medians ) | |
class_a = combination_median[1][1] # [1][1] is class a | |
class_b = combination_median[1][2] # [1][2] is class b | |
median = combination_median[2] # [2] is the medianAB | |
monthly_std_a = | |
class_a.returns_std_deviation |> | |
yearly_std -> Interval.yearly_std_to_interval(yearly_std, interval) | |
monthly_std_b = | |
class_b.returns_std_deviation |> | |
yearly_std -> Interval.yearly_std_to_interval(yearly_std, interval) | |
sum += | |
class_a.percentage[1, 1] * class_b.percentage[1, 1] * | |
monthly_std_a * monthly_std_b * median | |
end | |
sum | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment