Contents
Consider an example of creating a model of determinate equation with operands, for which lag is set.
Executing the example requires that the repository contains a modeling container with the MS identifier and a database with the TSDB identifier. This time series database must contain mandatory custom attributes of series being references to the dictionary.
Add links to the Cubes, Dimensions, Metabase, Ms system assemblies.
Sub UserProc;
Var
Mb: IMetabase;
CrInf: IMetabaseObjectCreateInfo;
Model: IMsModel;
Transform: IMsFormulaTransform;
TransformVarables: IMsFormulaTransformVariables;
RubrDescr: IMetabaseObjectDescriptor;
Stub: IVariableStub;
TransVar: IMsFormulaTransformVariable;
Tree: IMsFormulaTransformSlicesTree;
Slice: IMsFormulaTransformSlice;
Selector: IMsFormulaTransformSelector;
Formula: IMsFormula;
Determ: IMsDeterministicTransform;
TermX1, TermX2: IMsFormulaTerm;
TermInfo: IMsFormulaTermInfo;
Period: IMsModelPeriod;
Begin
Mb := MetabaseClass.Active;
// Specify basic parameters of the model as a repository object
CrInf := Mb.CreateCreateInfo;
CrInf.ClassId := MetabaseObjectClass.KE_CLASS_MSMODEL;
// Specify model identifier
CrInf.Id := Mb.GenerateId("MODEL_DETERMINISTIC_LAG", Mb.ItemById("MS").Key);
// Specify model name
CrInf.Name := "Determinate equation with lagged operands";
// Specify modeling container which will contain the model
CrInf.Parent := Mb.ItemById("MS");
// Create a model
Model := Mb.CreateObject(CrInf).Edit As IMsModel;
// Get object to set up model parameters
Transform := Model.Transform;
// Get object to work with output variable
TransformVarables := Transform.Outputs;
// Get time series database (TSDB)
RubrDescr := Mb.ItemById("TSDB");
// Cast the obtained TSDB to the abstract data source
Stub := RubrDescr.Bind As IVariableStub;
// Use TSDB as a data source of output variable
TransVar := TransformVarables.Add(Stub);
// Specify output variable slice
Tree := TransVar.SlicesTree(TransVar);
Slice := Tree.CreateSlice(1);
// Get model settings for output variable slice
Selector := Transform.CreateSelector;
Selector.Slice := Slice;
Formula := Transform.Transform(Selector);
// Specify calculation method - determinate equation
Formula.Kind := MsFormulaKind.Deterministic;
// Set the calendar frequency of calculation
Formula.Level := DimCalendarLevel.Year;
// Get object for setting up determinate equation
Determ := Formula.Method As IMsDeterministicTransform;
// Add the factor with the TSDB data source in the model
TransVar := Transform.Inputs.Add(Stub);
Tree := TransVar.SlicesTree(TransVar);
Slice := Tree.CreateSlice(2);
// Add the first factor to the collection of elements of expressions used for equation creation
TermX1 := Determ.Operands.Add(Slice);
// Get expression element parameters
TermInfo := TermX1.TermInfo;
// Set type of expression element: date-specific value
TermInfo.Type := MsFormulaTermType.Date;
// Set date for expression element
TermInfo.SetDate(DateTime.ComposeDay(2014, 1, 1), "YYYY");
// Set changed parameters for expression element
TermX1.TermInfo := TermInfo;
// Add the factor with the TSDB data source in the model
TransVar := Transform.Inputs.Add(Stub);
Tree := TransVar.SlicesTree(TransVar);
Slice := Tree.CreateSlice(3);
// Add the second factor to the collection of elements of expressions used for equation creation
TermX2 := Determ.Operands.Add(Slice);
// Get expression element parameters
TermInfo := TermX2.TermInfo;
// Set lag for expression element
TermInfo.Lag := "-2";
// Set changed parameters for expression element
TermX2.TermInfo := TermInfo;
// Set expression for determinate equation calculation
Determ.Expression.AsString := "Iif(" + TermX1.TermToInnerText + ">-0.5, " +
TermX2.TermToInnerText + "+1, " + TermX2.TermToInnerText + "-1)";
// Specify model calculation periods
Period := Transform.Period;
Period.IdentificationStartDate := DateTime.ComposeDay(2000, 1, 1);
Period.IdentificationEndDate := DateTime.ComposeDay(2014, 12, 31);
Period.ForecastStartDate := DateTime.ComposeDay(2015, 1, 1);
Period.ForecastEndDate := DateTime.ComposeDay(2020, 12, 31);
// Save the model to the repository
(Model As IMetabaseObject).Save;
End Sub UserProc;
Imports Prognoz.Platform.Interop.Cubes;
Imports Prognoz.Platform.Interop.Dimensions;
Imports Prognoz.Platform.Interop.Ms;
…
Public Shared Sub Main(Params: StartParams);
Var
Mb: IMetabase;
CrInf: IMetabaseObjectCreateInfo;
Model: IMsModel;
Transform: IMsFormulaTransform;
TransformVarables: IMsFormulaTransformVariables;
RubrDescr: IMetabaseObjectDescriptor;
Stub: IVariableStub;
TransVar: IMsFormulaTransformVariable;
Tree: IMsFormulaTransformSlicesTree;
Slice: IMsFormulaTransformSlice;
Selector: IMsFormulaTransformSelector;
Formula: IMsFormula;
Determ: IMsDeterministicTransform;
TermX1, TermX2: IMsFormulaTerm;
TermInfo: IMsFormulaTermInfo;
Period: IMsModelPeriod;
Begin
Mb := Params.Metabase;
// Specify basic parameters of the model as a repository object
CrInf := Mb.CreateCreateInfo();
CrInf.ClassId := MetabaseObjectClass.KE_CLASS_MSMODEL As integer;
// Specify model identifier
CrInf.Id := Mb.GenerateId("MODEL_DETERMINISTIC_LAG", Mb.ItemById["MS"].Key);
// Specify model name
CrInf.Name := "Determinate equation with lagged operands";
// Specify modeling container which will contain the model
CrInf.Parent := Mb.ItemById["MS"];
// Create a model
Model := Mb.CreateObject(CrInf).Edit() As IMsModel;
// Get object to set up model parameters
Transform := Model.Transform;
// Get object to work with output variable
TransformVarables := Transform.Outputs;
// Get time series database (TSDB)
RubrDescr := Mb.ItemById["TSDB"];
// Cast the obtained TSDB to the abstract data source
Stub := RubrDescr.Bind() As IVariableStub;
// Use TSDB as a data source of output variable
TransVar := TransformVarables.Add(Stub);
// Specify output variable slice
Tree := TransVar.SlicesTree[TransVar];
Slice := Tree.CreateSlice(1);
// Get model settings for output variable slice
Selector := Transform.CreateSelector();
Selector.Slice := Slice;
Formula := Transform.Transform[Selector];
// Specify calculation method - determinate equation
Formula.Kind := MsFormulaKind.mfkDeterministic;
// Set the calendar frequency of calculation
Formula.Level := DimCalendarLevel.dclYear;
// Get object for setting up determinate equation
Determ := Formula.Method As IMsDeterministicTransform;
// Add the factor with the TSDB data source in the model
TransVar := Transform.Inputs.Add(Stub);
Tree := TransVar.SlicesTree[TransVar];
Slice := Tree.CreateSlice(2);
// Add the first factor to the collection of elements of expressions used for equation creation
TermX1 := Determ.Operands.Add(Slice);
// Get expression element parameters
TermInfo := TermX1.TermInfo;
// Set type of expression element: date-specific value
TermInfo.Type := MsFormulaTermType.mfttDate;
// Set date for expression element
TermInfo.SetDate(DateTime.Parse("2014.1.1"), "YYYY");
// Set changed parameters for expression element
TermX1.TermInfo := TermInfo;
// Add the factor with the TSDB data source in the model
TransVar := Transform.Inputs.Add(Stub);
Tree := TransVar.SlicesTree[TransVar];
Slice := Tree.CreateSlice(3);
// Add the second factor to the collection of elements of expressions used for equation creation
TermX2 := Determ.Operands.Add(Slice);
// Get expression element parameters
TermInfo := TermX2.TermInfo;
// Set lag for expression element
TermInfo.Lag := "-2";
// Set changed parameters for expression element
TermX2.TermInfo := TermInfo;
// Set expression for determinate equation calculation
Determ.Expression.AsString := "Iif(" + TermX1.TermToInnerText() + ">-0.5, " +
TermX2.TermToInnerText() + "+1, " + TermX2.TermToInnerText() + "-1)";
// Specify model calculation periods
Period := Transform.Period;
Period.IdentificationStartDate := DateTime.Parse("2000.1.1");
Period.IdentificationEndDate := DateTime.Parse("2014.12.31");
Period.ForecastStartDate := DateTime.Parse("2015.1.1");
Period.ForecastEndDate := DateTime.Parse("2020.12.31");
// Save the model to the repository
(Model As IMetabaseObject).Save();
End Sub;
After executing the example the MS modeling container has a model of determinate equation with the following parameters:
Two factors are in the model: X1 and X2.
The first factor is passed to equation as value for 2014.
The second factor is passed to equation pointwise with two points forward lag at the time period.
The following expression is used for model calculation: iif(X1 > -0.5, X2 + 1, X2 - 1). If value of the first factor for 2014 is greater than -0.5, value of the second factor is increased by 1, otherwise, it is decreased by 1.
Annual calculation frequency is used.
Sample period: start - "2000", end - "2014".
Forecasting period: start - "2015", end - "2020'.
See also: