Class Discrete
An arbitrary distribution over integers [0,D-1].
Implements
Inherited Members
Namespace: Microsoft.ML.Probabilistic.Distributions
Assembly: Microsoft.ML.Probabilistic.dll
Syntax
[Serializable]
[Quality(QualityBand.Mature)]
[DataContract]
public class Discrete : IDistribution<int>, IDistribution, ICloneable, HasPoint<int>, CanGetLogProb<int>, SettableTo<Discrete>, SettableToProduct<Discrete>, SettableToProduct<Discrete, Discrete>, Diffable, SettableToUniform, SettableToPartialUniform<Discrete>, SettableToRatio<Discrete>, SettableToRatio<Discrete, Discrete>, SettableToPower<Discrete>, SettableToWeightedSumExact<Discrete>, SettableToWeightedSum<Discrete>, CanGetLogAverageOf<Discrete>, CanGetLogAverageOfPower<Discrete>, CanGetAverageLog<Discrete>, CanGetLogNormalizer, Sampleable<int>, CanGetMean<double>, CanGetVariance<double>, CanGetMode<int>
Remarks
The distribution is represented by a normalized Vector of length D. The Vector may be all zero to indicate an empty distribution such as the product of conflicting point masses. The probability of value x is available as this[x] or GetLogProb(x).
Constructors
Discrete(Discrete)
Copy constructor
Declaration
public Discrete(Discrete that)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | that | The discrete instance to copy |
Discrete(Vector)
Creates a Discrete distribution from the given probabilities.
Declaration
[Construction(new string[]{"GetWorkspace"})]
public Discrete(Vector probs)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | probs |
Discrete(Double[])
Creates a Discrete distribution from the given probabilities.
Declaration
public Discrete(params double[] probs)
Parameters
| Type | Name | Description |
|---|---|---|
| Double[] | probs |
Discrete(Int32)
Creates a uniform Discrete distribution, from 0 to dimension-1.
Declaration
protected Discrete(int dimension)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | dimension |
Discrete(Int32, Sparsity)
Creates a uniform Discrete distribution with a specified sparsity, from 0 to dimension-1.
Declaration
protected Discrete(int dimension, Sparsity sparsity)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | dimension | Dimension |
| Sparsity | sparsity | Sparsity |
Fields
prob
Probability of each value (when not a point mass).
Declaration
[DataMember]
protected Vector prob
Field Value
| Type | Description |
|---|---|
| Vector |
Remarks
prob.Length == D. prob[i] >= 0. sum_i prob[i] = 1.
Properties
Dimension
Gets the dimension of this discrete distribution
Declaration
public int Dimension { get; }
Property Value
| Type | Description |
|---|---|
| Int32 |
IsPointMass
Indicates whether or not this instance is a point mass.
Declaration
[IgnoreDataMember]
public bool IsPointMass { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
Item[Int32]
Gets or sets the probability at the given index.
Declaration
public double this[int index] { get; }
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | index |
Property Value
| Type | Description |
|---|---|
| Double |
Point
Sets/gets this distribution as a point distribution
Declaration
[IgnoreDataMember]
public int Point { get; set; }
Property Value
| Type | Description |
|---|---|
| Int32 |
Sparsity
Gets the Sparsity specification of this Distribution.
Declaration
public Sparsity Sparsity { get; }
Property Value
| Type | Description |
|---|---|
| Sparsity |
Methods
Clone()
Clones this discrete distribution.
Declaration
public object Clone()
Returns
| Type | Description |
|---|---|
| Object | An object which is a clone of the current instance. This must be cast if you want to assign the result to a Discrete type |
Equals(Object)
Override of the Equals method
Declaration
public override bool Equals(object that)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | that | The instance to compare to |
Returns
| Type | Description |
|---|---|
| Boolean | True if the two distributions are the same in value, false otherwise |
Overrides
Evaluate(Int32)
Evaluates the density at the specified domain value
Declaration
public double Evaluate(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | value | The point at which to evaluate |
Returns
| Type | Description |
|---|---|
| Double | The density |
GetAverageLog(Discrete)
The expected logarithm of that distribution under this distribution.
Declaration
public double GetAverageLog(Discrete that)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | that | The distribution to take the logarithm of. |
Returns
| Type | Description |
|---|---|
| Double |
|
Remarks
This is also known as the cross entropy.
GetHashCode()
Override of GetHashCode method
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| Int32 | The hash code for this instance |
Overrides
GetLogAverageOf(Discrete)
The log of the integral of the product of this discrete and that discrete
Declaration
public double GetLogAverageOf(Discrete that)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | that | That discrete distribution |
Returns
| Type | Description |
|---|---|
| Double | The log inner product |
GetLogAverageOfPower(Discrete, Double)
Get the integral of this distribution times another distribution raised to a power.
Declaration
public double GetLogAverageOfPower(Discrete that, double power)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | that | |
| Double | power |
Returns
| Type | Description |
|---|---|
| Double |
GetLogNormalizer()
Gets the log normalizer of the distribution
Declaration
public double GetLogNormalizer()
Returns
| Type | Description |
|---|---|
| Double |
GetLogProb(Int32)
Evaluates the log density at the specified domain value
Declaration
public double GetLogProb(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | value | The point at which to evaluate |
Returns
| Type | Description |
|---|---|
| Double | The log density |
GetLogProbs()
Gets the vector of log probabilities for this distribution.
Declaration
public Vector GetLogProbs()
Returns
| Type | Description |
|---|---|
| Vector |
GetMean()
Gets the mean of the distribution
Declaration
public double GetMean()
Returns
| Type | Description |
|---|---|
| Double |
GetMedian()
Gets the median of the distribution
Declaration
public int GetMedian()
Returns
| Type | Description |
|---|---|
| Int32 | The median |
GetMode()
Gets the mode of the distribution
Declaration
public int GetMode()
Returns
| Type | Description |
|---|---|
| Int32 |
GetProbs()
Gets the probability at each index.
Declaration
public Vector GetProbs()
Returns
| Type | Description |
|---|---|
| Vector | The vector of probabilities |
GetProbs(Vector)
Gets the probability at each index.
Declaration
public Vector GetProbs(Vector result)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | result | When used internally, can be the same object as prob. |
Returns
| Type | Description |
|---|---|
| Vector | result |
GetVariance()
Gets the variance of the distribution
Declaration
public double GetVariance()
Returns
| Type | Description |
|---|---|
| Double |
GetWorkspace()
Gets a Vector of size this.Dimension.
Declaration
public Vector GetWorkspace()
Returns
| Type | Description |
|---|---|
| Vector | A pointer to the internal probs Vector of the object. |
Remarks
This function is intended to be used with SetProbs, to avoid allocating a new Vector. The return value should not be interpreted as a probs vector, but only a workspace filled with unknown data that can be overwritten. Until SetProbs is called, the distribution object is invalid once this workspace is modified.
IsPartialUniform()
Checks whether the distribution is uniform over its support.
Declaration
public bool IsPartialUniform()
Returns
| Type | Description |
|---|---|
| Boolean | True if the distribution is uniform over its support, false otherwise. |
IsUniform()
Returns whether the discrete distribution is uniform or not
Declaration
public bool IsUniform()
Returns
| Type | Description |
|---|---|
| Boolean | True if uniform |
IsZero()
Declaration
public bool IsZero()
Returns
| Type | Description |
|---|---|
| Boolean |
MaxDiff(Object)
Gets the maximum difference between the parameters of this discrete and that discrete
Declaration
public double MaxDiff(object that)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | that | That discrete |
Returns
| Type | Description |
|---|---|
| Double | The maximum difference |
Normalize()
Normalizes this distribution - i.e. sets the probabilities to sum to 1. This is called internally after product operations, sum operations etc.
Declaration
protected double Normalize()
Returns
| Type | Description |
|---|---|
| Double | The normalizing factor |
PointMass(Int32, Int32)
Creates a Discrete distribution which allows only one value.
Declaration
[Construction(new string[]{"Point", "Dimension"}, UseWhen = "IsPointMass")]
public static Discrete PointMass(int value, int numValues)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | value | The allowed value. |
| Int32 | numValues | The number of values in the domain. |
Returns
| Type | Description |
|---|---|
| Discrete |
ProbEqual(Discrete)
The integral of the product between this discrete and that discrete. This is the probability that samples from this instance and that instance are equal
Declaration
public double ProbEqual(Discrete that)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | that | That discrete distribution |
Returns
| Type | Description |
|---|---|
| Double | The inner product |
Sample()
Returns a sample from this discrete distribution
Declaration
public int Sample()
Returns
| Type | Description |
|---|---|
| Int32 | The sample value |
Sample(Vector)
Returns a sample from a discrete distribution with the specified probabilities
Declaration
public static int Sample(Vector probs)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | probs | The parameters of the discrete distribution |
Returns
| Type | Description |
|---|---|
| Int32 | The sample value |
Sample(Int32)
Returns a sample from this discrete distribution
Declaration
public int Sample(int result)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | result | This parameter is ignored and is only present to support the Sampleable interface |
Returns
| Type | Description |
|---|---|
| Int32 | The sample value |
SetProbs(Vector)
Sets the probability of each index.
Declaration
public void SetProbs(Vector probs)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector | probs | A vector of non-negative, finite numbers. Need not sum to 1. |
Remarks
Instead of allocating your own Vector to pass to SetProbs, you can call GetWorkspace(), fill in the resulting Vector, and then pass it to SetProbs.
SetTo(Discrete)
Sets the parameters of this instance to the parameters of that instance
Declaration
public void SetTo(Discrete value)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | value | That instance |
SetToPadded(Discrete)
Set this distribution to match the given distribution, but possibly over a larger domain
Declaration
public void SetToPadded(Discrete value)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | value |
SetToPartialUniform()
Sets the distribution to be uniform over its support.
Declaration
public void SetToPartialUniform()
SetToPartialUniformOf(Discrete)
Sets the distribution to be uniform over the support of a given distribution.
Declaration
public void SetToPartialUniformOf(Discrete dist)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | dist | The distribution which support will be used to setup the current distribution. |
SetToPower(Discrete, Double)
Sets the parameters to represent the power of a discrete distributions.
Declaration
public void SetToPower(Discrete dist, double exponent)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | dist | The discrete distribution |
| Double | exponent | The exponent |
SetToProduct(Discrete, Discrete)
Sets the parameters to represent the product of two discrete distributions.
Declaration
public void SetToProduct(Discrete a, Discrete b)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | a | The first discrete distribution |
| Discrete | b | The second discrete distribution |
SetToRatio(Discrete, Discrete, Boolean)
Declaration
public void SetToRatio(Discrete numerator, Discrete denominator, bool forceProper = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | numerator | |
| Discrete | denominator | |
| Boolean | forceProper |
SetToSum(Double, Discrete, Double, Discrete)
Sets the parameters to represent the weighted sum of two discrete distributions.
Declaration
public void SetToSum(double weight1, Discrete dist1, double weight2, Discrete dist2)
Parameters
| Type | Name | Description |
|---|---|---|
| Double | weight1 | The first weight |
| Discrete | dist1 | The first discrete distribution. Can be the same object as |
| Double | weight2 | The second weight |
| Discrete | dist2 | The second discrete distribution. Cannot be the same object as |
SetToUniform()
Sets this instance to a uniform discrete (i.e. probabilities all equal)
Declaration
public void SetToUniform()
ToString()
Override of ToString method
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| String | String representation of this instance |
Overrides
ToString(String)
Declaration
public virtual string ToString(string format)
Parameters
| Type | Name | Description |
|---|---|---|
| String | format |
Returns
| Type | Description |
|---|---|
| String |
ToString(String, String)
Declaration
public virtual string ToString(string format, string delimiter)
Parameters
| Type | Name | Description |
|---|---|---|
| String | format | |
| String | delimiter |
Returns
| Type | Description |
|---|---|
| String |
Uniform(Int32)
Creates a uniform Discrete distribution over the values from 0 to numValues-1
Declaration
public static Discrete Uniform(int numValues)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | numValues |
Returns
| Type | Description |
|---|---|
| Discrete |
Uniform(Int32, Sparsity)
Creates a uniform Discrete distribution with a specified sparsity over the values from 0 to numValues-1
Declaration
[Construction(new string[]{"Dimension", "Sparsity"}, UseWhen = "IsUniform")]
public static Discrete Uniform(int numValues, Sparsity sparsity)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | numValues | Number of values |
| Sparsity | sparsity | Sparsity |
Returns
| Type | Description |
|---|---|
| Discrete |
UniformInRange(Int32, Int32, Int32)
Creates a Discrete distribution which is uniform over values from start to end inclusive.
Declaration
public static Discrete UniformInRange(int numValues, int start, int end)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | numValues | Number of values |
| Int32 | start | The first value included in the distribution |
| Int32 | end | The last value included in the distribution |
Returns
| Type | Description |
|---|---|
| Discrete | Discrete which is uniform over the specified range (and zero elsewhere). |
UniformInRanges(Int32, IEnumerable<Int32>)
Creates a Discrete distribution which is uniform over values in multiple ranges specified by pairs of start and end values. These pairs are specified as adjacent values in an enumerable whose length must therefore be even.
Declaration
public static Discrete UniformInRanges(int numValues, IEnumerable<int> startEndPairs)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | numValues | Number of values |
| IEnumerable<Int32> | startEndPairs | Sequence of start and end pairs |
Returns
| Type | Description |
|---|---|
| Discrete | Discrete which is uniform over the specified ranges (and zero elsewhere). |
UniformInRanges(Int32, Int32[])
Creates a Discrete distribution which is uniform over values in multiple ranges specified by pairs of start and end values. These pairs are specified as adjacent values in an array whose length must therefore be even.
Declaration
public static Discrete UniformInRanges(int numValues, params int[] startEndPairs)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | numValues | Number of values |
| Int32[] | startEndPairs | Sequence of start and end pairs |
Returns
| Type | Description |
|---|---|
| Discrete | Discrete which is uniform over the specified ranges (and zero elsewhere). |
Zero(Int32)
Declaration
public static Discrete Zero(int numValues)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | numValues |
Returns
| Type | Description |
|---|---|
| Discrete |
Operators
Division(Discrete, Discrete)
Creates a Discrete distribution which is the ratio of two Discrete distributions
Declaration
public static Discrete operator /(Discrete numerator, Discrete denominator)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | numerator | The numerator distribution |
| Discrete | denominator | The denominator distribution |
Returns
| Type | Description |
|---|---|
| Discrete | The resulting Discrete distribution |
ExclusiveOr(Discrete, Double)
Raises a distribution to a power.
Declaration
public static Discrete operator ^(Discrete dist, double exponent)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | dist | The distribution. |
| Double | exponent | The power to raise to. |
Returns
| Type | Description |
|---|---|
| Discrete |
|
Multiply(Discrete, Discrete)
Creates a Discrete distribution which is the product of two Discrete distributions
Declaration
public static Discrete operator *(Discrete a, Discrete b)
Parameters
| Type | Name | Description |
|---|---|---|
| Discrete | a | The first distribution |
| Discrete | b | The second distribution |
Returns
| Type | Description |
|---|---|
| Discrete | The resulting Discrete distribution |