Skip to content
Merged
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
4 changes: 2 additions & 2 deletions docs/GeneralEfficientFrontier.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ For example, perhaps our objective is to construct a basket of assets that best
particular index, in other words, to minimise the **tracking error**. This does not fit within
a mean-variance optimization paradigm, but we can still implement it in PyPortfolioOpt::

from pypfopt.base_optimizer import BaseConvexOptimizer
from pypfopt.base import BaseConvexOptimizer
from pypfopt.objective_functions import ex_post_tracking_error

historic_rets = ... # dataframe of historic asset returns
Expand Down Expand Up @@ -231,7 +231,7 @@ or a ``nonconvex_objective``, which uses ``scipy.optimize`` as the backend and t
different API. For more examples, check out this `cookbook recipe
<https://github.com/PyPortfolio/PyPortfolioOpt/blob/main/cookbook/3-Advanced-Mean-Variance-Optimization.ipynb>`_.

.. class:: pypfopt.base_optimizer.BaseConvexOptimizer
.. class:: pypfopt.base.BaseConvexOptimizer

.. automethod:: convex_objective

Expand Down
6 changes: 3 additions & 3 deletions docs/MeanVariance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ Basic Usage
If you would like to use the ``portfolio_performance`` function independently of any
optimizer (e.g for debugging purposes), you can use::

from pypfopt import base_optimizer
from pypfopt.base import portfolio_performance

base_optimizer.portfolio_performance(
portfolio_performance(
weights, expected_returns, cov_matrix, verbose=True, risk_free_rate=0.02
)

Expand All @@ -135,7 +135,7 @@ EfficientFrontier inherits from the BaseConvexOptimizer class. In particular, th
add constraints and objectives are documented below:


.. class:: pypfopt.base_optimizer.BaseConvexOptimizer
.. class:: pypfopt.base.BaseConvexOptimizer
:noindex:

.. automethod:: add_constraint
Expand Down
4 changes: 2 additions & 2 deletions docs/OtherOptimizers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ to code up compared to custom objective functions.

To implement a custom optimizer that is compatible with the rest of PyPortfolioOpt, just
extend ``BaseOptimizer`` (or ``BaseConvexOptimizer`` if you want to use ``cvxpy``),
both of which can be found in ``base_optimizer.py``. This gives you access to utility
both of which can be found in ``pypfopt.base``. This gives you access to utility
methods like ``clean_weights()``, as well as making sure that any output is compatible
with ``portfolio_performance()`` and post-processing methods.

.. automodule:: pypfopt.base_optimizer
.. automodule:: pypfopt.base

.. autoclass:: BaseOptimizer
:members:
Expand Down
9 changes: 9 additions & 0 deletions pypfopt/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Base classes."""

from pypfopt.base._base_optimizer import (
BaseConvexOptimizer,
BaseOptimizer,
portfolio_performance,
)

__all__ = ["BaseOptimizer", "BaseConvexOptimizer", "portfolio_performance"]
File renamed without changes.
6 changes: 3 additions & 3 deletions pypfopt/black_litterman.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import numpy as np
import pandas as pd

from . import base_optimizer
from pypfopt.base import BaseOptimizer, portfolio_performance


def market_implied_prior_returns(
Expand Down Expand Up @@ -97,7 +97,7 @@ def market_implied_risk_aversion(market_prices, frequency=252, risk_free_rate=0.
return (r - risk_free_rate) / var


class BlackLittermanModel(base_optimizer.BaseOptimizer):
class BlackLittermanModel(BaseOptimizer):
"""
A BlackLittermanModel object (inheriting from BaseOptimizer) contains requires
a specific input format, specifying the prior, the views, the uncertainty in views,
Expand Down Expand Up @@ -542,7 +542,7 @@ def portfolio_performance(self, verbose=False, risk_free_rate=0.0):
"""
if self.posterior_cov is None:
self.posterior_cov = self.bl_cov()
return base_optimizer.portfolio_performance(
return portfolio_performance(
self.weights,
self.posterior_rets,
self.posterior_cov,
Expand Down
6 changes: 3 additions & 3 deletions pypfopt/cla.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import numpy as np
import pandas as pd

from . import base_optimizer
from pypfopt.base import BaseOptimizer, portfolio_performance


class CLA(base_optimizer.BaseOptimizer):
class CLA(BaseOptimizer):
"""
Instance variables:

Expand Down Expand Up @@ -506,7 +506,7 @@ def portfolio_performance(self, verbose=False, risk_free_rate=0.0):
(float, float, float)
expected return, volatility, Sharpe ratio.
"""
return base_optimizer.portfolio_performance(
return portfolio_performance(
self.weights,
self.expected_returns,
self.cov_matrix,
Expand Down
8 changes: 5 additions & 3 deletions pypfopt/efficient_frontier/efficient_frontier.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import numpy as np
import pandas as pd

from .. import base_optimizer, exceptions, objective_functions
from pypfopt.base import BaseConvexOptimizer, portfolio_performance

from .. import exceptions, objective_functions

class EfficientFrontier(base_optimizer.BaseConvexOptimizer):

class EfficientFrontier(BaseConvexOptimizer):
"""
An EfficientFrontier object (inheriting from BaseConvexOptimizer) contains multiple
optimization methods that can be called (corresponding to different objective
Expand Down Expand Up @@ -500,7 +502,7 @@ def portfolio_performance(self, verbose=False, risk_free_rate=0.0):
)
risk_free_rate = self._risk_free_rate

return base_optimizer.portfolio_performance(
return portfolio_performance(
self.weights,
self.expected_returns,
self.cov_matrix,
Expand Down
11 changes: 5 additions & 6 deletions pypfopt/hierarchical_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import scipy.cluster.hierarchy as sch
import scipy.spatial.distance as ssd

from . import base_optimizer, risk_models
from pypfopt.base import BaseOptimizer, portfolio_performance
from pypfopt.risk_models import cov_to_corr


class HRPOpt(base_optimizer.BaseOptimizer):
class HRPOpt(BaseOptimizer):
"""
A HRPOpt object (inheriting from BaseOptimizer) constructs a hierarchical
risk parity portfolio.
Expand Down Expand Up @@ -180,7 +181,7 @@ def optimize(self, linkage_method="single"):

if self.returns is None:
cov = self.cov_matrix
corr = risk_models.cov_to_corr(self.cov_matrix).round(6)
corr = cov_to_corr(self.cov_matrix).round(6)
else:
corr, cov = self.returns.corr(), self.returns.cov()

Expand Down Expand Up @@ -234,6 +235,4 @@ def portfolio_performance(self, verbose=False, risk_free_rate=0.0, frequency=252
cov = self.returns.cov() * frequency
mu = self.returns.mean() * frequency

return base_optimizer.portfolio_performance(
self.weights, mu, cov, verbose, risk_free_rate
)
return portfolio_performance(self.weights, mu, cov, verbose, risk_free_rate)
4 changes: 2 additions & 2 deletions tests/test_base_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

from pypfopt import EfficientFrontier, exceptions, objective_functions
from pypfopt.base_optimizer import BaseOptimizer, portfolio_performance
from pypfopt.base import BaseOptimizer, portfolio_performance
from tests.utilities_for_tests import get_data, setup_efficient_frontier


Expand Down Expand Up @@ -218,7 +218,7 @@ def test_save_weights_to_file():

def test_portfolio_performance():
"""
Cover logic in base_optimizer.portfolio_performance not covered elsewhere.
Cover logic in base.portfolio_performance not covered elsewhere.
"""
ef = setup_efficient_frontier()
ef.min_volatility()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_custom_objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
objective_functions,
risk_models,
)
from pypfopt.base_optimizer import BaseConvexOptimizer
from pypfopt.base import BaseConvexOptimizer
from tests.utilities_for_tests import get_data, setup_efficient_frontier


Expand Down
4 changes: 3 additions & 1 deletion tests/test_imports.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from pypfopt.base import BaseConvexOptimizer, BaseOptimizer, portfolio_performance


def test_import_modules():
from pypfopt import (
base_optimizer,
black_litterman,
cla,
discrete_allocation,
Expand Down
Loading