Distributions used in implementing Hidden Markov Models¶
These distribution classes are designed specifically for HMM’s and not for general use in statistics. For example, they have fixed or non-fixed status, which only make sense relative to being used in a hidden Markov model.
AUTHOR:
- William Stein, 2010-03 
- class sage.stats.hmm.distributions.DiscreteDistribution[source]¶
- Bases: - Distribution
- class sage.stats.hmm.distributions.Distribution[source]¶
- Bases: - object- A distribution. - plot(*args, **kwds)[source]¶
- Return a plot of the probability density function. - INPUT: - argsand- kwds, passed to the Sage- plot()function
 - OUTPUT: a - Graphicsobject- EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.2,-10,.5),(.6,1,1),(.2,20,.5)]) sage: P.plot(-10,30) # needs sage.plot Graphics object consisting of 1 graphics primitive - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.2'),-Integer(10),RealNumber('.5')),(RealNumber('.6'),Integer(1),Integer(1)),(RealNumber('.2'),Integer(20),RealNumber('.5'))]) >>> P.plot(-Integer(10),Integer(30)) # needs sage.plot Graphics object consisting of 1 graphics primitive 
 - prob(x)[source]¶
- The probability density function evaluated at \(x\). - INPUT: - x– object
 - OUTPUT: float - EXAMPLES: - This method must be defined in a derived class: - sage: import sage.stats.hmm.distributions sage: sage.stats.hmm.distributions.Distribution().prob(0) Traceback (most recent call last): ... NotImplementedError - >>> from sage.all import * >>> import sage.stats.hmm.distributions >>> sage.stats.hmm.distributions.Distribution().prob(Integer(0)) Traceback (most recent call last): ... NotImplementedError 
 - sample(n=None)[source]¶
- Return either a single sample (the default) or \(n\) samples from this probability distribution. - INPUT: - n–- Noneor a positive integer
 - OUTPUT: a single sample if \(n\) is 1; otherwise many samples - EXAMPLES: - This method must be defined in a derived class: - sage: import sage.stats.hmm.distributions sage: sage.stats.hmm.distributions.Distribution().sample() Traceback (most recent call last): ... NotImplementedError - >>> from sage.all import * >>> import sage.stats.hmm.distributions >>> sage.stats.hmm.distributions.Distribution().sample() Traceback (most recent call last): ... NotImplementedError 
 
- class sage.stats.hmm.distributions.GaussianDistribution[source]¶
- Bases: - Distribution
- class sage.stats.hmm.distributions.GaussianMixtureDistribution[source]¶
- Bases: - Distribution- A probability distribution defined by taking a weighted linear combination of Gaussian distributions. - EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.3,1,2),(.7,-1,1)]); P 0.3*N(1.0,2.0) + 0.7*N(-1.0,1.0) sage: P[0] (0.3, 1.0, 2.0) sage: P.is_fixed() False sage: P.fix(1) sage: P.is_fixed(0) False sage: P.is_fixed(1) True sage: P.unfix(1) sage: P.is_fixed(1) False - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.3'),Integer(1),Integer(2)),(RealNumber('.7'),-Integer(1),Integer(1))]); P 0.3*N(1.0,2.0) + 0.7*N(-1.0,1.0) >>> P[Integer(0)] (0.3, 1.0, 2.0) >>> P.is_fixed() False >>> P.fix(Integer(1)) >>> P.is_fixed(Integer(0)) False >>> P.is_fixed(Integer(1)) True >>> P.unfix(Integer(1)) >>> P.is_fixed(Integer(1)) False - fix(i=None)[source]¶
- Set that this - GaussianMixtureDistribution(or its \(i\)-th component) is fixed when using Baum-Welch to update the corresponding HMM.- INPUT: - i–- None(default) or integer; if given, only fix the \(i\)-th component
 - EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.2,-10,.5),(.6,1,1),(.2,20,.5)]) sage: P.fix(1); P.is_fixed() False sage: P.is_fixed(1) True sage: P.fix(); P.is_fixed() True - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.2'),-Integer(10),RealNumber('.5')),(RealNumber('.6'),Integer(1),Integer(1)),(RealNumber('.2'),Integer(20),RealNumber('.5'))]) >>> P.fix(Integer(1)); P.is_fixed() False >>> P.is_fixed(Integer(1)) True >>> P.fix(); P.is_fixed() True 
 - is_fixed(i=None)[source]¶
- Return whether or not this - GaussianMixtureDistributionis fixed when using Baum-Welch to update the corresponding HMM.- INPUT: - i–- None(default) or integer; if given, only return whether the \(i\)-th component is fixed
 - EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.2,-10,.5),(.6,1,1),(.2,20,.5)]) sage: P.is_fixed() False sage: P.is_fixed(0) False sage: P.fix(0); P.is_fixed() False sage: P.is_fixed(0) True sage: P.fix(); P.is_fixed() True - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.2'),-Integer(10),RealNumber('.5')),(RealNumber('.6'),Integer(1),Integer(1)),(RealNumber('.2'),Integer(20),RealNumber('.5'))]) >>> P.is_fixed() False >>> P.is_fixed(Integer(0)) False >>> P.fix(Integer(0)); P.is_fixed() False >>> P.is_fixed(Integer(0)) True >>> P.fix(); P.is_fixed() True 
 - prob(x)[source]¶
