To summarize, this code consists of two primary functions, and...

July 1, 2025 at 06:11 AM

input 13 month index values, and then generate the lookback returns for 12 month # returns sequence of 12 returns # calc.lookback.ret <- function(in.vec) { in.vec.cnt <- length(in.vec) out.vec <- rep(0, in.vec.cnt - 1) for(i in c(1:length(out.vec))) { out.vec[i] <- in.vec[in.vec.cnt] / in.vec[in.vec.cnt - i] - 1 } return(out.vec) } # # apply momentum weighting and expand the weights from monthly to daily # input is the daily index value, monthly return check date, optionally # dates. if dates is not provided, then the dates are stripped from # index values, which is assumed to be a 1 dimensional vector. # the output is the weights and index values, in a matrix whose # columns are "Weights" and "Index Values", with dates as the row names # gen.plus.w <- function(idx, calc.dates, roll.dates, idx.dates=NULL) { if(is.null(idx.dates)) idx.dates <- names(idx) idx.dates <- as.Date(idx.dates, format="%m/%d/%Y") idx.ts <- as.timeSeries(idx, idx.dates) select.key <- (calc.dates > 0) roll.key <- (roll.dates > 0) idx.select.ts <- idx.ts[select.key] idx.select.dates <- idx.dates[select.key] idx.select.w <- rep(0, length(idx.select.ts)) for(i in c(14:length(idx.select.w))) { short.ret <- calc.lookback.ret(idx.select.ts[(i-12):(i)]) short.w <- as.numeric(short.ret >= 0) idx.select.w[i] <- mean(short.w) * 100 } idx.roll.dates <- idx.dates[roll.key] roll.start <- length(idx.select.ts) - length(idx.roll.dates) + 1 roll.end <- length(idx.select.ts) result.w <- idx.select.w[c(roll.start:roll.end)] w.ts <- fill.w(idx.dates, idx.roll.dates, result.w) return(w.ts) }

To summarize, this code consists of two primary functions, and their purpose is as follows:


Function: calc.lookback.ret

This function calculates the lookback returns (percentage changes) for a vector of index values over a period of 12 months. Here's how it works:

  1. Inputs:
    • A vector in.vec that represents a series of index values.
  2. Process:
    • It initializes a vector out.vec to store 12 return values (length = length(in.vec) - 1).
    • For each step in out.vec, it calculates the return defined as: [ \text{Return}[i] = \frac{\text{Last Value}}{\text{Value at } (N-i)} - 1 ] where ( N ) is the length of the input vector.
  3. Outputs:
    • A vector containing the sequence of returns (percentage changes over the past 12 months).

Function: gen.plus.w

This function applies momentum-based weighting to index values and expands monthly weights to daily weights. Here's how it works:

  1. Inputs:
    • idx: A vector of daily index values.
    • calc.dates: A logical vector indicating which points to include in the calculation.
    • roll.dates: A logical vector indicating rolling dates for the output.
    • idx.dates: (Optional) A sequence of dates corresponding to the index values. If not provided, dates are extracted from names.
  2. Process:
    • The function converts the index into a time-series (assumes as.timeSeries is used for this purpose).
    • It filters out the sections of the index relevant to the calculation (calc.dates).
    • For each month after the first 13 months (loop from i = 14 to end), it calculates 12-month lookback returns using the calc.lookback.ret function.
    • Then, it determines a binary "momentum signal" (short.w) where: [ \text{Signal}[i] = 1 \text{ if return} \geq 0, \text{ else } 0 ]
    • The weights for each period are computed as the mean momentum signal (percentage of positive returns).
    • These weights are then mapped to the rolling dates provided.
  3. Outputs:
    • A time-series of weights for all the rolling dates over the index.

Overall Purpose

  • This script is designed for momentum-based investment strategies.
  • It calculates 12-month momentum signals (calc.lookback.ret) and expands them into daily time-series weights (gen.plus.w).
  • The resulting weight vector represents momentum signals applied to daily periods for use in portfolio construction, risk management, or other investment 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