OptimizationMethod: NLSOptimizationMethod;
OptimizationMethod: Prognoz.Platform.Interop.Stat.NLSOptimizationMethod;
The OptimizationMethod property determines the optimization method in use.
By default, the Levenberg-Marquardt method is used, that is, OptimizationMethod = NLSOptimizationMethod.LevenbergMarquardt.
Executing the example requires that the repository contains a modeling container with the MS identifier containing a model with the MODEL_NONLINEAR identifier calculated by the Non-Linear Regression (Non-Linear Least-Squares Method Estimation) method.
Add links to the Metabase, Ms, Stat system assemblies.
Sub UserProc;
Var
mb: IMetabase;
MsKey: Integer;
Model: IMsModel;
Transform: IMsFormulaTransform;
NonLinear: IMsNonLinearRegressionTransform;
InitAprox: Array Of Double;
i: Double;
Calc: IMsModelCalculation;
OutputVar: IMsFormulaTransformVariable;
Coord: IMsFormulaTransformCoord;
CoefData: Array Of Double;
Coef: Array Of String;
j: Integer;
ConstCoeff: ISlConstCoefficients;
MCalc: IMsMethodCalculation;
Result: IMsFormulaTerm;
ResultData: Array Of Double;
Begin
// Get current repository
mb := MetabaseClass.Active;
// Get modelling container key
MsKey := mb.GetObjectKeyById("MS");
// Get the model
Model := mb.ItemByIdNamespace("MODEL_NONLINEAR", MsKey).Edit As IMsModel;
// Get model parameters
Transform := Model.Transform;
// Get calculation method parameters
NonLinear := Transform.FormulaItem(0).Method As IMsNonLinearRegressionTransform;
// Set significance of confidence limits
NonLinear.ConfidenceLevel := 0.90;
// Add a new coefficient to model equation
NonLinear.Expression.AsString := "(" + NonLinear.Expression.AsString + ") * A1";
// Get array of initial approximations
InitAprox := NonLinear.InitApproximation;
// Set new values of initial approximations
For Each i In InitAprox Do
i := i + 0.01;
End For;
// Do not use initial values by default
NonLinear.UseDefaultInitValues := False;
// Set maximum number of iterations,
// for which optimal values of coefficients are calculated
NonLinear.MaxIteration := 600;
// Set missing data treatment method
NonLinear.MissingData.Method := MissingDataMethod.LinTrend;
// Set optimization method
NonLinear.OptimizationMethod := NLSOptimizationMethod.QuasiNewton;
// Set calculation accuracy
NonLinear.Tolerance := 0.0002;
// Use analytical derivatives
NonLinear.UseDerivatives := True;
// Create model calculation settings
Calc := Model.CreateCalculation;
// Set calculation periods
Calc.Period.IdentificationStartDate := DateTime.Parse("01.01.1990");
Calc.Period.IdentificationEndDate := DateTime.Parse("01.01.2010");
Calc.Period.ForecastStartDate := DateTime.Parse("01.01.2011");
Calc.Period.ForecastEndDate := DateTime.Parse("01.01.2017");
// Get output variable
OutputVar := Transform.Outputs.Item(0);
// Get output variable parameters required for model calculation
Coord := Transform.CreateCoord(OutputVar);
// Identify model coefficients
NonLinear.Identify(Calc As IMsMethodCalculation, Coord);
// Get identifier coefficients
CoefData := NonLinear.CoefficientsData(Coord);
// If coefficients are not saved, save them
If Not NonLinear.IsCoefficientsSaved(Coord) Then
NonLinear.CoefficientsData(Coord) := CoefData;
End If;
// Get coefficient names
Coef := NonLinear.Coefficients;
// Output names, values and summary statistics of coefficients in the console window
Debug.WriteLine("Values and standard coefficient error");
For j := 0 To CoefData.Length - 1 Do
// Output name, value of coefficient
Debug.Write(" " + Coef[j] + ": " + CoefData[j].ToString);
// Get summary statistics of coefficient
ConstCoeff := NonLinear.StatCoefficients(Coord, Coef[j]);
// Output standard coefficient error
Debug.WriteLine("; " + ConstCoeff.StandardError.ToString);
End For;
// Get output series
Result := NonLinear.Result;
// Create method calculation parameters
MCalc := Transform.CreateCalculation;
// Set calculation periods
MCalc.Period.IdentificationStartDate := DateTime.Parse("01.01.1990");
MCalc.Period.IdentificationEndDate := DateTime.Parse("01.01.2010");
MCalc.Period.ForecastStartDate := DateTime.Parse("01.01.2011");
MCalc.Period.ForecastEndDate := DateTime.Parse("01.01.2017");
// Get output series data
ResultData := Result.Serie(MCalc);
// Display output series values in the console window
Debug.WriteLine("Output series values");
For Each i In ResultData Do
Debug.WriteLine(" " + i.ToString);
End For;
// Save changes in the model
(Model As IMetabaseObject).Save;
End Sub UserProc;
After executing the example the model parameters are changed, the console window displays values and standard error of model coefficients and model series.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.ForeSystem;
Imports Prognoz.Platform.Interop.Ms;
Imports Prognoz.Platform.Interop.Stat;
…
Public Shared Sub Main(Params: StartParams);
Var
mb: IMetabase;
MsKey: uinteger;
Model: IMsModel;
Transform: IMsFormulaTransform;
NonLinear: IMsNonLinearRegressionTransform;
InitAprox: System.Array;
i: Double;
Calc: IMsModelCalculation;
OutputVar: IMsFormulaTransformVariable;
Coord: IMsFormulaTransformCoord;
CoefData: System.Array;
Coef: System.Array;
j: Integer;
ConstCoeff: ISlConstCoefficients;
MCalc: IMsMethodCalculation;
Result: IMsFormulaTerm;
ResultData: System.Array;
Begin
// Get current repository
mb := Params.Metabase;
// Get modelling container key
MsKey := mb.GetObjectKeyById("MS");
// Get the model
Model := mb.ItemByIdNamespace["MODEL_NONLINEAR", MsKey].Edit() As IMsModel;
// Get model parameters
Transform := Model.Transform;
// Get calculation method parameters
NonLinear := Transform.FormulaItem[0].Method As IMsNonLinearRegressionTransform;
// Set significance of confidence limits
NonLinear.ConfidenceLevel := 0.90;
// Add a new coefficient to model equation
NonLinear.Expression.AsString := "(" + NonLinear.Expression.AsString + ") * A1";
// Get array of initial approximations
InitAprox := NonLinear.InitApproximation;
// Set new values of initial approximations
For j := 0 To InitAprox.Length - 1 Do
InitAprox[j] := (InitAprox[j] As double) + 0.01;
End For;
// Do not use initial values by default
NonLinear.UseDefaultInitValues := False;
// Set maximum number of iterations,
// for which optimal values of coefficients are calculated
NonLinear.MaxIteration := 600;
// Set missing data treatment method
NonLinear.MissingData.Method := MissingDataMethod.mdmLinTrend;
// Set optimization method
NonLinear.OptimizationMethod := NLSOptimizationMethod.nlsomQuasiNewton;
// Set calculation accuracy
NonLinear.Tolerance := 0.0002;
// Use analytical derivatives
NonLinear.UseDerivatives := True;
// Create model calculation settings
Calc := Model.CreateCalculation();
// Set calculation periods
Calc.Period.IdentificationStartDate := DateTime.Parse("01.01.1990");
Calc.Period.IdentificationEndDate := DateTime.Parse("01.01.2010");
Calc.Period.ForecastStartDate := DateTime.Parse("01.01.2011");
Calc.Period.ForecastEndDate := DateTime.Parse("01.01.2017");
// Get output variable
OutputVar := Transform.Outputs.Item[0];
// Get output variable parameters required for model calculation
Coord := Transform.CreateCoord(OutputVar);
// Identify model coefficients
NonLinear.Identify(Calc As IMsMethodCalculation, Coord);
// Get identified coefficients
CoefData := NonLinear.CoefficientsData[Coord];
// If coefficients are not saved, save them
If Not NonLinear.IsCoefficientsSaved[Coord] Then
NonLinear.CoefficientsData[Coord] := CoefData;
End If;
// Get coefficient names
Coef := NonLinear.Coefficients;
// Output names, values and summary statistics of coefficients in the console window
System.Diagnostics.Debug.WriteLine("Values and standard coefficient error");
For j := 0 To CoefData.Length - 1 Do
// Output name, value of coefficient
System.Diagnostics.Debug.Write(" " + Coef[j] + ": " + CoefData[j].ToString());
// Get summary statistics of coefficient
ConstCoeff := NonLinear.StatCoefficients[Coord, Coef[j] As String];
// Output standard coefficient error
System.Diagnostics.Debug.WriteLine("; " + ConstCoeff.StandardError.ToString());
End For;
// Get output series
Result := NonLinear.Result;
// Create method calculation parameters
MCalc := Transform.CreateCalculation();
// Set calculation periods
MCalc.Period.IdentificationStartDate := DateTime.Parse("01.01.1990");
MCalc.Period.IdentificationEndDate := DateTime.Parse("01.01.2010");
MCalc.Period.ForecastStartDate := DateTime.Parse("01.01.2011");
MCalc.Period.ForecastEndDate := DateTime.Parse("01.01.2017");
// Get output series data
ResultData := Result.Serie[MCalc];
// Display output series values in the console window
System.Diagnostics.Debug.WriteLine("Output series values");
For Each i In ResultData Do
System.Diagnostics.Debug.WriteLine(" " + i.ToString());
End For;
// Save changes in the model
(Model As IMetabaseObject).Save();
End Sub;
See also: