public void SampleSequenceIsDistributedCorrectlyDiscrete(IDiscreteDistribution distribution) { distribution.RandomSource = new SystemRandomSource(1, false); var samples = distribution.Samples().Take(NumberOfTestSamples).ToArray(); DiscreteVapnikChervonenkisTest(ErrorTolerance, ErrorProbability, samples, distribution); }
public static IDiscreteDistribution <T> Where <T>([NotNull] this IDiscreteDistribution <T> d, [NotNull] Func <T, bool> predicate) { var s = d.Support().Where(predicate).ToList(); return(s.ToWeighted(s.Select(d.Weight))); }
public static IDiscreteDistribution <TProjection> SelectMany <TPrior, TSample, TProjection>( this IDiscreteDistribution <TPrior> prior, Func <TPrior, IDiscreteDistribution <TSample> > likelihood, Func <TPrior, TSample, TProjection> projection) { var priorSupport = prior.Support().ToList(); int lcm = priorSupport .Select(a => likelihood(a).TotalWeight()) .Where(totalWeight => totalWeight != 0) .LCM(); var weights = from a in priorSupport // Iterate over support let weight = prior.Weight(a) // get weight. let probability = likelihood(a) // Get inner distribution let totalWeight = probability.TotalWeight() // Sum weight of inner distribution from support in probability.Support() // iterate inner distribution support group weight *probability.Weight(support) // "Combine Fractions" by multiplication * lcm / totalWeight by projection(a, support); var dictionary = weights.ToDictionary(group => group.Key, group => group.Sum()); return(dictionary.Keys.ToWeighted(dictionary.Values)); }
private Projected([NotNull] IDiscreteDistribution <TA> underlying, [NotNull] Func <TA, TR> projection) { this.underlying = underlying; this.projection = projection; weights = underlying.Support().GroupBy(projection, underlying.Weight) .ToDictionary(g => g.Key, g => g.Sum()); }
public static IDiscreteDistribution <T> Where <T>(this IDiscreteDistribution <T> distribution, Func <T, bool> predicate) { var validSupport = distribution.Support().Where(predicate).EnsureList(); var weights = validSupport.Select(distribution.Weight); return(validSupport.ToWeighted(weights)); }
private Conditioned(IDiscreteDistribution <T> underlying, Func <T, bool> predicate) { this.underlying = underlying; this.predicate = predicate; this.support = underlying.Support() .Where(predicate) .ToList(); }
private Projected(IDiscreteDistribution <A> underlying, Func <A, R> projection) { _underlying = underlying; _projection = projection; _weights = underlying.Support() .GroupBy(projection, underlying.Weight) .ToDictionary(group => group.Key, group => group.Sum()); }
private static void ColdSneeze() { var colds = new List <Cold> { Cold.No, Cold.Yes }; IDiscreteDistribution <Cold> cold = colds.ToWeighted(90, 10); Console.WriteLine(cold.SelectMany(SneezedGivenCold).Histogram()); }
/// <summary> /// Populates a matrix with random elements. /// </summary> /// <param name="matrix">The matrix to populate.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param> protected override void DoRandom(Matrix <double> matrix, IDiscreteDistribution distribution) { for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { matrix.At(i, j, distribution.Sample()); } } }
public static IDiscreteDistribution <TR> Distribution([NotNull] IDiscreteDistribution <TA> underlying, [NotNull] Func <TA, TR> projection) { var result = new Projected <TA, TR>(underlying, projection); if (result.Support().Count() == 1) { return(Singleton <TR> .Distribution(result.Support().First())); } return(result); }
private Combined([NotNull] IDiscreteDistribution <TA> prior, [NotNull] Func <TA, IDiscreteDistribution <TR> > likelihood) { this.prior = prior; this.likelihood = likelihood; IEnumerable <TR> q = from a in prior.Support() from b in this.likelihood(a)?.Support() select b; support = q.Distinct().ToList(); }
public static string ShowWeights <T>(this IDiscreteDistribution <T> d) where T : notnull { int labelMax = d.Support() .Select(x => x.ToString() !.Length) .Max(); return(string.Join('\n', d.Support().Select(x => $"{ToLabel(x)}:{d.Weight(x)}"))); string ToLabel(T t) => t.ToString() !.PadLeft(labelMax); }
/// <summary> /// Populates a matrix with random elements. /// </summary> /// <param name="matrix">The matrix to populate.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param> protected override void DoRandom(Matrix <Complex32> matrix, IDiscreteDistribution distribution) { CommonParallel.For( 0, matrix.RowCount, i => { for (var j = 0; j < matrix.ColumnCount; j++) { matrix.At(i, j, distribution.Sample()); } }); }
public static IDiscreteDistribution <TR> Select <TA, TR>([NotNull] this IDiscreteDistribution <TA> d, [NotNull] Func <TA, TR> projection) { var dict = d.Support() .GroupBy(projection, d.Weight) .ToDictionary(g => g.Key, g => g.Sum()); var rs = dict.Keys.ToList(); return(Projected <int, TR> .Distribution( WeightedInteger.Distribution( rs.Select(r => dict[r])), i => rs[i])); }
public static string ShowWeights <T>([NotNull] this IDiscreteDistribution <T> d) { int labelMax = d.Support() .Select(x => x.ToString().Length) .Max(); return(string.Join("\n", d.Support() .Select(s => $"{ToLabel(s)}:{d.Weight(s)}"))); string ToLabel(T t) { return(t.ToString().PadLeft(labelMax)); } }
public static IDiscreteDistribution <TResult> Select <TSource, TResult>( this IDiscreteDistribution <TSource> distribution, Func <TSource, TResult> projection) { var dictionary = distribution.Support() .GroupBy(projection, distribution.Weight) .ToDictionary(group => group.Key, group => group.Sum()); var keys = dictionary.Keys.EnsureList(); var values = keys.Select(key => dictionary[key]); var weighted = WeightedInteger.Distribution(values.ToArray()); return(Projected <int, TResult> .Distribution(weighted, i => keys[i])); }
/// <summary> /// Generates a vector with random elements /// </summary> /// <param name="length">Number of elements in the vector.</param> /// <param name="randomDistribution">Continuous Random Distribution or Source</param> /// <returns> /// A vector with n-random elements distributed according /// to the specified random distribution. /// </returns> /// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception> public override Vector <Complex32> Random(int length, IDiscreteDistribution randomDistribution) { if (length < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } var v = (DenseVector)CreateVector(length); for (var index = 0; index < v.Data.Length; index++) { v.Data[index] = new Complex32(randomDistribution.Sample(), randomDistribution.Sample()); } return(v); }
/// <summary> /// Generates a vector with random elements /// </summary> /// <param name="length">Number of elements in the vector.</param> /// <param name="randomDistribution">Continuous Random Distribution or Source</param> /// <returns> /// A vector with n-random elements distributed according /// to the specified random distribution. /// </returns> /// <exception cref="ArgumentException">If the n vector is not positive.</exception> public override Vector <Complex32> Random(int length, IDiscreteDistribution randomDistribution) { if (length < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } var vector = CreateVector(length); for (var index = 0; index < length; index++) { vector.At(index, new Complex32(Convert.ToSingle(randomDistribution.Sample()), Convert.ToSingle(randomDistribution.Sample()))); } return(vector); }
/// <summary> /// Generates a vector with random elements /// </summary> /// <param name="length">Number of elements in the vector.</param> /// <param name="randomDistribution">Continuous Random Distribution or Source</param> /// <returns> /// A vector with n-random elements distributed according /// to the specified random distribution. /// </returns> /// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception> public override Vector <float> Random(int length, IDiscreteDistribution randomDistribution) { if (length < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } var v = CreateVector(length); for (var index = 0; index < Count; index++) { v[index] = Convert.ToSingle(randomDistribution.Sample()); } return(v); }
/// <summary> /// Generates a matrix with random elements. /// </summary> /// <param name="numberOfRows">Number of rows.</param> /// <param name="numberOfColumns">Number of columns.</param> /// <param name="distribution">Continuous Random Distribution or Source</param> /// <returns> /// An <c>numberOfRows</c>-by-<c>numberOfColumns</c> matrix with elements distributed according to the provided distribution. /// </returns> /// <exception cref="ArgumentException">If the parameter <paramref name="numberOfRows"/> is not positive.</exception> /// <exception cref="ArgumentException">If the parameter <paramref name="numberOfColumns"/> is not positive.</exception> public virtual Matrix <T> Random(int numberOfRows, int numberOfColumns, IDiscreteDistribution distribution) { if (numberOfRows < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfRows"); } if (numberOfColumns < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); } var matrix = CreateMatrix(numberOfRows, numberOfColumns); DoRandom(matrix, distribution); return(matrix); }
public static IDiscreteDistribution <R> Distribution(IDiscreteDistribution <A> underlying, Func <A, R> projection) { var result = new Projected <A, R>(underlying, projection); if (result._weights.Count == 0) { return(Empty <R> .Distribution()); } var support = result.Support().ToList(); if (support.Count == 1) { return(Singleton <R> .Distribution(support.First())); } return(result); }
public static IDiscreteDistribution <T> Distribution( IDiscreteDistribution <T> underlying, Func <T, bool> predicate) { var conditioned = new Conditioned <T>(underlying, predicate); var support = conditioned.support; switch (support.Count) { case 0: throw new ArgumentException(); case 1: return(Singleton <T> .Distribution(support.First())); default: return(conditioned); } }
public static IDiscreteDistribution <TR> Distribution([NotNull] IDiscreteDistribution <TA> prior, [NotNull] Func <TA, IDiscreteDistribution <TR> > likelihood) { return(new Combined <TA, TR>(prior, likelihood)); }
/// <summary> /// Generates matrix with random elements. /// </summary> /// <param name="numberOfRows">Number of rows.</param> /// <param name="numberOfColumns">Number of columns.</param> /// <param name="distribution">Continuous Random Distribution or Source</param> /// <returns> /// An <c>numberOfRows</c>-by-<c>numberOfColumns</c> matrix with elements distributed according to the provided distribution. /// </returns> /// <exception cref="ArgumentException">If the parameter <paramref name="numberOfRows"/> is not positive.</exception> /// <exception cref="ArgumentException">If the parameter <paramref name="numberOfColumns"/> is not positive.</exception> public override Matrix Random(int numberOfRows, int numberOfColumns, IDiscreteDistribution distribution) { if (numberOfRows < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfRows"); } if (numberOfColumns < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); } var matrix = (SparseMatrix)CreateMatrix(numberOfRows, numberOfColumns); for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { var value = distribution.Sample(); if (value != 0.0) { matrix.SetValueAt(i, j, value); } } } return matrix; }
/// <summary> /// Vapnik Chervonenkis test. /// </summary> /// <param name="epsilon">The error we are willing to tolerate.</param> /// <param name="delta">The error probability we are willing to tolerate.</param> /// <param name="s">The samples to use for testing.</param> /// <param name="dist">The distribution we are testing.</param> public static void DiscreteVapnikChervonenkisTest(double epsilon, double delta, int[] s, IDiscreteDistribution dist) { // Using VC-dimension, we can bound the probability of making an error when estimating empirical probability // distributions. We are using Theorem 2.41 in "All Of Nonparametric Statistics". // http://books.google.com/books?id=MRFlzQfRg7UC&lpg=PP1&dq=all%20of%20nonparametric%20statistics&pg=PA22#v=onepage&q=%22shatter%20coe%EF%AC%83cients%20do%20not%22&f=false .</para> // For intervals on the real line the VC-dimension is 2. Assert.Greater(s.Length, Math.Ceiling(32.0 * Math.Log(16.0 / delta) / epsilon / epsilon)); var min = s.Min(); var max = s.Max(); var histogram = new int[max - min + 1]; for (int i = 0; i < s.Length; i++) { histogram[s[i] - min]++; } for (int i = 0; i < histogram.Length; i++) { var p = dist.CumulativeDistribution(i + min) - dist.CumulativeDistribution(i + min - 1.0); var pe = histogram[i]/(double)s.Length; Assert.Less(Math.Abs(p - pe), epsilon, dist.ToString()); } }
/// <summary> /// Populates a matrix with random elements. /// </summary> /// <param name="matrix">The matrix to populate.</param> /// <param name="distribution">Continuous Random Distribution to generate elements from.</param> protected abstract void DoRandom(Matrix <T> matrix, IDiscreteDistribution distribution);
this IDiscreteDistribution <TPrior> distribution, Func <TPrior, IDiscreteDistribution <TResult> > likelihood) { return(distribution.SelectMany(likelihood, (a, b) => (a, b)));
/// <summary> /// Generates a vector with random elements /// </summary> /// <param name="length">Number of elements in the vector.</param> /// <param name="randomDistribution">Continuous Random Distribution or Source</param> /// <returns> /// A vector with n-random elements distributed according /// to the specified random distribution. /// </returns> /// <exception cref="ArgumentNullException">If the n vector is non poisitive<see langword="null" />.</exception> public override Vector Random(int length, IDiscreteDistribution randomDistribution) { if (length < 0) { throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } var v = (SparseVector)this.CreateVector(length); for (var index = 0; index < v.Count; index++) { v[index] = randomDistribution.Sample(); } return v; }
private DiscreteUniform(IEnumerable <T> items) { _support = items.EnsureList(); _standard = SDU.Distribution(0, _support.Count - 1); }
public void HasRandomSourceDiscrete(IDiscreteDistribution distribution) { Assert.IsNotNull(distribution.RandomSource); }
/// <summary> /// Vapnik Chervonenkis test. /// </summary> /// <param name="epsilon">The error we are willing to tolerate.</param> /// <param name="delta">The error probability we are willing to tolerate.</param> /// <param name="s">The samples to use for testing.</param> /// <param name="dist">The distribution we are testing.</param> public static void DiscreteVapnikChervonenkisTest(double epsilon, double delta, int[] s, IDiscreteDistribution dist) { // Using VC-dimension, we can bound the probability of making an error when estimating empirical probability // distributions. We are using Theorem 2.41 in "All Of Nonparametric Statistics". // http://books.google.com/books?id=MRFlzQfRg7UC&lpg=PP1&dq=all%20of%20nonparametric%20statistics&pg=PA22#v=onepage&q=%22shatter%20coe%EF%AC%83cients%20do%20not%22&f=false .</para> // For intervals on the real line the VC-dimension is 2. Assert.Greater(s.Length, Math.Ceiling(32.0 * Math.Log(16.0 / delta) / epsilon / epsilon)); var min = s.Min(); var max = s.Max(); var histogram = new int[max - min + 1]; for (int i = 0; i < s.Length; i++) { histogram[s[i] - min]++; } for (int i = 0; i < histogram.Length; i++) { var p = dist.CumulativeDistribution(i + min) - dist.CumulativeDistribution(i + min - 1.0); var pe = histogram[i] / (double)s.Length; Assert.Less(Math.Abs(p - pe), epsilon, dist.ToString()); } }
public void HasRandomSourceEvenAfterSetToNullDiscrete(IDiscreteDistribution distribution) { Assert.DoesNotThrow(() => distribution.RandomSource = null); Assert.IsNotNull(distribution.RandomSource); }
public void CanSetRandomSourceDiscrete(IDiscreteDistribution distribution) { distribution.RandomSource = MersenneTwister.Default; }
/// <summary> /// Generates a vector with random elements /// </summary> /// <param name="length">Number of elements in the vector.</param> /// <param name="randomDistribution">Continuous Random Distribution or Source</param> /// <returns> /// A vector with n-random elements distributed according /// to the specified random distribution. /// </returns> /// <exception cref="ArgumentNullException">If the n vector is non positive<see langword="null" />.</exception> public override Vector Random(int length, IDiscreteDistribution randomDistribution) { if (length < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } var v = (DenseVector)CreateVector(length); for (var index = 0; index < v.Data.Length; index++) { v.Data[index] = randomDistribution.Sample(); } return v; }
/// <summary> /// Generates matrix with random elements. /// </summary> /// <param name="numberOfRows">Number of rows.</param> /// <param name="numberOfColumns">Number of columns.</param> /// <param name="distribution">Continuous Random Distribution or Source</param> /// <returns> /// An <c>numberOfRows</c>-by-<c>numberOfColumns</c> matrix with elements distributed according to the provided distribution. /// </returns> /// <exception cref="ArgumentException">If the parameter <paramref name="numberOfRows"/> is not positive.</exception> /// <exception cref="ArgumentException">If the parameter <paramref name="numberOfColumns"/> is not positive.</exception> public virtual Matrix Random(int numberOfRows, int numberOfColumns, IDiscreteDistribution distribution) { if (numberOfRows < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfRows"); } if (numberOfColumns < 1) { throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); } var matrix = CreateMatrix(numberOfRows, numberOfColumns); CommonParallel.For( 0, ColumnCount, j => { for (var i = 0; i < matrix.RowCount; i++) { matrix[i, j] = distribution.Sample(); } }); return matrix; }