-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscottish-english-example.html
300 lines (244 loc) · 7.04 KB
/
scottish-english-example.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Are you Scottish or English?</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Trucy Phan">
<!-- Date: 2012-07-03 -->
<style type="text/css">
body {
font-family: "Helvetica Neue", "Helvetica", "Arial";
}
a {
color: #666;
text-decoration: none;
cursor: pointer;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
#questions {
width: 10000px;
-webkit-transition: margin-left 0.25s ease;
-moz-transition: margin-left 0.25s ease;
-ms-transition: margin-left 0.25s ease;
-o-transition: margin-left 0.25s ease;
transition: margin-left 0.25s ease;
}
#questionsFrame{
text-align: center;
width: 600px;
height: 700px;
overflow: hidden;
margin: 0 auto 20px auto;
}
#questions .question {
width: 600px;
height:600px;
overflow:hidden;
float:left;
text-align: center;
font-size: 14px;
}
#questions .question p {
font-size: 18px;
}
input[type=submit]{
margin-top: 30px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="./js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="./js/jquery-ui-1.8.21.custom.min.js"></script>
<script type="text/javascript">
var test_x = new Array();
$(document).ready(function() {
$('input[type=radio], input[type=submit]').click(function(){
var pos = $('#questions').css('margin-left').replace('px', '') - 600;
$('#questions').css(
"margin-left", pos);
});
$('input[type=radio][name=shortbread]:nth(0)').click(function(){
test_x[0] = 1;
});
$('input:radio[name=shortbread]:nth(1)').click(function(){
test_x[0] = 0;
});
$('input:radio[name=lager]:nth(0)').click(function(){
test_x[1] = 1;
});
$('input:radio[name=lager]:nth(1)').click(function(){
test_x[1] = 0;
});
$('input:radio[name=whiskey]:nth(0)').click(function(){
test_x[2] = 1;
});
$('input:radio[name=whiskey]:nth(1)').click(function(){
test_x[2] = 0;
});
$('input:radio[name=porridge]:nth(0)').click(function(){
test_x[3] = 1;
});
$('input:radio[name=porridge]:nth(1)').click(function(){
test_x[3] = 0;
});
$('input:radio[name=football]:nth(0)').click(function(){
test_x[4] = 1;
});
$('input:radio[name=football]:nth(1)').click(function(){
test_x[4] = 0;
});
$('input[name=submit]').click(function(e){
e.preventDefault();
/*
Loop through all selected radio buttons.
Build array and pass to calc_prob.
*/
calc_prob(test_x);
return false;
});
});
</script>
</head>
<body>
<div id="questionsFrame">
<h2>Are you Scottish or English?</h2>
<form name="input" onsubmit="test()">
<div id="questions" class="clearfix">
<div class="question">
<p>1. Do you like shortbread?</p>
<input type="radio" name="shortbread" value="y" /> Yes<br />
<input type="radio" name="shortbread" value="n" /> No<br />
</div>
<div class="question">
<p>2. Do you like lager?</p>
<input type="radio" name="lager" value="y" /> Yes<br />
<input type="radio" name="lager" value="n" /> No<br />
</div>
<div class="question">
<p>3. Do you drink whiskey?</p>
<input type="radio" name="whiskey" value="y" /> Yes<br />
<input type="radio" name="whiskey" value="n" /> No<br />
</div>
<div class="question">
<p>4. Do you eat porridge?</p>
<input type="radio" name="porridge" value="y" /> Yes<br />
<input type="radio" name="porridge" value="n" /> No<br />
</div>
<div class="question">
<p>5. Did you watch England play football?</p>
<input type="radio" name="football" value="y" /> Yes<br />
<input type="radio" name="football" value="n" /> No<br />
</div>
<div class="question">
<input type="submit" name="submit" id="submit" value="Try me." />
</div>
<div class="question final">
<a href="/">Try again</a>
</div>
</div>
</form>
</div>
<script type=text/javascript>
function calc_prob(newX) {
/* JavaScript version of this Matlab example: http://www.inf.ed.ac.uk/teaching/courses/lfd/lectures/lfd_2005_naive.pdf
Classify a person as English or Scottish given their nationality and the following attributes:
* likes shortbread
* likes lager
* drinks whiskey
* eats porridge
* watched England play football
1 reads positive for the attribute and 0 is negative (e.g. does not eat porridge)
*/
// English people and their (dis)likes
var xE = [
[0, 1, 1, 1, 0, 0], // 1/2
[0, 0, 1, 1, 1, 0], // 1/2
[1, 1, 0, 0, 0, 0], // 1/3
[1, 1, 1, 0, 0, 1], // 2/3
[1, 0, 1, 1, 1, 0], // 2/3
];
// Scottish people and their (dis)likes
var xS = [
[1, 1, 1, 1, 1, 1, 1], // 1
[0, 1, 1, 1, 1, 0, 0], // 4/7
[0, 1, 1, 0, 0, 1, 1], // 4/7
[1, 0, 0, 1, 1, 0, 0], // 3/7
[1, 1, 0, 0, 1, 0, 0] // 3/7
];
// probabilities that a person in the db is Scottish or English
var pE = xE[0].length/(xE[0].length + xS[0].length); // 6/13
var pS = 1 - pE; // 7/13
var px_E = new Array();
var px_S = new Array();
var calc_px_S = 1;
var calc_px_E = 1;
// initialize arrays with zeros
for(i=0; i<xE.length; i++) {
px_E[i] = 0;
px_S[i] = 0;
}
// probability that a given attribute will be positive given that the person is English
for( var i in xE ) { // 5
for( var j in xE[0] ) { // 6
px_E[i] += xE[i][j]/xE[0].length;
}
calc_px_E *= px_E[i];
}
// probability that a given attribute will be positive given that the person is Scottish
// if x = 1, the average already represents the probability of x = 1
// if x = 0, take 1 minus the mean because the rest of the data set is 0's
for( i in xS ) { // 5
for( j in xS[0] ) { // 7
px_S[i] += xS[i][j]/xS[0].length;
}
if(newX[i] == 1){
px_S[i] = px_S[i];
}
else {
px_S[i] = 1 - px_S[i];
}
calc_px_S *= px_S[i];
}
// calculate the probability of someone being Scottish given set of x (dis)likes
// p(S|x) = p(x|S) . p(S) / p(x|S)p(S) + p(x|E)p(E)
var prob_S = calc_px_S*pS/((calc_px_S*pS) + (calc_px_E*pE));
/*if (prob_S >= 0.5) {
document.write('<div class=\"question\"><img src =\"./images/scottish.jpg\" /><p>Congratulations! You\'re Scottish.</p></div>');
// return("Congratulations! You're Scottish.");
}
else if (prob_S == 0.5) {
return("Hmm, we're not sure what you are.");
}
else {
return("We think you're English.");
}*/
if (prob_S >= 0.5) {
$('.final').prepend('<img src ="./images/scottish.jpg" /><p>Congratulations! You\'re Scottish.</p>');
}
else if (prob_S == 0.5) {
$('.final').prepend('<p>We have no idea what you are!</p>');
}
else {
$('.final').prepend('<img src ="./images/english.jpg" /><p>We think you\'re English!</p>');
}
}
</script>
</body>
</html>