public void Update(double omega, double[] alpha, double[] rate) { this.omega = omega; Blas.Dcopy(ndim, alpha, this.alpha); Blas.Dcopy(ndim, rate, this.rate); TransformCanonicalForm(); SetScaledRate(); }
public HErlangParam(int m, double omega, double[] alpha, int[] shape, double[] rate) { this.m = m; this.alpha = new double[m]; this.shape = new int[m]; Array.Copy(shape, this.shape, m); this.rate = new double[m]; Blas.Dcopy(m, alpha, this.alpha); Blas.Dcopy(m, rate, this.rate); }
public void CopyTo(IModelParam other) { OriginalParam v = other as OriginalParam; if (n == v.n) { Blas.Dcopy(n, param, v.param); } else { throw new InvalidCastException(); } }
public void CopyTo(IModelParam p) { HErlangParam v = p as HErlangParam; if (m == v.m) { v.omega = omega; Blas.Dcopy(m, alpha, v.alpha); Array.Copy(shape, v.shape, m); Blas.Dcopy(m, rate, v.rate); } else { throw new InvalidCastException(); } }
public void CopyTo(IModelParam p) { CPHParam v = p as CPHParam; if (ndim == v.ndim) { v.omega = omega; v.lambda = lambda; Blas.Dcopy(ndim, alpha, v.alpha); Blas.Dcopy(ndim, rate, v.rate); Blas.Dcopy(ndim, scaledRate, v.scaledRate); } else { throw new InvalidCastException(); } }
public CPHDist(int ndim, GetParamVec alphaFunc, GetParamVec rateFunc, GetParam lambdaFunc, GetParamVec scaledRateFunc, double epsi) { this.ndim = ndim; this.alphaFunc = alphaFunc; this.rateFunc = rateFunc; this.lambdaFunc = lambdaFunc; this.epsi = epsi; cache_x = new double[ndim]; cache_t = 0.0; Blas.Dcopy(ndim, Alpha, cache_x); max_right = 10; prob = new double[max_right + 1]; unif = new CPHUniformization(ndim, lambdaFunc, scaledRateFunc); }
private void CalcProb(double t) { double dt; if (t < cache_t) { dt = t; Blas.Dcopy(ndim, Alpha, cache_x); } else { dt = t - cache_t; } int right = AllocProb(dt); unif.DoForward(dt, cache_x, right, prob); cache_t = t; }
// 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); }
private void SetScaledRate() { lambda = Blas.Amax(ndim, rate) * factor; Blas.Dcopy(ndim, rate, scaledRate); Blas.Dscal(ndim, 1.0 / lambda, scaledRate); }
public void ClearCache() { // System.Console.WriteLine("clear cache"); cache_t = 0.0; Blas.Dcopy(ndim, Alpha, cache_x); }
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); }