public static string GetHeaderString(ModelEvaluator eval)
        {
            string result = SpecialFunctions.CreateTabString(
                "LeafDistribution",
                "rowIndex",
                "rowCount",
                "pieceIndex",
                Tabulate.NullIndexColumnName,
                Tabulate.PredictorVariableColumnName,
                Tabulate.TargetVariableColumnName,
                //Tabulate.GlobalNonMissingCountColumnName, //TODO: probably want this in somewhere
                eval.ToHeaderString());

            return(result);
        }
        public void CreateMerSparseStream(TextWriter textWriter, int merLength, bool keepOneValueVariables)
        {
            Dictionary <string, AASeq>  caseToCompressedAASeq = RemoveDeletesAndStopsFromData(textWriter);
            Dictionary <string, string> merStringToBestOriginalAA0Position = FindMerStringToBestOriginalAA0Position(merLength, textWriter, caseToCompressedAASeq);

            Debug.Assert(Set <string> .GetInstance(merStringToBestOriginalAA0Position.Keys).Equals(
                             Set <string> .GetInstance(EveryUnambiguousMer(merLength, caseToCompressedAASeq))));


            textWriter.WriteLine("var\tcid\tval");

            foreach (string mer in merStringToBestOriginalAA0Position.Keys)
            {
                Dictionary <bool, int>    valueToNonZeroCount;
                Dictionary <string, bool> caseIdToValue = FindMerValues(mer, caseToCompressedAASeq, out valueToNonZeroCount);
                Debug.Assert(valueToNonZeroCount.Count != 0);

                if (valueToNonZeroCount.Count == 1 && !keepOneValueVariables)
                {
                    continue;
                }

                foreach (string caseId in caseIdToValue.Keys)
                {
                    int    val          = (caseIdToValue[caseId]) ? 1 : 0;
                    string variableName = string.Format("{0}@{1}", mer, merStringToBestOriginalAA0Position[mer]);
                    textWriter.WriteLine(SpecialFunctions.CreateTabString(variableName, caseId, val));
                }
            }
        }
Пример #3
0
        public string ToString(bool includeInputPeptide, bool includeHlaInOutput)
        {
            StringBuilder sb      = new StringBuilder();
            bool          needTab = false;

            if (includeInputPeptide)
            {
                sb.Append(InputPeptide);
                needTab = true;
            }

            if (includeHlaInOutput)
            {
                if (needTab)
                {
                    sb.Append('\t');
                }
                sb.Append(Hla);
                needTab = true;
            }

            if (needTab)
            {
                sb.Append('\t');
            }

            Debug.Assert(NEC.N.Length == NEC.C.Length); // real assert
            sb.Append(SpecialFunctions.CreateTabString(PosteriorProbability, WeightOfEvidence, NEC.N, NEC.E, NEC.C, NEC.E.Length, NEC.N.Length, EStartPosition, ELastPosition, Source));
            return(sb.ToString());
        }
Пример #4
0
        private static List <string> ProcessLine(ShowBy showBy, PredictorCollection predictorCollection, string line,
                                                 MerLength merLength, int?dOfCenter, HlaSetSpecification hlaSetSpecification, bool modelOnly)
        {
            try
            {
                string hlaOrSupertypeOrNull;
                string inputPeptide = ExtractInputs(hlaSetSpecification, line, predictorCollection, out hlaOrSupertypeOrNull);

                List <string> output = new List <string>();
                foreach (List <Prediction> predictionList in predictorCollection.MaxProbabilityPredictions(showBy, inputPeptide, merLength, dOfCenter, hlaSetSpecification, hlaOrSupertypeOrNull, modelOnly).Values)
                {
                    string outputLine = InsertMaterial(line, hlaSetSpecification.InputHeaderCollection().Length, Prediction.CollectionToString(predictionList, false, hlaSetSpecification.IncludeHlaInOutput()));
                    output.Add(outputLine);
                }
                return(output);
            }
            catch (Exception exception)
            {
                string errorString = SpecialFunctions.CreateTabString(
                    line,
                    string.Format("Error: {0}{1}", exception.Message, exception.InnerException == null ? "" : string.Format(" ({0})", exception.InnerException)));
                List <string> output = new List <string>();
                output.Add(errorString);
                return(output);
            }
        }
        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 static void SubmitQueue(int taskPerJobCount, string caseName, RangeCollection nullIndexRangeCollection, ICluster cluster, string id, string password, Queue <ITask> taskQueue)
        {
            while (taskQueue.Count != 0)
            {
                IJob   job = cluster.CreateJob();
                string firstPieceIndex = null, lastPieceIndex = null;
                for (int iTaskPerJob = 0; iTaskPerJob < taskPerJobCount; ++iTaskPerJob)
                {
                    ITask  task           = taskQueue.Dequeue();
                    string taskPieceIndex = task.Name.Split(' ')[1];
                    if (iTaskPerJob == 0)
                    {
                        firstPieceIndex = taskPieceIndex.ToString();
                    }
                    lastPieceIndex = taskPieceIndex;
                    job.AddTask(task);
                    job.Runtime = task.Runtime;

                    if (taskQueue.Count == 0)
                    {
                        break;
                    }
                }
                string jobName = SpecialFunctions.CreateTabString(caseName, firstPieceIndex + "-" + lastPieceIndex,
                                                                  nullIndexRangeCollection).Replace("\t", " "); //!!!create a nicer way to make a space delimited list
                job.Name = jobName.Substring(0, Math.Min(jobName.Length, 70));
                Console.WriteLine(job.Name);
                job.IsExclusive = false;
                cluster.AddJob(job);
                cluster.SubmitJob(job.Id, id, password, true, 0);
            }
        }
