public GPOut Compute(Matrix <double> mTr, Matrix <double> mTeTr, Vector <double> vTrY) { var Mtr = (mTr).RowCount; var gpOut = new GPOut(); var Kte = _kernel.Main(DistanceMatrix.Instance.Matrix, _length); if (Mtr > 0) { var Kxx_p_noise = _kernel.Main(mTr, _length); for (int i = 0; i < Mtr; i++) { Kxx_p_noise[i, i] += _noise; } var svd1 = Kxx_p_noise.Svd(); for (int i = 0; i < Mtr; i++) { svd1.S[i] = svd1.S[i] > Double.Epsilon ? 1.0 / svd1.S[i] : 0; } var tmp = _kernel.Main(mTeTr, _length) * svd1.U; gpOut.mu = tmp * (svd1.S.PointwiseMultiply(svd1.U.Transpose() * (vTrY))); var cov = tmp * Matrix <double> .Build.DenseOfDiagonalVector(svd1.S.PointwiseSqrt()); cov = cov * cov.Transpose(); cov = Kte /*.SubMatrix(0,100,0,100) */ - cov; var svd2 = cov.Svd(); for (int i = 0; i < Mte; i++) { if (svd2.S[i] < Double.Epsilon) { svd2.S[i] = 0.0; } } gpOut.proj = svd2.U * Matrix <double> .Build.DenseOfDiagonalVector(svd2.S.PointwiseSqrt()); gpOut.sd95 = 1.98 * (gpOut.proj * gpOut.proj.Transpose()).Diagonal().PointwiseSqrt(); } else { gpOut.sd95 = 1.98 * (Kte.Diagonal()).PointwiseSqrt(); var svd = Kte.Svd(); gpOut.proj = svd.U * Matrix <double> .Build.DenseOfDiagonalVector(svd.S.PointwiseSqrt()); gpOut.mu = Vector <double> .Build.DenseOfArray(Enumerable.Range(0, Mte).Select(_ => 0d).ToArray()); } return(gpOut); }
public GPOut Predict(double TestPointX, Svd <double> svd1 = null, int results = 1) { //if(results< TrainingPointsX.Count) // results = TrainingPointsX.Count; double[] TestPointsX; if (results > 1) { TestPointsX = MathNet.Numerics.Generate.LinearSpaced(results, TrainingPointsX.Min(), TestPointX); } else { TestPointsX = new double[] { TestPointX } }; if (Kte == null || results != Kte.ColumnCount) { if (results == 1) { Kte = kernel.Main(Matrix <double> .Build.Dense(1, 1), length); } else { Kte = kernel.Main(DistanceMatrix.Instance.Matrix.SubMatrix(0, results, 0, results), length); } } var gpOut = new GPOut(); Svd <double> svd; if (TrainingPointsX.Count > 0 && svd1 != null /* && TestPointsX.Length>= TrainingPointsX.Count*/) { (gpOut.mu, svd) = MathHelper.Evaluate(TestPointsX, TrainingPointsY.ToArray(), TrainingPointsX.ToArray(), kernel, length, svd1, Kte); } else { gpOut.mu = Vector <double> .Build.DenseOfArray(Enumerable.Range(0, results).Select(_ => 0d).ToArray()); //gpOut.sd95 = 1.98 * (Kte.Diagonal()).PointwiseSqrt(); svd = Kte.Svd(); } gpOut.proj = svd.U * Matrix <double> .Build.DenseOfDiagonalVector(svd.S.PointwiseSqrt()); gpOut.sd95 = 1.98 * (gpOut.proj * gpOut.proj.Transpose()).Diagonal().PointwiseSqrt(); gpOut.X = TestPointsX; return(gpOut); } }
public static (Vector <double>, Svd <double>) Evaluate(double[] testPointsX, double[] trainingPointsX, double[] trainingPointsY, IMatrixkernel kernel, double length, Svd <double> svd1, Matrix <double> Kte) { var gpOut = new GPOut(); Svd <double> svd; var dmTeTr = MathHelper.ComputeDistanceMatrix(testPointsX, trainingPointsX.ToArray()); var tmp = kernel.Main(dmTeTr, length) * svd1.U; Vector <double> v = Vector <double> .Build.DenseOfEnumerable(trainingPointsY); var mu = tmp * (svd1.S.PointwiseMultiply(svd1.U.Transpose() * v)); var cov = tmp * Matrix <double> .Build.DenseOfDiagonalVector(svd1.S.PointwiseSqrt()); cov = cov * cov.Transpose(); cov = Kte - cov; svd = cov.Svd(); for (int i = 0; i < svd.S.Count; i++) { svd.S[i] = svd.S[i] < Double.Epsilon ? 0 : svd.S[i]; } return(mu, svd); }
public GPOut Predict(double[] trainingPointsX, double[] trainingPointsY, params double[] TestPointsX) { if (TestPointsX.Count() == 1) { Kte = kernel.Main(Matrix <double> .Build.Dense(1, 1), length); } else { Kte = kernel.Main(DistanceMatrix.Instance.Matrix.SubMatrix(0, TestPointsX.Count(), 0, TestPointsX.Count()), length); } var svd1 = Task.Run(() => trainingPointsX.Length > 0 ? Update(trainingPointsX, trainingPointsY) : GetDefaultSvd()); var gpOut = new GPOut(); Svd <double> svd; (gpOut.mu, svd) = Task.Run(() => { if (trainingPointsX.Count() > 0) { return(Evaluate(TestPointsX, trainingPointsX.ToArray(), trainingPointsY.ToArray(), kernel, length, svd1.Result, Kte)); } else { return(Vector <double> .Build.DenseOfArray(Enumerable.Range(0, TestPointsX.Count()).Select(_ => 0d).ToArray()), Kte.Svd()); //gpOut.sd95 = 1.98 * (Kte.Diagonal()).PointwiseSqrt(); } }).Result; gpOut.proj = svd.U * Matrix <double> .Build.DenseOfDiagonalVector(svd.S.PointwiseSqrt()); gpOut.sd95 = 1.98 * (gpOut.proj * gpOut.proj.Transpose()).Diagonal().PointwiseSqrt(); gpOut.X = TestPointsX; return(gpOut); }
public static IEnumerable <KeyValuePair <DateTime, Tuple <double, double>[]> > GetPositionsAndVelocities(this GPOut gpout, DateTime start, double control) { double lasttime = 0; double lastvariance; double lastposition; double velocity = 0; using (IEnumerator <double> i2 = gpout.mu.ToList().GetEnumerator()) //using (IEnumerator<double> i1 = gpout.X.ToList().GetEnumerator()) using (IEnumerator <double> i3 = gpout.sd95.ToList().GetEnumerator()) foreach (double time in gpout.X.ToArray()) { lastposition = i2.Current; //lasttime = i1.Current; lastvariance = i3.Current; /* while (i1.MoveNext() &&*/ i2.MoveNext(); i3.MoveNext(); //{ //double time=i1.Current; if ((time - lasttime) != 0) { velocity = (i2.Current - lastposition) / (time - lasttime); } yield return(new KeyValuePair <DateTime, Tuple <double, double>[]>(start + TimeSpan.FromSeconds(time), new[] { Tuple.Create(i2.Current + control, i3.Current), Tuple.Create(velocity, i3.Current) //}); })); lastposition = i2.Current; lasttime = time;// i1.Current; lastvariance = i3.Current; } }
public static IEnumerable <KeyValuePair <DateTime, Tuple <double, double> > > GetVelocities(this GPOut gpout, DateTime dt) { double lastposition = 0; double lasttime = 0; double lastvariance = 0; using (var i1 = gpout.X.ToList().GetEnumerator()) using (var i2 = gpout.mu.ToList().GetEnumerator()) using (var i3 = gpout.sd95.ToList().GetEnumerator()) { lastposition = i2.Current; lasttime = i1.Current; lastvariance = i3.Current; while (i1.MoveNext() && i2.MoveNext() && i3.MoveNext()) { var velocity = (i2.Current - lastposition) / TimeSpan.FromDays(i1.Current - lasttime).TotalSeconds; yield return(new KeyValuePair <DateTime, Tuple <double, double> >(dt + TimeSpan.FromSeconds(i1.Current), Tuple.Create(i2.Current, i3.Current))); lastposition = i2.Current; lasttime = i1.Current; lastvariance = i3.Current; } } }
public static IEnumerable <KeyValuePair <DateTime, Tuple <double, double> > > GetPositions(this GPOut gpout, DateTime dt) { using (var i1 = gpout.X.ToList().GetEnumerator()) using (var i2 = gpout.mu.ToList().GetEnumerator()) using (var i3 = gpout.sd95.ToList().GetEnumerator()) { while (i1.MoveNext() && i2.MoveNext() && i3.MoveNext()) { yield return(new KeyValuePair <DateTime, Tuple <double, double> >(dt + TimeSpan.FromSeconds(i1.Current), Tuple.Create(i2.Current, i3.Current))); } } }
public static KeyValuePair <DateTime, Tuple <double, double> > Last(this GPOut gpout, DateTime dt) { return(new KeyValuePair <DateTime, Tuple <double, double> > (dt + TimeSpan.FromDays(gpout.X.Last()), Tuple.Create(gpout.mu.Last(), gpout.sd95.Last()))); }