Skip to content

Commit

Permalink
Plotter inside class so it does not generate every time a function is…
Browse files Browse the repository at this point in the history
… called
  • Loading branch information
Divasco committed Aug 25, 2024
1 parent aea0545 commit de8765f
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 189 deletions.
14 changes: 12 additions & 2 deletions garpar/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ def _get_optimizer(self, pf):
weight_bounds=weight_bounds,
)
return optimizer

def _get_market_neutral(self):
"""Get the market_neutral parameter."""
return self.market_neutral

def _get_method(self):
"""Get current optimization method"""
return this.method

def __calculate_weights_by_risk(self, pf):
"""Calculate weights based on the risk of the portfolio.
Expand Down Expand Up @@ -256,9 +264,10 @@ def __calculate_weights_general(self, pf):
A tuple containing the optimal weights and optimizer metadata.
"""
optimizer = self._get_optimizer(pf)
market_neutral = self._get_market_neutral()
# market_neutral = self.market_neutral FIXME comentario pasarlo a acc

weights_dict = getattr(optimizer, self.method)() # TODO Pasarlo a acc
weights_dict = getattr(optimizer, self._get_method())() # TODO Pasarlo a acc
weights = [weights_dict[stock] for stock in pf.stocks]

optimizer_metadata = {
Expand Down Expand Up @@ -358,7 +367,8 @@ def _calculate_weights(self, pf):
"""
optimizer = self._get_optimizer(pf)
target_return = self._coerce_target_return(pf)
market_neutral = self.market_neutral
market_neutral = self._get_market_neutral()
# market_neutral = self.market_neutral

weights_dict = optimizer.efficient_return(
target_return, market_neutral=market_neutral
Expand Down
318 changes: 131 additions & 187 deletions tests/core/test_plot_acc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# License: MIT
# Full Text: https://github.com/quatrope/garpar/blob/master/LICENSE

# TODO: Meter en una clase para no crear todo el tiempo el plotter

from garpar.core.plot_acc import PortfolioPlotter

Expand All @@ -15,189 +14,134 @@

import seaborn as sns


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_line(
fig_test, fig_ref, risso_portfolio, returns, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.line(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._ddf(returns=returns)
ax_ref = sns.lineplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_heatmap(
fig_test, fig_ref, risso_portfolio, returns, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.heatmap(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._ddf(returns=returns)
ax_ref = sns.heatmap(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_wheatmap(
fig_test, fig_ref, risso_portfolio, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.wheatmap(ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._wdf()
ax_ref = sns.heatmap(data=data.T)
ax_ref.set_title(title)
ax_ref.set_xlabel("Stocks")


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_hist(
fig_test, fig_ref, risso_portfolio, returns, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.hist(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._ddf(returns=returns)
ax_ref = sns.histplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_whist(
fig_test, fig_ref, risso_portfolio, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.whist(ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._wdf()
ax_ref = sns.histplot(data=data.T)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_box(
fig_test, fig_ref, risso_portfolio, returns, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.box(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._ddf(returns=returns)
ax_ref = sns.boxplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_wbox(
fig_test, fig_ref, risso_portfolio, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.wbox(ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._wdf()
ax_ref = sns.boxplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_kde(
fig_test, fig_ref, risso_portfolio, returns, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.kde(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._ddf(returns=returns)
ax_ref = sns.kdeplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_wkde(
fig_test, fig_ref, risso_portfolio, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.wkde(ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._wdf()
ax_ref = sns.kdeplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_PortfolioPlotter_ogive(
fig_test, fig_ref, risso_portfolio, returns, distribution
):
pf = risso_portfolio(random_state=3, distribution=distribution)

plotter = PortfolioPlotter(pf)

ax_test = fig_test.subplots()
plotter.ogive(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = plotter._ddf(returns=returns)
ax_ref = sns.ecdfplot(data=data)
ax_ref.set_title(title)
class TestPortfolioPlotter:
@pytest.fixture(autouse=True)
def setup_plotter(self, risso_portfolio, distribution):
self.pf = risso_portfolio(random_state=3, distribution=distribution)
self.plotter = PortfolioPlotter(self.pf)

@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_line(self, fig_test, fig_ref, returns):
ax_test = fig_test.subplots()
self.plotter.line(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._ddf(returns=returns)
sns.lineplot(data=data, ax=ax_ref)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_heatmap(self, fig_test, fig_ref, returns):
ax_test = fig_test.subplots()
self.plotter.heatmap(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._ddf(returns=returns)
ax_ref = sns.heatmap(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_wheatmap(self, fig_test, fig_ref):
ax_test = fig_test.subplots()
self.plotter.wheatmap(ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._wdf()
ax_ref = sns.heatmap(data=data.T)
ax_ref.set_title(title)
ax_ref.set_xlabel("Stocks")


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_hist(self, fig_test, fig_ref, returns):
ax_test = fig_test.subplots()
self.plotter.hist(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._ddf(returns=returns)
ax_ref = sns.histplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_whist(self, fig_test, fig_ref):
ax_test = fig_test.subplots()
self.plotter.whist(ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._wdf()
ax_ref = sns.histplot(data=data.T)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_box(self, fig_test, fig_ref, returns):
ax_test = fig_test.subplots()
self.plotter.box(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._ddf(returns=returns)
ax_ref = sns.boxplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_wbox(self, fig_test, fig_ref):
ax_test = fig_test.subplots()
self.plotter.wbox(ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._wdf()
ax_ref = sns.boxplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_kde(self, fig_test, fig_ref, returns):
ax_test = fig_test.subplots()
self.plotter.kde(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._ddf(returns=returns)
ax_ref = sns.kdeplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_wkde(self, fig_test, fig_ref):
ax_test = fig_test.subplots()
self.plotter.wkde(ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._wdf()
ax_ref = sns.kdeplot(data=data)
ax_ref.set_title(title)


@check_figures_equal()
@pytest.mark.parametrize("returns", [True, False])
@pytest.mark.parametrize("distribution", pytest.DISTRIBUTIONS)
def test_ogive(self, fig_test, fig_ref, returns):
ax_test = fig_test.subplots()
self.plotter.ogive(returns=returns, ax=ax_test)

ax_ref = fig_ref.subplots()
data, title = self.plotter._ddf(returns=returns)
ax_ref = sns.ecdfplot(data=data)
ax_ref.set_title(title)

0 comments on commit de8765f

Please sign in to comment.