Portfolio: IMsFormulaTermInfo;
The Portfolio property determines the term that corresponds to variable containing portfolio data.
Portfolio contains the list of organizations and number of financial tools that organizations have. The variable, that contains the information about portfolio, consists of the following dimensions:
Organizations. The dimension must be specified in IMsValueAtRiskTransform.OrganizationsDimension.
Tools. The dimension must be specified in IMsValueAtRiskTransform.InstrumentsDimension.
Executing the example requires that the repository contains dictionaries with the identifiers:
DIM_INSTRUMENTS. It contains the list of financial instrucments.
DIM_ORGANIZATIONS. It contains the list of organizations.
These dictionaries are used for variable dimensions described below.
The repository should contain a modeling container with the MS_PRIMARY identifier that contains a model with the M_VAR identifier. The container should include variables with identifiers:
V_STOCKPRICES. Variable of financial tools. It contains the DIM_INSTRUMENTS dimension
V_PORTFOLIO. Variable of portfolio. It contains the DIM_INSTRUMENTS and DIM_ORGANIZATIONS dimensions
V_VALUEATRISK. Variable, to which the results are loaded. It contains the DIM_ORGANIZATIONS dimension.
Add links to the Dimensions, Cubes, Metabase, Ms, Stat system assemblies.
Sub UserProc;
Var
mb: IMetabase;
pMsDescrKey: Integer;
pModel: IMsModel;
pTrans: IMsFormulaTransform;
pInsStub, pPortStub, pVaRStub: IVariableStub;
pOutVar, pPortVar, pInsVar: IMsFormulaTransformVariable;
pOutSlice, pPortSlice, pInsSlice: IMsFormulaTransformSLice;
pDimIns, pDimOrg: IDimensionModel;
pInstIns, pInstOrg: IDimInstance;
pSel: IDimSelection;
pSelSet: IDimSelectionSet;
pFactory: IDimSelectionSetFactory;
pSelector: IMsFormulaTransformSelector;
pFormula: IMsFOrmula;
pVaR: IMsValueAtRiskTransform;
pTerm: IMsFormulaTermInfo;
pBT: IMsValueAtRiskBacktesting;
coo: IMsFormulaTransformCoord;
calc: IMsMethodCalculation;
res: IMsValueAtRiskBacktestingResult;
i: integer;
Begin
mb := MetabaseClass.Active;
pMsDescrKey := mb.ItemById("MS_PRIMARY").Key;
pModel := mb.ItemByIdNamespace("M_VAR", pMsDescrKey).Edit As IMsModel;
pTrans := pModel.Transform;
// Delete all variables from model
pTrans.Inputs.Clear;
pTrans.Outputs.Clear;
pTrans.Series.Clear;
// Set sample and forecast periods
pModel.Period.IdentificationStartDate := DateTime.ComposeDay(2000, 1, 1);
pModel.Period.IdentificationEndDate := DateTime.ComposeDay(2000, 1, 10);
pModel.Period.ForecastStartDate := DateTime.ComposeDay(2000, 1, 11);
pModel.Period.ForecastEndDate := DateTime.ComposeDay(2000, 1, 20);
// Get variables for VaR model
pInsStub := mb.ItemByIdNamespace("V_STOCKPRICES", pMsDescrKey).Bind As IVariableStub;
pPortStub := mb.ItemByIdNamespace("V_PORTFOLIO", pMsDescrKey).Bind As IVariableStub;
pVaRStub := mb.ItemByIdNamespace("V_VALUEATRISK", pMsDescrKey).Bind As IVariableStub;
// Get dimension of financial tools
pInstIns := mb.ItemById("DIM_INSTRUMENTS").Open(Null) As IDimInstance;
pDimIns := pInstIns.Dimension;
// Get dimension of organizations
pInstOrg := mb.ItemById("DIM_ORGANIZATIONS").Open(Null) As IDimInstance;
pDimOrg := pInstOrg.Dimension;
// Add output variable
pOutVar := pTrans.Outputs.Add(pVaRStub);
// Get input variable slice
pFactory := New DimSelectionSetFactory.Create;
pSelSet := pFactory.CreateDimSelectionSet;
pSel := pSelSet.Add(pInstOrg);
pSel.SelectAll;
pOutSlice := pOutVar.Slices.Add(pSelSet);
// Add the variable of portfolio
pPortVar := pTrans.Inputs.Add(pPortStub);
// Get the slice of portfolio
pSelSet.Clear;
pSel := pSelSet.Add(pInstIns);
pSel.SelectAll;
pSel := pSelSet.Add(pInstOrg);
pSel.SelectAll;
pPortSlice := pPortVar.Slices.Add(pSelSet);
// Add variable of financial tools
pInsVar := pTrans.Inputs.Add(pInsStub);
// Get the slice of financial tools
pSelSet.Clear;
pSel := pSelSet.Add(pInstIns);
pSel.SelectAll;
pInsSlice := pInsVar.Slices.Add(pSelSet);
// Set model calculation method
pSelector := pTrans.CreateSelector;
pSelector.Slice := pOutSlice;
pSelector.Level := DimCalendarLevel.Day;
pFormula := pTrans.Transform(pSelector);
pFormula.Kind := MsFormulaKind.ValueAtRisk;
pModel.Kind := MsModelKind.ValueAtRisk;
pFormula.Level := DimCalendarLevel.Day;
pVaR := pFormula.Method As IMsValueAtRiskTransform;
// Set dimensions of tools and organizations
pVaR.InstrumentsDimension := pDimIns;
pVaR.OrganizationsDimension := pDimOrg;
// Set "portfolio"
pTerm := pTrans.CreateTermInfo;
pTerm.Slice := pPortSlice;
pVaR.Portfolio := pTerm;
// Set financial tools
pTerm := pTrans.CreateTermInfo;
pTerm.Slice := pInsSlice;
pVaR.StockPrices := pTerm;
// Set output variable
pTerm := pTrans.CreateTermInfo;
pTerm.Slice := pOutSlice;
pVaR.ValueAtRisk := pTerm;
// Specify VaR calculation method
pVaR.MethodType := MsValueAtRiskMethodType.MonteCarlo;
// Specify confidence limit significance
pVaR.ConfidenceLevel := 0.05;
// Set missing data treatment method
pVaR.UseFillGaps := True;
pVaR.MissingData.Method := MissingDataMethod.SampleAverage;
// Use logarithmic yield and zero mean hypothesis
pVaR.LogarithmicProfit := True;
pVaR.ZeroMean := True;
// Set forecasting horizon
pVaR.ForecastingHorizon := 1;
// Set up Monte Carlo method parameters:
// Use the Cholesky factorization
pVaR.UseCholeskyFactorization := True;
// Set distribution for financial tools
For i := 0 To pInstIns.Elements.Count - 1 Do
pVaR.InstrumentDistribution(i) := MsValueAtRiskDistributionType.Uniform;
End For;
// Set backtesting VaR
pBT := pVaR.Backtesting;
pBT.Enabled := True;
pBT.Observations := 10;
// Set calculation parameters
coo := ptrans.CreateCoord(pTrans.Outputs.Item(0));
calc := pTrans.CreateCalculation;
calc.Period.IdentificationStartDate := pModel.Period.IdentificationStartDate;
calc.Period.IdentificationEndDate:=pModel.Period.IdentificationEndDate;
calc.Period.ForecastStartDate := pModel.Period.ForecastStartDate;
calc.Period.ForecastEndDate := pModel.Period.ForecastEndDate;
// Calculate model
pVaR.Execute(calc, coo);
// display results
For i := 0 To pInstOrg.Elements.Count - 1 Do
res := pBT.Result(i);
Debug.WriteLine("=====Organization: " + pInstOrg.Elements.Name(i) + "=======");
If res <> Null Then
Debug.WriteLine("Confidence level = " + res.ActualConfidenceLevel.ToString);
Debug.WriteLine("Average uncovered risk = " + res.AverageUncoveredRisk.ToString);
Debug.WriteLine("Average unused risk = " + res.AverageUnusedRisk.ToString);
Debug.WriteLine("Binary loss function = " + res.BinaryLossFunction.ToString);
Debug.WriteLine("Multiple to obtain coverage = " + res.CoveringFactor.ToString);
Debug.WriteLine("Quadratic loss function = " + res.QuadraticLossFunction.ToString);
Select Case res.TrafficLight
Case MsValueAtRiskTrafficLight.Green: Debug.WriteLine("Traffic light = is green");
Case MsValueAtRiskTrafficLight.Yellow: Debug.WriteLine("Traffic light = is yellow");
Case MsValueAtRiskTrafficLight.Red: Debug.WriteLine("Traffic light = is red");
End Select;
Debug.WriteLine("Correlation between VaR and actual losses = " + res.VaRLossCorrelation.ToString);
Else
Debug.WriteLine("Result for organization " + i.ToString + " is empty!");
End If;
End For;
(pModel As IMetabaseObject).Save;
End Sub UserProc;
After executing the example the Value-At-Risk model is created. Parameters of its calculation are set by the Monte-Carlo method. Model calculation results are displayed in the console window.
See also: