mktRiskFactorReturns
First introduced in version: 3.00.6.4
Syntax
mktRiskFactorReturns(mktDataTable, asOf, [settings],
[baseMktData])
Details
Converts a date-aligned historical market data table into a standardized risk factor
return matrix, which can be used as input to the historical simulation engine
(historicalRiskEngine) and the EWMA simulation engine
(ewmaRiskEngine).
Currently, the following three market data types can be expanded into risk factors:
-
Price: Calculates continuously compounded log returns.
-
FxSpotRate: Calculates continuously compounded log returns.
-
IrYieldCurve: Expands all tenor nodes in the curve, converts them into zero-coupon bond prices of the corresponding maturities, and then calculates log returns.
Parameters
mktDataTable is a historical market data table. Currently, Price, FxSpotRate, and IrYieldCurve types are supported. The first column must be a DATE column named dates. Other columns are MKTDATA instance columns for different assets. All columns in the table must be aligned by date. Dates must be strictly increasing, and missing values are not allowed. At least two rows are required to calculate returns between adjacent dates.
asOf is a DATE scalar indicating the base date for risk analysis.
settings (optional) is a STRING-ANY dictionary. It is currently a reserved parameter and no specific configuration items are supported yet.
baseMktData (optional) is an MKTDATA vector that specifies the base market data on the base date. If asOf is the same as the last date in mktDataTable, the last row of market data is used as the base snapshot by default. If asOf is different from the last date in the historical table, baseMktData must be specified.
Returns
Returns a STRING-ANY dictionary with the following keys:
-
asOf: A DATE scalar indicating the base valuation date.
-
keys: An ANY vector recording the metadata definitions of each expanded risk factor, with a length equal to the total number of factors. Each element is a dictionary (STRING → ANY) containing the following keys:
-
type: STRING. The factor type, such as "Price", "FxSpotRate", or "IrYieldCurve".
-
name: STRING. The factor name or asset identifier.
-
term: DOUBLE. The tenor of a curve node in years. For price and FX rate risk factors, this value is NULL.
-
-
dates: A DATE vector indicating the historical dates corresponding to the returns. Its length is size(mktDataTable) - 1.
-
factorReturns: A DOUBLE matrix containing the log returns of all factors. The number of rows equals the length of dates, and the number of columns corresponds to the total number of expanded factors.
-
baseLevels: A DOUBLE vector containing the base level of each factor on the base date.
-
baseMktData: An ANY vector containing a list of base market data objects. The number of elements equals the number of market data columns in mktDataTable (excluding the dates column), where each element corresponds to a market data object (MKTDATA).
Examples
The following example obtains the trading days in a specified period, constructs
historical CNY treasury yield curves in batches, and calls
mktRiskFactorReturns to convert historical market data into a
risk factor return object.
// Define a function to construct a single-period interest rate curve MKTDATA object
def makeExampleCurve(d, rates) {
return parseMktData({
"mktDataType": "Curve",
"curveType": "IrYieldCurve",
"curveName": "CNY_TREASURY_BOND",
"referenceDate": d,
"currency": "CNY",
"dayCountConvention": "Actual365",
"compounding": "Continuous",
"frequency": "Annual",
"interpMethod": "Linear",
"extrapMethod": "Flat",
"dates": d + 365 * (1..size(rates)),
"values": rates
})
}
// Obtain the trading days in the specified period
dates = getMarketCalendar("SSE", 2025.10.01, 2025.12.31)
curveHistory = array(ANY, 0)
for (i in 0..(size(dates)-1)) {
// Simulate a yield curve with small fluctuations
rates = [0.020, 0.022, 0.025] + rand(0.002, 3) - 0.001
curveHistory.append!(makeExampleCurve(dates[i], rates))
}
mktDataTable = table(dates as dates, curveHistory as CNY_TREASURY_BOND)
// Call mktRiskFactorReturns to calculate factor returns
asOf = mktDataTable.dates.last()
mfr = mktRiskFactorReturns(mktDataTable=mktDataTable, asOf=asOf)
Related Functions: ewmaRiskEngine, historicalRiskEngine, parseMktData, parseInstrument, instrumentPricer
