portfolio_toolkit.math.get_matrix_returns module
- portfolio_toolkit.math.get_matrix_returns.get_matrix_returns(market_assets: List[MarketAsset]) DataFrame [source]
Creates a DataFrame of logarithmic returns from a list of MarketAsset objects.
- Parameters:
market_assets (List[MarketAsset]) – List of MarketAsset objects, each containing a ‘price’ attribute as pd.Series with price data
- Returns:
- DataFrame where:
Rows are dates (datetime index from price series)
Columns are ticker symbols from MarketAsset.ticker
Values are logarithmic returns calculated using get_log_returns()
- Return type:
pd.DataFrame
Example
>>> market_assets = [ ... MarketAsset(ticker="AAPL", price=aapl_prices), ... MarketAsset(ticker="MSFT", price=msft_prices), ... MarketAsset(ticker="GOOGL", price=googl_prices) ... ] >>> returns_df = get_matrix_returns(market_assets) >>> print(returns_df.head())
AAPL MSFT GOOGL
2023-01-01 NaN NaN NaN 2023-01-02 0.0123 0.0089 0.0156 2023-01-03 -0.0067 0.0034 -0.0023
Notes
First row will contain NaN values (no previous price for return calculation)
Uses get_log_returns() function for consistent logarithmic return calculation
Automatically aligns dates across all assets
Missing data points will appear as NaN in the resulting DataFrame
- portfolio_toolkit.math.get_matrix_returns.get_matrix_returns_aligned(market_assets: List[MarketAsset], start_date: str = None, end_date: str = None) DataFrame [source]
Creates an aligned DataFrame of returns with optional date filtering.
- Parameters:
market_assets – List of MarketAsset objects
start_date – Start date in ‘YYYY-MM-DD’ format (optional)
end_date – End date in ‘YYYY-MM-DD’ format (optional)
- Returns:
Aligned returns DataFrame with only overlapping dates
- Return type:
pd.DataFrame
Example
>>> # Get returns for specific period >>> returns_df = get_matrix_returns_aligned( ... market_assets, ... start_date="2023-01-01", ... end_date="2023-12-31" ... )