This R function `calcExcess` is designed to calculate **excess returns**...
This R function calcExcess
is designed to calculate excess returns for a set of financial strategies or sub-strategies, based on a benchmark return (bm.ret
) and a specified granularity level (pivot.col
). Let's break down what the code is doing:
Purpose:
The function calculates excess returns for individual strategies or sub-pivots within a dataset. For finance, "excess returns" typically refer to the returns generated above a benchmark's return, weighted appropriately by allocation or importance.
The granularity of the grouping (determined by pivot.col
) allows the user to aggregate and calculate excess returns at a specific strategy or category level.
Input Parameters:
-
core.sub.mat
: A data frame or matrix containing inputs for the calculations. It should include:"dt_record"
: Dates for the time periods."ret"
: Returns for each row."wts"
: Weights (allocations) corresponding to each return.- Other columns, including the pivot column specified by
pivot.col
.
-
bm.ret
: A scalar representing the benchmark return, possibly a vector or constant return value. -
pivot.col
: (Default:"name_strategy"
) A string specifying the column name that determines the group or strategy to aggregate returns on.
How the Code Works:
-
Set Up Number of Pivot Levels:
- The function determines how many unique values exist in the
pivot.col
. This indicates the number of strategies or pivot groups for calculating excess returns.
- The function determines how many unique values exist in the
-
Aggregate Total Weights by Dates:
- The code first sums the absolute values of the weights (
wts
) per date (dt_record
) usingaggregate
. - This produces the total weight for all strategies on each day.
- The code first sums the absolute values of the weights (
-
Aggregate Returns and Weights for Each Pivot Level:
- Using the grouping column (
pivot.col
), the function aggregates the returns and weights per group and date.
- Using the grouping column (
-
Compute Weight Proportions for Each Pivot:
- The weight for each pivot strategy relative to the total weight (
tot.wt
) across all strategies is calculated. - This ensures the function considers the relative proportion of each pivot's contribution to the benchmark.
- The weight for each pivot strategy relative to the total weight (
-
Calculate Excess Returns for Each Pivot:
- Excess returns for each pivot strategy (
ret.excess.pivot
) are calculated as: [ \text{Excess Return} = \text{Pivot Return} - (\text{Weight Proportion} \times \text{Benchmark Return}) ]
- Excess returns for each pivot strategy (
-
Match Sub-Pivots to Their Pivot Group:
- Sub-pivots (rows in the dataset) are matched to their aggregated pivot-level data. This is necessary to allocate excess returns back to sub-pivots.
-
Compute Weights of Sub-Pivots Within Pivots:
- Within a pivot, weights of sub-pivots are computed (
wt.pivot
), and they are normalized relative to the pivot total weight.
- Within a pivot, weights of sub-pivots are computed (
-
Allocate Excess Returns to Sub-Pivots:
- Each sub-pivot is allocated a proportion of the pivot's excess return, using the calculated weights of sub-pivots.
Final Output:
The function returns nothing directly (perhaps because the function is incomplete). However, it internally calculates:
ret.excess.pivot
: The excess return for each pivot strategy.- Allocated excess returns for sub-pivots (last computation).
Currently, the last computed result (wt.pivot * ret.excess.pivot[pivot.map.idx]
) is not stored or returned, which suggests that either the function is incomplete or expects further implementation to handle the results.
Key Notes:
-
Dependencies:
- The function requires
strToDt
anddtToString
functions for date conversions. These seem to be custom functions, as they are not native to R.
- The function requires
-
Ensure Input Data Structure:
- The input
core.sub.mat
needs to contain specific columns like"dt_record"
,"ret"
,"wts"
, and the grouping columnpivot.col
.
- The input
-
Incomplete Implementation:
- The function appears incomplete as it doesn't return or modify any data explicitly, though it performs the excess return calculation.
-
Use Case:
- The function would likely be used in financial applications where strategies or portfolios are benchmarked against a common index/return, and there is a need to determine excess performance contributions at granular levels (strategies or sub-strategies).