isort; edit workflow

pull/533/head
88d52bdba0366127fffca9dfa93895 2023-05-03 22:54:45 +07:00
parent 48659587ca
commit fe3b1d2baf
30 changed files with 104 additions and 77 deletions

3
.flake8 100644
View File

@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
extend-ignore = E203

View File

@ -1,9 +1,13 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
name: pytest
on:
pull_request:
push:
branches:
- master
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
pytest:
@ -28,7 +32,16 @@ jobs:
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pytest isort black flake8
- name: Test with pytest
run: |
pip install pytest
pytest ./tests
pytest ./tests
- name: Check with isort
run: |
isort --check --diff .
- name: Check with black
run: |
black --check --diff .
- name: Check with flake8
run: |
flake8 --show-source --statistics .

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.git
*.pyc
.idea
build

View File

@ -13,8 +13,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys
import os
import sys
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the

View File

@ -1,17 +1,19 @@
import pandas as pd
import numpy as np
import cvxpy as cp
from pypfopt import risk_models
from pypfopt import expected_returns
from pypfopt import EfficientFrontier
from pypfopt import objective_functions
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices
from pypfopt import HRPOpt
from pypfopt import CLA
from pypfopt import black_litterman
from pypfopt import BlackLittermanModel
from pypfopt import plotting
import numpy as np
import pandas as pd
from pypfopt import (
CLA,
BlackLittermanModel,
EfficientFrontier,
HRPOpt,
black_litterman,
expected_returns,
objective_functions,
plotting,
risk_models,
)
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices
# Reading in the data; preparing expected returns and a risk model
df = pd.read_csv("tests/resources/stock_prices.csv", parse_dates=True, index_col="date")

View File

