Open
Description
Hi bro
defmodule Xirr do
defmodule Volatility do
def calculate_volatility(daily_prices) do
# pairs of prices using zip
pairs = Enum.zip(daily_prices, Enum.drop(daily_prices, 1))
# pct change for each pair
pct_changes = Enum.map(pairs, fn {a, b} -> (b - a) / a end)
# standard deviation of pct changes
mean = Enum.sum(pct_changes) / Enum.count(pct_changes)
# sum of squared differences
variance = Enum.sum(Enum.map(pct_changes, fn x -> (x - mean) * (x - mean) end))
# standard deviation
stddev = :math.sqrt(variance / (Enum.count(pct_changes) - 1))
# annualize
stddev * :math.sqrt(252)
end
end
end