forked from bigfootproject/schedsim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnorta.py
More file actions
29 lines (25 loc) · 739 Bytes
/
norta.py
File metadata and controls
29 lines (25 loc) · 739 Bytes
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
# Based on Lu et al.'s model for generating distributions with
# arbitrary coefficients
import math
import numpy
import scipy.stats
normal = scipy.stats.norm()
def generate(r, n, real_dist=normal, est_dist=None, eps=0.01):
rho = r
if est_dist is None:
est_dist = real_dist
low, high = 0, 1
while True:
y1, x2 = normal.rvs((2, n))
y2 = rho * y1 + (1 - rho ** 2) ** 0.5 * x2
u1, u2 = normal.cdf([y1, y2])
est = est_dist.ppf(u1)
real = real_dist.ppf(u2)
diff = r - numpy.corrcoef(est, real)[0, 1]
if diff < -eps:
high = rho
elif diff < eps:
return est, real
else:
low = rho
rho = (low + high) / 2