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#

calc_summary

get_date_range

calculate_decile_analysis

Calculate analysis metrics comparing decile portfolio returns with benchmark US corporate bond returns.

split_decile_returns

Splits the decile returns DataFrame into two parts based on a cutoff date.

plot_cumulative_returns

Plot the cumulative returns for each portfolio in the reproduction DataFrame.

load_analysis

load_reproduction

load_replication

load_benchmark_summary

load_replicate_summary

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)#