Пример #7
0
        protected override string CreateReportLine(
            ModelScorer modelScorer,
            PhyloTree phyloTree,
            RowData rowAndTargetData,
            UniversalWorkList workList,
            int rowIndex, int workListCount, int workIndex)
        {
            Dictionary <string, string> row = rowAndTargetData.Row;
            string predictorVariable        = row[PhyloTree.PredictorVariableColumnName]; // e.g. hla
            string targetVariable           = row[PhyloTree.TargetVariableColumnName];    // e.g. A@182 (amino acid "A" at position 182)
            int    nullIndex = int.Parse(row[PhyloTree.NullIndexColumnName]);

            //Dictionary<string, bool> caseIdToNonMissingPredictorValue = workList.NullIndexToPredictorToCaseIdToNonMissingValue[nullIndex][predictorVariable];
            Dictionary <string, SufficientStatistics> caseIdToNonMissingPredictorValue = rowAndTargetData.PredictorData; //workList.GetCaseIdToNonMissingValueForNullIndexAndPredictorVariable(nullIndex, predictorVariable);
            Dictionary <string, SufficientStatistics> caseIdToNonMissingTargetValue    = rowAndTargetData.TargetData;

            TwoByTwo fishers2by2 = TwoByTwo.GetInstance(
                SufficientStatisticsMapToIntMap(caseIdToNonMissingPredictorValue),
                SufficientStatisticsMapToIntMap(caseIdToNonMissingTargetValue));

            double pValue = fishers2by2.FisherExactTest;

            string reportLine = SpecialFunctions.CreateTabString(this, rowIndex, workListCount, workIndex, nullIndex,
                                                                 predictorVariable,
                                                                 targetVariable,
                                                                 fishers2by2.CountsString(),
                                                                 fishers2by2.FisherExactTest);

            return(reportLine);
        }
        public static Leaf GetLeafInstance(StreamReader streamreader)
        {
            StringBuilder sb = new StringBuilder();

            while (true)
            {
                char peek = Peek(streamreader);
                if (peek == ':')
                {
                    break;
                }
                SpecialFunctions.CheckCondition(!("()".Contains(peek.ToString())));
                sb.Append(Read(streamreader));
            }

            Leaf leaf = new Leaf();

            leaf.CaseName = sb.ToString();
            if (leaf.CaseName.Contains("->")) //!!!const
            {
                int pos = leaf.CaseName.IndexOf("->");
                Debug.Assert(pos >= 0); // real assert
                int nodeId = int.Parse(leaf.CaseName.Substring(0, pos));
                leaf.CaseName = leaf.CaseName.Substring(pos + 2);
                Debug.WriteLine(SpecialFunctions.CreateTabString("NodeId", nodeId));
            }
            return(leaf);
        }
 public static void Plot(Converter <double, double> oneDRealFunctionInDoubleSpace, double start, double last, double inc)
 {
     for (double r1 = start; r1 < last; r1 += inc)
     {
         Debug.WriteLine(SpecialFunctions.CreateTabString(r1, oneDRealFunctionInDoubleSpace(r1)));
     }
 }
        private void ReportPerHla(OptimizationParameterList qmrrParams, Dictionary <string, BestSoFar <double, TrueCollection> > peptideToBestHlaAssignmentSoFar,
                                  string directory, string name)
        {
            string fileName = string.Format(@"{0}\NoisyOr.PeptideHlaProbability.{1}.new.txt", directory, name);

            using (StreamWriter output = File.CreateText(fileName))
            {
                output.WriteLine(SpecialFunctions.CreateTabString("Peptide", "HLA", "LogOdds", "Probability"));
                foreach (QmrrPartialModel qmrrPartialModel in QmrrPartialModelCollection)
                {
                    QmrrModelMissingAssignment         aQmrrModelMissingAssignment = QmrrModelMissingAssignment.GetInstance(ModelLikelihoodFactories, qmrrPartialModel, qmrrParams);
                    BestSoFar <double, TrueCollection> bestHlaAssignment           = peptideToBestHlaAssignmentSoFar[qmrrPartialModel.Peptide];
                    Set <Hla> trueCollectionFullAsSet = new Set <Hla>(bestHlaAssignment.Champ);

                    double loglikelihoodFull = bestHlaAssignment.ChampsScore;
                    foreach (Hla hla in trueCollectionFullAsSet)
                    {
                        bool known = qmrrPartialModel.KnownHlaSet.Contains(hla);
                        if (!known)
                        {
                            Set <Hla>      allLessOne            = trueCollectionFullAsSet.SubtractElement(hla);
                            TrueCollection trueCollectionWithout = TrueCollection.GetInstance(allLessOne);
                            double         loglikelihoodWithout  = aQmrrModelMissingAssignment.LogLikelihoodOfCompleteModelConditionedOnKnownHlas(trueCollectionWithout);
                            double         logOdds            = loglikelihoodFull - loglikelihoodWithout;
                            double         probabilityFull    = Math.Exp(loglikelihoodFull);
                            double         probabilityWithout = Math.Exp(loglikelihoodWithout);
                            double         probability        = probabilityFull / (probabilityFull + probabilityWithout);
                            Debug.Assert(logOdds >= 0); // real assert
                            output.WriteLine(SpecialFunctions.CreateTabString(qmrrPartialModel.Peptide, hla, logOdds, probability));
                        }
                    }
                }
            }
        }
        private static IJob CreateJob(int taskPerJobCount, string leafDistributionName, string niceName, RangeCollection nullIndexRangeCollection, ICluster cluster, Queue <ITask> taskQueue)
        {
            IJob   job = cluster.CreateJob();
            string firstPieceIndex = null, lastPieceIndex = null;

            for (int iTaskPerJob = 0; iTaskPerJob < taskPerJobCount; ++iTaskPerJob)
            {
                ITask  task           = taskQueue.Dequeue();
                string taskPieceIndex = task.Name.Split(' ')[1];
                if (iTaskPerJob == 0)
                {
                    firstPieceIndex = taskPieceIndex.ToString();
                }
                lastPieceIndex = taskPieceIndex;
                job.AddTask(task);
                job.Runtime = task.Runtime;

                if (taskQueue.Count == 0)
                {
                    break;
                }
            }
            job.Name = SpecialFunctions.CreateTabString(niceName, firstPieceIndex + "-" + lastPieceIndex,
                                                        leafDistributionName, nullIndexRangeCollection).Replace("\t", " "); //!!!create a nicer way to make a space delimited list
            if (job.Name.Length > 80)
            {
                job.Name = job.Name.Substring(0, 80);
            }
            Console.WriteLine(job.Name);
            job.IsExclusive = false;

            return(job);
        }
        protected override double ComputeLLR(ModelScorer modelScorer, PhyloTree phyloTree, StringBuilder stringBuilder, double targetMarginal, double predictorMarginal,
                                             Converter <Leaf, SufficientStatistics> predictorDistributionClassFunction, Converter <Leaf, SufficientStatistics> targetDistributionClassFunction)
        {
            Converter <Leaf, SufficientStatistics> LeafToJointDistributionClass =
                CreateAlternativeSufficientStatisticsMap(predictorDistributionClassFunction, targetDistributionClassFunction);

            double             logLikelihoodIndependentModel, logLikelihoodJointModel;
            Score              scoreIndTarget, scoreIndPredictor, scoreJoint;
            MessageInitializer messageInitializer;

            // first score the target.
            NullModelDistribution.EmpiricalEquilibrium = targetMarginal;
            messageInitializer = modelScorer.CreateMessageInitializer(predictorDistributionClassFunction, targetDistributionClassFunction, NullModelDistribution);
            scoreIndTarget     = modelScorer.ScoreModel(messageInitializer, false);

            NullModelDistribution.EmpiricalEquilibrium = predictorMarginal;
            messageInitializer = modelScorer.CreateMessageInitializer(targetDistributionClassFunction, predictorDistributionClassFunction, NullModelDistribution);
            scoreIndPredictor  = modelScorer.ScoreModel(messageInitializer, false);

            DistributionDiscreteJointBinary jointDistn = (DistributionDiscreteJointBinary)AlternativeModelDistribution;

            jointDistn.SetInitialParams(scoreIndPredictor.OptimizationParameters, scoreIndTarget.OptimizationParameters);
            messageInitializer = modelScorer.CreateMessageInitializer(null, LeafToJointDistributionClass, jointDistn);
            scoreJoint         = modelScorer.ScoreModel(messageInitializer, false);

            logLikelihoodIndependentModel = scoreIndTarget.Loglikelihood + scoreIndPredictor.Loglikelihood;
            logLikelihoodJointModel       = scoreJoint.Loglikelihood;

            stringBuilder.Append(SpecialFunctions.CreateTabString(scoreIndPredictor.ToString(NullModelDistribution), scoreIndTarget.ToString(NullModelDistribution),
                                                                  logLikelihoodIndependentModel, scoreJoint.ToString(jointDistn), ""));

            double diff = logLikelihoodJointModel - logLikelihoodIndependentModel;

            return(diff);
        }
