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); }
// markov operation public void DoBackward(double t, double[] x, int right, double[] prob) { //int right; //if (t > maxt) //{ // right = setMaxT(t); //} //else //{ // right = PoiDist.getRightBound(lambda * t, epsi); //} double weight = PoiDist.CompProb(Lambda * t, 0, right, prob); Blas.Dcopy(ndim, x, xi); Blas.Fill(ndim, x, 0.0); Blas.Daxpy(ndim, prob[0], xi, x); for (int l = 1; l <= right; l++) { Blas.Dcopy(ndim, xi, tmp); DgemvNoTrans(1.0, tmp, 0.0, xi); Blas.Daxpy(ndim, prob[l], xi, x); } Blas.Dscal(ndim, 1.0 / weight, x); }
public void DoSojournForward(double t, double[] f, double[] b, double[] h, int right, double[] prob, double[][] vc) { //int right; //if (t > maxt) //{ // right = setMaxT(t); //} //else //{ // right = PoiDist.getRightBound(lambda * t, epsi); //} double weight = PoiDist.CompProb(Lambda * t, 0, right + 1, prob); // forward and backward Blas.Fill(ndim, vc[right + 1], 0.0); Blas.Daxpy(ndim, prob[right + 1], b, vc[right + 1]); for (int l = right; l >= 1; l--) { DgemvNoTrans(1.0, vc[l + 1], 0.0, vc[l]); Blas.Daxpy(ndim, prob[l], b, vc[l]); } Blas.Dcopy(ndim, f, xi); Blas.Fill(ndim, f, 0.0); Blas.Daxpy(ndim, prob[0], xi, f); Blas.Fill(ndim * 2, h, 0.0); Dger(1.0, xi, vc[1], h); for (int l = 1; l <= right; l++) { Blas.Dcopy(ndim, xi, tmp); DgemvTrans(1.0, tmp, 0.0, xi); Blas.Daxpy(ndim, prob[l], xi, f); Dger(1.0, xi, vc[l + 1], h); } Blas.Dscal(ndim, 1.0 / weight, f); Blas.Dscal(ndim * 2, 1.0 / Lambda / weight, h); }
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); }