-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcalc.html
123 lines (121 loc) · 4.5 KB
/
pcalc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html>
<head>
<title>Discrete Distribution Calculator</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<style type="text/css">
.panel > textarea.form-control {
border: none;
resize: vertical;
box-shadow: none;
}
#footer-text {
text-align: center;
color: #888;
}
</style>
</head>
<body>
<div class="container">
<h1>Discrete Distribution Calculator</h1>
<ul class="nav nav-tabs" role="tabset">
<li class="active" role="presentation"><a href="#calc" data-toggle="tab">Calculator</a></li>
<li role="presentation"><a href="#help" data-toggle="tab">Help</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" role="tabpanel" id="calc">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Input</h3>
</div>
<textarea id="input-area" class="form-control" rows=15></textarea>
</div>
<div id="err-alert" class="alert alert-danger" style="display:none;">
<strong>Error:</strong> <span id="err-msg"></span>
</div>
<div class="form-group">
<button id="calc-btn" class="btn btn-md btn-success">Calculate</button>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Output</h3>
</div>
<table class="table table-striped table-bordered">
<thead>
<tr><th>Query</th><th>Result</th></tr>
</thead>
<tbody id="output"></tbody>
</table>
</div>
<div class="row">
<div class="col-xs-12">
<p id="footer-text">
Made by <a href="/">Dylan Hart</a>
—
View source on <a href="https://github.com/dylanhart/pcalc">Github</a>
</p>
</div>
</div>
</div>
<div class="tab-pane" role="tabpanel" id="help">
<h3>Help</h3>
<p>To create a random variable, use the <code>~</code> operator with a random distribution. Random variables <strong>MUST</strong> start with a capital letter.</p>
<pre><code>
X ~ Geometric(.4)
</code></pre>
<p>The available distributions are: <code>Bernoulli</code>, <code>Binomial</code>, <code>Geometric</code>, <code>HyperGeometric</code>, and <code>Poisson</code>.
<p>To create a constant, use the <code>=</code> operator. Constants <strong>MUST</strong> start with a lowercase letter.</p>
<pre><code>
a = 2 + 2
</code></pre>
<p>Probilities may be calculated with the <code>P</code> function.</p>
<pre><code>
b = P(X > a)
</code></pre>
<p>To get the calculated data, you may use the query syntax. A query consists of an optional query tag followed by <code>?</code> followed by the value to be queried.</p>
<pre><code>
example one? b
? E(X)
</code></pre>
<p>The output for these queries is:</p>
<table class="table table-striped table-bordered">
<thead>
<tr><th>Query</th><th>Result</th></tr>
</thead>
<tbody id="output"><tr><td>example one</td><td>0.12959999999999994</td></tr><tr><td></td><td>2.5</td></tr></tbody>
</table>
</div>
</div>
<script type="text/javascript" src="parser.js"></script>
<script type="text/javascript">
window.onload = function() {
var btn = document.getElementById('calc-btn');
var input = document.getElementById('input-area');
var output = document.getElementById('output');
var errAlert = document.getElementById('err-alert');
var errMsg = document.getElementById('err-msg');
btn.addEventListener('click', function() {
try {
var out = parser.parse(input.value);
console.log(out);
var queries = out.filter(Array.isArray);
console.log(queries);
var html = queries.reduce(function(acc, q) {
return acc + '<tr><td>' + q[0] + '</td><td>' + q[1] + '</td></tr>';
}, '');
output.innerHTML = html;
errAlert.style = "display:none;";
} catch (e) {
errMsg.innerHTML = e.message;
errAlert.style = "display:block;";
}
});
};
</script>
</div>
</body>
</html>