Skip to content

Instantly share code, notes, and snippets.

@tschm
Created December 15, 2015 09:42
Show Gist options
  • Save tschm/3104426a22c05aee246b to your computer and use it in GitHub Desktop.
Save tschm/3104426a22c05aee246b to your computer and use it in GitHub Desktop.
def monthlytable(nav, year=1900):
r = nav.pct_change().dropna()
# Works better in the first month
# Compute all the intramonth-returns, instead of reapplying some monthly resampling of the NAV
return_monthly = r.groupby([lambda x: x.year, lambda x: x.month]).apply(lambda x: (1 + x).prod() - 1.0)
frame = return_monthly.unstack(level=1).rename(columns=lambda x: calendar.month_abbr[x])
a = (frame + 1.0).prod(axis=1) - 1.0
frame["STDev"] = np.sqrt(12) * frame.std(axis=1)
# make sure that you don't include the column for the STDev in your computation
frame["YTD"] = a
frame.index.name = "year"
return frame.ix[frame.index >= year]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment