Skip to main content

A Pipeline-Oriented Portfolio Optimization Framework

Project description

Pipefolio: A Pipeline-Oriented Portfolio Optimization Framework

logo

About the project

Pipefolio is a Python-based portfolio optimization framework designed with a pipeline-oriented approach. While several excellent Python libraries for portfolio optimization already exist—such as PyPortfolioOpt, Riskfolio-Lib, and skfolio—Pipefolio takes a different direction.

Built on top of scikit-learn, Pipefolio leverages existing optimization algorithms while minimizing boilerplate code through the use of the pipe (|) operator, enabling a streamlined and efficient workflow.

The framework provides the following types of models, each serving a specific purpose in the portfolio optimization process:

  • Selector
    Screens assets to narrow down the investment universe.

  • Transformer
    Transforms and preprocesses data to ensure compatibility with optimization models.

  • Optimizer
    Implements portfolio optimization algorithms to generate optimal allocations.

  • Adaptor
    Extracts and formats the optimized weights for further analysis or implementation.

Getting started

Installation

pip install pipefolio

Documentation

Usages

This example download stock data using yfinance.

import yfinance as yf
from pipefolio.adaptor import WeightMappingAdaptor, WeightArrayAdaptor
from pipefolio.data import DataPlaceHolder
from pipefolio.enums import RiskMetric, SelectMode
from pipefolio.optimizer import (
    EqualWeightOptimizer,
    InverseVolatilityOptimizer,
    RiskAverseOptimizer,
)
from pipefolio.selector import (
    AnnualVolatilitySelector,
    AverageTradeValueSelector,
    TotalReturnSelector,
)
from pipefolio.transformer import PriceToReturnTransformer

# download data via yfinance
data = yf.download(
    ["AAPL", "AMZN", "NVDA", "BRK-B", "KO", "JNJ", "TSLA", "GS", "WMT", "MCD"],
    period="1y",
    interval="1d",
)

# data must be wrapped by DataPlaceHolder
data = DataPlaceHolder.from_dataframe(data)

# pipeline -> return optimized weights
(
    data
    | AverageTradeValueSelector(5)  # selector
    | PriceToReturnTransformer()  # transformer
    | InverseVolatilityOptimizer()  # optimizer
    | WeightMappingAdaptor()  # adaptor
)
# {'NVDA': np.float64(0.10316247719888795),
#  'TSLA': np.float64(0.08487498757904818),
#  'AAPL': np.float64(0.24167068511508258),
#  'AMZN': np.float64(0.19489272842965072),
#  'BRK-B': np.float64(0.3753991216773305)}


# chaining multiple selectors
(
    data
    | AverageTradeValueSelector(8, SelectMode.LARGEST)
    | TotalReturnSelector(5, SelectMode.LARGEST)
    | AnnualVolatilitySelector(3, SelectMode.SMALLEST)
    | PriceToReturnTransformer()
    | EqualWeightOptimizer()
    | WeightMappingAdaptor()
)
# {'WMT': np.float64(0.3333333333333333),
#  'GS': np.float64(0.3333333333333333),
#  'AMZN': np.float64(0.3333333333333333)}


# with different adaptor
(
    data
    | PriceToReturnTransformer()
    | RiskAverseOptimizer(RiskMetric.STANDARD_DEVIATION, risk_aversion=2)
    | WeightArrayAdaptor()
)
# array([6.38077192e-02, 1.94597392e-02, 1.59637665e-01, 9.15442233e-03,
#        1.94553086e-01, 2.87905182e-01, 3.25055500e-02, 6.43214713e-02,
#        2.21196278e-09, 1.68655163e-01])

Supported Models

  • Selector
    • AnnualVolatilitySelector
    • AverageTradeValueSelector
    • TotalReturnSelector
  • Transformer
    • PriceToReturnTransformer
  • Optimizer
    • EqualWeightOptimizer
    • InverseVolatilityOptimizer
    • MaxRatioOptimizer
    • MaxReturnOptimizer
    • MinRiskOptimizer
    • RiskAverseOptimizer
  • Adaptor
    • WeightArrayAdaptor
    • WeightMappingAdaptor

License

Distributed under the MIT License. See LICENSE for more information.

Maintainers

pipefolio is currently maintained by kfuangsung (kachain.f@outlook.com).

Acknowledgments

  • skfolio: Python library for portfolio optimization built on top of scikit-learn.
  • PyPortfolioOpt: Financial portfolio optimisation in python, including classical efficient frontier, Black-Litterman, Hierarchical Risk Parity.
  • Riskfolio-Lib: Portfolio Optimization and Quantitative Strategic Asset Allocation in Python.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pipefolio-0.0.1.tar.gz (322.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pipefolio-0.0.1-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file pipefolio-0.0.1.tar.gz.

File metadata

  • Download URL: pipefolio-0.0.1.tar.gz
  • Upload date:
  • Size: 322.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pipefolio-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7bdeac1432aebf9d458b2dde4228c09b9519348141d6fc5d0cb8fff9be3a915d
MD5 a5a773ad82c2e0a0c8002596b43a34d3
BLAKE2b-256 c4e7e6d388f3bdf3a06c484c83cbea9cfbe960b74e4bfb964839d10fc71f73b3

See more details on using hashes here.

File details

Details for the file pipefolio-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pipefolio-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for pipefolio-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 54c2476d225d2efd4a3b2aa9738c8a5e16b554bbf24f34336e92530749a635f9
MD5 a064bc3eb7ad587f3fa5c493737184a0
BLAKE2b-256 3e9047c604fc2ad82fd9dc090e43e2abd2831e90f2d86d08659cbfc8e83afbbf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page