Debian Codegraph @

fon/Distributions_and_Transition.cpp

raw symbols SCIP
1 /* Distributions_and_Transition.cpp
2 *
3 * Copyright (C) 1997-2011,2015,2016,2017 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "Distributions_and_Transition.h"
20
21 autoTransition Distributions_to_Transition (Distributions underlying, Distributions surface, integer environment,
22 Transition adjacency, bool greedy)
23 {
24 try {
25 if (! underlying) return autoTransition();
26
27 /*
28 * Preconditions: range check and matrix matching.
29 */
30 if (environment < 1 || environment > underlying -> numberOfColumns)
31 Melder_throw (U"Environment (", environment, U") out of range (1-", underlying -> numberOfColumns, U").");
32 if (surface && (underlying -> numberOfColumns != surface -> numberOfColumns || underlying -> numberOfRows != surface -> numberOfRows))
33 Melder_throw (U"Sizes of underlying and surface distributions do not match.");
34 if (adjacency && adjacency -> numberOfStates != underlying -> numberOfColumns)
35 Melder_throw (U"Number of states (", adjacency -> numberOfStates, U") in adjacency matrix "
36 U"does not match number of distributions (", underlying -> numberOfColumns, U")");
37
38 /*
39 * Defaults.
40 */
41 if (! surface) surface = underlying;
42
43 /*
44 * Create the output object.
45 */
46 autoTransition thee = Transition_create (underlying -> numberOfColumns);
47
48 /*
49 * Copy labels and set name.
50 */
51 for (integer i = 1; i <= thy numberOfStates; i ++) {
52 thy stateLabels [i] = Melder_dup (underlying -> columnLabels [i].get());
53 }
54 Thing_setName (thee.get(), underlying -> columnLabels [environment].get());
55
56 /*
57 * Compute the off-diagonal elements of the transition matrix in environment 'environment'.
58 */
59 for (integer i = 1; i <= thy numberOfStates; i ++) {
60
61 /*
62 * How many states are available for the learner to step to (excluding current state)?
63 */
64 integer numberOfAdjacentStates;
65 if (adjacency) {
66 numberOfAdjacentStates = 0;
67 for (integer j = 1; j <= thy numberOfStates; j ++)
68 if (i != j && adjacency -> data [i] [j] != 0.0)
69 numberOfAdjacentStates ++;
70 } else {
71 numberOfAdjacentStates = thy numberOfStates - 1;
72 }
73
74 /*
75 * Try all possible steps to adjacent states.
76 */
77 for (integer j = 1; j <= thy numberOfStates; j ++) if (i != j) {
78
79 /*
80 * Local: grammar step only possible to adjacent grammar.
81 */
82 if (adjacency && adjacency -> data [i] [j] == 0) continue;
83
84 /*
85 * Compute element (i, j): sum over all possible data.
86 */
87 for (integer m = 1; m <= underlying -> numberOfRows; m ++) {
88
89 /*
90 * Error-driven: grammar step only triggered by positive evidence.
91 * If the datum does not conflict with the current hypothesis (i), ignore it.
92 */
93 if (underlying -> data [m] [i] != 0.0) continue;
94
95 /*
96 * Greedy: grammar step only taken if new grammar accepts datum.
97 */
98 if (greedy && underlying -> data [m] [j] == 0) continue;
99
100 /*
101 * The step is taken if this datum occurs and this grammar (j) is chosen.
102 */
103 thy data [i] [j] += surface -> data [m] [environment] / numberOfAdjacentStates;
104 }
105 }
106 }
107
108 /*
109 * Compute the elements on the diagonal, so that the sum of each row is unity.
110 */
111 for (integer i = 1; i <= thy numberOfStates; i ++) {
112 longdouble sum = 0.0;
113 for (integer j = 1; j <= thy numberOfStates; j ++) if (j != i)
114 sum += thy data [i] [j];
115 thy data [i] [i] = sum > 1.0 ? 0.0 : 1.0 - (double) sum; // guard against rounding errors
116 }
117
118 return thee;
119 } catch (MelderError) {
120 Melder_throw (underlying, U": Transition not computed.");
121 }
122 }
123
124 autoDistributions Distributions_Transition_map (Distributions me, Transition map) {
125 try {
126 /*
127 * Preconditions: matrix matching.
128 */
129 if (map -> numberOfStates != my numberOfRows)
130 Melder_throw (U"Number of data (", map -> numberOfStates, U") in mapping matrix "
131 U"does not match number of data (", my numberOfRows, U") in distribution.");
132
133 /*
134 * Create the output object.
135 */
136 autoDistributions thee = Data_copy (me);
137
138 /*
139 * Compute the elements of the surface distributions.
140 */
141 for (integer irow = 1; irow <= my numberOfRows; irow ++) {
142 for (integer icol = 1; icol <= my numberOfColumns; icol ++) {
143 thy data [irow] [icol] = 0.0;
144 for (integer istate = 1; istate <= map -> numberOfStates; istate ++) {
145 thy data [irow] [icol] += my data [istate] [icol] * map -> data [istate] [irow];
146 }
147 }
148 }
149
150 return thee;
151 } catch (MelderError) {
152 Melder_throw (me, U": not mapped to Transition.");
153 }
154 }
155
156 autoDistributions Transition_to_Distributions_conflate (Transition me) {
157 try {
158 autoDistributions thee = Distributions_create (my numberOfStates, 1);
159
160 /*
161 * Copy labels.
162 */
163 for (integer i = 1; i <= my numberOfStates; i ++) {
164 thy rowLabels [i] = Melder_dup (my stateLabels [i].get());
165 }
166
167 /*
168 * Average rows.
169 */
170 for (integer i = 1; i <= my numberOfStates; i ++) {
171 for (integer j = 1; j <= my numberOfStates; j ++)
172 thy data [i] [1] += my data [j] [i];
173 thy data [i] [1] /= my numberOfStates;
174 }
175
176 return thee;
177 } catch (MelderError) {
178 Melder_throw (me, U": not conflated to Distributions.");
179 }
180 }
181
182 /* End of file Distributions_and_Transition.cpp */