- Return the probability of \(x\). - Since this is a continuous distribution, this is defined to be the limit of the p’s such that the probability of [x,x+h] is p*h. - INPUT: - x– float
 - OUTPUT: float - EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.2,-10,.5),(.6,1,1),(.2,20,.5)]) sage: P.prob(.5) 0.21123919605857971 sage: P.prob(-100) 0.0 sage: P.prob(20) 0.1595769121605731 - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.2'),-Integer(10),RealNumber('.5')),(RealNumber('.6'),Integer(1),Integer(1)),(RealNumber('.2'),Integer(20),RealNumber('.5'))]) >>> P.prob(RealNumber('.5')) 0.21123919605857971 >>> P.prob(-Integer(100)) 0.0 >>> P.prob(Integer(20)) 0.1595769121605731 
 - prob_m(x, m)[source]¶
- Return the probability of \(x\) using just the \(m\)-th summand. - INPUT: - x– float
- m– integer
 - OUTPUT: float - EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.2,-10,.5),(.6,1,1),(.2,20,.5)]) sage: P.prob_m(.5, 0) 2.7608117680508...e-97 sage: P.prob_m(.5, 1) 0.21123919605857971 sage: P.prob_m(.5, 2) 0.0 - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.2'),-Integer(10),RealNumber('.5')),(RealNumber('.6'),Integer(1),Integer(1)),(RealNumber('.2'),Integer(20),RealNumber('.5'))]) >>> P.prob_m(RealNumber('.5'), Integer(0)) 2.7608117680508...e-97 >>> P.prob_m(RealNumber('.5'), Integer(1)) 0.21123919605857971 >>> P.prob_m(RealNumber('.5'), Integer(2)) 0.0 
 - sample(n=None)[source]¶
- Return a single sample from this distribution (by default), or if \(n>1\), return a - TimeSeriesof samples.- INPUT: - n– integer or- None(default:- None)
 - OUTPUT: - float if - nis- None(default); otherwise a- TimeSeries
 - EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.2,-10,.5),(.6,1,1),(.2,20,.5)]) sage: type(P.sample()) <class 'float'> sage: l = P.sample(1) sage: len(l) 1 sage: type(l) <class 'sage.stats.time_series.TimeSeries'> sage: l = P.sample(5) sage: len(l) 5 sage: type(l) <class 'sage.stats.time_series.TimeSeries'> sage: l = P.sample(0) sage: len(l) 0 sage: type(l) <class 'sage.stats.time_series.TimeSeries'> sage: P.sample(-3) Traceback (most recent call last): ... ValueError: n must be nonnegative - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.2'),-Integer(10),RealNumber('.5')),(RealNumber('.6'),Integer(1),Integer(1)),(RealNumber('.2'),Integer(20),RealNumber('.5'))]) >>> type(P.sample()) <class 'float'> >>> l = P.sample(Integer(1)) >>> len(l) 1 >>> type(l) <class 'sage.stats.time_series.TimeSeries'> >>> l = P.sample(Integer(5)) >>> len(l) 5 >>> type(l) <class 'sage.stats.time_series.TimeSeries'> >>> l = P.sample(Integer(0)) >>> len(l) 0 >>> type(l) <class 'sage.stats.time_series.TimeSeries'> >>> P.sample(-Integer(3)) Traceback (most recent call last): ... ValueError: n must be nonnegative 
 - unfix(i=None)[source]¶
- Set that this - GaussianMixtureDistribution(or its \(i\)-th component) is not fixed when using Baum-Welch to update the corresponding HMM.- INPUT: - i–- None(default) or integer; if given, only fix the \(i\)-th component
 - EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.2,-10,.5),(.6,1,1),(.2,20,.5)]) sage: P.fix(1); P.is_fixed(1) True sage: P.unfix(1); P.is_fixed(1) False sage: P.fix(); P.is_fixed() True sage: P.unfix(); P.is_fixed() False - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.2'),-Integer(10),RealNumber('.5')),(RealNumber('.6'),Integer(1),Integer(1)),(RealNumber('.2'),Integer(20),RealNumber('.5'))]) >>> P.fix(Integer(1)); P.is_fixed(Integer(1)) True >>> P.unfix(Integer(1)); P.is_fixed(Integer(1)) False >>> P.fix(); P.is_fixed() True >>> P.unfix(); P.is_fixed() False 
 
- sage.stats.hmm.distributions.unpickle_gaussian_mixture_distribution_v1(c0, c1, param, fixed)[source]¶
- Used in unpickling - GaussianMixtureDistributionobjects.- EXAMPLES: - sage: P = hmm.GaussianMixtureDistribution([(.2,-10,.5),(.6,1,1),(.2,20,.5)]) sage: loads(dumps(P)) == P # indirect doctest True - >>> from sage.all import * >>> P = hmm.GaussianMixtureDistribution([(RealNumber('.2'),-Integer(10),RealNumber('.5')),(RealNumber('.6'),Integer(1),Integer(1)),(RealNumber('.2'),Integer(20),RealNumber('.5'))]) >>> loads(dumps(P)) == P # indirect doctest True