Пример #13
0
        public void Tabulate(string directory, string caseName, List <KeyValuePair <int, int> > firstNullAndLastNullList)
        {
            List <Dictionary <string, string> > realRowCollectionToSort = new List <Dictionary <string, string> >();
            List <double> nullDiffCollectionToBeSorted = new List <double>();

            //!!!move VirusCount.PhyloTree.RangeCollection to a more general place
            RangeCollection rangeCollection = RangeCollection.GetInstance();
            int             minFirstNull    = int.MaxValue;
            int             maxLastNull     = int.MinValue;


            foreach (KeyValuePair <int, int> firstNullAndLastNull in firstNullAndLastNullList)
            {
                int firstNull = firstNullAndLastNull.Key;
                minFirstNull = Math.Min(minFirstNull, firstNull);
                int lastNull = firstNullAndLastNull.Value;
                maxLastNull = Math.Max(maxLastNull, lastNull);
                //!!!string repeated elsewere
                //!!!what "new" or not?
                string inputFileName = string.Format(@"{0}\{1}.{2}.{3}-{4}.pValues.new.txt", directory, SelectionName, CaseName, firstNull, lastNull); //!!!const
                int    maxNullSeen   = int.MinValue;

                //foreach (Dictionary<string, string> row in SpecialFunctions.TabFileTable(inputFileName, true))
                foreach (Dictionary <string, string> row in SpecialFunctions.TabFileTable(inputFileName, PValueDetails.Header, true))
                {
                    int nullIndex = int.Parse(row["NullIndex"]);
                    rangeCollection.TryAdd(nullIndex);
                    if (nullIndex == -1)
                    {
                        realRowCollectionToSort.Add(row);
                    }
                    else
                    {
                        double value = AccessPValueFromRow(row);
                        nullDiffCollectionToBeSorted.Add(value);
                        maxNullSeen = Math.Max(maxNullSeen, nullIndex);
                    }
                }
            }
            SpecialFunctions.CheckCondition(realRowCollectionToSort.Count != 0);
            SpecialFunctions.CheckCondition(rangeCollection.IsComplete(minFirstNull, maxLastNull));
            int nullCount = maxLastNull - Math.Max(0, minFirstNull) + 1;


            Dictionary <Dictionary <string, string>, double>
            qValueList = SpecialFunctions.ComputeQValues(ref realRowCollectionToSort, AccessPValueFromRow, ref nullDiffCollectionToBeSorted, nullCount);

            string outputFile = string.Format(@"{0}\{1}.{2}.{3}.qValues.new.txt", directory, SelectionName, CaseName, nullCount); //!!!const

            using (StreamWriter outputStream = File.CreateText(outputFile))
            {
                outputStream.WriteLine(SpecialFunctions.CreateTabString(PValueDetails.Header, "qValue"));
                foreach (Dictionary <string, string> row in realRowCollectionToSort)
                {
                    double qValue = qValueList[row];
                    outputStream.WriteLine(SpecialFunctions.CreateTabString(row[""], qValue));
                }
            }
        }
 public override string ToString()
 {
     return(SpecialFunctions.CreateTabString(
                SelectionName, NullIndex, Peptide, Hla, Score1, Score2,
                Diff, PValue(),
                SpecialFunctions.Join(",", KnownHlas),
                BestHlaSetSoFar == null? null : SpecialFunctions.Join(",", BestHlaSetSoFar),
                LeakProbability, LinkProbability));
 }
        private string WeightIfToString(WeightIf weightIf)
        {
            //string hla = FindFeatureOrEmpty(weightIf.FeatureCollection[0], typeof(IsHla));
            //string aa = FindFeatureOrEmpty(weightIf.FeatureCollection[0], typeof(IsAA));
            //string prop = FindFeatureOrEmpty(weightIf.FeatureCollection[0], typeof(HasAAProp));
            SpecialFunctions.CheckCondition(weightIf.FeatureCollection.Length == 1);
            Feature innerFeature = weightIf.FeatureCollection[0];

            return(string.Format(SpecialFunctions.CreateTabString(weightIf.Weight, Math.Abs(weightIf.Weight), innerFeature)));
        }
Пример #16
0
        static public string InsertMaterial(List <string> columnCollection, int splitPoint, string middleStuff)
        {
            SpecialFunctions.CheckCondition(columnCollection.Count >= splitPoint, string.Format("Input requires {0} column(s)", splitPoint));
            string startOfLine = SpecialFunctions.Join("\t", SpecialFunctions.First(columnCollection, splitPoint));
            string restOfLine  = SpecialFunctions.Join("\t", SpecialFunctions.Rest(columnCollection, splitPoint));

            return(SpecialFunctions.CreateTabString(
                       startOfLine,
                       middleStuff,
                       restOfLine));
        }
Пример #17
0
 public static string ExtraHeader(bool includeHlaInOutput)
 {
     if (includeHlaInOutput)
     {
         return(SpecialFunctions.CreateTabString("BestHla", RestOfExtra));
     }
     else
     {
         return(RestOfExtra);
     }
 }
        public void CreateMainKmerPositionsStream(int merLength, TextWriter textWriter)
        {
            Dictionary <string, AASeq>  caseToCompressedAASeq = RemoveDeletesAndStopsFromData(textWriter);
            Dictionary <string, string> merStringToBestOriginalAA0Position = FindMerStringToBestOriginalAA0Position(merLength, textWriter, caseToCompressedAASeq);

            textWriter.WriteLine(SpecialFunctions.CreateTabString("kMer", "MostCommonOriginalAA1Position"));
            foreach (string merString in merStringToBestOriginalAA0Position.Keys)
            {
                string bestOriginalAA0Position = merStringToBestOriginalAA0Position[merString];
                textWriter.WriteLine(SpecialFunctions.CreateTabString(merString, bestOriginalAA0Position));
            }
        }
Пример #19
0
        public override string GetParameterValueString(OptimizationParameterList parameters)
        {
            double eTimesLambdaA = parameters[(int)ParameterIndex.ETimesLambaA].Value;
            double Lambda_Hla    = parameters[(int)ParameterIndex.Lambda_Hla].Value;
            double P_aa          = parameters[(int)ParameterIndex.P_AA].Value;
            double P_Hla         = parameters[(int)ParameterIndex.P_Hla].Value;
            double rTimesLambdaA = parameters[(int)ParameterIndex.RTimesLambdaA].Value;

            //double P_r = parameters[(int)ParameterIndex.P_r].Value;

            return(SpecialFunctions.CreateTabString(eTimesLambdaA, rTimesLambdaA, Lambda_Hla, P_aa, P_Hla));
        }
