From 5e3a6832992142065e7c9b2c468d58d9fe3ee87a Mon Sep 17 00:00:00 2001 From: wp Date: Mon, 20 Aug 2018 23:01:37 +0000 Subject: [PATCH] TAChart: Add fit parameter confidence limits to FitSeries. git-svn-id: trunk@58747 - --- components/tachart/demo/fit/Main.lfm | 4 ++-- components/tachart/demo/fit/Main.pas | 20 ++++++++++++++++++++ components/tachart/tafuncseries.pas | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/components/tachart/demo/fit/Main.lfm b/components/tachart/demo/fit/Main.lfm index 557ec9f86d..ac48a66071 100644 --- a/components/tachart/demo/fit/Main.lfm +++ b/components/tachart/demo/fit/Main.lfm @@ -25,9 +25,9 @@ object frmMain: TfrmMain Height = 487 Top = 0 Width = 400 - ActivePage = TabSheet2 + ActivePage = TabSheet1 Align = alClient - TabIndex = 1 + TabIndex = 0 TabOrder = 0 object TabSheet1: TTabSheet Caption = 'Preparation' diff --git a/components/tachart/demo/fit/Main.pas b/components/tachart/demo/fit/Main.pas index 66fe292398..5c52f2380c 100644 --- a/components/tachart/demo/fit/Main.pas +++ b/components/tachart/demo/fit/Main.pas @@ -378,6 +378,7 @@ procedure TfrmMain.FitCompleteHandler(Sender:TObject); const {$IF FPC_FullVersion >= 30004} MASK = '%-4s %10s %10s %10s %10s'; + CONF_MASK = '%-4s %10s %10s %10s'; {$ELSE} MASK = '%-4s %10s %10s %10s'; {$IFEND} @@ -389,6 +390,7 @@ var L: Integer; decsep: Char; paramName: String; + confL, confH: Double; begin decsep := DefaultFormatSettings.DecimalSeparator; with lbResults.Items do begin @@ -421,6 +423,24 @@ begin ])); end; Add(''); + {$IF FPC_FullVersion >= 30004} + Add('CONFIDENCE LIMITS'); + Add(Format(CONF_MASK, ['Name', 'Value', 'Lower', 'Upper'])); + for i := 0 to FitSeries.ParamCount - 1 do begin + case FitSeries.FitEquation of + fePolynomial: paramname := Format('b[%d]', [i]); + else paramname := PARAM_NAME[i]; + end; + FitSeries.GetConfidenceLimits(i, confL, confH); + Add(Format(CONF_MASK, [ + paramName, + MyFormatFloat(FitSeries.Param[i], STD_FMT, EXP_FMT), + MyFormatFloat(confL, STD_FMT, EXP_FMT), + MyFormatFloat(confH, STD_FMT, EXP_FMT) + ])); + end; + Add(''); + {$IFEND} Add('ANALYSIS OF VARIANCE'); lbResults.Canvas.Font.Assign(lbResults.Font); FReportDecimals := 5; diff --git a/components/tachart/tafuncseries.pas b/components/tachart/tafuncseries.pas index 92f00e0ef0..db701243e8 100644 --- a/components/tachart/tafuncseries.pas +++ b/components/tachart/tafuncseries.pas @@ -335,6 +335,9 @@ type procedure ExecFit; virtual; function EquationText: IFitEquationText; function FitParams: TDoubleDynArray; + {$IF FPC_FullVersion >= 30004} + procedure GetConfidenceLimits(AIndex: Integer; out ALower, AUpper: Double); + {$IFEND} function GetFitEquationString( ANumFormat: String; AXText: String = 'x'; AYText: String = 'y'): String; deprecated 'Use EquationText'; @@ -1721,6 +1724,25 @@ begin Result[i] := Param[i]; end; +{$IF FPC_FullVersion >= 30004} +procedure TFitSeries.GetConfidenceLimits(AIndex: Integer; out ALower, AUpper: Double); +var + val, sig, t: Double; + alpha: Double; +begin + val := GetParam_RawValue(AIndex); + sig := GetParam_RawError(AIndex); + alpha := 1.0 - FConfidenceLevel; + t := invtdist(alpha, Statistics.DOF, 2); + ALower := val - sig*t; + AUpper := val + sig*t; + if (FFitEquation in [feExp, fePower]) and (AIndex = 0) then begin + ALower := exp(ALower); + AUpper := exp(AUpper); + end; +end; +{$IFEND} + function TFitSeries.GetFitEquationString(ANumFormat: String; AXText: String; AYText: String): String; begin