8000 Volatility calculation · Issue #7 · tubedude/finance-elixir · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Volatility calculation #7
Open
Open
@roehst

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0