Ejemplo n.º 1
0
        private static void coxProportionalHazards()
        {
            // Let's say we have the following survival problem. Each row in the table below
            // represents a patient under care in a hospital. The first colum represents their
            // age (a single feature, but there could have been many like age, height, weight,
            // etc), the time until an event has happened (like, for example, unfortunatey death)
            // and the event outcome (i.e. what has exactly happened after this amount of time,
            // has the patient died or did he simply leave the hospital and we couldn't get more
            // data about him?)

            object[,] data =
            {
                //    input         time until           outcome
                // (features)     event happened     (what happened?)
                { 50,  1, SurvivalOutcome.Censored },
                { 70,  2, SurvivalOutcome.Failed   },
                { 45,  3, SurvivalOutcome.Censored },
                { 35,  5, SurvivalOutcome.Censored },
                { 62,  7, SurvivalOutcome.Failed   },
                { 50, 11, SurvivalOutcome.Censored },
                { 45,  4, SurvivalOutcome.Censored },
                { 57,  6, SurvivalOutcome.Censored },
                { 32,  8, SurvivalOutcome.Censored },
                { 57,  9, SurvivalOutcome.Failed   },
                { 60, 10, SurvivalOutcome.Failed   },
            }; // Note: Censored means that we stopped recording data for that person,
               // so we do not know what actually happened to them, except that things
               // were going fine until the point in time appointed by "time to event"

            // Parse the data above
            double[][]        inputs = data.GetColumn(0).ToDouble().ToJagged();
            double[]          time   = data.GetColumn(1).ToDouble();
            SurvivalOutcome[] output = data.GetColumn(2).To <SurvivalOutcome[]>();

            // Create a new PH Newton-Raphson learning algorithm
            var teacher = new ProportionalHazardsNewtonRaphson()
            {
                ComputeBaselineFunction = true,
                ComputeStandardErrors   = true,
                MaxIterations           = 100
            };

            // Use the learning algorithm to infer a Proportional Hazards model
            ProportionalHazards regression = teacher.Learn(inputs, time, output);

            // Use the regression to make predictions (problematic)
            SurvivalOutcome[] prediction = regression.Decide(inputs);

            // Use the regression to make score estimates
            double[] score = regression.Score(inputs);

            // Use the regression to make probability estimates
            double[] probability = regression.Probability(inputs);
        }
        /// <summary>
        /// Learns a model that can map the given inputs to the given outputs.
        /// </summary>
        /// <param name="x">The model inputs.</param>
        /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param>
        /// <param name="weights">The weight of importance for each input-output pair.</param>
        /// <returns>
        /// A model that has learned how to produce <paramref name="y" /> given <paramref name="x" />.
        /// </returns>
        public ProportionalHazards Learn(Tuple <double[], double>[] x, int[] y, double[] weights = null)
        {
            var learning = new ProportionalHazardsNewtonRaphson(regression);

            Array.Clear(regression.Coefficients, 0, regression.Coefficients.Length);


            learning.Iterations = Iterations;
            learning.Tolerance  = Tolerance;

            learning.Learn(x, y, weights);

            // Check if the full model has converged
            computeInformation();
            innerComputed = false;

            return(regression);
        }
Ejemplo n.º 3
0
        private bool compute()
        {
            var learning = new ProportionalHazardsNewtonRaphson(regression);

            Array.Clear(regression.Coefficients, 0, regression.Coefficients.Length);


            learning.MaxIterations = Iterations;
            learning.Tolerance     = Tolerance;

            learning.Learn(inputData, timeData, censorData);

            // Check if the full model has converged
            bool converged = learning.CurrentIteration < Iterations;


            computeInformation();

            innerComputed = false;

            // Returns true if the full model has converged, false otherwise.
            return(converged);
        }
        private void computeInner()
        {
            if (inputCount <= 2)
            {
                return;
            }

            // Perform likelihood-ratio tests against diminished nested models
            ProportionalHazards innerModel            = new ProportionalHazards(inputCount - 1);
            ProportionalHazardsNewtonRaphson learning = new ProportionalHazardsNewtonRaphson(innerModel);

            for (int i = 0; i < inputCount; i++)
            {
                // Create a diminished inner model without the current variable
                double[][] data = inputData.RemoveColumn(i);

#if DEBUG
                if (data[0].Length == 0)
                {
                    throw new Exception();
                }
#endif

                Array.Clear(innerModel.Coefficients, 0, inputCount - 1);

                learning.Iterations = Iterations;
                learning.Tolerance  = Tolerance;

                learning.Learn(data, timeData, censorData);


                double ratio = 2.0 * (logLikelihood - innerModel.GetPartialLogLikelihood(data, timeData, censorData));
                ratioTests[i] = new ChiSquareTest(ratio, 1);
            }

            innerComputed = true;
        }
Ejemplo n.º 5
0
        public void doc_learn()
        {
            // Data from: http://www.sph.emory.edu/~cdckms/CoxPH/prophaz2.html / http://statpages.info/prophaz2.html

            #region doc_learn
            // Let's say we have the following survival problem. Each row in the
            // table below represents a patient under care in a hospital. The first
            // colum represents their age (a single feature, but there could have
            // been many like age, height, weight, etc), the time until an event
            // has happened (like, for example, unfortunatey death) and the event
            // outcome (i.e. what has exactly happened after this amount of time,
            // has the patient died or did he simply leave the hospital and we
            // couldn't get more data about him?)

            object[,] data =
            {
                //    input         time until           outcome
                // (features)     event happened     (what happened?)
                { 50,  1, SurvivalOutcome.Censored },
                { 70,  2, SurvivalOutcome.Failed   },
                { 45,  3, SurvivalOutcome.Censored },
                { 35,  5, SurvivalOutcome.Censored },
                { 62,  7, SurvivalOutcome.Failed   },
                { 50, 11, SurvivalOutcome.Censored },
                { 45,  4, SurvivalOutcome.Censored },
                { 57,  6, SurvivalOutcome.Censored },
                { 32,  8, SurvivalOutcome.Censored },
                { 57,  9, SurvivalOutcome.Failed   },
                { 60, 10, SurvivalOutcome.Failed   },
            }; // Note: Censored means that we stopped recording data for that person,
               // so we do not know what actually happened to them, except that things
               // were going fine until the point in time appointed by "time to event"

            // Parse the data above
            double[][]        inputs = data.GetColumn(0).ToDouble().ToJagged();
            double[]          time   = data.GetColumn(1).ToDouble();
            SurvivalOutcome[] output = data.GetColumn(2).To <SurvivalOutcome[]>();

            // Create a new PH Newton-Raphson learning algorithm
            var teacher = new ProportionalHazardsNewtonRaphson()
            {
                ComputeBaselineFunction = true,
                ComputeStandardErrors   = true,
                MaxIterations           = 100
            };

            // Use the learning algorithm to infer a Proportional Hazards model
            ProportionalHazards regression = teacher.Learn(inputs, time, output);

            // Use the regression to make predictions (problematic)
            SurvivalOutcome[] prediction = regression.Decide(inputs);

            // Use the regression to make score estimates
            double[] score = regression.Score(inputs);

            // Use the regression to make probability estimates
            double[] probability = regression.Probability(inputs);
            #endregion

            string   str      = probability.ToCSharp();
            double[] expected = { 0.640442743460877, 1206.22665747906, 0.0972172106179122, 0.00224010744584941, 59.0812230260151, 0.640442743460877, 0.0972172106179122, 8.9683453534747, 0.000722814003252998, 8.9683453534747, 27.7942279934438 };
            Assert.IsTrue(expected.IsEqual(probability, rtol: 1e-8));
        }