multinomial.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>CertainTrust with Multinomial Opinion Representation</title>
  6. <script type="text/javascript" src="CertainTrust.js"></script>
  7. <script type="text/javascript" src="certainTrustHTI.js"></script>
  8. <link rel="stylesheet" type="text/css" href="certainTrustHTI.css"/>
  9. <style type="text/css">
  10. body {
  11. text-align: center;
  12. }
  13. table {
  14. margin-left:auto;
  15. margin-right:auto;
  16. }
  17. th:after {
  18. content: ":";
  19. }
  20. tr#splitpoints > th:after {
  21. content: "";
  22. }
  23. tr#frequentist-mles > td:after, tr#posterior-mles > td:after {
  24. content: " %";
  25. }
  26. input {
  27. width: 5em;
  28. }
  29. ul#graph {
  30. position: relative;
  31. margin-left: auto;
  32. margin-right: auto;
  33. padding: 0;
  34. }
  35. ul#graph li {
  36. border: 1px solid #000;
  37. bottom: 0;
  38. list-style:none;
  39. margin: 0;
  40. padding: 0;
  41. position: absolute;
  42. text-align: center;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <script type="text/javascript">
  48. var ctObject = new CertainTrust(50);
  49. var hti = new CertainTrustHTI(ctObject, { readonly: true, confidence: { width: 1 } } );
  50. var CATEGORIES = new Array(1, 5, 14, 8, 4);
  51. var PRIORS = new Array(1, 1, 1, 1, 1);
  52. var DEFAULT_CATEGORY = 4;
  53. var GRAPH_HEIGHT = 300;
  54. var GRAPH_BAR_WIDTH = 60;
  55. if (CATEGORIES.length !== PRIORS.length)
  56. alert("Count of Categories and Prior do not match! Expect bogus output…");
  57. function _experiencesChangedKeyPress(e) {
  58. if (e.keyCode != 13)
  59. return;
  60. _experiencesChangedBlur();
  61. }
  62. function _experiencesChangedBlur() {
  63. var split = _findSplitPoint();
  64. _betaAlphaRangeChanged(split);
  65. }
  66. function _findSplitPoint() {
  67. var splits = document.getElementsByName('splitpoints');
  68. for (var i = 0; i < splits.length; ++i)
  69. if (splits[i].checked == true)
  70. return i;
  71. return DEFAULT_CATEGORY - 1;
  72. }
  73. // rounds numbers to at most 3 decimal places
  74. function _formatNumber(number) {
  75. return Math.round(number * 1000) / 1000;
  76. }
  77. function _betaAlphaRangeChanged(split) {
  78. // calculate amount of positive and negative experiences and a maximum
  79. var alpha = 0, beta = 0, maxv = 0;
  80. for (var i = 0; i < split; ++i) {
  81. var v = parseInt(document.getElementById('experience-' + i).value);
  82. var p = parseFloat(document.getElementById('prior-' + i).value);
  83. beta += v;
  84. maxv = Math.max(v + p, maxv);
  85. }
  86. for (var i = split; i < CATEGORIES.length; ++i) {
  87. var v = parseInt(document.getElementById('experience-' + i).value);
  88. var p = parseFloat(document.getElementById('prior-' + i).value);
  89. alpha += v;
  90. maxv = Math.max(v + p, maxv);
  91. }
  92. // update HTI
  93. ctObject.setRS(alpha, beta);
  94. // delete the current bar chart
  95. var graph = document.getElementById('graph');
  96. graph.setAttribute('style', 'height: ' + GRAPH_HEIGHT + 'px;width: ' + (GRAPH_BAR_WIDTH * CATEGORIES.length) + 'px;');
  97. while (graph.lastChild !== null)
  98. graph.removeChild(graph.lastChild);
  99. var all = alpha + beta;
  100. ++maxv;
  101. var scale = GRAPH_HEIGHT / maxv;
  102. // add up all priors
  103. var postall = all;
  104. for (var i = 0; i < PRIORS.length; ++i)
  105. postall += PRIORS[i];
  106. for (var i = 0; i < CATEGORIES.length; ++i) {
  107. var a = parseInt(document.getElementById('experience-' + i).value);
  108. var prior = parseFloat(document.getElementById('prior-' + i).value);
  109. // calculate and set the three table fields
  110. var freq = (100 * a) / all;
  111. document.getElementById('frequentist-mle-' + i).firstChild.nodeValue = _formatNumber(freq);
  112. var post = (100 * (a + prior)) / postall;
  113. document.getElementById('posterior-mle-' + i).firstChild.nodeValue = _formatNumber(post);
  114. var K = 7.87; // FIXME: Magic number
  115. var n = all;
  116. var p = a/n;
  117. var goodman = hti.intervalCertainty(K, a, n, p, 1);
  118. var estimate = 1 - (goodman.upper - goodman.lower);
  119. document.getElementById('certainty-' + i).firstChild.nodeValue = _formatNumber(estimate);
  120. // draw the bar chart
  121. var element = document.createTextNode(_formatNumber(estimate));
  122. var bar = document.createElement('li');
  123. bar.appendChild(element);
  124. var grey = document.createElement('li');
  125. var styling = 'left: ' + (i * GRAPH_BAR_WIDTH) + 'px;';
  126. styling += 'width: ' + (GRAPH_BAR_WIDTH - 1) + 'px;';
  127. grey.setAttribute('style', styling + 'background-color: grey;height: ' + Math.round(scale * prior) + 'px;');
  128. styling += 'height: ' + Math.round(scale * (a + prior)) + 'px;';
  129. // gradient is from red → yellow → green (adding green → substracting red)
  130. var colorpos = Math.round(((255 * 2) / (CATEGORIES.length - 1)) * i);
  131. if (colorpos < 255)
  132. styling += 'background-color: rgb(255, ' + colorpos + ', 0);';
  133. else
  134. styling += 'background-color: rgb(' + (510 - colorpos) + ', 255, 0);';
  135. bar.setAttribute('style', styling);
  136. graph.appendChild(bar);
  137. graph.appendChild(grey);
  138. }
  139. }
  140. function _categorySplitClicked() {
  141. _betaAlphaRangeChanged(parseInt(this.value));
  142. }
  143. </script>
  144. <div><ul id="graph"></ul></div>
  145. <table>
  146. <tr id="frequentist-mles"><th>Frequentist MLE</th></tr>
  147. <tr id="posterior-mles"><th>Posterior MLE</th></tr>
  148. <tr id="certainties"><th>Certainty</th></tr>
  149. <tr id="experiences"><th>Experience</th></tr>
  150. <tr id="priors"><th>Prior</th></tr>
  151. <tr id="splitpoints"><th>&gt;=</th></tr>
  152. </table>
  153. <script>
  154. function _addCell(id, element) {
  155. var cell = document.createElement('td');
  156. cell.appendChild(element);
  157. document.getElementById(id).appendChild(cell);
  158. }
  159. function _addTextCell(id, textid, value) {
  160. var element = document.createTextNode(value);
  161. var cell = document.createElement('td');
  162. cell.setAttribute('id', textid);
  163. cell.appendChild(element);
  164. document.getElementById(id).appendChild(cell);
  165. }
  166. function _addInput(parentid, base, nr, value) {
  167. var input = document.createElement('input');
  168. input.setAttribute('type', 'text');
  169. input.setAttribute('id', base + '-' + nr);
  170. input.setAttribute('value', value);
  171. input.addEventListener('keypress', _experiencesChangedKeyPress, false);
  172. input.addEventListener('blur', _experiencesChangedBlur, false);
  173. _addCell(parentid, input);
  174. }
  175. for (var i = 0; i < CATEGORIES.length; ++i) {
  176. _addTextCell('frequentist-mles', 'frequentist-mle-' + i, '0');
  177. _addTextCell('posterior-mles', 'posterior-mle-' + i, '0');
  178. _addTextCell('certainties', 'certainty-' + i, '0.0');
  179. _addInput('experiences', 'experience', i, CATEGORIES[i]);
  180. _addInput('priors', 'prior', i, PRIORS[i]);
  181. var splitpoint = document.createElement('input');
  182. splitpoint.setAttribute('type', 'radio');
  183. splitpoint.setAttribute('id', 'splitpoint-' + i);
  184. splitpoint.setAttribute('name', 'splitpoints');
  185. splitpoint.setAttribute('value', i);
  186. splitpoint.addEventListener('click', _categorySplitClicked, false);
  187. _addCell('splitpoints', splitpoint);
  188. }
  189. document.getElementById('splitpoint-' + (DEFAULT_CATEGORY - 1)).setAttribute('checked', 'checked');
  190. </script>
  191. <div>
  192. </div>
  193. <script>_betaAlphaRangeChanged(_findSplitPoint());</script>
  194. </body>
  195. </html>