Skip to content

Commit 3abaf8e

Browse files
Solutions to Exercises from STA663-2015
1 parent ca1a701 commit 3abaf8e

8 files changed

+7596
-0
lines changed

PublishedSolutions/Exercise05-Solutions.ipynb

Lines changed: 617 additions & 0 deletions
Large diffs are not rendered by default.

PublishedSolutions/Exercise06-Solutions.ipynb

Lines changed: 765 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:1f652cb57d42a081aa2ae79bae5664160b451147c9a2113e053bb812c7c2ae68"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "code",
13+
"collapsed": false,
14+
"input": [
15+
"import string\n",
16+
"import numpy as np\n",
17+
"from collections import Counter\n",
18+
"import urllib2"
19+
],
20+
"language": "python",
21+
"metadata": {},
22+
"outputs": [],
23+
"prompt_number": 1
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"### Exercise 1"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"collapsed": false,
35+
"input": [
36+
"# Possible solution\n",
37+
"\n",
38+
"x3 = [3*i for i in range(1, (1000+2)/3)]\n",
39+
"x5 = [5*i for i in range(1, (1000+4)/5)]\n",
40+
"x35 = [15*i for i in range(1, (1000+14)/15)]\n",
41+
"print sum(x3) + sum(x5) - sum(x35)"
42+
],
43+
"language": "python",
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"output_type": "stream",
48+
"stream": "stdout",
49+
"text": [
50+
"233168\n"
51+
]
52+
}
53+
],
54+
"prompt_number": 2
55+
},
56+
{
57+
"cell_type": "code",
58+
"collapsed": false,
59+
"input": [
60+
"# Alternative solution\n",
61+
"\n",
62+
"s = 0\n",
63+
"for i in range(1,1000):\n",
64+
" if i % 3 == 0 or i % 5 == 0:\n",
65+
" s += i\n",
66+
"print s"
67+
],
68+
"language": "python",
69+
"metadata": {},
70+
"outputs": [
71+
{
72+
"output_type": "stream",
73+
"stream": "stdout",
74+
"text": [
75+
"233168\n"
76+
]
77+
}
78+
],
79+
"prompt_number": 3
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"### Exercise 2"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"collapsed": false,
91+
"input": [
92+
"def sample_mean(xs):\n",
93+
" \"\"\"Samplele mean.\"\"\"\n",
94+
" return sum(xs)/float(len(xs))\n",
95+
"\n",
96+
"def sample_std(xs):\n",
97+
" \"\"\"Sample standard deviaiton.\"\"\"\n",
98+
" n = len(xs)\n",
99+
" xbar = sample_mean(xs)\n",
100+
" s = 0.0\n",
101+
" for x in xs:\n",
102+
" s += (x - xbar)**2\n",
103+
" return (s/(n-1))**0.5\n",
104+
"\n",
105+
"def sample_correlation(xs, ys):\n",
106+
" \"\"\"Sample correlation coefficient.\"\"\"\n",
107+
" n = len(xs)\n",
108+
" xbar = sample_mean(xs)\n",
109+
" ybar = sample_mean(ys)\n",
110+
" sx = sample_std(xs)\n",
111+
" sy = sample_std(ys)\n",
112+
" r = 0.0\n",
113+
" for x, y in zip(xs, ys):\n",
114+
" r += ((x - xbar)/sx) * ((y - ybar)/sy)\n",
115+
" return r/(n-1)\n",
116+
"\n",
117+
"x = [10.0, 8.0, 13.0, 9.0, 11.0, 14.0, 6.0, 4.0, 12.0, 7.0, 5.0]\n",
118+
"y = [8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68]\n",
119+
"print sample_correlation(x, y)"
120+
],
121+
"language": "python",
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"output_type": "stream",
126+
"stream": "stdout",
127+
"text": [
128+
"0.816420516345\n"
129+
]
130+
}
131+
],
132+
"prompt_number": 4
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"metadata": {},
137+
"source": [
138+
"### Exercise 3"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"collapsed": false,
144+
"input": [
145+
"def hailstone(n):\n",
146+
" \"\"\"Given a positive integer n, return the series of hailstone numbers.\"\"\"\n",
147+
" acc = []\n",
148+
" while n != 1:\n",
149+
" acc.append(n)\n",
150+
" if n%2 == 0:\n",
151+
" n /= 2\n",
152+
" else:\n",
153+
" n = n*3 + 1\n",
154+
" acc.append(1)\n",
155+
" return acc\n",
156+
"\n",
157+
"seq = hailstone(23)\n",
158+
"print seq\n",
159+
"print len(seq)"
160+
],
161+
"language": "python",
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"output_type": "stream",
166+
"stream": "stdout",
167+
"text": [
168+
"[23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]\n",
169+
"16\n"
170+
]
171+
}
172+
],
173+
"prompt_number": 5
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"### Exercise 4"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"collapsed": false,
185+
"input": [
186+
"def let2num(c):\n",
187+
" \"\"\"Convert lowercase character to number.\"\"\"\n",
188+
" return ord(c) - ord('a')\n",
189+
"\n",
190+
"def num2let(n):\n",
191+
" \"\"\"Convert number to lowercase character.\"\"\"\n",
192+
" return chr(ord('a') + n)\n",
193+
"\n",
194+
"def encode(cs, n):\n",
195+
" \"\"\"Caesar cipher with offset n.\"\"\"\n",
196+
" return ''.join([num2let((let2num(c) + n) % 26) if c.islower() else c for c in cs])"
197+
],
198+
"language": "python",
199+
"metadata": {},
200+
"outputs": [],
201+
"prompt_number": 6
202+
},
203+
{
204+
"cell_type": "code",
205+
"collapsed": false,
206+
"input": [
207+
"def chisq(os, es):\n",
208+
" \"\"\"Retruns chi-square score given observed os and expected es frequencies.\"\"\"\n",
209+
" os = np.array(os)\n",
210+
" es = np.array(es)\n",
211+
" return np.sum((os - es)**2.0/es)"
212+
],
213+
"language": "python",
214+
"metadata": {},
215+
"outputs": [],
216+
"prompt_number": 7
217+
},
218+
{
219+
"cell_type": "code",
220+
"collapsed": false,
221+
"input": [
222+
"def freqs(text):\n",
223+
" \"\"\"Returns relative frequenies of lowercase letter in text.\"\"\"\n",
224+
" ctr = Counter(text)\n",
225+
" counts = np.array([ctr[c] for c in string.lowercase], dtype='float')\n",
226+
" return counts/counts.sum()"
227+
],
228+
"language": "python",
229+
"metadata": {},
230+
"outputs": [],
231+
"prompt_number": 8
232+
},
233+
{
234+
"cell_type": "code",
235+
"collapsed": false,
236+
"input": [
237+
"# Use Pride and Prejudice to build up base frequencies\n",
238+
"text = urllib2.urlopen('http://www.gutenberg.org/ebooks/1342.txt.utf-8').read()\n",
239+
"ref_freqs = freqs(text)"
240+
],
241+
"language": "python",
242+
"metadata": {},
243+
"outputs": [],
244+
"prompt_number": 9
245+
},
246+
{
247+
"cell_type": "code",
248+
"collapsed": false,
249+
"input": [
250+
"def crack(code):\n",
251+
" \"\"\"Find the original text by identifying the most likely encoding offset.\"\"\"\n",
252+
" shift = np.argmin([chisq(freqs(encode(code, n)), ref_freqs) for n in range(26)])\n",
253+
" return encode(code, shift)"
254+
],
255+
"language": "python",
256+
"metadata": {},
257+
"outputs": [],
258+
"prompt_number": 10
259+
},
260+
{
261+
"cell_type": "code",
262+
"collapsed": false,
263+
"input": [
264+
"cs = 'Statistical computing and computation is fun!'\n",
265+
"code = encode(cs, np.random.randint(1, 26))\n",
266+
"print code\n",
267+
"crack(code)"
268+
],
269+
"language": "python",
270+
"metadata": {},
271+
"outputs": [
272+
{
273+
"output_type": "stream",
274+
"stream": "stdout",
275+
"text": [
276+
"Szgzoyzoigr iusvazotm gtj iusvazgzout oy lat!\n"
277+
]
278+
},
279+
{
280+
"metadata": {},
281+
"output_type": "pyout",
282+
"prompt_number": 11,
283+
"text": [
284+
"'Statistical computing and computation is fun!'"
285+
]
286+
}
287+
],
288+
"prompt_number": 11
289+
},
290+
{
291+
"cell_type": "code",
292+
"collapsed": false,
293+
"input": [],
294+
"language": "python",
295+
"metadata": {},
296+
"outputs": [],
297+
"prompt_number": 11
298+
}
299+
],
300+
"metadata": {}
301+
}
302+
]
303+
}

0 commit comments

Comments
 (0)