| 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"cxx . . $ `<file>/fon/Distributions_and_Transition.h`/ |
| 20 | |
| 21 | autoTransitioncxx . . $ autoTransition# Distributions_to_Transitioncxx . . $ Distributions_to_Transition(cbcbbeee1789cd7f). (Distributionscxx . . $ Distributions# underlying, Distributionscxx . . $ Distributions# surface, integercxx . . $ integer# environment, |
| 22 | Transitioncxx . . $ Transition# adjacency, bool greedy) |
| 23 | { |
| 24 | try { |
| 25 | if (! underlying) return autoTransitioncxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).(); |
| 26 | |
| 27 | /* |
| 28 | * Preconditions: range check and matrix matching. |
| 29 | */ |
| 30 | if (environment < 1 || environment > underlying -> numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.) |
| 31 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Environment (", environment, U") out of range (1-", underlying -> numberOfColumns, U")."); |
| 32 | if (surface && (underlying -> numberOfColumnscxx . . $ structTableOfReal#numberOfColumns. != surface -> numberOfColumnscxx . . $ structTableOfReal#numberOfColumns. || underlying -> numberOfRowscxx . . $ structTableOfReal#numberOfRows. != surface -> numberOfRowscxx . . $ structTableOfReal#numberOfRows.)) |
| 33 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Sizes of underlying and surface distributions do not match."); |
| 34 | if (adjacency && adjacency -> numberOfStatescxx . . $ structTransition#numberOfStates. != underlying -> numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.) |
| 35 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (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 | autoTransitioncxx . . $ autoTransition# thee = Transition_createcxx . . $ Transition_create(e8d3201cb23395bc). (underlying -> numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 47 | |
| 48 | /* |
| 49 | * Copy labels and set name. |
| 50 | */ |
| 51 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; i ++) { |
| 52 | thycxx . . $ `melder/melder.h:45:9`! stateLabelscxx . . $ structTransition#stateLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (underlying -> columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 53 | } |
| 54 | Thing_setNamecxx . . $ Thing_setName(b5a42e5e01c267b5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), underlying -> columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).environment].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 55 | |
| 56 | /* |
| 57 | * Compute the off-diagonal elements of the transition matrix in environment 'environment'. |
| 58 | */ |
| 59 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; i ++) { |
| 60 | |
| 61 | /* |
| 62 | * How many states are available for the learner to step to (excluding current state)? |
| 63 | */ |
| 64 | integercxx . . $ integer# numberOfAdjacentStates; |
| 65 | if (adjacency) { |
| 66 | numberOfAdjacentStates = 0; |
| 67 | for (integercxx . . $ integer# j = 1; j <= thycxx . . $ `melder/melder.h:45:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; j ++) |
| 68 | if (i != j && adjacency -> datacxx . . $ structTransition#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] != 0.0) |
| 69 | numberOfAdjacentStates ++; |
| 70 | } else { |
| 71 | numberOfAdjacentStates = thycxx . . $ `melder/melder.h:45:9`! numberOfStatescxx . . $ structTransition#numberOfStates. - 1; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Try all possible steps to adjacent states. |
| 76 | */ |
| 77 | for (integercxx . . $ integer# j = 1; j <= thycxx . . $ `melder/melder.h:45:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; j ++) if (i != j) { |
| 78 | |
| 79 | /* |
| 80 | * Local: grammar step only possible to adjacent grammar. |
| 81 | */ |
| 82 | if (adjacency && adjacency -> datacxx . . $ structTransition#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] == 0) continue; |
| 83 | |
| 84 | /* |
| 85 | * Compute element (i, j): sum over all possible data. |
| 86 | */ |
| 87 | for (integercxx . . $ integer# m = 1; m <= underlying -> numberOfRowscxx . . $ structTableOfReal#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 -> datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).m] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] != 0.0) continue; |
| 94 | |
| 95 | /* |
| 96 | * Greedy: grammar step only taken if new grammar accepts datum. |
| 97 | */ |
| 98 | if (greedy && underlying -> datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).m] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] == 0) continue; |
| 99 | |
| 100 | /* |
| 101 | * The step is taken if this datum occurs and this grammar (j) is chosen. |
| 102 | */ |
| 103 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTransition#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] += surface -> datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).m] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).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 (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; i ++) { |
| 112 | longdoublecxx . . $ longdouble# sum = 0.0; |
| 113 | for (integercxx . . $ integer# j = 1; j <= thycxx . . $ `melder/melder.h:45:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; j ++) if (j != i) |
| 114 | sum += thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTransition#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j]; |
| 115 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTransition#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = sum > 1.0 ? 0.0 : 1.0 - (double) sum; // guard against rounding errors |
| 116 | } |
| 117 | |
| 118 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 119 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 120 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (underlying, U": Transition not computed."); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | autoDistributionscxx . . $ autoDistributions# Distributions_Transition_mapcxx . . $ Distributions_Transition_map(e6904883d31e4915). (Distributionscxx . . $ Distributions# me, Transitioncxx . . $ Transition# map) { |
| 125 | try { |
| 126 | /* |
| 127 | * Preconditions: matrix matching. |
| 128 | */ |
| 129 | if (map -> numberOfStatescxx . . $ structTransition#numberOfStates. != mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.) |
| 130 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Number of data (", map -> numberOfStates, U") in mapping matrix " |
| 131 | U"does not match number of data (", mycxx . . $ `melder/melder.h:44:9`! numberOfRows, U") in distribution."); |
| 132 | |
| 133 | /* |
| 134 | * Create the output object. |
| 135 | */ |
| 136 | autoDistributionscxx . . $ autoDistributions# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 137 | |
| 138 | /* |
| 139 | * Compute the elements of the surface distributions. |
| 140 | */ |
| 141 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 142 | for (integercxx . . $ integer# icol = 1; icol <= mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; icol ++) { |
| 143 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = 0.0; |
| 144 | for (integercxx . . $ integer# istate = 1; istate <= map -> numberOfStatescxx . . $ structTransition#numberOfStates.; istate ++) { |
| 145 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] += mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).istate] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] * map -> datacxx . . $ structTransition#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).istate] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow]; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 151 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 152 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not mapped to Transition."); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | autoDistributionscxx . . $ autoDistributions# Transition_to_Distributions_conflatecxx . . $ Transition_to_Distributions_conflate(3ff48c356c22d471). (Transitioncxx . . $ Transition# me) { |
| 157 | try { |
| 158 | autoDistributionscxx . . $ autoDistributions# thee = Distributions_createcxx . . $ Distributions_create(6e2acaa0535ba708). (mycxx . . $ `melder/melder.h:44:9`! numberOfStatescxx . . $ structTransition#numberOfStates., 1); |
| 159 | |
| 160 | /* |
| 161 | * Copy labels. |
| 162 | */ |
| 163 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; i ++) { |
| 164 | thycxx . . $ `melder/melder.h:45:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (mycxx . . $ `melder/melder.h:44:9`! stateLabelscxx . . $ structTransition#stateLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Average rows. |
| 169 | */ |
| 170 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; i ++) { |
| 171 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; j ++) |
| 172 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] += mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTransition#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 173 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] /= mycxx . . $ `melder/melder.h:44:9`! numberOfStatescxx . . $ structTransition#numberOfStates.; |
| 174 | } |
| 175 | |
| 176 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 177 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 178 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not conflated to Distributions."); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* End of file Distributions_and_Transition.cpp */ |