/// <summary> /// Add two positive-definite matrices. /// </summary> /// <param name="a">First matrix.</param> /// <param name="b">Second matrix.</param> /// <returns>Their sum.</returns> public static PositiveDefiniteMatrix operator +(PositiveDefiniteMatrix a, PositiveDefiniteMatrix b) { PositiveDefiniteMatrix result = (PositiveDefiniteMatrix)a.Clone(); result.SetToSum(result, b); return(result); }
/// <summary> /// Adds a weighted observation /// </summary> /// <param name="x"></param> /// <param name="weight"></param> public void Add(Matrix x, double weight) { // new_mean = mean + w/(count + w)*(x - mean) // new_var = count/(count + w)*(var + w/(count+w)*(x-mean)*(x-mean)') Count += weight; if (Count == 0) { return; } diff.SetToDifference(x, Mean); double s = weight / Count; Mean.SetToSum(1, Mean, s, diff); diff.SetToElementwiseProduct(diff, diff); Variance.SetToSum(1 - s, Variance, s * (1 - s), diff); }
/// <summary> /// Adds a noisy observation. /// </summary> /// <param name="x"></param> /// <param name="noiseVariance"></param> /// <param name="weight"></param> public void Add(Vector x, PositiveDefiniteMatrix noiseVariance, double weight) { // new_cov = count/(count + w)*(cov + w/(count+w)*(x-mean)*(x-mean)') + w/(count+w)*noiseVariance Add(x, weight); if (count == 0) { return; } double s = weight / count; cov.SetToSum(1, cov, s, noiseVariance); }