Ejemplo n.º 1
0
        private int AllocProb(double t)
        {
            int right = PoiDist.GetRightBound(Lambda * t, epsi);

            if (right > max_right)
            {
                max_right = right;
                prob      = new double[right + 1];
            }
            return(right);
        }
Ejemplo n.º 2
0
        public double Emstep(SRMData data)
        {
            int dsize = data.Size;

            double[] time = data.Time;
            int[]    num  = data.Fault;
            int[]    type = data.Type;

            int    x;
            double t, llf, tmpv;

            int right = PoiDist.GetRightBound(param.Lambda * NMath.Max(time), epsi);

            double[]   prob = new double[right + 2];
            double[][] vc   = Array2.Create(right + 2, ndim);

            // initialize for estep
            eno = 0.0;
            Blas.Fill(ndim, eb, 0.0);
            Blas.Fill(ndim, eb2, 0.0);
            Blas.Fill(ndim * 2, en, 0.0);

            // backward: compute eb
            Blas.Fill(ndim, vb[0], 1.0);
            Blas.Fill(ndim, vb2[0], 0.0);
            vb2[0][ndim - 1] = param.Rate[ndim - 1];

            llf = 0.0;
            for (int k = 1; k <= dsize; k++)
            {
                t = time[k - 1];              // dat.getTime(k);
                x = num[k - 1];               // dat.getNumber(k);

                Blas.Dcopy(ndim, vb[k - 1], vb[k]);
                cuni.DoBackward(t, vb[k], right, prob);
                if (x != 0)
                {
                    Blas.Dcopy(ndim, vb[k - 1], tmp);
                    Blas.Daxpy(ndim, -1.0, vb[k], tmp);
                    blf[k] = Blas.Ddot(ndim, param.Alpha, tmp);
                    llf   += x * NMath.Log(param.Omega * blf[k]) - NMath.Lgamma(x + 1);

                    eno += x;
                    Blas.Daxpy(ndim, x / blf[k], tmp, eb);
                }
                else
                {
                    blf[k] = 1.0;                     // to avoid NaN
                }

                Blas.Dcopy(ndim, vb2[k - 1], vb2[k]);
                cuni.DoBackward(t, vb2[k], right, prob);
                if (type[k - 1] == 1)               // (dat.getType(k) == 1)
                {
                    blf2[k] = Blas.Ddot(ndim, param.Alpha, vb2[k]);
                    llf    += NMath.Log(param.Omega * blf2[k]);
                    eno    += 1.0;
                    Blas.Daxpy(ndim, 1.0 / blf2[k], vb2[k], eb2);
                }
            }
            barblf = Blas.Ddot(ndim, param.Alpha, vb[dsize]);
            llf   += -param.Omega * (1.0 - barblf);
            Blas.Daxpy(ndim, param.Omega, vb[dsize], eb);

            // compute pi2
            tmpv = 0.0;
            for (int i = 0; i < ndim - 1; i++)
            {
                tmpv  += param.Alpha[i];
                pi2[i] = tmpv / param.Rate[i];
            }
            pi2[ndim - 1] = 1.0 / param.Rate[ndim - 1];

            // sojourn:
            Blas.Fill(ndim, tmp, 0.0);
            Blas.Daxpy(ndim, -num[dsize - 1] / blf[dsize] + param.Omega, pi2, tmp);
            if (type[dsize - 1] == 1)
            {
                Blas.Daxpy(ndim, 1.0 / blf2[dsize], param.Alpha, tmp);
            }
            cuni.DoSojournForward(time[dsize - 1], tmp, vb2[dsize - 1], h0, right, prob, vc);
            Blas.Daxpy(ndim * 2, 1.0, h0, en);
            for (int k = dsize - 1; k >= 1; k--)
            {
                Blas.Daxpy(ndim, num[k] / blf[k + 1] - num[k - 1] / blf[k], pi2, tmp);
                if (type[k - 1] == 1)
                {
                    Blas.Daxpy(ndim, 1.0 / blf2[k], param.Alpha, tmp);
                }
                cuni.DoSojournForward(time[k - 1], tmp, vb2[k - 1], h0, right, prob, vc);
                Blas.Daxpy(ndim * 2, 1.0, h0, en);
            }

            /* concrete algorithm: M-step */
            for (int i = 0; i < ndim - 1; i++)
            {             // <-- not <=ndim!
                ey[i] = param.Rate[i]
                        * (en[2 * i + 1] + eb[i + 1] * pi2[i])
                        / (en[2 * i] + eb[i] * pi2[i]);
            }
            tmpv = en[2 * (ndim - 1)] + eb[ndim - 1] * pi2[ndim - 1];
            double sum = 0.0;

            for (int i = 0; i < ndim; i++)
            {
                eb[i] = param.Alpha[i] * (eb[i] + eb2[i]);
                sum  += eb[i];
            }
            ey[ndim - 1] = sum / tmpv;
            for (int i = 0; i < ndim; i++)
            {
                eb[i] /= sum;
            }
            param.Update(eno + param.Omega * barblf, eb, ey);
            return(llf);
        }