Skip to content

Commit

Permalink
risk tested
Browse files Browse the repository at this point in the history
  • Loading branch information
juanbc committed Apr 21, 2023
1 parent 0e34b40 commit 9a0827b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions 1 archivos sin guardar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c
78 changes: 78 additions & 0 deletions tests/core/test_risk_acc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# This file is part of the
# Garpar Project (https://github.com/quatrope/garpar).
# Copyright (c) 2021, 2022, Nadia Luczywo, Juan Cabral and QuatroPe
# License: MIT
# Full Text: https://github.com/quatrope/garpar/blob/master/LICENSE


# =============================================================================
# IMPORTS
# =============================================================================

from garpar.core import div_acc

import numpy as np

import pandas as pd

import pytest


# =============================================================================
# DRISK TESTS
# =============================================================================


def test_RiskAccessor_stock_beta(risso_portfolio):
pf = risso_portfolio(random_state=42, stocks=3)
expected = pd.Series(
[0.42396259758517374, 1.748117962092154, 0.8279194403226721],
name="beta",
index=["S0", "S1", "S2"],
)
expected.index.name = "Stocks"
pd.testing.assert_series_equal(pf.risk.stock_beta(), expected)


def test_RiskAccessor_stock_beta_force_another_market_column(
risso_portfolio,
):
pf = risso_portfolio(random_state=42, stocks=4)
other_pf = pf.copy(stocks=["_mkt_", "_mkt_0_", "_mkt_1_", "_mkt_2_"])
np.testing.assert_allclose(other_pf.risk.pf_beta(), pf.risk.pf_beta())


def test_RiskAccessor_portfolio_beta(risso_portfolio):
pf = risso_portfolio(random_state=42)
expected = 1.0
np.testing.assert_allclose(pf.risk.portfolio_beta(), expected)


def test_RiskAccessor_treynor_ratio(risso_portfolio):
pf = risso_portfolio(random_state=42)
expected = -0.51879158
np.testing.assert_allclose(pf.risk.treynor_ratio(), expected)


def test_RiskAccessor_portfolio_variance(risso_portfolio):
pf = risso_portfolio(random_state=42)
expected = 0.01078666
np.testing.assert_allclose(pf.risk.portfolio_variance(), expected)


def test_RiskAccessor_sharpe_ratio(risso_portfolio):
pf = risso_portfolio(random_state=42)
expected = -4.802591
np.testing.assert_allclose(pf.risk.sharpe_ratio(), expected)


def test_RiskAccessor_value_at_risk(risso_portfolio):
pf = risso_portfolio(random_state=42, stocks=3)
expected = pd.Series(
[0.0016324583798860148, 0.0030751474549569613, 0.0037714120615394142],
name="VaR",
index=["S0", "S1", "S2"],
)
expected.index.name = "Stocks"

pd.testing.assert_series_equal(pf.risk.value_at_risk(), expected)

0 comments on commit 9a0827b

Please sign in to comment.