Esempio n. 1
0
        private static double GetLikelihood_Normal(List <RDataRecord> data, double[] beta_hat)
        {
            int    N = data.Count;
            int    k = beta_hat.Length;
            double residual_sum_of_squares = 0;

            double[] y = new double[N];
            for (int i = 0; i < N; ++i)
            {
                y[i] = data[i].YValue;
            }

            double sigma = StdDev.GetStdDev(y, Mean.GetMean(y)) / (N - k - 1);

            for (int i = 0; i < N; ++i)
            {
                double      linear_predictor = 0;
                RDataRecord rec = data[i];
                for (int j = 0; j < k; ++j)
                {
                    linear_predictor += rec.data[j] * beta_hat[j];
                }
                double residual = rec.YValue - linear_predictor;
                residual_sum_of_squares += residual * residual;
            }

            return(System.Math.Exp(-residual_sum_of_squares / (2 * sigma)) / System.Math.Sqrt(2 * System.Math.PI * sigma));
        }
Esempio n. 2
0
        public virtual Glm CreateSolver(IList <RDataRecord> records)
        {
            RDataRecord rec = records[0];
            int         d   = rec.Dimension;
            int         m   = records.Count;

            int n = 0;
            Dictionary <int, Dictionary <int, int> > levelOrg;

            Analyze(records, out levelOrg, out n);

            double[,] A = new double[m, n];
            double[] b = new double[m];

            for (int i = 0; i < m; ++i)
            {
                rec = records[i];
                int index = 0;
                for (int j = 0; j < d; ++j)
                {
                    A[i, index++] = rec.data[j];
                }
                b[i] = rec.YValue;
            }

            return(new GlmIrlsQrNewton(mDistributionFamily, A, b));
        }
Esempio n. 3
0
        private static void Analyze(IList <RDataRecord> records, out Dictionary <int, Dictionary <int, int> > levelOrg, out int n)
        {
            RDataRecord rec = records[0];
            int         m   = records.Count;

            n        = 1;
            levelOrg = new Dictionary <int, Dictionary <int, int> >();
            for (int i = 1; i < n; ++i)
            {
                n++;
            }
        }
Esempio n. 4
0
        public static double Predict(Glm solver, IList <RDataRecord> records, RDataRecord input_0)
        {
            int d = input_0.Dimension;

            int n = 0;
            Dictionary <int, Dictionary <int, int> > levelOrg;

            Analyze(records, out levelOrg, out n);

            double[] x = new double[n];

            int index = 0;

            for (int j = 0; j < d; ++j)
            {
                x[index++] = input_0.data[j];
            }

            return(solver.Predict(x));
        }