private (List <string> allVaryingParameterNames, int[] indicesOfThisFitElementsVaryingParametersInAllVaryingParameters) CreateCachedMembers()
        {
            var fitElement = _fitDocument.FitEnsemble[_fitElementIndex];

            _cachedFitFunction = fitElement.FitFunction;
            _cachedParameters  = _fitDocument.GetParametersForFitElement(_fitElementIndex);
            _cachedParametersForJacobianEvaluation = _fitDocument.GetParametersForFitElement(_fitElementIndex);
            _cachedJacobian = new double[_cachedParameters.Length];
            _functionValues = new double[_cachedFitFunction.NumberOfDependentVariables];

            // CovarianceMatrix: we have to pick exactly the varying parameters of this fitelement!

            // next line retrieves all varying parameters of all fitelements, this corresponds to the rows of the provided covariance matrix
            var allVaryingParameterNames = new List <string>(_fitDocument.CurrentParameters.Where(x => x.Vary).Select(x => x.Name));

            // Indices of the varying parameters of this fit element (with respect to the parameter set of this fitelement)
            _cachedIndicesOfVaryingParametersOfThisFitElement = Enumerable.Range(0, fitElement.NumberOfParameters).Where(i => allVaryingParameterNames.IndexOf(fitElement.ParameterName(i)) >= 0).ToArray();

            // Indices in allVaryingParameters of the parameters in this fitlement, which are varying
            var indicesOfThisFitElementsVaryingParametersInAllVaryingParameters = Enumerable.Range(0, _cachedIndicesOfVaryingParametersOfThisFitElement.Length).Select(i => allVaryingParameterNames.IndexOf(fitElement.ParameterName(_cachedIndicesOfVaryingParametersOfThisFitElement[i]))).ToArray();

            // now we are able to pick the values out of the covariance matrix

            if (!(_numberOfFitPoints > _cachedIndicesOfVaryingParametersOfThisFitElement.Length))
            {
                _cachedQuantileOfStudentsDistribution = double.NaN; // 0 degrees of freedom
            }
            else
            {
                // for a given confidence interval, e.g. 0.95, we need to make 0.975 out of it
                _cachedQuantileOfStudentsDistribution = Altaxo.Calc.Probability.StudentsTDistribution.Quantile(1 - (0.5 * (1 - _confidenceLevel)), _numberOfFitPoints - _cachedIndicesOfVaryingParametersOfThisFitElement.Length);
            }

            // for convenience, we let this covariance matrix to be of dimension NumberOfParameters x NumberOfParameters, but we
            // don't set the elements corresponding to the non-varying parameters, thus they remain zero
            _covarianceMatrix = new double[fitElement.NumberOfParameters, fitElement.NumberOfParameters];

            return(allVaryingParameterNames, indicesOfThisFitElementsVaryingParametersInAllVaryingParameters);
        }
Пример #2
0
        public void OnAfterFittingStep()
        {
            if (_view != null)
            {
                _view.SetChiSquare(this._chiSquare);
            }



            if (_doc.FitContext is Altaxo.Graph.GUI.GraphController)
            {
                // for every dependent variable in the FitEnsemble, create a function graph
                Altaxo.Graph.GUI.GraphController graph = _doc.FitContext as Altaxo.Graph.GUI.GraphController;

                int funcNumber = 0;
                for (int i = 0; i < _doc.FitEnsemble.Count; i++)
                {
                    FitElement fitEle = _doc.FitEnsemble[i];

                    for (int k = 0; k < fitEle.NumberOfDependentVariables; k++, funcNumber++)
                    {
                        if (funcNumber < _functionPlotItems.Count && _functionPlotItems[funcNumber] != null)
                        {
                            XYFunctionPlotItem plotItem = (XYFunctionPlotItem)_functionPlotItems[funcNumber];
                            FitFunctionToScalarFunctionDDWrapper wrapper = (FitFunctionToScalarFunctionDDWrapper)plotItem.Data.Function;
                            wrapper.Initialize(fitEle.FitFunction, k, 0, _doc.GetParametersForFitElement(i));
                        }
                        else
                        {
                            FitFunctionToScalarFunctionDDWrapper wrapper = new FitFunctionToScalarFunctionDDWrapper(fitEle.FitFunction, k, _doc.GetParametersForFitElement(i));
                            XYFunctionPlotData plotdata = new XYFunctionPlotData(wrapper);
                            XYFunctionPlotItem plotItem = new XYFunctionPlotItem(plotdata, new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line));
                            graph.ActiveLayer.PlotItems.Add(plotItem);
                            _functionPlotItems.Add(plotItem);
                        }
                    }
                }

                // if there are more elements in _functionPlotItems, remove them from the graph
                for (int i = _functionPlotItems.Count - 1; i >= funcNumber; --i)
                {
                    if (_functionPlotItems[i] != null)
                    {
                        graph.ActiveLayer.PlotItems.Remove((IGPlotItem)_functionPlotItems[i]);
                        _functionPlotItems.RemoveAt(i);
                    }
                }
                graph.RefreshGraph();
            }
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XYNonlinearFitFunctionPlotData"/> class.
        /// </summary>
        /// <param name="fitDocumentIdentifier">The fit document identifier.</param>
        /// <param name="fitDocument">The fit document. The document will be cloned before stored in this instance.</param>
        /// <param name="fitElementIndex">Index of the fit element.</param>
        /// <param name="dependentVariableIndex">Index of the dependent variable of the fit element.</param>
        /// <param name="dependentVariableTransformation">Transformation, which is applied to the result of the fit function to be then shown in the plot. Can be null.</param>
        /// <param name="independentVariableIndex">Index of the independent variable of the fit element.</param>
        /// <param name="independentVariableTransformation">Transformation, which is applied to the x value before it is applied to the fit function. Can be null.</param>
        public XYNonlinearFitFunctionPlotData(string fitDocumentIdentifier, NonlinearFitDocument fitDocument, int fitElementIndex, int dependentVariableIndex, IVariantToVariantTransformation dependentVariableTransformation, int independentVariableIndex, IVariantToVariantTransformation independentVariableTransformation)
        {
            if (null == fitDocumentIdentifier)
            {
                throw new ArgumentNullException(nameof(fitDocumentIdentifier));
            }
            if (null == fitDocument)
            {
                throw new ArgumentNullException(nameof(fitDocument));
            }

            ChildCloneToMember(ref _fitDocument, fitDocument); // clone here, because we want to have a local copy which can not change.
            _fitDocumentIdentifier  = fitDocumentIdentifier;
            _fitElementIndex        = fitElementIndex;
            _dependentVariableIndex = dependentVariableIndex;
            Function = new FitFunctionToScalarFunctionDDWrapper(_fitDocument.FitEnsemble[fitElementIndex].FitFunction, dependentVariableIndex, dependentVariableTransformation, independentVariableIndex, independentVariableTransformation, _fitDocument.GetParametersForFitElement(fitElementIndex));
        }