Distributions_and_Transition.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "Distributions_and_Transition.h"
  19. autoTransition Distributions_to_Transition (Distributions underlying, Distributions surface, integer environment,
  20. Transition adjacency, bool greedy)
  21. {
  22. try {
  23. if (! underlying) return autoTransition();
  24. /*
  25. * Preconditions: range check and matrix matching.
  26. */
  27. if (environment < 1 || environment > underlying -> numberOfColumns)
  28. Melder_throw (U"Environment (", environment, U") out of range (1-", underlying -> numberOfColumns, U").");
  29. if (surface && (underlying -> numberOfColumns != surface -> numberOfColumns || underlying -> numberOfRows != surface -> numberOfRows))
  30. Melder_throw (U"Sizes of underlying and surface distributions do not match.");
  31. if (adjacency && adjacency -> numberOfStates != underlying -> numberOfColumns)
  32. Melder_throw (U"Number of states (", adjacency -> numberOfStates, U") in adjacency matrix "
  33. U"does not match number of distributions (", underlying -> numberOfColumns, U")");
  34. /*
  35. * Defaults.
  36. */
  37. if (! surface) surface = underlying;
  38. /*
  39. * Create the output object.
  40. */
  41. autoTransition thee = Transition_create (underlying -> numberOfColumns);
  42. /*
  43. * Copy labels and set name.
  44. */
  45. for (integer i = 1; i <= thy numberOfStates; i ++) {
  46. thy stateLabels [i] = Melder_dup (underlying -> columnLabels [i].get());
  47. }
  48. Thing_setName (thee.get(), underlying -> columnLabels [environment].get());
  49. /*
  50. * Compute the off-diagonal elements of the transition matrix in environment 'environment'.
  51. */
  52. for (integer i = 1; i <= thy numberOfStates; i ++) {
  53. /*
  54. * How many states are available for the learner to step to (excluding current state)?
  55. */
  56. integer numberOfAdjacentStates;
  57. if (adjacency) {
  58. numberOfAdjacentStates = 0;
  59. for (integer j = 1; j <= thy numberOfStates; j ++)
  60. if (i != j && adjacency -> data [i] [j] != 0.0)
  61. numberOfAdjacentStates ++;
  62. } else {
  63. numberOfAdjacentStates = thy numberOfStates - 1;
  64. }
  65. /*
  66. * Try all possible steps to adjacent states.
  67. */
  68. for (integer j = 1; j <= thy numberOfStates; j ++) if (i != j) {
  69. /*
  70. * Local: grammar step only possible to adjacent grammar.
  71. */
  72. if (adjacency && adjacency -> data [i] [j] == 0) continue;
  73. /*
  74. * Compute element (i, j): sum over all possible data.
  75. */
  76. for (integer m = 1; m <= underlying -> numberOfRows; m ++) {
  77. /*
  78. * Error-driven: grammar step only triggered by positive evidence.
  79. * If the datum does not conflict with the current hypothesis (i), ignore it.
  80. */
  81. if (underlying -> data [m] [i] != 0.0) continue;
  82. /*
  83. * Greedy: grammar step only taken if new grammar accepts datum.
  84. */
  85. if (greedy && underlying -> data [m] [j] == 0) continue;
  86. /*
  87. * The step is taken if this datum occurs and this grammar (j) is chosen.
  88. */
  89. thy data [i] [j] += surface -> data [m] [environment] / numberOfAdjacentStates;
  90. }
  91. }
  92. }
  93. /*
  94. * Compute the elements on the diagonal, so that the sum of each row is unity.
  95. */
  96. for (integer i = 1; i <= thy numberOfStates; i ++) {
  97. longdouble sum = 0.0;
  98. for (integer j = 1; j <= thy numberOfStates; j ++) if (j != i)
  99. sum += thy data [i] [j];
  100. thy data [i] [i] = sum > 1.0 ? 0.0 : 1.0 - (double) sum; // guard against rounding errors
  101. }
  102. return thee;
  103. } catch (MelderError) {
  104. Melder_throw (underlying, U": Transition not computed.");
  105. }
  106. }
  107. autoDistributions Distributions_Transition_map (Distributions me, Transition map) {
  108. try {
  109. /*
  110. * Preconditions: matrix matching.
  111. */
  112. if (map -> numberOfStates != my numberOfRows)
  113. Melder_throw (U"Number of data (", map -> numberOfStates, U") in mapping matrix "
  114. U"does not match number of data (", my numberOfRows, U") in distribution.");
  115. /*
  116. * Create the output object.
  117. */
  118. autoDistributions thee = Data_copy (me);
  119. /*
  120. * Compute the elements of the surface distributions.
  121. */
  122. for (integer irow = 1; irow <= my numberOfRows; irow ++) {
  123. for (integer icol = 1; icol <= my numberOfColumns; icol ++) {
  124. thy data [irow] [icol] = 0.0;
  125. for (integer istate = 1; istate <= map -> numberOfStates; istate ++) {
  126. thy data [irow] [icol] += my data [istate] [icol] * map -> data [istate] [irow];
  127. }
  128. }
  129. }
  130. return thee;
  131. } catch (MelderError) {
  132. Melder_throw (me, U": not mapped to Transition.");
  133. }
  134. }
  135. autoDistributions Transition_to_Distributions_conflate (Transition me) {
  136. try {
  137. autoDistributions thee = Distributions_create (my numberOfStates, 1);
  138. /*
  139. * Copy labels.
  140. */
  141. for (integer i = 1; i <= my numberOfStates; i ++) {
  142. thy rowLabels [i] = Melder_dup (my stateLabels [i].get());
  143. }
  144. /*
  145. * Average rows.
  146. */
  147. for (integer i = 1; i <= my numberOfStates; i ++) {
  148. for (integer j = 1; j <= my numberOfStates; j ++)
  149. thy data [i] [1] += my data [j] [i];
  150. thy data [i] [1] /= my numberOfStates;
  151. }
  152. return thee;
  153. } catch (MelderError) {
  154. Melder_throw (me, U": not conflated to Distributions.");
  155. }
  156. }
  157. /* End of file Distributions_and_Transition.cpp */