Skip to content

Commit a054491

Browse files
author
Matthias Koeppe
committed
src/sage/stats/hmm/hmm.pyx: Docstring cosmetics
1 parent 045d0e7 commit a054491

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

src/sage/stats/hmm/hmm.pyx

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# sage.doctest: needs numpy sage.modules
2-
"""
2+
r"""
33
Hidden Markov Models
44
55
This is a complete pure-Cython optimized implementation of Hidden
@@ -48,11 +48,11 @@ cdef HMM_Util util = HMM_Util()
4848
###########################################
4949

5050
cdef class HiddenMarkovModel:
51-
"""
51+
r"""
5252
Abstract base class for all Hidden Markov Models.
5353
"""
5454
def initial_probabilities(self):
55-
"""
55+
r"""
5656
Return the initial probabilities as a :class:`TimeSeries` of
5757
length `N`, where `N` is the number of states of the Markov model.
5858
@@ -90,7 +90,7 @@ cdef class HiddenMarkovModel:
9090
return TimeSeries(self.pi)
9191

9292
def transition_matrix(self):
93-
"""
93+
r"""
9494
Return the state transition matrix.
9595
9696
OUTPUT: a Sage matrix with real double precision (RDF) entries.
@@ -133,15 +133,15 @@ cdef class HiddenMarkovModel:
133133
return matrix(RDF, self.N, self.A.list())
134134

135135
def graph(self, eps=1e-3):
136-
"""
136+
r"""
137137
Create a weighted directed graph from the transition matrix,
138138
not including any edge with a probability less than ``eps``.
139139
140140
INPUT:
141141
142-
- eps -- nonnegative real number
142+
- ``eps`` -- nonnegative real number
143143
144-
OUTPUT: a digraph
144+
OUTPUT: a :class:`DiGraph`
145145
146146
EXAMPLES::
147147
@@ -165,7 +165,7 @@ cdef class HiddenMarkovModel:
165165
return DiGraph(m, weighted=True)
166166

167167
def sample(self, Py_ssize_t length, number=None, starting_state=None):
168-
"""
168+
r"""
169169
Return number samples from this HMM of given length.
170170
171171
INPUT:
@@ -225,7 +225,7 @@ cdef class HiddenMarkovModel:
225225
# HMM algorithms.
226226
#########################################################
227227
cdef TimeSeries _baum_welch_gamma(self, TimeSeries alpha, TimeSeries beta):
228-
"""
228+
r"""
229229
Used internally to compute the scaled quantity gamma_t(j)
230230
appearing in the Baum-Welch reestimation algorithm.
231231
@@ -256,7 +256,7 @@ cdef class HiddenMarkovModel:
256256

257257

258258
cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
259-
"""
259+
r"""
260260
A discrete Hidden Markov model implemented using double precision
261261
floating point arithmetic.
262262
@@ -328,7 +328,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
328328
cdef object _emission_symbols, _emission_symbols_dict
329329

330330
def __init__(self, A, B, pi, emission_symbols=None, bint normalize=True):
331-
"""
331+
r"""
332332
Create a discrete emissions HMM with transition probability
333333
matrix A, emission probabilities given by B, initial state
334334
probabilities pi, and given emission symbols (which default
@@ -371,7 +371,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
371371
util.normalize_probability_TimeSeries(self.B, i*self.n_out, (i+1)*self.n_out)
372372

373373
def __reduce__(self):
374-
"""
374+
r"""
375375
Used in pickling.
376376
377377
EXAMPLES::
@@ -384,7 +384,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
384384
(self.A, self.B, self.pi, self.n_out, self._emission_symbols, self._emission_symbols_dict)
385385

386386
def __richcmp__(self, other, op):
387-
"""
387+
r"""
388388
EXAMPLES::
389389
390390
sage: m = hmm.DiscreteHiddenMarkovModel([[0.4,0.6],[0.1,0.9]], [[0.0,1.0],[0.5,0.5]], [.5,.5])
@@ -404,7 +404,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
404404
other.__reduce__()[1], op)
405405

406406
def emission_matrix(self):
407-
"""
407+
r"""
408408
Return the matrix whose `i`-th row specifies the emission
409409
probability distribution for the `i`-th state.
410410
@@ -456,12 +456,12 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
456456
return s
457457

458458
def _emission_symbols_to_IntList(self, obs):
459-
"""
459+
r"""
460460
Internal function used to convert a list of emission symbols to an :class:`IntList`.
461461
462462
INPUT:
463463
464-
- obs -- a list of objects
464+
- ``obs`` -- a list of objects
465465
466466
OUTPUT: an IntList
467467
@@ -475,12 +475,12 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
475475
return IntList([d[x] for x in obs])
476476

477477
def _IntList_to_emission_symbols(self, obs):
478-
"""
478+
r"""
479479
Internal function used to convert a list of emission symbols to an IntList.
480480
481481
INPUT:
482482
483-
- obs -- a list of objects
483+
- ``obs`` -- a list of objects
484484
485485
OUTPUT: an IntList
486486
@@ -494,7 +494,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
494494
return [d[x] for x in obs]
495495

496496
def log_likelihood(self, obs, bint scale=True):
497-
"""
497+
r"""
498498
Return the logarithm of the probability that this model produced the given
499499
observation sequence. Thus the output is a non-positive number.
500500
@@ -549,7 +549,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
549549
return self._forward(obs)
550550

551551
def _forward(self, IntList obs):
552-
"""
552+
r"""
553553
Memory-efficient implementation of the forward algorithm, without scaling.
554554
555555
INPUT:
@@ -558,7 +558,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
558558
559559
OUTPUT:
560560
561-
- ``float`` -- the log of the probability that the model produced this sequence
561+
``float`` -- the log of the probability that the model produced this sequence
562562
563563
EXAMPLES::
564564
@@ -604,7 +604,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
604604
return log(alpha.sum())
605605

606606
def _forward_scale(self, IntList obs):
607-
"""
607+
r"""
608608
Memory-efficient implementation of the forward algorithm, with scaling.
609609
610610
INPUT:
@@ -613,7 +613,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
613613
614614
OUTPUT:
615615
616-
- ``float`` -- the log of the probability that the model produced this sequence
616+
``float`` -- the log of the probability that the model produced this sequence
617617
618618
EXAMPLES::
619619
@@ -693,7 +693,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
693693
return log_probability
694694

695695
def generate_sequence(self, Py_ssize_t length, starting_state=None):
696-
"""
696+
r"""
697697
Return a sample of the given length from this HMM.
698698
699699
INPUT:
@@ -704,7 +704,6 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
704704
instead of the initial probabilities to determine the
705705
starting state.
706706
707-
708707
OUTPUT:
709708
710709
- an :class:`IntList` or list of emission symbols
@@ -812,7 +811,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
812811
return self._IntList_to_emission_symbols(obs), states
813812

814813
cdef int _gen_symbol(self, int q, double r):
815-
"""
814+
r"""
816815
Generate a symbol in state q using the randomly chosen
817816
floating point number r, which should be between 0 and 1.
818817
@@ -823,7 +822,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
823822
824823
OUTPUT:
825824
826-
- a nonnegative int
825+
a nonnegative int
827826
828827
EXAMPLES::
829828
@@ -848,7 +847,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
848847
return self.n_out - 1
849848

850849
def viterbi(self, obs, log_scale=True):
851-
"""
850+
r"""
852851
Determine "the" hidden sequence of states that is most likely
853852
to produce the given sequence seq of observations, along with
854853
the probability that this hidden sequence actually produced
@@ -899,7 +898,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
899898
return self._viterbi(obs)
900899

901900
cpdef _viterbi(self, IntList obs):
902-
"""
901+
r"""
903902
Used internally to compute the viterbi path, without
904903
rescaling. This can be useful for short sequences.
905904
@@ -979,12 +978,12 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
979978

980979

981980
cpdef _viterbi_scale(self, IntList obs):
982-
"""
981+
r"""
983982
Used internally to compute the viterbi path with rescaling.
984983
985984
INPUT:
986985
987-
- obs -- IntList
986+
- ``obs`` -- IntList
988987
989988
OUTPUT:
990989
@@ -1110,7 +1109,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
11101109
return beta
11111110

11121111
cdef _forward_scale_all(self, IntList obs):
1113-
"""
1112+
r"""
11141113
Return scaled values alpha_t(i), the sequence of scalings, and
11151114
the log probability.
11161115
@@ -1171,7 +1170,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
11711170
return alpha, scale, log_probability
11721171

11731172
cdef TimeSeries _baum_welch_xi(self, TimeSeries alpha, TimeSeries beta, IntList obs):
1174-
"""
1173+
r"""
11751174
Used internally to compute the scaled quantity xi_t(i,j)
11761175
appearing in the Baum-Welch reestimation algorithm.
11771176
@@ -1204,7 +1203,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
12041203
return xi
12051204

12061205
def baum_welch(self, obs, int max_iter=100, double log_likelihood_cutoff=1e-4, bint fix_emissions=False):
1207-
"""
1206+
r"""
12081207
Given an observation sequence obs, improve this HMM using the
12091208
Baum-Welch algorithm to increase the probability of observing obs.
12101209
@@ -1225,8 +1224,8 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
12251224
12261225
OUTPUT:
12271226
1228-
- changes the model in places, and returns the log
1229-
likelihood and number of iterations.
1227+
changes the model in place, and returns the log
1228+
likelihood and number of iterations.
12301229
12311230
EXAMPLES::
12321231
@@ -1355,7 +1354,7 @@ cdef class DiscreteHiddenMarkovModel(HiddenMarkovModel):
13551354

13561355
# Keep this -- it's for backwards compatibility with the GHMM based implementation
13571356
def unpickle_discrete_hmm_v0(A, B, pi, emission_symbols, name):
1358-
"""
1357+
r"""
13591358
TESTS::
13601359
13611360
sage: m = hmm.DiscreteHiddenMarkovModel([[0.4,0.6],[0.1,0.9]], [[0.0,1.0],[0.5,0.5]], [1,0])

0 commit comments

Comments
 (0)