-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomSimulationFunctions.py
More file actions
executable file
·78 lines (58 loc) · 2.79 KB
/
randomSimulationFunctions.py
File metadata and controls
executable file
·78 lines (58 loc) · 2.79 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#Take in two lists (x-data and y-data for some points) and return
# the same lists, with a random rearrangement of which x-y pairings.
import random
from cantrips import removeListElement
def simulateNormallyDistributedData(x_data, function, y_errs, n_draws = -1, replace = 0):
if n_draws < 0: n_draws = len(x_data)
new_x_data = []
new_y_data = []
new_y_errs = []
for i in range(n_draws):
x_index = random.randint(0, len(x_data) - 1)
#print 'x_index = ' + str(x_index)
#print 'y_index = ' + str(y_index)
new_x_data = new_x_data + [x_data[x_index]]
new_y_data = new_y_data + [function(x_data[x_index]) + random.gauss(0.0, y_errs[x_index])]
new_y_errs = new_y_errs + [y_errs[x_index]]
if not replace:
x_data = removeListElement(x_data[:], x_index)
y_errs =removeListElement(y_errs[:], x_index)
return new_x_data, new_y_data, new_y_errs
def randomSortNSigma(x_data, y_data, y_errs, exp_values, n_draws = -1, replace = 0):
if n_draws < 0: n_draws = len(x_data)
n_sigma = [(y_data[i] - exp_values[i]) / y_errs[i] for i in range(len(x_data)) ]
new_x_data = []
new_y_data = []
new_y_errs = []
for i in range(n_draws):
x_index = random.randint(0, len(x_data) - 1)
n_sigma_index = random.randint(0, len(n_sigma) - 1)
new_x_data = new_x_data + [x_data[x_index]]
new_n_sigma = n_sigma[n_sigma_index]
new_y_data = new_y_data + [new_n_sigma * y_errs[x_index] + exp_values[x_index]]
new_y_errs = new_y_errs + [y_errs[x_index]]
if not replace:
x_data = removeListElement(x_data[:], x_index)
n_sigma =removeListElement(n_sigma[:], n_sigma_index)
y_errs =removeListElement(y_errs[:], x_index)
return new_x_data, new_y_data, new_y_errs
def randomSortData(x_data, y_data, n_draws = -1, y_errs = None, replace = 0):
if n_draws < 0: n_draws = len(x_data)
new_x_data = []
new_y_data = []
if not y_errs is None: new_y_errs = []
for i in range(n_draws):
#print 'i = ' + str(i)
x_index = random.randint(0, len(x_data) - 1)
y_index = random.randint(0, len(x_data) - 1)
#print 'x_index = ' + str(x_index)
#print 'y_index = ' + str(y_index)
new_x_data = new_x_data + [x_data[x_index]]
new_y_data = new_y_data + [y_data[y_index]]
if not y_errs is None: new_y_errs = new_y_errs + [y_errs[y_index]]
if not replace:
x_data = removeListElement(x_data[:], x_index)
y_data =removeListElement(y_data[:], y_index)
if not y_errs is None: y_errs =removeListElement(y_errs[:], y_index)
if not y_errs is None: return new_x_data, new_y_data, new_y_errs
else: return new_x_data, new_y_data