class Iro::Stock

def volatility duration: 1.year, recompute: false

def volatility duration: 1.year, recompute: false
  if self[:volatility]
    if !recompute
      return self[:volatility]
    end
  end
  stock = self
  begin_on = Time.now - duration - 1.day
  points = Iro::Datapoint.where( kind: 'STOCK', symbol: stock.ticker,
    :date.gte => begin_on,
  ).order_by( date: :asc )
  puts! [points.first.date, points.last.date], "from,to"
  points_p = []
  points.each_with_index do |p, idx|
    next if idx == 0
    prev = points[idx-1]
    out = p.value / prev.value - 1
    points_p.push out
  end
  n = points_p.length
  avg = points_p.reduce(&:+) / n
  _sum_of_sq = []
  points_p.map do |p|
    _sum_of_sq.push( ( p - avg )*( p - avg ) )
  end
  sum_of_sq = _sum_of_sq.reduce( &:+ ) / n
  # n_periods = begin_on.to_date.business_days_until( Date.today )
  out = Math.sqrt( sum_of_sq )*sqrt( n )
  adjustment = 2.0
  out = out * adjustment
  puts! out, 'volatility (adjusted)'
  self.update volatility: out
  return out
end