Ejemplo n.º 1
0
        /// <summary>
        /// See <see cref="IVectorView.DotProduct(IVectorView)"/>.
        /// </summary>
        public double DotProduct(IVectorView vector)
        {
            Preconditions.CheckVectorDimensions(this, vector);

            if (vector is Vector dense)
            {
                return(SparseBlas.Ddoti(values.Length, values, indices, 0, dense.RawData, 0));
            }
            else if ((vector is SparseVector sparse) && HasSameIndexer(sparse))
            {
                return(Blas.Ddot(values.Length, this.values, 0, 1, sparse.values, 0, 1));
            }

            double sum = 0;

            for (int i = 0; i < values.Length; ++i)
            {
                sum += values[i] * vector[indices[i]];
            }
            return(sum);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Calculates the dot (or inner/scalar) product of this vector with <paramref name="vector"/>:
 /// result = sum over all i of this[i] * <paramref name="vector"/>[i]).
 /// </summary>
 /// <param name="vector">A vector with the same <see cref="Length"/> as this.</param>
 public double DotProduct(Vector vector)
 {
     Preconditions.CheckVectorDimensions(this, vector);
     return(Blas.Ddot(Length, this.data, 0, 1, vector.data, 0, 1));
 }
Ejemplo n.º 3
0
 /// <summary>
 /// See https://software.intel.com/en-us/mkl-developer-reference-fortran-dot#D4E53C70-D8FA-4095-A800-4203CAFE64FE
 /// </summary>
 public double Ddot(int n, double[] x, int offsetX, int incX, double[] y, int offsetY, int incY)
 => Blas.Ddot(ref n, ref x[offsetX], ref incX, ref y[offsetY], ref incY);
Ejemplo n.º 4
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);
        }