pyform.returnseries.
CashSeries
Bases: pyform.returnseries.ReturnSeries
pyform.returnseries.ReturnSeries
constant
Creates a constant cash daily returns stream
annualized_return – Annualized return of the cash, in decimals. i.e. 1% annual cash return will be entered as annualized_return=0.01. Defaults to 0.
annualized_return=0.01
constant return cash stream
pyform.CashSeries
read_fred_libor_1m
Creates one month libor daily returns from fred data
one month libor daily returns
ReturnSeries
Bases: pyform.timeseries.TimeSeries
pyform.timeseries.TimeSeries
A return series that’s datetime indexed and has one column of returns data
add_bm
Adds a benchmark for the return series.
A benchmark is useful and needed in order to calculate:
‘correlation’: is the correlation between the return series andthe benchmark ‘beta’: is the CAPM beta between the return series and the benchmark
the benchmark
‘beta’: is the CAPM beta between the return series and the benchmark
benchmark – A benchmark. Should be a ReturnSeries object.
name – name of the benchmark. This will be used to display results. Defaults to “None”, which will use the column name of the benchmark.
add_rf
Adds a risk free rate for the return series.
A risk free rate is useful and needed in order to calculate:
‘sharpe ratio’
risk_free – A risk free rate. Should be a ReturnSeries object.
name – name of the risk free rate. This will be used to display results. Defaults to “None”, which will use the column name of the risk free rate.
get_ann_ret
Computes annualized return of the series
method – method to use when compounding return. Defaults to “geometric”.
include_bm – whether to compute annualized return for benchmarks as well. Defaults to True.
meta –
whether to include meta data in output. Defaults to False. Available meta are:
method: method used to compound annualized return
start: start date for calculating annualized return
end: end date for calculating annualized return
annualized return results with the following columns
name: name of the series field: name of the field. In this case, it is ‘annualized return’for all value: annualized return value, in decimals
name: name of the series
for all
value: annualized return value, in decimals
Data described in meta will also be available in the returned DataFrame if meta is set to True.
pd.DataFrame
get_ann_vol
Computes annualized volatility of the series
freq – Returns are converted to the same frequency before volatility is compuated. Defaults to “M”.
include_bm – whether to compute annualized volatility for benchmarks as well. Defaults to True.
method – {‘sample’, ‘population’}. method used to compute volatility (standard deviation). Defaults to “sample”.
compound_method – method to use when compounding return. Defaults to “geometric”.
freq: frequency of the series
method: method used to compute annualized volatility
start: start date for calculating annualized volatility
end: end date for calculating annualized volatility
annualized volatility results with the following columns
name: name of the series field: name of the field. In this case, it is ‘annualized volatility’for all value: annualized volatility value, in decimals
value: annualized volatility value, in decimals
get_corr
Calculates correlation of the return series with its benchmarks
freq – Returns are converted to the same frequency before correlation is compuated. Defaults to “M”.
method –
{‘pearson’, ‘kendall’, ‘spearman’}. Defaults to “pearson”.
pearson : standard correlation coefficient
kendall : Kendall Tau correlation coefficient
spearman : Spearman rank correlation
compound_method – {‘geometric’, ‘arithmetic’, ‘continuous’}. Defaults to “geometric”.
freq: frequency used to compute correlation
method: method used to compute correlation
start: start date for calculating correlation
end: end date for calculating correlation
total: total number of data points in returns series
skipped: number of data points skipped when computing correlation
ValueError – when no benchmark is set
correlation results with the following columns
benchmark: name of the benchmark field: name of the field. In this case, it is ‘correlation’ for all value: correlation value
benchmark: name of the benchmark
field: name of the field. In this case, it is ‘correlation’ for all
value: correlation value
get_index_series
get_rolling_ann_ret
Computes rolling annualized returns of the series
window – the rolling window. Defaults to 36.
freq – Returns are converted to the same frequency before annualized return is compuated. Defaults to “M”.
compound_method – method to use when compounding return to desired frequency. Defaults to “geometric”.
include_bm – whether to compute rolling annualized returns for benchmarks as well. Defaults to True.
dictionary of rolling annualized returns
key: name of the series value: rolling annualized returns, in a datetime indexed pandasdataframe
key: name of the series
dataframe
Dict[pd.DataFrame]
get_rolling_ann_vol
Computes rolling volatility (standard deviation) of the series
include_bm – whether to compute rolling volatility for benchmarks as well. Defaults to True.
dictionary of rolling annualized volatilities
key: name of the series value: rolling annualized volatilities, in a datetime indexedpandas dataframe
pandas dataframe
get_rolling_tot_ret
Computes rolling total return of the series
freq – frequency of the series. Defaults to “M”.
include_bm – whether to compute rolling total return for benchmarks as well. Defaults to True.
method – method to use when compounding total return. Defaults to “geometric”.
dictionary of rolling total returns
key: name of the series value: rolling total returns, in a datetime indexed pandas dataframe
value: rolling total returns, in a datetime indexed pandas dataframe
get_sharpe
Computes Sharpe ratio of the series
freq – Returns are converted to the same frequency before Sharpe ratio is compuated. Defaults to “M”.
risk_free – the risk free rate to use. Can be a float or a string. If is float, use the value as annualized risk free return. Should be given in decimals. i.e. 1% annual cash return will be entered as annualized_return=0.01. If is string, look for the corresponding DataFrame of risk free rate in self.risk_free. self.risk_free can be set via the add_rf() class method. Defaults to 0.
self.risk_free
add_rf()
include_bm – whether to compute Sharpe ratio for benchmarks as well. Defaults to True.
risk_free: the risk free rate used
start: start date for calculating Sharpe ratio
end: end date for calculating Sharpe ratio
Sharpe ratio with the following columns
names: name of the series field: name of the field. In this case, it is ‘Sharpe ratio’for all value: Shapre ratio value
names: name of the series
value: Shapre ratio value
get_tot_ret
Computes total return of the series
include_bm – whether to compute total return for benchmarks as well. Defaults to True.
method: method used to compound total return
start: start date for calculating total return
end: end date for calculating total return
total return results with the following columns
name: name of the series field: name of the field. In this case, it is ‘total return’ for all value: total return value, in decimals
field: name of the field. In this case, it is ‘total return’ for all
value: total return value, in decimals
to_month
Converts return series to monthly frequency.
method – compounding method. Defaults to “geometric”.
return series, in monthly frequency
to_period
Converts return series to a different (and lower) frequency.
freq – frequency to convert the return series to. Available options can be found here.
compounding method when converting to lower frequency.
’geometric’: geometric compounding (1+r1) * (1+r2) - 1
(1+r1) * (1+r2) - 1
’arithmetic’: arithmetic compounding r1 + r2
r1 + r2
’continuous’: continous compounding exp(r1+r2) - 1
exp(r1+r2) - 1
return series in desired frequency
to_quarter
Converts return series to quarterly frequency.
return series, in quarterly frequency
to_week
Converts return series to weekly frequency.
return series, in weekly frequency
to_year
Converts return series to annual frequency.
return series, in annual frequency
pyform.timeseries.
TimeSeries
Bases: object
object
TimeSeries is a representation of a form of data that changes with time.
For a timeseries object, time index should be unique, meaning it would only accept a “wide” dataframe and not a “long” dataframe.
Args:df: a dataframe with datetime index, or a ‘date’/’datetime’ column
df: a dataframe with datetime index, or a ‘date’/’datetime’ column
align_daterange
Aligns daterange of the incoming series with the main series
series – series’ to align daterange for
read_csv
Creates a time series object from a csv file
path – path to the csv file
a TimeSeries object
pyform.TimeSeries
read_db
read_excel
Creates a time series object from a Excel file
Note
using this method requires additional dependency openpyxl
path – path to the Excel file
sheet_name – the index or name of the sheet to read in. Defaults to 0.
reset
Resets TimeSeries to its initial state
set_daterange
Sets the period of the series we are interested in.
start – the start date, in YYYY-MM-DD HH:MM:SS, hour is optional. Defaults to None.
end – the end date, in YYYY-MM-DD HH:MM:SS, hour is optional. Defaults to None.