Portfolio Optimization and Quantitative Strategic Asset Allocation in Python
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Comprehensive portfolio reporting and analysis tools for generating detailed portfolio performance reports in Jupyter notebooks and Excel formats. The Reports module provides automated report generation with portfolio statistics, risk analysis, and visualizations.
Generate comprehensive portfolio analysis reports in Jupyter notebook format with interactive visualizations and detailed statistics.
def jupyter_report(returns, w, rm='MV', rf=0, alpha=0.05, others=0.05, nrow=25,
height=6, width=14, ax=None):
"""
Generate comprehensive portfolio report in Jupyter notebook format.
Parameters:
- returns (DataFrame): Assets returns data
- w (DataFrame): Portfolio weights
- rm (str): Risk measure for analysis ('MV', 'MAD', 'CVaR', etc.)
- rf (float): Risk-free rate
- alpha (float): Significance level for risk measures
- others (float): Threshold for grouping small positions
- nrow (int): Maximum number of rows to display
- height (float): Figure height in inches
- width (float): Figure width in inches
- ax (matplotlib.Axes): Matplotlib axes object
Returns:
None: Displays report directly in Jupyter notebook
"""Generate detailed portfolio analysis reports in Excel format with multiple worksheets containing portfolio statistics, risk analysis, and performance metrics.
def excel_report(returns, w, rf=0, alpha=0.05, others=0.05, nrow=25,
name='report', folder='Reports/', sheet_name='Main_Report'):
"""
Generate comprehensive portfolio report in Excel format.
Parameters:
- returns (DataFrame): Assets returns data
- w (DataFrame): Portfolio weights
- rf (float): Risk-free rate
- alpha (float): Significance level for risk measures
- others (float): Threshold for grouping small positions as "Others"
- nrow (int): Maximum number of assets to display individually
- name (str): Base filename for the Excel report
- folder (str): Directory path to save the report
- sheet_name (str): Name of the main worksheet
Returns:
str: Path to the generated Excel file
"""Both Jupyter and Excel reports include:
import riskfolio as rp
import pandas as pd
# Load data and optimize portfolio
returns = pd.read_csv('returns.csv', index_col=0, parse_dates=True)
port = rp.Portfolio(returns=returns)
port.assets_stats(method_mu='hist', method_cov='hist')
w = port.optimization(model='Classic', rm='MV', obj='Sharpe', rf=0.02)
# Generate Jupyter report
rp.jupyter_report(returns=returns, w=w, rm='MV', rf=0.02, alpha=0.05)# Generate Excel report with custom settings
report_path = rp.excel_report(
returns=returns,
w=w,
rf=0.02,
alpha=0.05,
others=0.03, # Group assets < 3% as "Others"
nrow=30, # Show top 30 assets individually
name='portfolio_analysis_2024',
folder='./reports/',
sheet_name='Portfolio_Analysis'
)
print(f"Report saved to: {report_path}")# Generate report with CVaR risk analysis
rp.jupyter_report(
returns=returns,
w=w,
rm='CVaR', # Use CVaR for risk analysis
rf=0.02,
alpha=0.01, # 99% confidence level
others=0.02,
height=8,
width=16
)The Reports module seamlessly integrates with Riskfolio-Lib's portfolio optimization and analysis workflows:
Install with Tessl CLI
npx tessl i tessl/pypi-riskfolio-lib