Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reverse cummulative returns metrics and plot #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
23 changes: 22 additions & 1 deletion turingquant/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,25 @@ def value_at_risk(returns, confidance_level = 0.95, window = 1, method = 'varian
var = var * np.sqrt(window)

return var


def reverse_cummulative_returns(returns, log_returns=True):
"""
Calcula o retorno acumulativo inverso.

Calcula a quantidade de capital que alguém teria se tivesse investido
uma unidade monetária em cada instante de tempo correspondente.

Args:
returns (pd.Series): série de retornos para a qual será calculado o mar ratio.
log_returns (float): indica se o retorno utilizado é logaritmico (True, default) ou simples (False)
Returns:
cum_returns (pd.Series): retorno simples do instante `t` até o presente.
"""
if log_returns is True:
cum_returns = returns.iloc[::-1].cumsum().iloc[::-1]
cum_returns = returns.apply(np.exp)
else:
returns = 1 + returns
cum_returns = returns.iloc[::-1].cumprod().iloc[::-1]

return cum_returns - 1
20 changes: 19 additions & 1 deletion turingquant/plot_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,22 @@ def plot_allocation(dictionary):
labels = list(dictionary.keys())
values = list(dictionary.values())
fig = px.pie(values=values, names=labels)
fig.show()
fig.show()

def plot_reverse_cummulative_returns(returns, log_returns=True):
"""
Plota o retorno acumulativo inverso.

Calcula a quantidade de capital que alguém teria se tivesse investido
uma unidade monetária em cada instante de tempo correspondente.

Args:
returns (pd.Series): série de retornos para a qual será calculado o mar ratio.
log_returns (float): indica se o retorno utilizado é logaritmico (True, default) ou simples (False)
"""
cum_returns = reverse_cummulative_returns(returns, log_returns)
fig = px.line(cum_returns, x=cum_returns.index,
y=cum_returns.name, title='Reverse Cummulative Returns')
fig.update_xaxes(title_text='Tempo')
fig.update_yaxes(title_text='Retorno Simples')
fig.show()