pyform.returns package

Submodules

pyform.returns.compound module

pyform.returns.compound.compound(method: str) → Callable

Factory for producing compound functions.

Parameters

method

method of compounding in the generated function

  • ’geometric’: geometric compounding (1+r1) * (1+r2) - 1

  • ’arithmetic’: arithmetic compounding r1 + r2

  • ’continuous’: continous compounding exp(r1+r2) - 1

Raises

ValueError – when method is not supported.

Returns

a function that takes a pandas series as its argument, and

compound it according to the method specified.

Return type

Callable

pyform.returns.compound.compound_arithmetic(returns: pandas.core.series.Series) → float

Performs arithmatic compounding.

e.g. if there are 3 returns r1, r2, r3, calculate r1 + r2 + r3

Parameters

returns – pandas series of returns, in decimals. i.e. 3% should be expressed as 0.03, not 3.

Returns

total compounded return

Return type

float

pyform.returns.compound.compound_continuous(returns: pandas.core.series.Series) → float

Performs continuous compounding.

e.g. if there are 3 returns r1, r2, r3, calculate exp(r1 + r2 + r3) - 1

Parameters

returns – pandas series of returns, in decimals. i.e. 3% should be expressed as 0.03, not 3.

Returns

total compounded return

Return type

float

pyform.returns.compound.compound_geometric(returns: pandas.core.series.Series) → float

Performs geometric compounding.

e.g. if there are 3 returns r1, r2, r3, calculate (1+r1) * (1+r2) * (1+r3) - 1

Parameters

returns – pandas series of returns, in decimals. i.e. 3% should be expressed as 0.03, not 3.

Returns

total compounded return

Return type

float

pyform.returns.compound.cumseries(method: str) → Callable

Factory for producing compound functions.

Parameters

method

method of compounding in the generated function

  • ’geometric’: geometric compounding

  • ’arithmetic’: arithmetic compounding

  • ’continuous’: continous compounding

Raises

ValueError – when method is not supported.

Returns

a function that takes a pandas series as its argument, and

compound it to create a cumulative index sereis according to the method specified.

Return type

Callable

pyform.returns.compound.cumseries_arithmetic(returns: pandas.core.series.Series) → pandas.core.series.Series

Performs arithmatic compounding to create cumulative index series.

e.g. if there are 3 returns r1, r2, r3, calculate

r1 r1 + r2 r1 + r2 + r3

Parameters

returns – pandas series of returns, in decimals. i.e. 3% should be expressed as 0.03, not 3.

Returns

pandas series of cumulative index, in decimals.

Return type

returns

pyform.returns.compound.cumseries_continuous(returns: pandas.core.series.Series) → float

Performs continuous compounding to create cumulative index series.

e.g. if there are 3 returns r1, r2, r3, calculate

exp(r1) - 1 exp(r1 + r2) - 1 exp(r1 + r2 + r3) - 1

Parameters

returns – pandas series of returns, in decimals. i.e. 3% should be expressed as 0.03, not 3.

Returns

pandas series of cumulative index, in decimals.

Return type

returns

pyform.returns.compound.cumseries_geometric(returns: pandas.core.series.Series) → pandas.core.series.Series

Performs geometric compounding to create cumulative index series.

e.g. if there are 3 returns r1, r2, r3, calculate

(1+r1) - 1, (1+r1) * (1+r2) - 1, (1+r1) * (1+r2) * (1+r3) - 1

Parameters

returns – pandas series of returns, in decimals. i.e. 3% should be expressed as 0.03, not 3.

Returns

pandas series of cumulative index, in decimals.

Return type

returns

pyform.returns.compound.ret_to_period(df: pandas.core.frame.DataFrame, freq: str, method: str)

Converts return series to a different (and lower) frequency.

Parameters
  • df – a time indexed pandas dataframe

  • freq – frequency to convert the return series to. Available options can be found here.

  • method

    compounding method when converting to lower frequency.

    • ’geometric’: geometric compounding (1+r1) * (1+r2) - 1

    • ’arithmetic’: arithmetic compounding r1 + r2

    • ’continuous’: continous compounding exp(r1+r2) - 1

Returns

return series in desired frequency

Return type

pd.DataFrame

pyform.returns.metrics module

pyform.returns.metrics.calc_ann_ret(series: Union[pandas.core.frame.DataFrame, pandas.core.series.Series], method: str, years: Optional[float] = None) → float

Computes annualized return of a time indexed pandas series

Parameters
  • series – a time indexed pandas DataFrame or Series of returns

  • method – {‘geometric’, ‘arithmetic’, ‘continuous’}. method used to compound returns

  • years – Useful when you want to specify how many years are actually in the data.

Returns

annualized volatility

Return type

float

pyform.returns.metrics.calc_ann_vol(series: Union[pandas.core.frame.DataFrame, pandas.core.series.Series], method: str, samples_per_year: Optional[float] = None) → float

Computes annualized volatility of a time indexed pandas series

Parameters
  • series – a time indexed pandas DataFrame or Series of returns

  • method – {‘sample’, ‘population’}. method used to compute volatility (standard deviation).

  • samples_per_year – Useful when you want to specify how many samples are there per year so annualization can be done properly. If None, this will be computed by using the series supplied. Defaults to None.

Returns

annualized volatility

Return type

float

Module contents