Mercurial > forge
annotate main/statistics/inst/hmmgenerate.m @ 2689:e8b0cde34d1b octave-forge
Improved texinfo.
author | whyly |
---|---|
date | Sun, 15 Oct 2006 19:50:42 +0000 |
parents | a613a7093388 |
children | 762ed030be30 |
rev | line source |
---|---|
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
1 ## Copyright (C) 2006 Arno Onken |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
2 ## |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
3 ## This program is free software; you can redistribute it and/or modify |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
4 ## it under the terms of the GNU General Public License as published by |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
5 ## the Free Software Foundation; either version 2 of the License, or |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
6 ## (at your option) any later version. |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
7 ## |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
8 ## This program is distributed in the hope that it will be useful, |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
11 ## GNU General Public License for more details. |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
12 ## |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
13 ## You should have received a copy of the GNU General Public License |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
14 ## along with this program; if not, write to the Free Software |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
16 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
17 ## -*- texinfo -*- |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
18 ## @deftypefn {Function File} {[@var{sequence}, @var{states}] =} hmmgenerate (@var{len}, @var{transprob}, @var{outprob}) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
19 ## @deftypefnx {Function File} {} hmmgenerate (@dots{}, 'symbols', @var{symbols}) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
20 ## @deftypefnx {Function File} {} hmmgenerate (@dots{}, 'statenames', @var{statenames}) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
21 ## Generates an output sequence and hidden states for a Hidden Markov Model. |
2689 | 22 ## The model starts in state @code{1} at step @code{0} but will not include |
23 ## step @code{0} in the generated states and sequence. | |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
24 ## |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
25 ## Arguments are |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
26 ## |
2689 | 27 ## @itemize @bullet |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
28 ## @item |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
29 ## @var{len} is the number of steps to generate. @var{sequence} and |
2689 | 30 ## @var{states} will have @var{len} entries each. |
31 ## | |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
32 ## @item |
2689 | 33 ## @var{transprob} is the matrix of transition probabilities for the states. |
34 ## @code{transprob (i, j)} is the probability for a transition to state | |
35 ## @code{j} given state @code{i}. | |
36 ## | |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
37 ## @item |
2689 | 38 ## @var{outprob} is the matrix of output probabilities. |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
39 ## @code{outprob (i, j)} is the probability for generating output @code{j} |
2689 | 40 ## given state @code{i}. |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
41 ## @end itemize |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
42 ## |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
43 ## Return values are |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
44 ## |
2689 | 45 ## @itemize @bullet |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
46 ## @item |
2689 | 47 ## @var{sequence} is a vector of length @var{len} of the generated |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
48 ## outputs. The outputs are integers ranging from @code{1} to |
2689 | 49 ## @code{columns (outprob)}. |
50 ## | |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
51 ## @item |
2689 | 52 ## @var{states} is a vector of length @var{len} of the generated hidden |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
53 ## states. The states are integers ranging from @code{1} to |
2689 | 54 ## @code{columns (transprob)}. |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
55 ## @end itemize |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
56 ## |
2689 | 57 ## If @code{'symbols'} is specified, then the elements of @var{symbols} are |
58 ## used for the output sequence instead of integers ranging from @code{1} to | |
59 ## @code{columns (outprob)}. @var{symbols} can be a cell array. | |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
60 ## |
2689 | 61 ## If @code{'statenames'} is specified, then the elements of |
62 ## @var{statenames} are used for the states instead of integers ranging from | |
63 ## @code{1} to @code{columns (transprob)}. @var{statenames} can be a cell | |
64 ## array. | |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
65 ## |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
66 ## Examples: |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
67 ## |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
68 ## @example |
2689 | 69 ## @group |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
70 ## transprob = [0.8, 0.2; 0.4, 0.6]; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
71 ## outprob = [0.2, 0.4, 0.4; 0.7, 0.2, 0.1]; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
72 ## [sequence, states] = hmmgenerate (25, transprob, outprob) |
2689 | 73 ## @end group |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
74 ## |
2689 | 75 ## @group |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
76 ## symbols = @{'A', 'B', 'C'@}; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
77 ## statenames = @{'One', 'Two'@}; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
78 ## [sequence, states] = hmmgenerate (25, transprob, outprob, 'symbols', symbols, 'statenames', statenames) |
2689 | 79 ## @end group |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
80 ## @end example |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
81 ## |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
82 ## References: |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
83 ## |
2689 | 84 ## @enumerate |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
85 ## @item |
2689 | 86 ## W. L. Martinez and A. R. Martinez. @cite{Computational Statistics |
87 ## Handbook with MATLAB.} Chapman & Hall/CRC, pages 547-557, 2001. | |
88 ## | |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
89 ## @item |
2689 | 90 ## Wikipedia contributors. Hidden Markov model. @cite{Wikipedia, The Free |
91 ## Encyclopedia.} | |
92 ## @uref{http://en.wikipedia.org/w/index.php?title=Hidden_Markov_model&oldid=81100294}, | |
93 ## October 2006. | |
94 ## @end enumerate | |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
95 ## @end deftypefn |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
96 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
97 ## Author: Arno Onken <whyly@gmx.net> |
2689 | 98 ## Description: Output sequence and hidden states for a Hidden Markov Model |
2376
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
99 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
100 function [sequence, states] = hmmgenerate (len, transprob, outprob, varargin) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
101 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
102 # Usage string |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
103 ustring = "[sequence, states] = hmmgenerate (len, transprob, outprob [, 'symbols', symbols] [, 'statenames', statenames])"; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
104 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
105 # Check arguments |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
106 if (nargin < 3 || mod (length (varargin), 2) != 0) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
107 usage (ustring); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
108 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
109 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
110 if (! isscalar (len) || len < 0) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
111 error ("hmmgenerate: len must be a non-negative scalar value") |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
112 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
113 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
114 if (! ismatrix (transprob)) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
115 error ("hmmgenerate: transprob must be a non-empty numeric matrix"); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
116 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
117 if (! ismatrix (outprob)) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
118 error ("hmmgenerate: outprob must be a non-empty numeric matrix"); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
119 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
120 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
121 # nstate is the number of states of the Hidden Markov Model |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
122 nstate = rows (transprob); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
123 # noutput is the number of different outputs that the Hidden Markov Model |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
124 # can generate |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
125 noutput = columns (outprob); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
126 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
127 # Check whether transprob and outprob are feasible for a Hidden Markov |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
128 # Model |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
129 if (columns (transprob) != nstate) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
130 error ("hmmgenerate: transprob must be a square matrix"); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
131 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
132 if (rows (outprob) != nstate) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
133 error ("hmmgenerate: outprob must have the same number of rows as transprob"); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
134 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
135 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
136 # Flag for symbols |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
137 usesym = false; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
138 # Flag for statenames |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
139 usesn = false; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
140 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
141 # Process varargin |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
142 for i = 1:2:length (varargin) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
143 # There must be an identifier: 'symbols' or 'statenames' |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
144 if (! ischar (varargin {i})) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
145 usage (ustring); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
146 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
147 # Upper case is also fine |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
148 lowerarg = lower (varargin {i}); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
149 if (strcmp (lowerarg, 'symbols')) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
150 if (length (varargin {i + 1}) != noutput) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
151 error ("hmmgenerate: number of symbols does not match number of possible outputs"); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
152 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
153 usesym = true; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
154 # Use the following argument as symbols |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
155 symbols = varargin {i + 1}; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
156 # The same for statenames |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
157 elseif (strcmp (lowerarg, 'statenames')) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
158 if (length (varargin {i + 1}) != nstate) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
159 error ("hmmgenerate: number of statenames does not match number of states"); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
160 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
161 usesn = true; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
162 # Use the following argument as statenames |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
163 statenames = varargin {i + 1}; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
164 else |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
165 error ("hmmgenerate: expected 'symbols' or 'statenames' but found '%s'", varargin {i}); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
166 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
167 endfor |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
168 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
169 # Each row in transprob and outprob should contain probabilities |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
170 # => scale so that the sum is 1 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
171 # A zero row remains zero |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
172 # - for transprob |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
173 s = sum (transprob, 2); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
174 s (s == 0) = 1; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
175 transprob = transprob ./ (s * ones (1, nstate)); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
176 # - for outprob |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
177 s = sum (outprob, 2); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
178 s (s == 0) = 1; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
179 outprob = outprob ./ (s * ones (1, noutput)); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
180 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
181 # Generate sequences of uniformly distributed random numbers between 0 and |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
182 # 1 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
183 # - for the state transitions |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
184 transdraw = rand (1, len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
185 # - for the outputs |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
186 outdraw = rand (1, len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
187 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
188 # Generate the return vectors |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
189 # They remain unchanged if the according probability row of transprob |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
190 # and outprob contain, respectively, only zeros |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
191 sequence = ones (1, len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
192 states = ones (1, len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
193 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
194 # Calculate cumulated probabilities backwards for easy comparison with the |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
195 # generated random numbers |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
196 # Cumulated probability in first column must always be 1 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
197 # We might have a zero row |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
198 # - for transprob |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
199 transprob (:, end:-1:1) = cumsum (transprob (:, end:-1:1), 2); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
200 transprob (:, 1) = 1; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
201 # - for outprob |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
202 outprob (:, end:-1:1) = cumsum (outprob (:, end:-1:1), 2); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
203 outprob (:, 1) = 1; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
204 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
205 # cstate is the current state |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
206 # Start in state 1 but do not include it in the states vector |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
207 cstate = 1; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
208 for i = 1:len |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
209 # Compare the randon number i of transdraw to the cumulated probability |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
210 # of the state transition and set the transition accordingly |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
211 states (i) = sum (transdraw (i) <= transprob (cstate, :)); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
212 cstate = states (i); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
213 # The same for the output of the state |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
214 sequence (i) = sum (outdraw (i) <= outprob (cstate, :)); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
215 endfor |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
216 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
217 # Transform default matrices into symbols/statenames if requested |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
218 if (usesym) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
219 sequence = reshape (symbols (sequence), 1, len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
220 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
221 if (usesn) |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
222 states = reshape (statenames (states), 1, len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
223 endif |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
224 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
225 endfunction |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
226 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
227 %!test |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
228 %! len = 25; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
229 %! transprob = [0.8, 0.2; 0.4, 0.6]; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
230 %! outprob = [0.2, 0.4, 0.4; 0.7, 0.2, 0.1]; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
231 %! [sequence, states] = hmmgenerate (len, transprob, outprob); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
232 %! assert (length (sequence), len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
233 %! assert (length (states), len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
234 %! assert (min (sequence) >= 1); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
235 %! assert (max (sequence) <= columns (outprob)); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
236 %! assert (min (states) >= 1); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
237 %! assert (max (states) <= rows (transprob)); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
238 |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
239 %!test |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
240 %! len = 25; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
241 %! transprob = [0.8, 0.2; 0.4, 0.6]; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
242 %! outprob = [0.2, 0.4, 0.4; 0.7, 0.2, 0.1]; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
243 %! symbols = {'A', 'B', 'C'}; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
244 %! statenames = {'One', 'Two'}; |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
245 %! [sequence, states] = hmmgenerate (len, transprob, outprob, 'symbols', symbols, 'statenames', statenames); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
246 %! assert (length (sequence), len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
247 %! assert (length (states), len); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
248 %! assert (strcmp (sequence, 'A') + strcmp (sequence, 'B') + strcmp (sequence, 'C') == ones (1, len)); |
a613a7093388
Changed the directory structure of statistics to match the package system
hauberg
parents:
diff
changeset
|
249 %! assert (strcmp (states, 'One') + strcmp (states, 'Two') == ones (1, len)); |