calc_metrics#
calc_metrics.py
This module provides functions to calculate analysis metrics comparing decile portfolio returns with benchmark US corporate bond returns, as well as to split the decile returns DataFrame into two separate DataFrames based on a cutoff date. The cutoff date is defined as the last date present in the US corporate bonds DataFrame.
Functions: - calculate_decile_analysis(decile_returns_df, us_corp_df) Calculates correlation, R², regression slope/intercept, MAE, RMSE, and tracking error for decile returns (for deciles 11-20).
- split_decile_returns(decile_returns_df, us_corp_df)
Splits the decile returns DataFrame into:
* replication_df: rows with dates ⤠cutoff date (last date in us_corp_df),
* updated_reproduction_df: rows with dates > cutoff date.
When run as a script, the module loads the necessary parquet files from DATA_DIR and OUTPUT_DIR, splits the decile returns, and saves the resulting DataFrames as ānozawa_replication.parquetā and ānozawa_updated_reproduction.parquetā. It does not print any additional output.
Module Contents#
Functions#
Calculate analysis metrics comparing decile portfolio returns with benchmark US corporate bond returns. |
|
Splits the decile returns DataFrame into two parts based on a cutoff date. |
|
Plot the cumulative returns for each portfolio in the reproduction DataFrame. |
|
Data#
API#
- calc_metrics.DATA_DIR#
āPath(ā¦)ā
- calc_metrics.OUTPUT_DIR#
āPath(ā¦)ā
- calc_metrics.calc_summary(series)#
- calc_metrics.get_date_range(df, col)#
- calc_metrics.calculate_decile_analysis(decile_returns_df, us_corp_df)#
Calculate analysis metrics comparing decile portfolio returns with benchmark US corporate bond returns.
Additionally, compute summary statistics (mean, standard deviation, cumulative return, start date, end date) for each column in the merged dataframe. For columns from decile_returns_df and us_corp_df, the portfolio column will simply be the decile number.
Parameters
decile_returns_df : pd.DataFrame DataFrame containing the replicated decile returns with a ādateā column and decile columns. This function expects decile columns (other than ādateā) that can be converted to integers. us_corp_df : pd.DataFrame DataFrame containing the benchmark US corporate bond returns with a ādateā column and columns named āUS_bonds_11ā, āUS_bonds_12ā, ā¦, āUS_bonds_20ā.
Returns
analysis_df : pd.DataFrame A DataFrame with one row per decile and columns: - āportfolioā - ācorrelationā - ār_squaredā - āslopeā - āinterceptā - āMAEā - āRMSEā - ātracking_errorā
benchmark_summary_df : pd.DataFrame A DataFrame containing summary statistics for each column from the us_corp_df (benchmark) merged into the common dataframe, plus an overall row.
replicate_summary_df : pd.DataFrame A DataFrame containing summary statistics for each column from the decile_returns_df (replication) merged into the common dataframe, plus an overall row.
- calc_metrics.split_decile_returns(decile_returns_df, us_corp_df)#
Splits the decile returns DataFrame into two parts based on a cutoff date.
The cutoff date is defined as the last date present in the US corporate bonds DataFrame. Rows in decile_returns_df with a date on or before the cutoff are placed in the replication sample, while rows with a date after the cutoff are placed in the updated reproduction sample.
Parameters
decile_returns_df : pd.DataFrame DataFrame containing decile returns with a ādateā column. us_corp_df : pd.DataFrame DataFrame containing benchmark US corporate bond returns with a ādateā column.
Returns
replication_df : pd.DataFrame DataFrame with rows from decile_returns_df where date ⤠cutoff_date. updated_reproduction_df : pd.DataFrame DataFrame with rows from decile_returns_df where date > cutoff_date.
- calc_metrics.plot_cumulative_returns(reproduction_df, save_path=None, show=True)#
Plot the cumulative returns for each portfolio in the reproduction DataFrame.
This function assumes that the input DataFrame contains:
a ādateā column with datetime values,
one or more portfolio return columns representing periodic returns (in decimal form).
It computes cumulative returns as: cumulative_return = (1 + r).cumprod() - 1 for each portfolio, plots them, and optionally saves the plot to a file.
Parameters
reproduction_df : pd.DataFrame DataFrame containing periodic returns for each portfolio. Must have a ādateā column and portfolio columns (e.g. columns 11, 12, ⦠20). save_path : str or Path, optional If provided, the plot is saved to this file. show : bool, optional If True (default), the plot is displayed immediately.
Returns
fig : matplotlib.figure.Figure The figure object for the plot. ax : matplotlib.axes.Axes The axes object for the plot.
- calc_metrics.load_analysis(output_dir=OUTPUT_DIR)#
- calc_metrics.load_reproduction(output_dir=OUTPUT_DIR)#
- calc_metrics.load_replication(output_dir=OUTPUT_DIR)#
- calc_metrics.load_benchmark_summary(output_dir=OUTPUT_DIR)#
- calc_metrics.load_replicate_summary(output_dir=OUTPUT_DIR)#