public ScenarioDescriptiveStatistics DescriptiveStatistics(TextWriter tw)
        {
            //Calculate the mean at each interval...
            int TT = T + 1;
            int D  = NodesAt(0)[0].Value.Length;

            double[,] M = new double[TT, D];
            double[,] S = new double[TT, D];

            for (int i = 0; i < TT; i++)
            {
                List <TreeNode> nodes = NodesAt(i);
                double[]        mean  = Mean(nodes);
                double[]        std   = Std(nodes);

                for (int d = 0; d < D; d++)
                {
                    M[i, d] = mean[d];
                    S[i, d] = std[d];
                }
            }

            tw.WriteLine("Mean:");
            for (int d = 0; d < D; d++)
            {
                for (int i = 0; i < TT; i++)
                {
                    tw.Write(M[i, d].ToString() + "\t");
                }

                tw.WriteLine();
            }

            tw.WriteLine("Std:");
            for (int d = 0; d < D; d++)
            {
                for (int i = 0; i < TT; i++)
                {
                    tw.Write(S[i, d].ToString() + "\t");
                }

                tw.WriteLine();
            }

            ScenarioDescriptiveStatistics sds = new ScenarioDescriptiveStatistics();

            sds.means     = M;
            sds.variances = S;
            return(sds);
        }
        public static double Distance2(ScenarioDescriptiveStatistics d1, ScenarioDescriptiveStatistics d2)
        {
            int    I    = d1.variances.GetLength(0);
            int    D    = d1.variances.GetLength(1);
            double d    = 0;
            double norm = 0;

            for (int i = 0; i < I; i++)
            {
                for (int c = 0; c < D; c++)
                {
                    if (d1.variances[i, c] > 0)
                    {
                        d    += Math.Pow((d1.variances[i, c] - d2.variances[i, c]) / d1.variances[i, c], 2);
                        norm += 1;
                    }
                }
            }

            return(Math.Sqrt(d) / Math.Sqrt(norm));
        }
        public static double Distance3(ScenarioDescriptiveStatistics d1, ScenarioDescriptiveStatistics d2)
        {
            int I = d1.variances.GetLength(0);
            int D = d1.variances.GetLength(1);

            double maxd = 0;

            for (int c = 0; c < D; c++)
            {
                double d    = 0;
                double norm = 0;
                for (int i = 0; i < I; i++)
                {
                    d    += Math.Abs(d1.variances[i, c] - d2.variances[i, c]);
                    norm += Math.Abs(d1.variances[i, c]);
                }

                maxd = Math.Max(maxd, d / norm);
            }

            return(maxd);
        }
        public static double Distance1(ScenarioDescriptiveStatistics d1, ScenarioDescriptiveStatistics d2)
        {
            int    I    = d1.variances.GetLength(0);
            int    D    = d1.variances.GetLength(1);
            double d    = 0;
            double norm = 0;

            for (int i = 0; i < I; i++)
            {
                double d0    = 0;
                double norm0 = 0;
                for (int c = 0; c < D; c++)
                {
                    d0    += Math.Pow(d1.variances[i, c] - d2.variances[i, c], 2);
                    norm0 += Math.Pow(d1.variances[i, c], 2);
                }

                d    += Math.Sqrt(d0);
                norm += Math.Sqrt(norm0);
            }

            return(d / norm);
        }