Пример #20
0
        public override string GetParameterValueString(OptimizationParameterList parameters)
        {
            double lambdaA = parameters[(int)ParameterIndex.Lambda_A].Value;
            double lambdaB = parameters[(int)ParameterIndex.Lambda_B].Value;
            double P_AB    = parameters[(int)ParameterIndex.P_AB].Value;
            double P_Ab    = parameters[(int)ParameterIndex.P_Ab].Value;
            double P_aB    = parameters[(int)ParameterIndex.P_aB].Value;
            double P_ab    = 1 - P_AB - P_Ab - P_aB;
            double P_A     = P_AB + P_Ab;
            double P_B     = P_AB + P_aB;

            return(SpecialFunctions.CreateTabString(lambdaA, lambdaB, P_AB, P_Ab, P_aB, P_ab, P_A, P_B));
        }
 public static void WriteDataToFile(string filename, Dictionary <string, Dictionary <string, BooleanStatistics> > variableToCidToValue)
 {
     using (TextWriter writer = File.CreateText(filename))
     {
         writer.WriteLine("var\tcid\tval");
         foreach (KeyValuePair <string, Dictionary <string, BooleanStatistics> > variableAndCidToValue in variableToCidToValue)
         {
             foreach (KeyValuePair <string, BooleanStatistics> cidAndValue in variableAndCidToValue.Value)
             {
                 writer.WriteLine(SpecialFunctions.CreateTabString(variableAndCidToValue.Key, cidAndValue.Key, (int)cidAndValue.Value));
             }
         }
     }
 }
        private void ReportPerHlaAssignment(Dictionary <string, BestSoFar <double, TrueCollection> > peptideToBestHlaAssignmentSoFar,
                                            string directory, string name)
        {
            string fileName = string.Format(@"{0}\NoisyOr.HlasPerPeptide.{1}.new.txt", directory, name);

            using (StreamWriter output = File.CreateText(fileName))
            {
                output.WriteLine(SpecialFunctions.CreateTabString("Peptide", "HLAAssignment", "LogLikelihood"));
                foreach (QmrrPartialModel qmrrPartialModel in QmrrPartialModelCollection)
                {
                    BestSoFar <double, TrueCollection> bestHlaAssignment = peptideToBestHlaAssignmentSoFar[qmrrPartialModel.Peptide];
                    TrueCollection trueCollectionFull = bestHlaAssignment.Champ;
                    double         loglikelihoodFull  = bestHlaAssignment.ChampsScore;
                    output.WriteLine(SpecialFunctions.CreateTabString(qmrrPartialModel.Peptide, trueCollectionFull, loglikelihoodFull));
                }
            }
        }
        public double Length = 1;   // default value...

        static public BranchOrLeaf GetInstance(StreamReader streamreader, bool isRoot)
        {
            BranchOrLeaf branchOrLeaf;
            char         peek1 = Peek(streamreader);

            if (peek1 == '(')
            {
                branchOrLeaf = Branch.GetBranchInstance(streamreader, isRoot);
            }
            else
            {
                branchOrLeaf = Leaf.GetLeafInstance(streamreader);
            }

            if (isRoot)
            {
                branchOrLeaf.Length = double.NaN;
                char peek2 = Peek(streamreader);
                if (peek2 == ':')
                {
                    Read(streamreader);
                    //branchOrLeaf.Length = ReadLength(streamreader);
                    //SpecialFunctions.CheckCondition(branchOrLeaf.Length == 0);
                }
            }
            else
            {
                char peek3 = Peek(streamreader);
                if (peek3 != ':')
                {
                    int nodeId = ReadInt(streamreader);
                    Debug.WriteLine(SpecialFunctions.CreateTabString("NodeId", nodeId));
                }
                char colon = Read(streamreader);
                SpecialFunctions.CheckCondition(colon == ':');
                branchOrLeaf.Length = ReadLength(streamreader, true);
            }
            return(branchOrLeaf);
        }
Пример #24
0
        private void SearchForBestParamsAndHlaAssignments(QmrrPartialModelCollection qmrrPartialModelCollection, double depth)
        {
            int    cStep = 100;
            double eps   = 1e-7;
            OptimizationParameterList oldQmrrParams = null;

            Debug.WriteLine(SpecialFunctions.CreateTabString("depth", "dataset", QmrrParamsStart.ToStringHeader(), "Step", "After", QmrrParamsStart.ToStringHeader(), "Score"));
            for (int iStep = 0; iStep < cStep && !(BestParamsSoFar.Champ.IsClose(oldQmrrParams, eps)); ++iStep)
            {
                oldQmrrParams = BestParamsSoFar.Champ.Clone();

                double hlaAssignmentSumScore = FindBestHlaAssignmentSet(qmrrPartialModelCollection, depth);

                Debug.WriteLine(SpecialFunctions.CreateTabString(depth, qmrrPartialModelCollection.DatasetName,
                                                                 QmrrParamsStart, iStep + 1, "AfterHla", BestParamsSoFar.Champ,
                                                                 hlaAssignmentSumScore));

                FindBestQmrrParams(qmrrPartialModelCollection);

                Debug.WriteLine(SpecialFunctions.CreateTabString(depth, qmrrPartialModelCollection.DatasetName,
                                                                 QmrrParamsStart, iStep + 1, "AfterParam", BestParamsSoFar.Champ, BestParamsSoFar.ChampsScore));
            }
        }
        protected override double ComputeLLR(ModelScorer modelScorer, PhyloTree phyloTree, StringBuilder stringBuilder, double targetMarginal, double predictorMarginal,
                                             Converter <Leaf, SufficientStatistics> predictorDistributionClassFunction, Converter <Leaf, SufficientStatistics> targetDistributionClassFunction)
        {
            NullModelDistribution.EmpiricalEquilibrium = targetMarginal;
            NullModelDistribution.InitialParamVals     = null;

            MessageInitializer messageInitializer = modelScorer.CreateMessageInitializer(predictorDistributionClassFunction, targetDistributionClassFunction, NullModelDistribution);

            List <double> logLikelihoodList = new List <double>();

            foreach (bool useParameter in new bool[] { false, true })
            {
                Score score = modelScorer.ScoreModel(messageInitializer, useParameter);

                stringBuilder.Append(SpecialFunctions.CreateTabString(score.ToString(useParameter ? AlternativeModelDistribution : NullModelDistribution), ""));
                logLikelihoodList.Add(score.Loglikelihood);
                AltModelDistribution.InitialParamVals = score.OptimizationParameters;
                Debug.WriteLine(SpecialFunctions.CreateTabString("AltModelDistribution.InitialParamVals = score.OptimizationParameters", score.OptimizationParameters));
            }

            double diff = logLikelihoodList[1] - logLikelihoodList[0];

            return(diff);
        }
