Class GenericDiscreteBase<T, TThis>
A generic base class for discrete distributions over a type T.
Implements
Inherited Members
Namespace: Microsoft.ML.Probabilistic.Distributions
Assembly: Microsoft.ML.Probabilistic.dll
Syntax
[DataContract]
public abstract class GenericDiscreteBase<T, TThis> : IDistribution<T>, IDistribution, ICloneable, Diffable, SettableToUniform, HasPoint<T>, CanGetLogProb<T>, SettableTo<TThis>, SettableToProduct<TThis>, SettableToProduct<TThis, TThis>, SettableToRatio<TThis>, SettableToRatio<TThis, TThis>, SettableToPower<TThis>, SettableToWeightedSumExact<TThis>, SettableToWeightedSum<TThis>, SettableToPartialUniform<TThis>, CanGetLogAverageOf<TThis>, CanGetLogAverageOfPower<TThis>, CanGetAverageLog<TThis>, Sampleable<T> where TThis : GenericDiscreteBase<T, TThis>, new()
Type Parameters
Name | Description |
---|---|
T | The domain type of this distribution |
TThis | The type of the subclass (allows factory methods return the correct type) |
Remarks
This class makes it straightforward to implement discrete distributions over types other than int. It is only necessary to make a subclass of this abstract class, add a constructor and implement two methods to convert the desired type T to and from an integer.
Internally, this class wraps a Discrete distribution and uses it to provide all functionality.
Constructors
GenericDiscreteBase(Int32, Sparsity)
Creates a discrete
Declaration
protected GenericDiscreteBase(int dimension, Sparsity sparsity)
Parameters
Type | Name | Description |
---|---|---|
Int32 | dimension | The dimension of the underlying discrete |
Sparsity | sparsity | The sparsity of the underlying discrete |
Fields
disc
Declaration
[DataMember]
protected Discrete disc
Field Value
Type | Description |
---|---|
Discrete |
Properties
Dimension
The dimension of this discrete distribution.
Declaration
[IgnoreDataMember]
public int Dimension { get; }
Property Value
Type | Description |
---|---|
Int32 |
IndexRange
Declaration
public IntPair IndexRange { get; }
Property Value
Type | Description |
---|---|
IntPair |
IsPointMass
Returns true if the distribution is a point mass.
Declaration
[IgnoreDataMember]
public bool IsPointMass { get; }
Property Value
Type | Description |
---|---|
Boolean |
Item[T]
Gets the probability of a given value.
Declaration
public double this[T value] { get; }
Parameters
Type | Name | Description |
---|---|---|
T | value |
Property Value
Type | Description |
---|---|
Double |
Point
Point property
Declaration
[IgnoreDataMember]
public T Point { get; set; }
Property Value
Type | Description |
---|---|
T |
ValueRange
Returns the domain values corresponding to the first and last indices of non-zero probabilities.
Declaration
public (T, T) ValueRange { get; }
Property Value
Type | Description |
---|---|
(T1, T2)<T, T> |
Methods
Clone()
Returns a clone of this distribution.
Declaration
public virtual object Clone()
Returns
Type | Description |
---|---|
Object |
ConvertFromInt(Int32)
Converts an integer to an item of type T.
Declaration
public abstract T ConvertFromInt(int i)
Parameters
Type | Name | Description |
---|---|---|
Int32 | i |
Returns
Type | Description |
---|---|
T |
ConvertToInt(T)
Converts an item of type T to an integer.
Declaration
public abstract int ConvertToInt(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value |
Returns
Type | Description |
---|---|
Int32 |
Equals(Object)
Override of the Equals method
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | The instance to compare to |
Returns
Type | Description |
---|---|
Boolean | True if the two distributions are the same in value, false otherwise |
Overrides
FromDiscrete(Discrete)
Creates a distribution using the probabilities from the given discrete distribution.
Declaration
public static TThis FromDiscrete(Discrete d)
Parameters
Type | Name | Description |
---|---|---|
Discrete | d |
Returns
Type | Description |
---|---|
TThis |
FromProbs(Double[])
Creates a distribution from the given probabilities.
Declaration
public static TThis FromProbs(params double[] probs)
Parameters
Type | Name | Description |
---|---|---|
Double[] | probs |
Returns
Type | Description |
---|---|
TThis |
FromVector(Vector)
Creates a distribution from the given vector of probabilities.
Declaration
[Construction(new string[]{"GetWorkspace"})]
public static TThis FromVector(Vector probs)
Parameters
Type | Name | Description |
---|---|---|
Vector | probs |
Returns
Type | Description |
---|---|
TThis |
GetAverageLog(TThis)
The expected logarithm of that distribution under this distribution.
Declaration
public double GetAverageLog(TThis that)
Parameters
Type | Name | Description |
---|---|---|
TThis | 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
GetInternalDiscrete()
Gets the internal discrete distribution that this distribution wraps.
Declaration
public Discrete GetInternalDiscrete()
Returns
Type | Description |
---|---|
Discrete | The internal discrete distribution |
GetLogAverageOf(TThis)
The log of the integral of the product of this discrete distribution and that discrete distribution
Declaration
public double GetLogAverageOf(TThis that)
Parameters
Type | Name | Description |
---|---|---|
TThis | that | That discrete distribution |
Returns
Type | Description |
---|---|
Double | The log inner product |
GetLogAverageOfPower(TThis, Double)
Get the integral of this distribution times another distribution raised to a power.
Declaration
public double GetLogAverageOfPower(TThis that, double power)
Parameters
Type | Name | Description |
---|---|---|
TThis | that | |
Double | power |
Returns
Type | Description |
---|---|
Double |
GetLogProb(T)
Evaluates the log density at the specified domain value
Declaration
public double GetLogProb(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The point at which to evaluate |
Returns
Type | Description |
---|---|
Double | The log density |
GetLogProbs()
Gets the log-probability at each index.
Declaration
public Vector GetLogProbs()
Returns
Type | Description |
---|---|
Vector | The vector of log-probabilities |
GetMode()
Gets the mode of the distribution
Declaration
public T GetMode()
Returns
Type | Description |
---|---|
T | The value with the highest probability |
GetProbs()
Gets the probability at each index.
Declaration
public Vector GetProbs()
Returns
Type | Description |
---|---|
Vector | The vector of probabilities |
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 true if the distribution is uniform.
Declaration
public bool IsUniform()
Returns
Type | Description |
---|---|
Boolean | True if uniform |
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 |
PointMass(T)
Creates a point mass distribution.
Declaration
[Construction(new string[]{"Point"}, UseWhen = "IsPointMass")]
public static TThis PointMass(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The allowed value. |
Returns
Type | Description |
---|---|
TThis |
Sample()
Returns a sample from the distribution
Declaration
public T Sample()
Returns
Type | Description |
---|---|
T | The sample value |
Sample(T)
Returns a sample from the distribution
Declaration
public T Sample(T result)
Parameters
Type | Name | Description |
---|---|---|
T | result | Not used |
Returns
Type | Description |
---|---|
T | 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(TThis)
Sets the parameters of this instance to the parameters of that instance
Declaration
public void SetTo(TThis value)
Parameters
Type | Name | Description |
---|---|---|
TThis | value | That instance |
SetToPartialUniform()
Sets the distribution to be uniform over its support.
Declaration
public void SetToPartialUniform()
SetToPartialUniformOf(TThis)
Sets the distribution to be uniform over the support of a given distribution.
Declaration
public void SetToPartialUniformOf(TThis dist)
Parameters
Type | Name | Description |
---|---|---|
TThis | dist | The distribution which support will be used to setup the current distribution. |
SetToPower(TThis, Double)
Sets the parameters to represent the power of a discrete distributions.
Declaration
public void SetToPower(TThis value, double exponent)
Parameters
Type | Name | Description |
---|---|---|
TThis | value | The discrete distribution |
Double | exponent | The exponent |
SetToProduct(TThis, TThis)
Sets the parameters to represent the product of two discrete distributions.
Declaration
public void SetToProduct(TThis a, TThis b)
Parameters
Type | Name | Description |
---|---|---|
TThis | a | The first discrete distribution |
TThis | b | The second discrete distribution |
SetToRatio(TThis, TThis, Boolean)
Sets the parameters to represent the ratio of two discrete distributions.
Declaration
public void SetToRatio(TThis numerator, TThis denominator, bool forceProper)
Parameters
Type | Name | Description |
---|---|---|
TThis | numerator | The numerator discrete distribution |
TThis | denominator | The denominator discrete distribution |
Boolean | forceProper | Whether to force the returned distribution to be proper |
SetToSum(Double, TThis, Double, TThis)
Sets the parameters to represent the weighted sum of two discrete distributions.
Declaration
public void SetToSum(double weight1, TThis value1, double weight2, TThis value2)
Parameters
Type | Name | Description |
---|---|---|
Double | weight1 | The first weight |
TThis | value1 | The first discrete distribution |
Double | weight2 | The second weight |
TThis | value2 | The second discrete distribution |
SetToUniform()
Sets this instance to a uniform discrete (i.e. probabilities all equal)
Declaration
public void SetToUniform()
ToString()
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String |
Overrides
ToString(T)
Implements a custom ToString() for items of type T.
Declaration
protected virtual string ToString(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value |
Returns
Type | Description |
---|---|
String |
ToString(String)
Declaration
public virtual string ToString(string format)
Parameters
Type | Name | Description |
---|---|---|
String | format |
Returns
Type | Description |
---|---|
String |
ToString(String, String)
Returns a string representation of this distribution.
Declaration
public virtual string ToString(string format, string delimiter)
Parameters
Type | Name | Description |
---|---|---|
String | format | |
String | delimiter |
Returns
Type | Description |
---|---|
String |
Uniform()
Creates a uniform distribution.
Declaration
[Construction(UseWhen = "IsUniform")]
public static TThis Uniform()
Returns
Type | Description |
---|---|
TThis |
UniformInRange(T, T)
Creates a Discrete distribution which is uniform over values from start to end inclusive.
Declaration
public static TThis UniformInRange(T start, T end)
Parameters
Type | Name | Description |
---|---|---|
T | start | The first value included in the distribution |
T | end | The last value included in the distribution |
Returns
Type | Description |
---|---|
TThis | Discrete which is uniform over the specified range (and zero elsewhere). |
UniformInRanges(T[])
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 TThis UniformInRanges(params T[] startEndPairs)
Parameters
Type | Name | Description |
---|---|---|
T[] | startEndPairs | Sequence of start and end pairs |
Returns
Type | Description |
---|---|
TThis | Discrete which is uniform over the specified range (and zero elsewhere). |
UniformInRanges(IEnumerable<T>)
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 TThis UniformInRanges(IEnumerable<T> startEndPairs)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | startEndPairs | Sequence of start and end pairs |
Returns
Type | Description |
---|---|
TThis | Discrete which is uniform over the specified range (and zero elsewhere). |
UniformOver(T[])
Creates a Discrete distribution which is uniform over the specified set of values and zero elsewhere.
Declaration
public static TThis UniformOver(params T[] values)
Parameters
Type | Name | Description |
---|---|---|
T[] | values | The values. |
Returns
Type | Description |
---|---|
TThis | Discrete which is uniform over the specified set of values and zero elsewhere. |
UniformOver(IEnumerable<T>)
Creates a Discrete distribution which is uniform over the specified set of values and zero elsewhere.
Declaration
public static TThis UniformOver(IEnumerable<T> values)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | values | The values. |
Returns
Type | Description |
---|---|
TThis | Discrete which is uniform over the specified set of values and zero elsewhere. |
Explicit Interface Implementations
CanGetLogProb<T>.GetLogProb(T)
Returns the log probability of the value under this distribution.
Declaration
double CanGetLogProb<T>.GetLogProb(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value |
Returns
Type | Description |
---|---|
Double |