public override void GlobalContext()
        {
            base.GlobalContext();
            _optimizedParameterValue1 = new OptimizedParameterValue("P1", 10, 11, 5, 100, Scalings.Log);
            _optimizedParameterValue2 = new OptimizedParameterValue("P2", 20, 211, 5, 100, Scalings.Linear);

            _parameterIdentificationRunResult = new ParameterIdentificationRunResult();

            _residualResults = new ResidualsResult();
            _bestRunResult   = new OptimizationRunResult {
                ResidualsResult = _residualResults
            };
            _parameterIdentificationRunResult.BestResult = _bestRunResult;
            _parameterIdentificationRunResult.Duration   = new TimeSpan(1, 2, 3, 4);
            _observedData1 = DomainHelperForSpecs.ObservedData("OBS1");
            _observedData2 = DomainHelperForSpecs.ObservedData("OBS2");
            _residualResults.AddOutputResiduals("OutputPath1", _observedData1, new[] { new Residual(1f, 2f, 1), new Residual(2f, 3f, 2) });
            _residualResults.AddOutputResiduals("OutputPath2", _observedData2, new[] { new Residual(3f, 4f, 4) });
            _bestRunResult.AddValue(_optimizedParameterValue1);
            _bestRunResult.AddValue(_optimizedParameterValue2);

            _bestRunResult.AddResult(DomainHelperForSpecs.IndividualSimulationDataRepositoryFor("S1"));
            _bestRunResult.AddResult(DomainHelperForSpecs.IndividualSimulationDataRepositoryFor("S2"));

            var workspace = IoC.Resolve <ICoreWorkspace>();
            var project   = IoC.Resolve <PKSimProject>();

            workspace.Project = project;
            project.AddObservedData(_observedData1);
            project.AddObservedData(_observedData2);



            _deserializedParameterIdentificationRunResult = SerializeAndDeserialize(_parameterIdentificationRunResult);
        }
Пример #2
0
        public ResidualsResult Calculate(IReadOnlyList <SimulationRunResults> simulationsResults, IReadOnlyList <OutputMapping> allOutputMappings)
        {
            if (simulationsResults.Any(x => !x.Success))
            {
                return(residualResultWithErrorFrom(simulationsResults));
            }

            var simulationColumnsCache = new Cache <string, DataColumn>(x => x.PathAsString, x => null);

            simulationsResults.Select(x => x.Results).Each(repo => simulationColumnsCache.AddRange(repo.AllButBaseGrid()));

            var residualResult = new ResidualsResult();

            foreach (var outputMapping in allOutputMappings)
            {
                var simulationColumn = simulationColumnsCache[outputMapping.FullOutputPath];
                if (simulationColumn == null)
                {
                    residualResult.ExceptionOccured = true;
                    residualResult.ExceptionMessage = Error.CannotFindSimulationResultsForOutput(outputMapping.FullOutputPath);
                    return(residualResult);
                }

                var outputResiduals = calculateOutputResiduals(simulationColumn, outputMapping);
                residualResult.AddOutputResiduals(outputMapping.FullOutputPath, outputMapping.WeightedObservedData, outputResiduals);
            }

            return(residualResult);
        }
        protected override void Context()
        {
            base.Context();
            _outputResidual1 = new OutputResiduals("O1", _observedData, new[] { new Residual(1f, 5f, 1), new Residual(2f, 10f, 1) });
            _outputResidual2 = new OutputResiduals("O2", _observedData, new[] { new Residual(3f, 15f, 1), new Residual(4f, 19f, 1), new Residual(4f, 20f, 1) });

            _residualResults       = new ResidualsResult();
            _optimizationRunResult = new OptimizationRunResult {
                ResidualsResult = _residualResults
            };
            _residualResults.AddOutputResiduals(_outputResidual1);
            _residualResults.AddOutputResiduals(_outputResidual2);

            _binInterval1 = new BinInterval(0, 17);
            _binInterval2 = new BinInterval(18, 25);
            A.CallTo(_binIntervalCreator).WithReturnType <IReadOnlyList <BinInterval> >().Returns(new[] { _binInterval1, _binInterval2 });
        }
        protected override void Context()
        {
            sut = new ConfidenceIntervalCalculator(new MatrixCalculator());
            _residualsResult = new ResidualsResult();
            _jacobianMatrix  = new JacobianMatrix(new[] { "P1", "P2" });
            _jacobianMatrix.AddRow(new JacobianRow("O1", 1, new[] { 1d, 5d }));
            _jacobianMatrix.AddRow(new JacobianRow("O1", 2, new[] { 2d, 11d }));
            _jacobianMatrix.AddRow(new JacobianRow("O1", 3, new[] { 3d, 12d }));
            _jacobianMatrix.AddRow(new JacobianRow("O1", 4, new[] { 4d, 2d }));

            _residualsResult.AddOutputResiduals(
                "01",
                new DataRepository("OBS"),
                new[] { new Residual(1, 0.1000, 1), new Residual(2, -0.200, 1), new Residual(3, 1.0000, 1), new Residual(4, 0.5000, 1) }
                );
        }
Пример #5
0
        protected override void Context()
        {
            base.Context();
            _simulation = A.Fake <ISimulation>();
            _project    = A.Fake <IProject>();
            _parameterIdentification = new ParameterIdentification();

            A.CallTo(() => _executionContext.Project).Returns(_project);
            A.CallTo(() => _project.AllParameterIdentifications).Returns(new[] { _parameterIdentification });

            _parameterIdentification.AddSimulation(_simulation);

            _initialObjectPath = new ObjectPath("oldName", "path");
            var parameterIdentificationRunResult = new ParameterIdentificationRunResult {
                BestResult = new OptimizationRunResult()
            };

            _simulationDataRepository  = DomainHelperForSpecs.SimulationDataRepositoryFor("oldName");
            _observationDataRepository = DomainHelperForSpecs.ObservedData();

            _observationDataRepository.AllButBaseGrid().Each(x => x.QuantityInfo.Path = _initialObjectPath);
            _simulationDataRepository.AllButBaseGrid().Each(x => x.QuantityInfo.Path  = _initialObjectPath);

            parameterIdentificationRunResult.BestResult.AddResult(_simulationDataRepository);
            _residualsResult = new ResidualsResult();
            _residualsResult.AddOutputResiduals(_initialObjectPath.PathAsString, _observationDataRepository, new List <Residual> {
                new Residual(0, 1, 1)
            });

            parameterIdentificationRunResult.BestResult.ResidualsResult = _residualsResult;
            _parameterIdentification.AddResult(parameterIdentificationRunResult);

            var outputMapping = new OutputMapping
            {
                WeightedObservedData = new WeightedObservedData(_observationDataRepository),
                OutputSelection      = new ParameterSelection(_simulation, _initialObjectPath.PathAsString)
            };

            _parameterIdentification.AddOutputMapping(outputMapping);
        }
        /// <summary>
        ///    Optimize objective function (x[0]-3)^2+(x[1]-4)^2
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        protected virtual OptimizationRunResult ObjectiveFunction(IReadOnlyList <OptimizedParameterValue> values)
        {
            var residualResult  = new ResidualsResult();
            var outputResiduals = new OutputResiduals("A|B", new DataRepository(), new[]
            {
                new Residual(0, values[0].Value - 3, 1),
                new Residual(0, values[1].Value - 4, 1)
            });

            residualResult.AddOutputResiduals(outputResiduals);

            var optimizationRunResult = new OptimizationRunResult
            {
                ResidualsResult = residualResult,
                Values          = values,
            };

            if (optimizationRunResult.TotalError < _optimizationResult.TotalError)
            {
                _optimizationResult = optimizationRunResult;
            }

            return(optimizationRunResult);
        }
Пример #7
0
        protected override void Context()
        {
            _matrixCalculator      = A.Fake <IMatrixCalculator>();
            _dataRepositoryCreator = A.Fake <IConfidenceIntervalDataRepositoryCreator>();
            sut = new TimeProfileConfidenceIntervalCalculator(_matrixCalculator, _dataRepositoryCreator);

            _identificationParameter1 = new IdentificationParameter().WithName("IP1");
            _identificationParameter2 = new IdentificationParameter().WithName("IP2");

            var parameterNames = new[] { _identificationParameter1.Name, _identificationParameter2.Name };

            _parameterIdentification = A.Fake <ParameterIdentification>();
            _runResult = A.Fake <ParameterIdentificationRunResult>();
            _runResult.JacobianMatrix            = new JacobianMatrix(parameterNames);
            _allOutputMappings                   = new List <OutputMapping>();
            _allVariableIdentificationParameters = new List <IdentificationParameter>();
            _residualResults = new ResidualsResult();
            _runResult.BestResult.ResidualsResult = _residualResults;

            A.CallTo(() => _parameterIdentification.AllOutputMappings).Returns(_allOutputMappings);
            A.CallTo(() => _parameterIdentification.AllVariableIdentificationParameters).Returns(_allVariableIdentificationParameters);

            _simulation1 = A.Fake <ISimulation>().WithName("S1");
            _simulation2 = A.Fake <ISimulation>().WithName("S2");

            _output1 = createOutput(_simulation1, "C1", Scalings.Log);
            _allOutputMappings.Add(_output1);

            _output2 = createOutput(_simulation2, "C2", Scalings.Linear);
            _allOutputMappings.Add(_output2);

            _output3 = createOutput(_simulation1, "C3", Scalings.Log);
            _allOutputMappings.Add(_output3);

            _output4 = createOutput(_simulation2, "C4", Scalings.Linear);
            _allOutputMappings.Add(_output4);


            _ps1_1 = createParameterSelection(_simulation1, "P1_1");
            _identificationParameter1.AddLinkedParameter(_ps1_1);

            _ps2_1 = createParameterSelection(_simulation1, "P2_1");
            A.CallTo(() => _ps2_1.Dimension).Returns(_ps1_1.Dimension);
            _identificationParameter1.AddLinkedParameter(_ps2_1);

            _ps1_2 = createParameterSelection(_simulation1, "P1_2");
            _identificationParameter2.AddLinkedParameter(_ps1_2);

            _ps2_2 = createParameterSelection(_simulation2, "P2_2");
            A.CallTo(() => _ps2_2.Dimension).Returns(_ps1_2.Dimension);
            _identificationParameter2.AddLinkedParameter(_ps2_2);

            _allVariableIdentificationParameters.Add(_identificationParameter1);
            _allVariableIdentificationParameters.Add(_identificationParameter2);

            _observedData1 = A.Fake <DataRepository>();
            _observedData2 = A.Fake <DataRepository>();
            _observedData3 = A.Fake <DataRepository>();
            _observedData4 = A.Fake <DataRepository>();
            _observedData5 = A.Fake <DataRepository>();

            _residualResults.AddOutputResiduals(_output1.FullOutputPath, _observedData1, new[]
            {
                new Residual(2, 0.10, 1),
                new Residual(4, 0.40, 2),
                new Residual(6, 1.00, 1),
                new Residual(8, 0, 0),
            });

            _residualResults.AddOutputResiduals(_output1.FullOutputPath, _observedData2, new[]
            {
                new Residual(6, 0.20, 2),
                new Residual(8, 0.80, 4),
                new Residual(10, -0.20, 2),
                new Residual(12, 0, 0),
            });

            _residualResults.AddOutputResiduals(_output2.FullOutputPath, _observedData3, new[]
            {
                new Residual(2, 0, 0),
                new Residual(4, 0.20, 2),
                new Residual(6, -0.20, 2),
                new Residual(8, 0, 0),
            });

            _residualResults.AddOutputResiduals(_output2.FullOutputPath, _observedData4, new[]
            {
                new Residual(6, 0.20, 2),
                new Residual(8, 0.80, 4),
                new Residual(10, -0.20, 2),
                new Residual(12, 0, 0),
            });

            _residualResults.AddOutputResiduals(_output2.FullOutputPath, _observedData5, new[]
            {
                new Residual(6, 0, 0),
                new Residual(8, 0, 0),
                new Residual(10, 0, 0),
                new Residual(12, 0, 0),
            });


            var baseGrid = new BaseGrid("Time", DomainHelperForSpecs.TimeDimensionForSpecs())
            {
                Values = new[] { 0f, 2f, 4f, 6f, 8f, 10f, 12f, 14f, 16f }
            };
            var C1 = new DataColumn
            {
                BaseGrid = baseGrid,
                Values   = new[] { 0f, 0.71f, 2.10f, 3.14f, 3.76f, 4.04f, 4.12f, 4.09f, 4.02f }
            };

            A.CallTo(() => _runResult.BestResult.SimulationResultFor(_output1.FullOutputPath)).Returns(C1);

            var C2 = new DataColumn
            {
                BaseGrid = baseGrid,
                Values   = new[] { 0f, 0.37f, 0.38f, 0.31f, 0.23f, 0.17f, 0.14f, 0.12f, 0.11f }
            };

            A.CallTo(() => _runResult.BestResult.SimulationResultFor(_output2.FullOutputPath)).Returns(C2);

            var C3 = new DataColumn
            {
                BaseGrid = baseGrid,
                Values   = new[] { 0f, 0.53f, 0.54f, 0.43f, 0.32f, 0.25f, 0.20f, 0.17f, 0.16f }
            };

            A.CallTo(() => _runResult.BestResult.SimulationResultFor(_output3.FullOutputPath)).Returns(C3);

            var C4 = new DataColumn
            {
                BaseGrid = baseGrid,
                Values   = new[] { 0f, 0.16f, 0.16f, 0.13f, 0.10f, 0.07f, 0.06f, 0.05f, 0.05f }
            };

            A.CallTo(() => _runResult.BestResult.SimulationResultFor(_output4.FullOutputPath)).Returns(C4);


            var covarianceMatrix = new Matrix(parameterNames, parameterNames);

            A.CallTo(() => _matrixCalculator.CovarianceMatrixFrom(_runResult.JacobianMatrix, _residualResults)).Returns(covarianceMatrix);
            covarianceMatrix.SetRow(0, new[] { 5.05, -1.22 });
            covarianceMatrix.SetRow(1, new[] { -1.22, 0.51 });

            _runResult.JacobianMatrix.AddPartialDerivatives(createPartialDerivatives(_output1.FullOutputPath, parameterNames, new[]
            {
                new[] { 0d, 0d },
                new[] { 1d, 2d },
                new[] { 2d, 3d },
                new[] { 1d, 1d },
                new[] { 0.5d, 0.7d },
                new[] { 0.4d, 0.5d },
                new[] { 0.3d, 0.5d },
                new[] { 0.3d, 0.2d },
                new[] { 0.1d, 0.2d }
            }));

            _runResult.JacobianMatrix.AddPartialDerivatives(createPartialDerivatives(_output2.FullOutputPath, parameterNames, new[]
            {
                new[] { 0d, 0d },
                new[] { -1d, -2d },
                new[] { -1d, -2d },
                new[] { -0.5d, -1d },
                new[] { 0.1d, 0.1d },
                new[] { -0.1d, -0.1d },
                new[] { 0.2d, 0.2d },
                new[] { 0.3d, 0.3d },
                new[] { 0.2d, 0.2d }
            }));

            _runResult.JacobianMatrix.AddPartialDerivatives(createPartialDerivatives(_output3.FullOutputPath, parameterNames, new[]
            {
                new[] { 0d, 0d },
                new[] { 1d, 2d },
                new[] { 2d, 3d },
                new[] { 1d, 1d },
                new[] { 0.5d, 0.7d },
                new[] { 0.4d, 0.5d },
                new[] { 0.3d, 0.5d },
                new[] { 0.3d, 0.2d },
                new[] { 0.1d, 0.2d },
            }));

            _runResult.JacobianMatrix.AddPartialDerivatives(createPartialDerivatives(_output4.FullOutputPath, parameterNames, new[]
            {
                new[] { 0d, 0d },
                new[] { -4d, -4d },
                new[] { -3d, -3d },
                new[] { -2d, -2d },
                new[] { -1d, -1d },
                new[] { -0.5d, -0.5d },
                new[] { -0.4d, -0.4d },
                new[] { -0.3d, -0.3d },
                new[] { -0.2d, -0.2d },
            }));

            _confidenceIntervals = new Cache <string, double[]>();
            A.CallTo(() => _dataRepositoryCreator.CreateFor(A <string> ._, A <double[]> ._, A <OutputMapping> ._, A <OptimizationRunResult> ._))
            .Invokes(x => { _confidenceIntervals.Add(x.GetArgument <OutputMapping>(2).FullOutputPath, x.GetArgument <double[]>(1)); })
            .Returns(new DataRepository());
        }