public void KaplanMeierTest1()
        {
            // Example from
            // http://sas-and-r.blogspot.fr/2010/05/example-738-kaplan-meier-survival.html

            double[]          times;
            SurvivalOutcome[] censor;
            CreateExample1(out times, out censor);

            var distribution = new EmpiricalHazardDistribution(SurvivalEstimator.KaplanMeier);

            Assert.AreEqual(SurvivalEstimator.KaplanMeier, distribution.Estimator);

            distribution.Fit(times, new EmpiricalHazardOptions(HazardEstimator.KaplanMeier, censor));

            int[]    t = { 1, 2, 3, 4, 6, 8, 9, 12, 14, 20 };
            double[] e = { 0.889, 0.833, 0.774, 0.714, 0.649, 0.577, 0.505, 0.421, 0.337, 0.168 };

            double[] actual = t.ToDouble().Apply(distribution.ComplementaryDistributionFunction);

            for (int i = 0; i < e.Length; i++)
            {
                Assert.AreEqual(e[i], actual[i], 1e-3);
            }

            // Assert.AreEqual(11.177, distribution.Mean);
            Assert.AreEqual(12, distribution.Median, 1e-5);
        }
        public void LeukemiaExampleCensoring_KaplanMeier_FlemingHarrington()
        {
            // The following are times of remission (weeks) for 21 leukemia
            // patients receiving control treatment (Table 1.1 of Cox & Oakes):

            double[] t = { 6, 6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35 };
            int[]    c = { 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 };

            var distribution = new EmpiricalHazardDistribution(SurvivalEstimator.FlemingHarrington);

            distribution.Fit(t, new SurvivalOptions {
                Outcome = c.To <SurvivalOutcome[]>()
            });

            int[] intervals = { 6, 7, 9, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 34, 35 };

            double[] expected =
            {
                0.8571, 0.8067, 0.8067, 0.7529, 0.7529, 0.6902,
                0.6275, 0.6275, 0.6275, 0.6275, 0.5378, 0.4482,
                0.4482, 0.4482, 0.4482, 0.4482
            };

            for (int i = 0; i < intervals.Length; i++)
            {
                double x      = intervals[i];
                double actual = distribution.ComplementaryDistributionFunction(x);

                double e = expected[i];
                Assert.AreEqual(e, actual, 0.1);
            }
        }
        public void LeukemiaExample_KaplanMeier()
        {
            // The following are times of remission (weeks) for 21 leukemia
            // patients receiving control treatment (Table 1.1 of Cox & Oakes):
            // http://www-personal.umich.edu/~yili/lect2notes.pdf

            double[] t = { 1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23 };


            var distribution = new EmpiricalHazardDistribution(SurvivalEstimator.KaplanMeier);

            distribution.Fit(t, new EmpiricalHazardOptions {
                Estimator = HazardEstimator.KaplanMeier
            });

            Assert.AreEqual(1, distribution.Survivals[0]);
            Assert.AreEqual(0.905, distribution.Survivals[1], 1e-3);
            Assert.AreEqual(0.809, distribution.Survivals[2], 1e-3);
            Assert.AreEqual(0.762, distribution.Survivals[3], 1e-3);

            /*
             * http://statpages.org/prophaz2.html
             *  1, 1
             *  1, 1
             *  2, 1
             *  2, 1
             *  3, 1
             *  4, 1
             *  4, 1
             *  5, 1
             *  5, 1
             *  8, 1
             *  8, 1
             *  8, 1
             *  8, 1
             *  11, 1
             *  11, 1
             *  12, 1
             *  12, 1
             *  15, 1
             *  17, 1
             *  22, 1
             *  23, 1
             */
        }
        public void KaplanMeierTest1()
        {
            // Example from
            // http://sas-and-r.blogspot.fr/2010/05/example-738-kaplan-meier-survival.html

            double[] times;
            SurvivalOutcome[] censor;
            CreateExample1(out times, out censor);

            var distribution = new EmpiricalHazardDistribution(SurvivalEstimator.KaplanMeier);

            Assert.AreEqual(SurvivalEstimator.KaplanMeier, distribution.Estimator);

            distribution.Fit(times, new EmpiricalHazardOptions(HazardEstimator.KaplanMeier, censor));

            int[] t = { 1, 2, 3, 4, 6, 8, 9, 12, 14, 20 };
            double[] e = { 0.889, 0.833, 0.774, 0.714, 0.649, 0.577, 0.505, 0.421, 0.337, 0.168 };

            double[] actual = t.ToDouble().Apply(distribution.ComplementaryDistributionFunction);

            for (int i = 0; i < e.Length; i++)
                Assert.AreEqual(e[i], actual[i], 1e-3);

            // Assert.AreEqual(11.177, distribution.Mean);
            Assert.AreEqual(12, distribution.Median, 1e-5);
        }
        public void LeukemiaExampleCensoring_KaplanMeier_FlemingHarrington()
        {
            // The following are times of remission (weeks) for 21 leukemia
            // patients receiving control treatment (Table 1.1 of Cox & Oakes):

            double[] t = { 6, 6, 6, 6, 7, 9, 10, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 32, 34, 35 };
            int[] c = { 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 };

            var distribution = new EmpiricalHazardDistribution(SurvivalEstimator.FlemingHarrington);

            distribution.Fit(t, new SurvivalOptions { Outcome = c.To<SurvivalOutcome[]>() });

            int[] intervals = { 6, 7, 9, 10, 11, 13, 16, 17, 19, 20, 22, 23, 25, 32, 34, 35 };

            double[] expected = 
            {
                0.8571 , 0.8067, 0.8067, 0.7529, 0.7529, 0.6902, 
                0.6275, 0.6275, 0.6275, 0.6275, 0.5378, 0.4482,
                0.4482, 0.4482, 0.4482, 0.4482
            };

            for (int i = 0; i < intervals.Length; i++)
            {
                double x = intervals[i];
                double actual = distribution.ComplementaryDistributionFunction(x);

                double e = expected[i];
                Assert.AreEqual(e, actual, 0.1);
            }
        }
        public void LeukemiaExample_KaplanMeier()
        {
            // The following are times of remission (weeks) for 21 leukemia
            // patients receiving control treatment (Table 1.1 of Cox & Oakes):
            // http://www-personal.umich.edu/~yili/lect2notes.pdf

            double[] t = { 1, 1, 2, 2, 3, 4, 4, 5, 5, 8, 8, 8, 8, 11, 11, 12, 12, 15, 17, 22, 23 };


            var distribution = new EmpiricalHazardDistribution(SurvivalEstimator.KaplanMeier);

            distribution.Fit(t, new EmpiricalHazardOptions { Estimator = HazardEstimator.KaplanMeier });

            Assert.AreEqual(1, distribution.Survivals[0]);
            Assert.AreEqual(0.905, distribution.Survivals[1], 1e-3);
            Assert.AreEqual(0.809, distribution.Survivals[2], 1e-3);
            Assert.AreEqual(0.762, distribution.Survivals[3], 1e-3);

            /*
             http://statpages.org/prophaz2.html
                1, 1 
                1, 1 
                2, 1 
                2, 1
                3, 1
                4, 1
                4, 1
                5, 1
                5, 1 
                8, 1 
                8, 1 
                8, 1 
                8, 1 
                11, 1
                11, 1 
                12, 1 
                12, 1 
                15, 1 
                17, 1 
                22, 1 
                23, 1
             */
        }