-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbox_muller.py
More file actions
60 lines (43 loc) · 1.3 KB
/
box_muller.py
File metadata and controls
60 lines (43 loc) · 1.3 KB
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
#!/usr/bin/env python
# coding: utf-8
# Write a computer program that implements the Box-Muller method
# imports
import math
# project imports
import rand
import halton
import bfs
# Write a computer program that implements the Box-Muller method
#
# Box-Muller: pseudorandom
# 1. Generate random $u_1$ , $u_2$ independently from U(0, 1)
# 2. Compute: $R^2$ = $−2\,log\,u_1$ and $θ = 2π\,u_2$
# 3. $X = R\,cos\,θ$
# $Y = R\,sin\,θ$
def box_muller(u1, u2):
r = -2 * math.log(u1)
r = math.sqrt(r)
theta = 2 * math.pi * u2
x = r * math.cos(theta)
y = r * math.sin(theta)
return x, y
def box_muller_seq(s, N, seq=rand.rand_seq):
''' return a list of N standard normal vectors (tuples) of dimension s '''
# save the original values for use later
s_orig = s
N_orig = N
# both s and N should be even
N = N if N % 2 == 0 else N + 1
s = s if s % 2 == 0 else s + 1
sns = []
rows = seq(s, N)
for row in rows:
snv = []
for i in range(0, len(row), 2):
x, y = box_muller(row[i], row[i+1])
snv.append(x); snv.append(y)
# resize vector to original dimension s and add to sns list
sns.append(tuple(snv[:s_orig]))
return sns[:N_orig]
#sn = box_muller_seq(s=2, N=10, seq=rand.rand_seq)
#print(sn[:10])