This R function `calcExcessBenchmarkAll` computes the excess benchmark returns for...

June 30, 2025 at 06:51 AM

#################### FUNCTION START #################### # returns a vector containing the excess benchmark return for each trade, retaining the ticker order as given in bm.map # core.mat will map tickers/trades to their respective strategy and benchmark, ret.bm is a matrix containing the time series of benchmark returns calcExcessBenchmarkAll <- function(core.mat, datasource) { source("execscheme_simple.r"); bm.list <- unique(core.mat[,"benchmark"]); all.dates <- unique(core.mat[,"dt_record"]);#added by kyle on 1/20/2012 # for each benchmark, calculate the excess returns of strategies getting compared to that benchmark core.temp.mat <- cbind(core.mat,ret_excessbm=core.mat[,"ret"]); for (i in 1:length(bm.list)) { if (bm.list[i] != "NONE") { #datakeys <- c(TICKER=bm.list[i],STARTDATE=all.dates[1],ENDDATE=all.dates[length(all.dates)],FEE=0);#added input params for the getExecReturns() by kyle on 1/20/2012 #scheme.params <- c(PRICINGSOURCE="tbl_idx_prices");#added input params for the getExecReturns() by kyle on 1/20/2012 #bm.ret <- getExecReturns(bm.list[i], start.date=dimnames(ret.tickers)[[1]][1], end.date=dimnames(ret.tickers)[[1]][nrow(ret.tickers)], "tbl_idx_prices")$gross; core.sub.mat <- core.temp.mat[core.temp.mat[,"benchmark"]==bm.list[i],]; # the sub-matrix of core which pertains to this benchmark bm.ret <- getExecReturns(ticker=bm.list[i], start.date=prevCalDay(core.sub.mat[1,"dt_record"],0), end.date=core.sub.mat[nrow(core.sub.mat),"dt_record"], tbl.name="tbl_idx_prices", datasource=datasource)$gross[-1];#updated by kyle on 1/20/2012; we dont' care about first day's return in this case -SZ core.temp.mat[core.temp.mat[,"benchmark"]==bm.list[i],"ret_excessbm"] <- calcExcess(core.sub.mat, bm.ret); } } # convert back to matrix form # return a matrix of the date and the excess benchmark return core.temp.mat; }

This R function calcExcessBenchmarkAll computes the excess benchmark returns for a series of trades, mapped to their respective strategies and benchmarks, while retaining the order of tickers from the input matrix. Here's a breakdown of the key tasks performed:


Key Purpose

The function calculates the excess returns for each trade relative to the benchmark associated with it and appends this information to the provided core.mat matrix as a new column (ret_excessbm).


Inputs

  1. core.mat:

    • A matrix containing information on trades.
    • Contains columns like "benchmark", "ret" (returns), and "dt_record" (dates on which the trades occurred).
  2. datasource:

    • A data source connection or an object used by the getExecReturns function to fetch external data (e.g., benchmark price data).

Steps

  1. Initialization:

    • The function identifies all unique benchmarks in the core.mat (bm.list).
    • It gathers all unique recorded dates (all.dates).
  2. Create a Temporary Working Matrix:

    • A new column, ret_excessbm, is added to core.mat to store the computed excess benchmark returns. Initially, it is set to the same value as the "ret" column.
  3. Iterate Over Each Benchmark:

    • For each unique benchmark (excluding the value "NONE"):
      • Subset the core.mat matrix for records tied to the current benchmark.
  4. Get Benchmark Returns:

    • The external function getExecReturns retrieves the benchmark's returns (bm.ret) for the relevant dates (start.date and end.date) from the data source (datasource).
    • The retrieval starts from the previous trading day (via prevCalDay) and excludes the first returned value (e.g., the first day's return isn't used).
  5. Calculate Excess Returns:

    • The function calcExcess computes the excess returns of the trades relative to the benchmark (bm.ret), using the subsetted data.
  6. Update the Output:

    • The computed excess returns are assigned back to the ret_excessbm column for the relevant benchmark group.
  7. Return Result:

    • After processing all benchmarks, the updated matrix is returned, with an added column (ret_excessbm) containing the excess benchmark returns for each trade.

Outputs

The function returns a matrix (core.temp.mat) with all the original columns from core.mat, plus an additional column (ret_excessbm) that includes the computed excess benchmark returns.


Dependent Functions

The function depends on these external functions:

  1. source("execscheme_simple.r"):

    • Presumably loads necessary logic (e.g., implementations of getExecReturns and calcExcess).
  2. getExecReturns:

    • Fetches benchmark returns for a given ticker and time range, associated with the provided datasource.
    • Returns an object with a $gross property representing the returns.
  3. calcExcess:

    • Calculates the excess returns of the strategy compared to the benchmark based on the input matrix and benchmark returns.
  4. prevCalDay:

    • Determines the previous calendar day relative to a given day.

Summary

This function processes a matrix of trades, calculates the excess benchmark return for each trade using associated benchmark data (queried from datasource), and outputs the modified matrix with the added excess return information. It is designed for use in portfolio management or financial performance analysis tasks.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node