def annualize_vol(r):
    '\n    Annualizes the vol of a set of returns\n    We should infer the periods per year\n    but that is currently left as an exercise\n    to the reader :-)\n    '
    r_valid = r[((r != 0) & r.notnull())]
    total_num_periods = r_valid.count()
    date_beg = r_valid.agg((lambda x: x.first_valid_index()))
    date_end = r_valid.agg((lambda x: x.last_valid_index()))
    try:
        years_fraction = ((date_end - date_beg).dt.days / 365.2425)
    except:
        years_fraction = ((date_end - date_beg).days / 365.2425)
    periods_per_year = (total_num_periods / years_fraction)
    return (r_valid.std() * (periods_per_year ** 0.5))
Annualizes the vol of a set of returns
We should infer the periods per year
but that is currently left as an exercise
to the reader :-)
def annualize_vol(r):
    '\n    Annualizes the vol of a set of returns\n    We should infer the periods per year\n    but that is currently left as an exercise\n    to the reader :-)\n    '
    r_valid = r[((r != 0) & r.notnull())]
    total_num_periods = r_valid.count()
    date_beg = r_valid.agg((lambda x: x.first_valid_index()))
    date_end = r_valid.agg((lambda x: x.last_valid_index()))
    try:
        years_fraction = ((date_end - date_beg).dt.days / 365.2425)
    except:
        years_fraction = ((date_end - date_beg).days / 365.2425)
    periods_per_year = (total_num_periods / years_fraction)
    return (r_valid.std() * (periods_per_year ** 0.5))