Пример #26
0
        protected Score MaximizeLikelihoodInternal(MessageInitializer messageInitializer)
        {
            OptimizationParameterList paramsToOptimize = messageInitializer.GetOptimizationParameters();
            int functionEvaluationCount = 0;

            bool useLogMethod = false;

            Converter <OptimizationParameterList, double> functionToOptimize =
                delegate(OptimizationParameterList paramList)
            {
                FuncCalls++;
                ++functionEvaluationCount;
                //Debug.WriteLine("EvalCount " + functionEvaluationCount.ToString());
                double loglikelihood = ComputeLogLikelihoodModelGivenData(messageInitializer, paramList, useLogMethod);

                if (!useLogMethod && double.IsNegativeInfinity(loglikelihood))
                {
                    useLogMethod  = true;
                    loglikelihood = ComputeLogLikelihoodModelGivenData(messageInitializer, paramList, useLogMethod);
                }
                //SpecialFunctions.CheckCondition(!double.IsNaN(loglikelihood), "for debugging: got a NaN from ComputeLogLikelihoodModelGivenData");
                //if (double.IsNaN(loglikelihood))
                //{
                //    return double.NegativeInfinity;
                //}
                return(loglikelihood);
            };

            double loglikelihoodExternal = GridSearch.Optimize(functionToOptimize, paramsToOptimize, 10, 5);

            Score score = Score.GetInstance(loglikelihoodExternal, paramsToOptimize, messageInitializer.PropogationDistribution);

            Debug.WriteLine(SpecialFunctions.CreateTabString(GridSearch.DebugCount, score, functionEvaluationCount));

            return(score);
        }
        public void ScoreTree(
            ModelScorer modelScorer,
            PhyloTree phyloTree,
            string predictorSparseFileName,
            string targetSparseFileName,
            string predictorVariableName,
            string targetVariableName,
            double[] nullModelArgs,
            double[] altModelArgs)
        {
            //Dictionary<string, Dictionary<string, SufficientStatistics>> predictorVariableToCaseIdToRealNonMissingValue = LoadSparseFileInMemory(predictorSparseFileName);
            IEnumerable <Pair <string, Dictionary <string, SufficientStatistics> > > predictorNameAndCaseIdToNonMissingValueEnumeration = LoadSparseFileEnumeration(predictorSparseFileName);
            IEnumerable <Pair <string, Dictionary <string, SufficientStatistics> > > targetNameAndCaseIdToNonMissingValueEnumeration    = LoadSparseFileEnumeration(targetSparseFileName);

            RangeCollection    nullIndexRangeCollection = RangeCollection.GetInstance(-1, -1);
            NullDataCollection nullDataGenerator        =
                CreateNullDataGenerator("PredictorPermutation", modelScorer, phyloTree, nullIndexRangeCollection,
                                        predictorNameAndCaseIdToNonMissingValueEnumeration, targetNameAndCaseIdToNonMissingValueEnumeration);

            UniversalWorkList workList = UniversalWorkList.GetInstance(
                predictorNameAndCaseIdToNonMissingValueEnumeration,
                targetNameAndCaseIdToNonMissingValueEnumeration,
                //targetNameAndCaseIdToNonMissingValueEnumeration,
                nullDataGenerator, nullIndexRangeCollection, AlwaysKeep <Dictionary <string, string> > .GetInstance());


            foreach (RowData rowAndTargetData in workList.List())
            {
                if (rowAndTargetData.Row[PhyloTree.PredictorVariableColumnName] == predictorVariableName &&
                    rowAndTargetData.Row[PhyloTree.TargetVariableColumnName] == targetVariableName)
                {
                    Dictionary <string, SufficientStatistics> caseIdToNonNullPredictorValue = rowAndTargetData.PredictorData;//workList.GetCaseIdToNonMissingValueForNullIndexAndPredictorVariable(-1, predictorVariableName);
                    Dictionary <string, SufficientStatistics> caseIdToNonMissingTargetValue = rowAndTargetData.TargetData;

                    Converter <Leaf, SufficientStatistics> targetDistributionMap = CreateSufficientStatisticsMap(caseIdToNonMissingTargetValue);
                    Converter <Leaf, SufficientStatistics> predictorDistributionClassFunction = CreateSufficientStatisticsMap(caseIdToNonNullPredictorValue);
                    Converter <Leaf, SufficientStatistics> altDistributionMap = CreateAlternativeSufficientStatisticsMap(predictorDistributionClassFunction, targetDistributionMap);
                    double                    logLikelihood;
                    Score                     scoreIndTarget, scoreIndPredictor, scoreAlt;
                    MessageInitializer        messageInitializer;
                    OptimizationParameterList nullParams = NullModelDistribution.GetParameters(nullModelArgs);
                    OptimizationParameterList altParams  = AltModelDistribution.GetParameters(altModelArgs);

                    Console.WriteLine(SpecialFunctions.CreateTabString("Variable", nullParams.ToStringHeader(), "LogL"));
                    messageInitializer = modelScorer.CreateMessageInitializer(predictorDistributionClassFunction, targetDistributionMap, NullModelDistribution);
                    logLikelihood      = modelScorer.ComputeLogLikelihoodModelGivenData(messageInitializer, nullParams);
                    scoreIndTarget     = Score.GetInstance(logLikelihood, nullParams);
                    Console.WriteLine("Target\t" + scoreIndTarget);

                    messageInitializer = modelScorer.CreateMessageInitializer(targetDistributionMap, predictorDistributionClassFunction, NullModelDistribution);
                    logLikelihood      = modelScorer.ComputeLogLikelihoodModelGivenData(messageInitializer, nullParams);
                    modelScorer.ComputeLogLikelihoodModelGivenData(messageInitializer, nullParams);
                    scoreIndPredictor = Score.GetInstance(logLikelihood, nullParams);
                    Console.WriteLine("Predictor\t" + scoreIndPredictor);

                    Console.WriteLine("\n" + SpecialFunctions.CreateTabString("Variable", altParams.ToStringHeader(), "LogL"));
                    messageInitializer = modelScorer.CreateMessageInitializer(null, altDistributionMap, AltModelDistribution);
                    logLikelihood      = modelScorer.ComputeLogLikelihoodModelGivenData(messageInitializer, altParams);
                    scoreAlt           = Score.GetInstance(logLikelihood, altParams);
                    Console.WriteLine(SpecialFunctions.CreateTabString(AltModelDistribution, scoreAlt));
                }
            }
        }
        static bool BracketNearPositiveInfinity(
            out double a, ref double b, ref double c,
            out double fOfA, ref double fOfB, ref double fOfC,
            out bool flatMinimumTowardPosInf,
            double inputTol, double outputTol, Converter <double, double> oneDRealFunctionInDoubleSpace, int debugCount)
        {
            SpecialFunctions.CheckCondition(!double.IsNaN(fOfB) && !double.IsNaN(fOfC), "One of the starting points for BracketNearInfinity evaluates to NaN");
            flatMinimumTowardPosInf = false;
            double pseudoInfinity = double.PositiveInfinity;
            double fOfInfinity    = oneDRealFunctionInDoubleSpace(pseudoInfinity);


            if (double.IsPositiveInfinity(fOfInfinity))
            {
                //Nevermind about minimizing evaluations and remembering evaluations. Just try to find a value that can take the place of infinity
                //Try 10000, 7500, ....
                //       If gets too close to c (relative to b), just give up
                //pseudoInfinity = 10000;
                //while (true)
                //{
                //    fOfInfinity = oneDRealFunctionInDoubleSpace(pseudoInfinity);
                //    if (!double.IsPositiveInfinity(fOfInfinity))
                //    {
                //        break;
                //    }
                //    pseudoInfinity = b + (pseudoInfinity - b) * .75; //This won't work: infinity = infinity *.75 because it will go to 0 in the limit and b and c could be negative.
                //    SpecialFunctions.CheckCondition(c < pseudoInfinity, "Can't find an evaluatable 'infinity' point");
                //}
                double high    = 10000;
                double fOfHigh = oneDRealFunctionInDoubleSpace(high);
                if (double.IsPositiveInfinity(fOfHigh))
                {
                    BinarySearchForNonInfiniteEvaluation(c, fOfC, high, fOfHigh, out pseudoInfinity, out fOfInfinity, oneDRealFunctionInDoubleSpace, inputTol, outputTol);
                }
                else
                {
                    pseudoInfinity = high;
                    fOfInfinity    = fOfHigh;
                }
            }

            if (!double.IsPositiveInfinity(pseudoInfinity) && fOfInfinity > fOfC + outputTol)
            {
                a    = b;
                b    = c;
                c    = pseudoInfinity;
                fOfA = fOfB;
                fOfB = fOfC;
                fOfC = fOfInfinity;
                return(false);
            }

            Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "Entering BracketNearPositiveInfinity\n",
                                                             "", "b", b, fOfB, "\n",
                                                             "", "c", c, fOfC, "\n",
                                                             "", "inf", "inf", fOfInfinity, "\n"));

            //FindTheSmallestInputThatGivesSameAnswerAsInf
            Debug.Assert(b < c && (fOfB >= fOfC || Math.Abs(fOfB - fOfC) < outputTol)); // real assert


            //DoubleCUntilFindSFInf - the current c might be good enough
            double previous            = b;
            double fOfPrevious         = fOfB;
            double cAfterDoubles       = c;
            double fOfCAfterDoubles    = fOfC;
            double inputBetterThanInf  = double.NaN;
            double outputBetterThanInf = double.NaN;

            Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "About to loop\n",
                                                             "", "b", b, fOfB, "\n",
                                                             "", "previous", previous, fOfPrevious, "\n",
                                                             "", "c", c, fOfC, "\n",
                                                             "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                             "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                             "", "inf", "inf", fOfInfinity, "\n"));


            while (true)
            {
                if (!double.IsPositiveInfinity(fOfInfinity) && Math.Abs(fOfCAfterDoubles - fOfInfinity) < outputTol) //!!!const
                {
                    Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if (Math.Abs(fOfCAfterDoubles - fOfInfinity) < outputTol){break loop}\n",
                                                                     "", "b", b, fOfB, "\n",
                                                                     "", "previous", previous, fOfPrevious, "\n",
                                                                     "", "c", c, fOfC, "\n",
                                                                     "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                                     "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                                     "", "inf", "inf", fOfInfinity, "\n"));
                    break;
                }


                //if (fOfCAfterDoubles < fOfInfinity)
                {
                    Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if (fOfCAfterDoubles < fOfInfinity)...\n",
                                                                     "", "b", b, fOfB, "\n",
                                                                     "", "previous", previous, fOfPrevious, "\n",
                                                                     "", "c", c, fOfC, "\n",
                                                                     "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                                     "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                                     "", "inf", "inf", fOfInfinity, "\n"));
                    if (fOfCAfterDoubles < fOfInfinity - outputTol && (double.IsNaN(inputBetterThanInf) || fOfCAfterDoubles < outputBetterThanInf)) // /*|| Math.Abs(fOfCAfterDoubles - outputBetterThanInf) < outputTol*/)
                    {
                        inputBetterThanInf  = cAfterDoubles;
                        outputBetterThanInf = fOfCAfterDoubles;

                        Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if (double.IsNaN(inputBetterThanInf) || fOfCAfterDoubles < outputBetterThanInf || Math.Abs(fOfCAfterDoubles - outputBetterThanInf) < outputTol){BetterThanInf = cAfterDoubles}\n",
                                                                         "", "b", b, fOfB, "\n",
                                                                         "", "previous", previous, fOfPrevious, "\n",
                                                                         "", "c", c, fOfC, "\n",
                                                                         "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                                         "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                                         "", "inf", "inf", fOfInfinity, "\n"));
                    }
                    else if (!double.IsNaN(inputBetterThanInf))
                    {
                        //We've found a min and after a value that is not quite as good but still better's than infinity's
                        if (c == inputBetterThanInf)
                        {
                            a    = b;
                            fOfA = fOfB;

                            Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "We've found a min and after a value that is not quite as good but still better's than infinity'sWe've found a min and after a value that is not quite as good but still better's than infinity's && c == inputBetterThanInf {a =  b;}\n",
                                                                             "", "a", a, fOfA, "\n",
                                                                             "", "b", b, fOfB, "\n",
                                                                             "", "previous", previous, fOfPrevious, "\n",
                                                                             "", "c", c, fOfC, "\n",
                                                                             "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                                             "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                                             "", "inf", "inf", fOfInfinity, "\n"));
                        }
                        else
                        {
                            a    = c;
                            fOfA = fOfC;

                            Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "We've found a min and after a value that is not quite as good but still better's than infinity'sWe've found a min and after a value that is not quite as good but still better's than infinity's && NOT c == inputBetterThanInf {a =  c;}\n",
                                                                             "", "a", a, fOfA, "\n",
                                                                             "", "b", b, fOfB, "\n",
                                                                             "", "previous", previous, fOfPrevious, "\n",
                                                                             "", "c", c, fOfC, "\n",
                                                                             "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                                             "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                                             "", "inf", "inf", fOfInfinity, "\n"));
                        }

                        b    = inputBetterThanInf;
                        c    = cAfterDoubles;
                        fOfB = outputBetterThanInf;
                        fOfC = fOfCAfterDoubles;

                        Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "b = inputBetterThanInf;c = cAfterDoubles;return false\n",
                                                                         "", "a", a, fOfA, "\n",
                                                                         "", "b", b, fOfB, "\n",
                                                                         "", "previous", previous, fOfPrevious, "\n",
                                                                         "", "c", c, fOfC, "\n",
                                                                         "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                                         "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                                         "", "inf", "inf", fOfInfinity, "\n"));

                        return(false);
                    }
                }

                previous      = cAfterDoubles;
                cAfterDoubles = b + (cAfterDoubles - b) * InvGold;
                //Debug.Assert(cAfterDoubles < pseudoInfinity);
                fOfPrevious = fOfCAfterDoubles;

                // we're guaranteed that pseudoInfinity is the highest number that doesn't evaluate to infinite.
                // if we set cAfterDoubles to pseudoInfinity, then we'll hit good stopping conditions above.
                if (cAfterDoubles > pseudoInfinity)
                {
                    cAfterDoubles    = pseudoInfinity;
                    fOfCAfterDoubles = fOfInfinity;
                }
                else
                {
                    fOfCAfterDoubles = oneDRealFunctionInDoubleSpace(cAfterDoubles);
                }

                Debug.Assert(!double.IsNaN(fOfInfinity) || !double.IsInfinity(fOfCAfterDoubles));
                //if (double.IsNaN(fOfInfinity) && double.IsNaN(fOfCAfterDoubles))
                //{
                //    // everything from here on out is assumed to be NaN
                //    Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "infinity return an exception and so did this value\n",
                //        "", "b", b, fOfB, "\n",
                //        "", "previous", previous, fOfPrevious, "\n",
                //        "", "c", c, fOfC, "\n",
                //        "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                //        "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                //        "", "inf", "inf", fOfInfinity, "\n"));

                //    Debug.WriteLine("Case of interest");

                //}

                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "setup for next loop: previous = cAfterDoubles; cAfterDoubles = b + (cAfterDoubles - b) * invGold; fOfPrevious = fOfCAfterDoubles;\n",
                                                                 "", "b", b, fOfB, "\n",
                                                                 "", "previous", previous, fOfPrevious, "\n",
                                                                 "", "c", c, fOfC, "\n",
                                                                 "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                                 "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                                 "", "inf", "inf", fOfInfinity, "\n"));
            }

            //We found a value better than inf and we found a value as good as inf
            if (!double.IsNaN(inputBetterThanInf))
            {
                a    = (c == inputBetterThanInf) ? b : c;
                fOfA = (c == inputBetterThanInf) ? fOfB : fOfC;
                b    = inputBetterThanInf;
                fOfB = outputBetterThanInf;
                c    = cAfterDoubles;
                fOfC = fOfCAfterDoubles;


                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "out of loop, something better than infinity: a = (c == inputBetterThanInf) ? b : c; b = inputBetterThanInf; c = cAfterDoubles; return false\n",
                                                                 "", "a", a, fOfA, "\n",
                                                                 "", "b", b, fOfB, "\n",
                                                                 "", "previous", previous, fOfPrevious, "\n",
                                                                 "", "c", c, fOfC, "\n",
                                                                 "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n",
                                                                 "", "betterThanInf", inputBetterThanInf, outputBetterThanInf, "\n",
                                                                 "", "inf", "inf", fOfInfinity, "\n"));

                return(false);
            }


            //BinarySearchToFindEdge
            double low    = previous;
            double fOfLow = fOfPrevious;

            Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "binary search for edge\n",
                                                             "", "b", b, fOfB, "\n",
                                                             "", "c", c, fOfC, "\n",
                                                             "", "low", low, fOfLow, "\n",
                                                             "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n"));

            Debug.Assert(low <= cAfterDoubles); // real assert
            if (Math.Abs(fOfLow - fOfInfinity) < outputTol)
            {
                flatMinimumTowardPosInf = true;
                a    = low;
                b    = cAfterDoubles;
                c    = pseudoInfinity;
                fOfA = fOfLow;
                fOfB = fOfCAfterDoubles;
                fOfC = fOfInfinity;

                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if (Math.Abs(fOfLow - fOfCAfterDoubles) < outputTol){flatMinimumTowardPosInf = true;;return false\n",
                                                                 "", "a", a, fOfA, "\n",
                                                                 "", "b", b, fOfB, "\n",
                                                                 "", "c", c, fOfC, "\n",
                                                                 "", "low", low, fOfLow, "\n",
                                                                 "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n"));

                return(false);
            }
            Debug.Assert(double.IsNaN(fOfCAfterDoubles) || fOfLow > fOfCAfterDoubles); // real assert
            while (true)
            {
                if (Math.Abs(low - cAfterDoubles) < inputTol)
                {
                    // double.PositiveInfinity is the answer
                    a    = double.NaN;
                    b    = pseudoInfinity;
                    c    = double.NaN;
                    fOfA = double.NaN;
                    fOfB = fOfInfinity;
                    fOfC = double.NaN;

                    Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "In loop: if (Math.Abs(low - cAfterDoubles) < inputTol){a = double.NaN; b = infinity; c = double.NaN;return true}\n",
                                                                     "", "a", a, fOfA, "\n",
                                                                     "", "b", b, fOfB, "\n",
                                                                     "", "c", c, fOfC, "\n"));

                    return(true);
                }
                double mid    = (low + cAfterDoubles) / 2; //Change of overflow???
                double fOfMid = oneDRealFunctionInDoubleSpace(mid);
                //Debug.Assert(!double.IsNaN(fOfMid));

                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "mid = (low + cAfterDoubles) / 2;\n",
                                                                 "", "b", b, fOfB, "\n",
                                                                 "", "c", c, fOfC, "\n",
                                                                 "", "mid", mid, fOfMid, "\n",
                                                                 "", "low", low, fOfLow, "\n",
                                                                 "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n"));

                if (Math.Abs(fOfMid - fOfInfinity) < outputTol)
                {
                    cAfterDoubles    = mid;
                    fOfCAfterDoubles = fOfMid;

                    Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if (Math.Abs(fOfMid - fOfInfinity) < outputTol) {cAfterDoubles = mid;continue}\n",
                                                                     "", "b", b, fOfB, "\n",
                                                                     "", "c", c, fOfC, "\n",
                                                                     "", "mid", mid, fOfMid, "\n",
                                                                     "", "low", low, fOfLow, "\n",
                                                                     "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n"));

                    continue;
                }
                if (fOfMid < fOfInfinity)
                {
                    // Found point better than infinity
                    a = low;
                    b = mid;
                    c = b + InvGold * (b - a);
                    Debug.Assert(Math.Abs(oneDRealFunctionInDoubleSpace(c) - fOfInfinity) < outputTol); // real assert
                    fOfA = fOfLow;
                    fOfB = fOfMid;
                    fOfC = fOfInfinity;

                    Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if (fOfMid < fOfInfinity) {a = low; b = mid; c = b + invGold * (b - a); return false}\n",
                                                                     "", "a", a, fOfA, "\n",
                                                                     "", "b", b, fOfB, "\n",
                                                                     "", "c", c, fOfC, "\n",
                                                                     "", "mid", mid, fOfMid, "\n",
                                                                     "", "low", low, fOfLow, "\n",
                                                                     "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n"));

                    return(false);
                }
                Debug.Assert(fOfMid > fOfInfinity); // real assert

                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "get ready for next loop: low = mid;}\n",
                                                                 "", "b", b, fOfB, "\n",
                                                                 "", "c", c, fOfC, "\n",
                                                                 "", "mid", mid, fOfMid, "\n",
                                                                 "", "low", low, fOfLow, "\n",
                                                                 "", "cAfterDoubles", cAfterDoubles, fOfCAfterDoubles, "\n"));

                low    = mid;
                fOfLow = fOfMid;
            }
        }
        override public double Run(Converter <OptimizationParameter, double> oneDRealFunction,
                                   /*ref*/ OptimizationParameter param, int gridLineCount, out double bestInput)
        {
            ++DebugCount;

            double outputTol = .00000001;

            Converter <double, double> oneDRealFunctionInDoubleSpace = delegate(double d)
            {
                Debug.Assert(!double.IsNaN(d));
                param.ValueForSearch = d;
                try
                {
                    double odrf = oneDRealFunction(param);
                    if (double.IsNaN(odrf))
                    {
                        return(double.PositiveInfinity);
                    }
                    //Debug.Assert(!double.IsNegativeInfinity(odrf)); //!!!for debugging
                    return(-odrf);
                }
                catch (Exception exception)
                {
                    Console.WriteLine("Exception turned to NaN in OneDOptimizationBrent");
                    Console.WriteLine(exception.Message);
                    if (exception.InnerException != null)
                    {
                        Console.WriteLine(exception.InnerException.Message);
                    }
                    return(double.NaN);
                }
            };

            double a = param.LowForSearch;
            double b = param.ValueForSearch;

            Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "Get a and b from param\n", "", "a", a, "\n", "", "b", b, "\n"));

            double c;

            if (a > b || Math.Abs(a - b) < InputTol || double.IsInfinity(b))
            {
                c = param.HighForSearch;
                b = a + GoldenRatio * (c - a);
                Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "if (a > b || Math.Abs(a - b) < inputTol || double.IsInfinity(b)){c = param.HighForSearch;b = a + goldenRatio * (c - a);}\n", "", "a", a, "\n", "", "b", b, "\n", "", "c", c, "\n"));
            }
            else
            {
                Debug.Assert(a <b && Math.Abs(a - b)> InputTol);   // real assert
                c = b + InvGold * (b - a);
                Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "NOT if (a > b || Math.Abs(a - b) < inputTol || double.IsInfinity(b)){c = b + invGold * (b - a);}", "a", a, "", "\n", "", "b", b, "", "\n", "", "c", c, "", "\n"));
            }


            double fOfA;
            double fOfB;
            double fOfC;

            if (FindBracketWithInfinities(ref a, ref b, ref c, oneDRealFunctionInDoubleSpace, InputTol, outputTol, out bestInput, out fOfA, out fOfB, out fOfC, DebugCount))
            {
                return(-fOfB);
            }


            Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "About to enter Brent\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
            SpecialFunctions.CheckCondition(fOfB < fOfA && fOfB < fOfC, string.Format("About to enter Brent, but prediction, f(b)<f(a) && f(b) < f(c), has failed"));
            FuncMin.FuncMinBrent(a, ref b, c, fOfA, ref fOfB, fOfC, InputTol, oneDRealFunctionInDoubleSpace);
            Debug.WriteLine(SpecialFunctions.CreateTabString(DebugCount, "After Brent\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
            bestInput = b;
            return(-fOfB);
        }
        static public bool FindBracketWithInfinities(ref double a, ref double b, ref double c, Converter <double, double> oneDRealFunctionInDoubleSpace, double inputTol, double outputTol, out double bestInput, out double fOfA, out double fOfB, out double fOfC, int debugCount)
        {
            fOfA = oneDRealFunctionInDoubleSpace(a);
            fOfB = oneDRealFunctionInDoubleSpace(b);
            fOfC = oneDRealFunctionInDoubleSpace(c);
            Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "Evaluate a,b,c\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));

            //double start = -13;
            //double last = 20;
            //double inc = .25;
            //Plot(oneDRealFunctionInDoubleSpace, start, last, inc);

            bool flatMinimumTowardPosInf = false;

            if (fOfC < fOfB || Math.Abs(fOfC - fOfB) < outputTol) //!!!Need to consider tolerance?
            {
                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if (fOfC < fOfB || Math.Abs(fOfC - fOfB) < outputTol) [about to enter BracketNearPositiveInfinity]\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));

                bool foundMin = BracketNearPositiveInfinity(out a, ref b, ref c, out fOfA, ref fOfB, ref fOfC, out flatMinimumTowardPosInf, inputTol, outputTol, oneDRealFunctionInDoubleSpace, debugCount);
                if (foundMin)
                {
                    Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "BracketNearPositiveInfinity returns true\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
                    bestInput = b;
                    return(true);
                }
                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "BracketNearPositiveInfinity returns false\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
                //Either everything to the right is flat or we found an  a,b,c with the right relation
                Debug.Assert(flatMinimumTowardPosInf || (fOfB < fOfA && fOfB < fOfC)); // real assert
            }

            if (flatMinimumTowardPosInf)
            {
                Debug.Assert(Math.Abs(fOfA - fOfB) < outputTol);
            }

            // why if flatMinToPosInf?
            if (fOfA < fOfB || flatMinimumTowardPosInf) //!!!Need to consider tolerance?
            {
                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if (fOfA < fOfB || double.IsNaN(c)) [about to enter BracketNearNegativeInfinity]\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
                bool flatMinimumTowardNegInf;
                bool foundMin = BracketNearNegativeInfinity(ref a, ref b, out c, ref fOfA, ref fOfB, out fOfC, out flatMinimumTowardNegInf, inputTol, outputTol, oneDRealFunctionInDoubleSpace, debugCount);

                if (foundMin)
                {
                    Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "BracketNearNegativeInfinity returns true\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
                    bestInput = b;
                    return(true);
                }
                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "BracketNearNegativeInfinity returns false\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));

                //If there was a min at pos Inf
                if (flatMinimumTowardPosInf)
                {
                    //If also one a min inf, then flat
                    if (flatMinimumTowardNegInf)
                    {
                        Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if flat [flatMinimumTowardPosInf && flatMinimumTowardNegInf] return\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
                        bestInput = 0;
                        return(true);
                    }
                    else
                    {
                        Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if min at Pos Inf only return\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
                        bestInput = double.PositiveInfinity;
                        return(true);
                    }
                }
                if (flatMinimumTowardNegInf)
                {
                    Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "if min at Neg Inf only return\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
                    bestInput = double.NegativeInfinity;
                    return(true);
                }
                Debug.Assert(fOfB < fOfA && fOfB < fOfC); // real assert
            }

            int countToCheck = 0;

            while ((Math.Abs((a + c) / 2.0 - b) < inputTol)) //!!!can this really loop more than once?
            {
                Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "while (Math.Abs((a + c) / 2.0 - b) < inputTol)) about to {c = c + (c - a)}\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
                c = c + (c - a);
                ++countToCheck;
                SpecialFunctions.CheckCondition(countToCheck < 10);
            }
            if (countToCheck > 0)
            {
                fOfC = oneDRealFunctionInDoubleSpace(c);
            }

            Debug.WriteLine(SpecialFunctions.CreateTabString(debugCount, "NOT (Math.Abs((a + c) / 2.0 - b) < inputTol))\n", "", "a", a, fOfA, "\n", "", "b", b, fOfB, "\n", "", "c", c, fOfC, "\n"));
            bestInput = double.NaN;
            return(false);
        }