private BestSoFar <double, double> OneDOptimizationInternal(Converter <double, double> oneDRealFunction,
                                                                    double logOddsLow, double logOddsHigh, double logOddsPrecision)
        {
            int gridLineCount = 10;

            double logOddsRangeIncrement = (logOddsHigh - logOddsLow) / (double)gridLineCount;

            BestSoFar <double, double> bestParameterSoFar = BestSoFar <double, double> .GetInstance(SpecialFunctions.DoubleGreaterThan);

            Debug.WriteLine(SpecialFunctions.CreateTabString("parameter", "parameterLogOdds", "score"));
            for (int gridLine = 0; gridLine <= gridLineCount; ++gridLine)
            {
                double parameterLogOdds = logOddsLow + gridLine * logOddsRangeIncrement;
                double parameter        = SpecialFunctions.Probability(parameterLogOdds);
                double score            = oneDRealFunction(parameter);
                Debug.WriteLine(SpecialFunctions.CreateTabString(parameter, parameterLogOdds, score));
                bestParameterSoFar.Compare(score, parameterLogOdds);
            }
            Debug.WriteLine("");

            if (logOddsHigh - logOddsLow < logOddsPrecision)
            {
                return(bestParameterSoFar);
            }

            return(OneDOptimizationInternal(oneDRealFunction, bestParameterSoFar.Champ - logOddsRangeIncrement, bestParameterSoFar.Champ + logOddsRangeIncrement, logOddsPrecision));
        }
        //private BestSoFar<double, double> OneDOptimizationInternal(oneDRealFunctionDelegate oneDRealFunction,
        //        double start, double increment, int gridStart, int gridLines)
        //{

        //    BestSoFar<double, double> bestParameterSoFar = BestSoFar<double, double>.GetInstance(delegate(double champScore, double challengerScore) { return challengerScore > champScore; });
        //    for(int gridLine = gridStart; gridLine < gridLines; ++gridLine)
        //    {
        //        double parameter = start + gridLine * increment;
        //        double score = oneDRealFunction(parameter);
        //        bestParameterSoFar.Compare(score, parameter);
        //    }
        //    if (Math.Abs(bestParameterSoFar.Champ - increment) < increment * .1)
        //    {
        //        return OneDOptimizationInternal(oneDRealFunction, 0.0, increment * .1, 1, 9);
        //    }
        //    else if (Math.Abs((1.0 - bestParameterSoFar.Champ) - increment) < increment * .1)
        //    {
        //        return OneDOptimizationInternal(oneDRealFunction, bestParameterSoFar.Champ, increment * .1, 1, 9);
        //    }
        //    else
        //    {
        //        return bestParameterSoFar;
        //    }
        //}

        static public BestSoFar <double, double> OneDOptimization(Converter <double, double> oneDRealFunction,
                                                                  double low, double high, double precision, int gridLineCount)
        {
            double rangeIncrement = (high - low) / (double)gridLineCount;

            BestSoFar <double, double> bestParameterSoFar = BestSoFar <double, double> .GetInstance(SpecialFunctions.DoubleGreaterThan);

            //Debug.WriteLine(SpecialFunctions.CreateTabString("parameter", "score"));
            //Debug.WriteLine(SpecialFunctions.CreateTabString(low, high, rangeIncrement));
            for (int gridLine = 0; gridLine <= gridLineCount; ++gridLine)
            {
                double parameter = low + gridLine * rangeIncrement;
                //double parameter = SpecialFunctions.Probability(parameterLogOdds);
                double score = oneDRealFunction(parameter);
                //Debug.WriteLine(SpecialFunctions.CreateTabString(parameter, score));
                bestParameterSoFar.Compare(score, parameter);
            }
            //Debug.WriteLine("");

            if (high - low < precision)
            {
                return(bestParameterSoFar);
            }

            return(OneDOptimization(oneDRealFunction, bestParameterSoFar.Champ - rangeIncrement, bestParameterSoFar.Champ + rangeIncrement,
                                    precision, gridLineCount));
        }
        //public void FindBestHlaAssignment()
        //{
        //    while (true)
        //    {
        //        int previousChangeCount = BestHlaAssignmentSoFar.ChangeCount;
        //        foreach(HlaAssignmentsToConsiderDelegate hlaAssignmentsToConsider in HlaAssignmentsToConsiderCollection)
        //        {
        //            RepeatUntilToImprovement(hlaAssignmentsToConsider);
        //        }
        //        if (previousChangeCount == BestHlaAssignmentSoFar.ChangeCount)
        //        {
        //            break;
        //        }
        //    }

        //}

        //private void RepeatUntilToImprovement(HlaAssignmentsToConsiderDelegate hlaAssignmentsToConsider)
        //{
        //    while (true)
        //    {
        //        int previousChangeCount = BestHlaAssignmentSoFar.ChangeCount;
        //        ScoreAssignments(hlaAssignmentsToConsider);
        //        if (previousChangeCount == BestHlaAssignmentSoFar.ChangeCount)
        //        {
        //            break;
        //        }
        //    }
        //}

        private void ScoreAssignments(HlaAssignmentsToConsider hlaAssignmentsToConsider)
        {
            foreach (TrueCollection hlaAssignment in hlaAssignmentsToConsider.Collection())
            {
                double score = QmrrModelMissingAssignment.LogLikelihoodOfCompleteModelConditionedOnKnownHlas(hlaAssignment);
                BestHlaAssignmentSoFar.Compare(score, hlaAssignment);
            }
        }
        internal void CreateNoSwitchablesHlaAssignment()
        {
            BestHlaAssignmentSoFar = BestSoFar <double, TrueCollection> .GetInstance(SpecialFunctions.DoubleGreaterThan);

            TrueCollection trueCollection       = TrueCollection.GetInstance(QmrrModelMissingAssignment.KnownHlaSet);
            double         scoreOnNoSwitchables = QmrrModelMissingAssignment.LogLikelihoodOfCompleteModelConditionedOnKnownHlas(trueCollection);

            BestHlaAssignmentSoFar.Compare(scoreOnNoSwitchables, trueCollection);
        }
Beispiel #5
0
        internal void FindBestQmrrParams(QmrrPartialModelCollection qmrrPartialModelCollection)
        {
            SpecialFunctions.CheckCondition(false, "Regression test this to be sure that switch to new optimization method didn't change anything important - cmk 5/1/2006");

            QmrrlModelMissingParametersCollection aQmrrlModelMissingParametersCollection =
                QmrrlModelMissingParametersCollection.GetInstance(ModelLikelihoodFactories, qmrrPartialModelCollection, PeptideToBestHlaAssignmentSoFar);

            double score;
            OptimizationParameterList qmrrParamsEnd = aQmrrlModelMissingParametersCollection.FindBestParams(BestParamsSoFar.Champ, out score);

            BestParamsSoFar.Compare(score, qmrrParamsEnd);
        }
        internal override Dictionary <Hla, PValueDetails> CreateHlaToPValueDetails(int nullIndex, string peptide, Dictionary <string, Set <Hla> > pidToHlaSetAll, Set <Hla> setOfHlasToConsiderAdding, TextWriter writer)
        {
            Dictionary <Hla, PValueDetails> hlaToPValueDetails = new Dictionary <Hla, PValueDetails>();

            if (setOfHlasToConsiderAdding.Count == 0)
            {
                return(hlaToPValueDetails);
            }

            Dictionary <string, double> pidToReactValue = ReactTableUnfiltered[peptide];

            Set <Hla> knownHlaSet = KnownTable(peptide);

            SpecialFunctions.CheckCondition(setOfHlasToConsiderAdding.Intersection(knownHlaSet).Count == 0);

            Set <Hla> bestHlaSetSoFar = Set <Hla> .GetInstance();

            while (setOfHlasToConsiderAdding.Count > 0)
            {
                BestSoFar <PValueDetails, Hla> bestHlaToAddSoFar = BestSoFar <PValueDetails, Hla> .GetInstance(delegate(PValueDetails pValueDetails1, PValueDetails pValueDetails2) { return(pValueDetails1.Diff.CompareTo(pValueDetails2.Diff)); });

                foreach (Hla hla in setOfHlasToConsiderAdding) //!!!only look at hla's of patients with reactivity to this peptide (how effects nulls?)
                {
                    PValueDetails pValueDetails = CreateAPValueDetail(nullIndex, peptide, pidToHlaSetAll, knownHlaSet, bestHlaSetSoFar, hla);
                    bestHlaToAddSoFar.Compare(pValueDetails, hla);
                }
                //Debug.WriteLine("");
                PValueDetails bestPValueDetails = bestHlaToAddSoFar.ChampsScore; //!!!weird that PValue details is the score and the object

                if (bestPValueDetails.PValue() > PValueCutOff)
                {
                    break;
                }

                Hla hlaToAdd = bestHlaToAddSoFar.Champ;

                setOfHlasToConsiderAdding.Remove(hlaToAdd);
                bestHlaSetSoFar = bestHlaSetSoFar.Union(hlaToAdd);

                hlaToPValueDetails.Add(hlaToAdd, bestPValueDetails);
                writer.WriteLine(bestPValueDetails);
                //Debug.WriteLine(bestPValueDetails);
                writer.Flush();
            }
            return(hlaToPValueDetails);
        }
Beispiel #7
0
        protected OptimizationParameterList FindBestParams(string peptide,
                                                           Set <Hla> candidateHlaSet,
                                                           Set <Hla> hlaWithLinkZero,
                                                           Dictionary <string, Set <Hla> > patientList,
                                                           //Dictionary<string, Dictionary<string,double>> reactTable,
                                                           out double score)
        {
            //SpecialFunctions.CheckCondition(false, "Regression test this to be sure that switch to new optimization method didn't change anything important - cmk 5/1/2006");
            Set <Hla> knownHlaSet = KnownTable(peptide);

            TrueCollection trueCollection = TrueCollection.GetInstance(candidateHlaSet, knownHlaSet);

            OptimizationParameterList qmrrParamsThisPeptide = CreateQmrrParamsStartForTheseCandidateHlas(trueCollection.CreateHlaAssignmentAsSet(), hlaWithLinkZero);
            //OptimizationParameterList qmrrParamsThisPeptide = CreateQmrrParamsX(causePrior, leakProbability, trueCollection.CreateHlaAssignmentAsSet());


            ModelLikelihoodFactories   modelLikelihoodFactories            = ModelLikelihoodFactories.GetInstanceLinkPerHla(qmrrParamsThisPeptide, trueCollection.CreateHlaAssignmentAsSet());
            QmrrPartialModelCollection singletonQmrrPartialModelCollection =
                QmrrPartialModelCollection.GetInstance(
                    peptide,
                    modelLikelihoodFactories,
                    qmrrParamsThisPeptide,
                    patientList,
                    ReactTableUnfiltered,
                    _knownTable,
                    HlaFactory.Name         //!!!would it be better to pass the actual factory???
                    );
            BestSoFar <double, TrueCollection> bestSoFar = BestSoFar <double, TrueCollection> .GetInstance(SpecialFunctions.DoubleGreaterThan);

            bestSoFar.Compare(double.NegativeInfinity, trueCollection);
            Dictionary <string, BestSoFar <double, TrueCollection> > peptideToBestHlaAssignmentSoFar = new Dictionary <string, BestSoFar <double, TrueCollection> >();

            peptideToBestHlaAssignmentSoFar.Add(peptide, bestSoFar);

            QmrrlModelMissingParametersCollection aQmrrlModelMissingParametersCollection
                = QmrrlModelMissingParametersCollection.GetInstance(modelLikelihoodFactories, singletonQmrrPartialModelCollection, peptideToBestHlaAssignmentSoFar);


            OptimizationParameterList qmrrParamsEnd = aQmrrlModelMissingParametersCollection.FindBestParams(qmrrParamsThisPeptide, out score);

            return(qmrrParamsEnd);
        }
Beispiel #8
0
        override public double Run(Converter <OptimizationParameter, double> oneDRealFunction,
                                   OptimizationParameter param)
        {
            double low  = param.LowForSearch;
            double high = param.HighForSearch;
            Converter <double, double> oneDRealFunctionInDoubleSpace = delegate(double d)
            {
                param.ValueForSearch = d;
                return(oneDRealFunction(param));
            };

            double initParamValue = param.ValueForSearch;
            BestSoFar <double, double> bestFound = OneDOptimizationX(oneDRealFunctionInDoubleSpace, low, high, GridLineCount, Tolerance);

            bestFound.Compare(oneDRealFunctionInDoubleSpace(initParamValue), initParamValue); // make sure we didn't get any worse

            //bestInput = bestFound.Champ;
            param.ValueForSearch = bestFound.Champ;
            return(bestFound.ChampsScore);
        }
Beispiel #9
0
        private void CreateBestParamsSoFar()
        {
            BestParamsSoFar = BestSoFar <double, OptimizationParameterList> .GetInstance(SpecialFunctions.DoubleGreaterThan);

            BestParamsSoFar.Compare(double.NegativeInfinity, QmrrParamsStart);
        }