/// <summary> /// Computes the element-wise square-root value for each row / col value. /// </summary> /// <param name="source">The source Vector.</param> /// <returns>Vector.</returns> public static Vector Sqrt(this Vector source) { if (source.Length == 0) { throw new InvalidOperationException("Cannot compute square root of an empty vector."); } return(source.Each(f => System.Math.Sqrt(f), true)); }
/// <summary> /// Rescales the input vector to the specified range. /// <para>When <paramref name="minValue"/> and <paramref name="maxValue"/> are null, the vector instance min and max values are used instead.</para> /// </summary> /// <param name="v">Vector to rescale.</param> /// <param name="min">New lower bound value.</param> /// <param name="max">New upper bound value.</param> /// <param name="minValue">Lower bound value prior to rescaling.</param> /// <param name="maxValue">Upper bound value prior to rescaling.</param> /// <returns></returns> public static Vector Rescale(this Vector v, double min, double max, double?minValue = null, double?maxValue = null) { double min_tm1 = (minValue ?? v.Min()); double max_tm1 = (maxValue ?? v.Max()); Vector v_t = v.Each(d => ((max - min) * (d - min_tm1)) / (max_tm1 - min_tm1), true); return(v_t); }
/// <summary> /// Returns a softmax function vector from the supplied inputs. /// </summary> /// <param name="x"></param> /// <returns></returns> public Vector Compute(Vector x) { double max = x.Max(); Vector softmax = x.Each(v => System.Math.Exp(v - max)); double sum = softmax.Sum(); softmax = softmax.Each(s => s / sum); return softmax; }
/// <summary> /// Normalizes the values so that the sum of all values is 1. /// <para>Values should be positive prior to normalization for correctness.</para> /// </summary> /// <param name="v">Vector to normalize.</param> /// <returns>Vector.</returns> public static Vector Normalize(this Vector v) { double sum = v.Sum(); if (sum == 0) { throw new InvalidOperationException("Cannot normalize a zero sequence."); } Vector v_t = v.Each(d => d / sum, true); return(v_t); }
/// <summary>Compute probability according to multivariate Gaussian.</summary> /// <param name="x">Vector in question.</param> /// <param name="mu">Mean.</param> /// <param name="sigma">diag(covariance)</param> /// <returns>Probability.</returns> public double Normal(Vector x, Vector mu, Vector sigma) { var p = 1 / sqrt(pow(2 * System.Math.PI, mu.Length) * sigma.Prod()); var exp = -0.5d * ((x - mu) * sigma.Each(d => 1 / d, true)).Dot(x - mu); var e_exp = pow(System.Math.E, exp); return p * e_exp; }
/// <summary> /// Compute probability according to multivariate Gaussian /// </summary> /// <param name="x">Vector in question</param> /// <param name="mu">Mean</param> /// <param name="sigma">diag(covariance)</param> /// <returns>Probability</returns> private double Normal(Vector x, Vector mu, Vector sigma) { // 1 / (2pi)^(2/D) where D = length of sigma var one_over_2pi = 1 / System.Math.Pow(2 * System.Math.PI, 2 / sigma.Length); // 1 / sqrt(det(sigma)) where det(sigma) is the product of the diagonals var one_over_det_sigma = System.Math.Sqrt(sigma.Aggregate(1d, (a, i) => a *= i)); // -.5 (x-mu).T sigma^-1 (x-mu) I have taken some liberties ;) var exp = -0.5d * ((x - mu) * sigma.Each(d => 1 / d, true)).Dot(x - mu); // e^(exp) var e_exp = System.Math.Pow(System.Math.E, exp); var result = one_over_2pi * one_over_det_sigma * e_exp; return result; }
/// <summary> /// Initializes the selection function. /// </summary> /// <param name="alpha">Alpha vector</param> /// <param name="gradient">Gradient vector.</param> public void Initialize(Vector alpha, Vector gradient) { alpha.Each((d) => 0, false); gradient.Each((d) => -1, false); }