@ -1,20 +1,19 @@
from .black_litterman import (
BlackLittermanModel,
market_implied_prior_returns,
market_implied_risk_aversion,
BlackLittermanModel,
)
from .cla import CLA
from .discrete_allocation import get_latest_prices, DiscreteAllocation
from .discrete_allocation import DiscreteAllocation, get_latest_prices
from .efficient_frontier import (
EfficientCDaR,
EfficientCVaR,
EfficientFrontier,
EfficientSemivariance,
EfficientCVaR,
EfficientCDaR,
)
from .hierarchical_portfolio import HRPOpt
from .risk_models import CovarianceShrinkage
__version__ = "1.5.4"
__all__ = [

View File

@ -13,13 +13,12 @@ import warnings
from collections.abc import Iterable
from typing import List
import cvxpy as cp
import numpy as np
import pandas as pd
import cvxpy as cp
import scipy.optimize as sco
from . import objective_functions
from . import exceptions
from . import exceptions, objective_functions
class BaseOptimizer:

View File

@ -8,8 +8,10 @@ views. In addition, two utility functions are defined, which calculate:
"""
import sys
import warnings
import numpy as np
import pandas as pd
from . import base_optimizer

View File

@ -6,6 +6,7 @@ by Marcos Lopez de Prado and David Bailey.
import numpy as np
import pandas as pd
from . import base_optimizer

View File

@ -3,11 +3,10 @@ The ``efficient_frontier`` module houses the EfficientFrontier class and its des
which generate optimal portfolios for various possible objective functions and parameters.
"""
from .efficient_frontier import EfficientFrontier
from .efficient_cvar import EfficientCVaR
from .efficient_semivariance import EfficientSemivariance
from .efficient_cdar import EfficientCDaR
from .efficient_cvar import EfficientCVaR
from .efficient_frontier import EfficientFrontier
from .efficient_semivariance import EfficientSemivariance
__all__ = [
"EfficientFrontier",

View File

@ -4,8 +4,9 @@ generates portfolios along the mean-CDaR (conditional drawdown-at-risk) frontier
"""
import warnings
import numpy as np
import cvxpy as cp
import numpy as np
from .. import objective_functions
from .efficient_frontier import EfficientFrontier

View File

@ -4,8 +4,9 @@ generates portfolios along the mean-CVaR frontier.
"""
import warnings
import numpy as np
import cvxpy as cp
import numpy as np
from .. import objective_functions
from .efficient_frontier import EfficientFrontier

View File

@ -4,12 +4,11 @@ classical mean-variance optimal portfolios for a variety of objectives and const
"""
import warnings
import cvxpy as cp
import numpy as np
import pandas as pd
import cvxpy as cp
from .. import exceptions
from .. import objective_functions, base_optimizer
from .. import base_optimizer, exceptions, objective_functions
class EfficientFrontier(base_optimizer.BaseConvexOptimizer):

View File

@ -4,8 +4,8 @@ generates portfolios along the mean-semivariance frontier.
"""
import numpy as np
import cvxpy as cp
import numpy as np
from .. import objective_functions
from .efficient_frontier import EfficientFrontier

View File

@ -13,6 +13,7 @@ Currently implemented:
"""
import collections
import numpy as np
import pandas as pd
import scipy.cluster.hierarchy as sch

View File

@ -28,8 +28,8 @@ Currently implemented:
- Ex-post (squared) tracking error
"""
import numpy as np
import cvxpy as cp
import numpy as np
def _objective_value(w, obj):

View File

@ -9,12 +9,13 @@ Currently implemented:
- ``plot_weights`` - bar chart of weights
"""
import copy
import numpy as np
from . import risk_models, exceptions
from . import EfficientFrontier, CLA
import scipy.cluster.hierarchy as sch
import warnings
import numpy as np
import scipy.cluster.hierarchy as sch
from . import CLA, EfficientFrontier, exceptions, risk_models
try:
import matplotlib.pyplot as plt

View File

@ -22,8 +22,10 @@ The format of the data input is the same as that in :ref:`expected-returns`.
"""
import warnings
import numpy as np
import pandas as pd
from .expected_returns import returns_from_prices
@ -486,13 +488,13 @@ class CovarianceShrinkage:
# compute shrinkage parameters
c = np.linalg.norm(sample - F, "fro") ** 2
y = Xm ** 2
p = 1 / t * np.sum(np.dot(y.T, y)) - np.sum(sample ** 2)
y = Xm**2
p = 1 / t * np.sum(np.dot(y.T, y)) - np.sum(sample**2)
# r is divided into diagonal
# and off-diagonal terms, and the off-diagonal term
# is itself divided into smaller terms
rdiag = 1 / t * np.sum(y ** 2) - sum(np.diag(sample) ** 2)
rdiag = 1 / t * np.sum(y**2) - sum(np.diag(sample) ** 2)
z = Xm * np.tile(xmkt, (n,))
v1 = 1 / t * np.dot(y.T, z) - np.tile(betas, (n,)) * sample
roff1 = (
@ -501,8 +503,8 @@ class CovarianceShrinkage:
)
v3 = 1 / t * np.dot(z.T, z) - varmkt * sample
roff3 = (
np.sum(v3 * np.dot(betas, betas.T)) / varmkt ** 2
- np.sum(np.diag(v3).reshape(-1, 1) * betas ** 2) / varmkt ** 2
np.sum(v3 * np.dot(betas, betas.T)) / varmkt**2
- np.sum(np.diag(v3).reshape(-1, 1) * betas**2) / varmkt**2
)
roff = 2 * roff1 - roff3
r = rdiag + roff
@ -540,12 +542,12 @@ class CovarianceShrinkage:
# Estimate pi
Xm = X - X.mean(axis=0)
y = Xm ** 2
pi_mat = np.dot(y.T, y) / t - 2 * np.dot(Xm.T, Xm) * S / t + S ** 2
y = Xm**2
pi_mat = np.dot(y.T, y) / t - 2 * np.dot(Xm.T, Xm) * S / t + S**2
pi_hat = np.sum(pi_mat)
# Theta matrix, expanded term by term
term1 = np.dot((Xm ** 3).T, Xm) / t
term1 = np.dot((Xm**3).T, Xm) / t
help_ = np.dot(Xm.T, Xm) / t
help_diag = np.diag(help_)
term2 = np.tile(help_diag, (n, 1)).T * S

View File

@ -1,2 +1,5 @@
[tool.black]
line-length = 88
line-length = 88
[tool.isort]
profile = "black"

View File

@ -1,4 +1,5 @@
import re
import setuptools
with open("README.md", "r") as f:

View File

@ -2,11 +2,11 @@ import numpy as np
import pandas as pd
import pytest
from pypfopt import risk_models, expected_returns
from pypfopt import expected_returns, risk_models
from pypfopt.black_litterman import (
BlackLittermanModel,
market_implied_risk_aversion,
market_implied_prior_returns,
market_implied_risk_aversion,
)
from tests.utilities_for_tests import get_data, get_market_caps, resource

View File

@ -3,7 +3,7 @@ import pandas as pd
import pytest
from cvxpy.error import SolverError
from pypfopt.discrete_allocation import get_latest_prices, DiscreteAllocation
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices
from tests.utilities_for_tests import get_data, setup_efficient_frontier

View File

@ -1,9 +1,9 @@
import numpy as np
import pytest
from pypfopt import expected_returns, EfficientCDaR, objective_functions
from pypfopt import EfficientCDaR, expected_returns, objective_functions
from pypfopt.exceptions import OptimizationError
from tests.utilities_for_tests import setup_efficient_cdar, get_data
from tests.utilities_for_tests import get_data, setup_efficient_cdar
def test_cdar_example():

View File

@ -1,9 +1,9 @@
import numpy as np
import pytest
from pypfopt import expected_returns, EfficientCVaR, objective_functions
from pypfopt import EfficientCVaR, expected_returns, objective_functions
from pypfopt.exceptions import OptimizationError
from tests.utilities_for_tests import setup_efficient_cvar, get_data
from tests.utilities_for_tests import get_data, setup_efficient_cvar
def test_cvar_example():

View File

@ -3,7 +3,7 @@ import pandas as pd
import pytest
from pypfopt import expected_returns
from tests.utilities_for_tests import get_data, get_benchmark_data
from tests.utilities_for_tests import get_benchmark_data, get_data
def test_returns_dataframe():

View File

@ -2,7 +2,7 @@ import numpy as np
import pandas as pd
import pytest
from pypfopt import HRPOpt, CovarianceShrinkage
from pypfopt import CovarianceShrinkage, HRPOpt
from tests.utilities_for_tests import get_data, resource

View File

@ -15,16 +15,16 @@ def test_import_modules():
def test_explicit_import():
from pypfopt.black_litterman import (
BlackLittermanModel,
market_implied_prior_returns,
market_implied_risk_aversion,
BlackLittermanModel,
)
from pypfopt.cla import CLA
from pypfopt.discrete_allocation import get_latest_prices, DiscreteAllocation
from pypfopt.discrete_allocation import DiscreteAllocation, get_latest_prices
from pypfopt.efficient_frontier import (
EfficientCVaR,
EfficientFrontier,
EfficientSemivariance,
EfficientCVaR,
)
from pypfopt.hierarchical_portfolio import HRPOpt
from pypfopt.risk_models import CovarianceShrinkage
@ -32,15 +32,15 @@ def test_explicit_import():
def test_import_toplevel():
from pypfopt import (
market_implied_prior_returns,
market_implied_risk_aversion,
BlackLittermanModel,
CLA,
get_latest_prices,
BlackLittermanModel,
CovarianceShrinkage,
DiscreteAllocation,
EfficientCVaR,
EfficientFrontier,
EfficientSemivariance,
EfficientCVaR,
HRPOpt,
CovarianceShrinkage,
get_latest_prices,
market_implied_prior_returns,
market_implied_risk_aversion,
)

View File

@ -6,16 +6,16 @@ import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest
from tests.utilities_for_tests import get_data, setup_efficient_frontier
from pypfopt import (
CLA,
EfficientFrontier,
expected_returns,
HRPOpt,
expected_returns,
plotting,
risk_models,
)
from tests.utilities_for_tests import get_data, setup_efficient_frontier
def test_correlation_plot():

View File

@ -2,7 +2,7 @@ import numpy as np
import pandas as pd
import pytest
from pypfopt import risk_models, expected_returns
from pypfopt import expected_returns, risk_models
from tests.utilities_for_tests import get_data

View File

@ -3,15 +3,14 @@ import os
import numpy as np
import pandas as pd
from pypfopt import expected_returns
from pypfopt import risk_models
from pypfopt import expected_returns, risk_models
from pypfopt.cla import CLA
from pypfopt.efficient_frontier import (
EfficientCDaR,
EfficientCVaR,
EfficientFrontier,
EfficientSemivariance,
EfficientCVaR,
EfficientCDaR,
)
from pypfopt.cla import CLA
def resource(name):