Portfolio Construction

PortfolioConstructor

A class to construct balanced and weighted portfolios with constraints.

Parameters (constructor)

  • max_iterations (int, default=1000): Maximum iterations for constraint enforcement.
  • tolerance (float, default=1e-6): Convergence tolerance for constraint enforcement.

Example

from bigdata_research_tools.portfolio import PortfolioConstructor, WeightMethod
import pandas as pd

df = pd.DataFrame({
    "company": ["A", "B", "C", "D"],
    "sector": ["Tech", "Tech", "Finance", "Finance"],
    "score": [0.9, 0.8, 0.7, 0.6],
    "market_cap": [100, 200, 150, 120]
})

constructor = PortfolioConstructor()
portfolio = constructor.construct_portfolio(
    df=df,
    score_col="score",
    balance_col="sector",
    weight_col="market_cap",
    size=4,
    max_position_weight=0.5,
    max_category_weight=0.8,
    weight_method=WeightMethod.COLUMN
)
print(portfolio)

construct_portfolio

Build a balanced and weighted portfolio with position and category constraints.

Parameters

  • df (pd.DataFrame): DataFrame containing company data.
  • score_col (str): Column name to use for ranking companies within each category.
  • balance_col (str): Column name to use for balancing (e.g., ‘sector’, ‘industry’, ‘region’).
  • weight_col (str, optional): Column name to use for weighting when using COLUMN or SCORE methods.
  • size (int, optional): Target number of companies in the portfolio.
  • max_position_weight (float, default=0.05): Maximum weight allowed for any single position.
  • max_category_weight (float, default=0.15): Maximum weight allowed for any category.
  • weight_method (WeightMethod, default=WeightMethod.EQUAL): Weighting methodology to use.

Returns

  • pd.DataFrame: Portfolio with calculated weights, sorted by weight (descending).

Example

portfolio = constructor.construct_portfolio(
    df=df,
    score_col="score",
    balance_col="sector",
    weight_col="market_cap",
    size=4,
    max_position_weight=0.5,
    max_category_weight=0.8,
    weight_method=WeightMethod.COLUMN
)

WeightMethod

Enumeration for portfolio weighting methods.

  • WeightMethod.EQUAL: Equal weighting for all companies.
  • WeightMethod.COLUMN: Weight based on a specific column (e.g., market cap).
  • WeightMethod.SCORE: Weight based on score values (softmax-normalized).

Example

from bigdata_research_tools.portfolio import WeightMethod

method = WeightMethod.SCORE