Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 51 additions & 31 deletions fecon236/util/group.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# Python Module for import Date : 2018-06-17
# vim: set fileencoding=utf-8 ff=unix tw=78 ai syn=python : per PEP 0263
'''
_______________| group.py :: Group utilities
"""Group utilities

CHANGE LOG For LATEST version, see https://git.io/fecon236
2018-06-17 Spin-off groupcotr() to futures.cftc module.
2018-06-16 Move covdiflog() to math.matrix module.
2018-06-14 Spin-off group stuff from top.py.
2018-03-12 fecon235.py, fecon235 v5.18.0312, https://git.io/fecon235
'''
Notes
-----
For LATEST version, see https://git.io/fecon236

Change Log
----------

* 2018-06-17 Spin-off `groupcotr` to `futures.cftc` module.
* 2018-06-16 Move `covdiflog` to `math.matrix` module.
* 2018-06-14 Spin-off group stuff from `top.py`.
* 2018-03-12 `fecon235.py`, fecon235 v5.18.0312, https://git.io/fecon235
"""

from __future__ import absolute_import, print_function, division

Expand Down Expand Up @@ -36,7 +41,7 @@


def groupget(ggdic=group4d, maxi=0):
'''Retrieve and create group dataframe, given group dictionary.'''
"""Retrieve and create group dataframe, given group dictionary."""
# Since dictionaries are unordered, create SORTED list of keys:
keys = [key for key in sorted(ggdic)]
# Download individual dataframes as values into a dictionary:
Expand All @@ -50,11 +55,16 @@ def groupget(ggdic=group4d, maxi=0):


def groupfun(fun, groupdf, *pargs, **kwargs):
'''Use fun(ction) column-wise, then output new group dataframe.'''
# In mathematics, this is known as an "operator":
# a function which takes another function as argument.
# Examples of fun: pcent, normalize, etc. See grouppc() next.
# See groupget() to retrieve and create group dataframe.
"""Use fun(ction) column-wise, then output new group dataframe.

Notes
-----
In mathematics, this is known as an "operator":
a function which takes another function as argument.
Examples of fun: `pcent`, `normalize`, etc. See `grouppc` next.

.. seealso:: `groupget` to retrieve and create group dataframe.
"""
keys = list(groupdf.columns)
# Compute individual columns as dataframes in a list:
out = [tool.todf(fun(tool.todf(groupdf[key]), *pargs, **kwargs))
Expand All @@ -68,24 +78,33 @@ def groupfun(fun, groupdf, *pargs, **kwargs):


def grouppc(groupdf, freq=1):
'''Create overlapping pcent dataframe, given a group dataframe.'''
# See groupget() to retrieve and create group dataframe.
# Very useful to visualize as boxplot, see fred-georeturns.ipynb
"""Create overlapping pcent dataframe, given a group dataframe.

Notes
-----
.. seealso:: `groupget` to retrieve and create group dataframe.

Very useful to visualize as boxplot, see fred-georeturns.ipynb
"""
return groupfun(tool.pcent, groupdf, freq)


def groupdiflog(groupdf, lags=1):
'''Difference between lagged log(data) for columns in group dataframe.'''
# See groupget() to retrieve and create group dataframe.
"""Difference between lagged log(data) for columns in group dataframe.

.. seealso:: See `groupget` to retrieve and create group dataframe.
"""
return groupfun(tool.diflog, groupdf, lags)


def groupgeoret(groupdf, yearly=256, order=True):
'''Geometric mean returns, non-overlapping, for group dataframe.
Argument "yearly" refers to annual frequency, e.g.
256 for daily trading days, 12 for monthly, 4 for quarterly.
ATTN: Use groupgemrat() instead for greater accuracy.
'''
"""Geometric mean returns, non-overlapping, for group dataframe.

Argument `yearly` refers to annual frequency, e.g.
256 for daily trading days, 12 for monthly, 4 for quarterly.

.. note:: Use `groupgemrat` instead for greater accuracy.
"""
keys = list(groupdf.columns)
# Use list comprehension to store lists from georet():
geo = [tool.georet(tool.todf(groupdf[k]), yearly) + [k] for k in keys]
Expand All @@ -97,12 +116,13 @@ def groupgeoret(groupdf, yearly=256, order=True):


def groupgemrat(groupdf, yearly=256, order=False, n=2):
'''Geometric mean rates, non-overlapping, for group dataframe.
Argument "yearly" refers to annual frequency, e.g.
256 for daily trading days, 12 for monthly, 4 for quarterly.
Output is rounded to n-decimal places.
Algorithm takes KURTOSIS into account for greater accuracy.
'''
"""Geometric mean rates, non-overlapping, for group dataframe.

Argument `yearly` refers to annual frequency, e.g.
256 for daily trading days, 12 for monthly, 4 for quarterly.
Output is rounded to n-decimal places.
Algorithm takes KURTOSIS into account for greater accuracy.
"""
keys = list(groupdf.columns)
# Use list comprehension to store lists from gemrat():
gem = [tool.roundit(gemrat(tool.todf(groupdf[k]), yearly), n, echo=False)
Expand All @@ -115,7 +135,7 @@ def groupgemrat(groupdf, yearly=256, order=False, n=2):


def groupholtf(groupdf, h=12, alpha=hw.hw_alpha, beta=hw.hw_beta):
'''Holt-Winters forecasts h-periods ahead from group dataframe.'''
"""Holt-Winters forecasts h-periods ahead from group dataframe."""
forecasts = []
keys = list(groupdf.columns)
for k in keys:
Expand Down
Loading