-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.py
449 lines (322 loc) · 9.43 KB
/
funcs.py
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import numpy as np
import matplotlib.pyplot as plt
import emcee
import corner
import csv
def initrange(p, numSpec):
"""
Return initial error estimates in each parameter.
Used to start the MCMC chains in a small ball near an estimated solution.
Input
-----
p : ndarray
Model parameters. See light_curve_model for the order.
numSpec: int
Number of observatories
Returns
-------
errs : ndarray
The standard deviation to use in each parameter
for MCMC walker initialization.
"""
errorEst = [5.8e-03, 1.4e-01, 2.5e-03, 2.5e-03, 3.8e-02]
for i in range(0, numSpec):
errorEst.append(4.8e-02)
errorEst.append(1e-5)
for i in range(0, numSpec):
errorEst.append(0.1)
errorEst = np.array(errorEst)
return errorEst
def kepler(M, e):
"""
Simple Kepler solver.
Iterative solution via Newton's method. Could likely be sped up,
but this works for now; it's not the major roadblock in the code.
Input
-----
M : ndarray
e : float or ndarray of same size as M
Returns
-------
E : ndarray
"""
M = np.array(M)
E = M * 1.
err = M * 0. + 1.
while err.max() > 1e-8:
#solve using Newton's method
guess = E - (E - e * np.sin(E) - M) / (1. - e * np.cos(E))
err = np.abs(guess - E)
E = guess
return E
def RV_model(t, period, ttran, ecosomega, esinomega, K, gamma):
"""
Given the orbital parameters compute the RV at times t, without gamma
Input
-----
t : ndarray
Times to return the model RV.
period : float [days]
ttran : float [days]
ecosomega : float
esinomega : float
K : float [km/s]
gamma : float [km/s]
Returns
-------
RV_model : ndarray
RV corresponding to the times in t [km/s].
"""
e = np.sqrt(ecosomega**2. + esinomega**2.)
omega = np.arctan2(esinomega, ecosomega)
#mean motion: n = 2pi/period
n = 2. * np.pi / period
# Sudarsky 2005 Eq. 9 to convert between center of transit
# and pericenter passage (tau)
edif = 1. - e**2.
fcen = np.pi/2. - omega
tau = (ttran + np.sqrt(edif) * period / (2 * np.pi) *
(e * np.sin(fcen) / (1. + e * np.cos(fcen)) - 2. / np.sqrt(edif) *
np.arctan(np.sqrt(edif) * np.tan(fcen / 2.) / (1. + e))))
#Define mean anomaly: M
M = (n * (t - tau)) % (2. * np.pi)
#Determine the Eccentric Anomaly: E
E = kepler(M, e)
#Solve for fanom (measure of location on orbit)
tanf2 = np.sqrt((1. + e) / (1. - e)) * np.tan(E / 2.)
fanom = (np.arctan(tanf2) * 2.) % (2. * np.pi)
#Calculate RV at given location on orbit
RV = K * (e * np.cos(omega) + np.cos(fanom + omega)) + gamma
return RV
def loglikelihood(p, t, RV, RVerr, chisQ=False):
"""
Compute the log likelihood of a RV signal with these orbital
parameters given the data.
Input
-----
p : ndarray
Orbital parameters. See RV model for order
t, RV, RVerr : MxNdarray
times, RV, and RV errors of the data.
arranged as a list of lists
len(array) = number of observing devices
chisQ : boolean, optional
If True, we are trying to minimize the chi-square rather than
maximize the likelihood. Default False.
Returns
------
likeli : float
Log likelihood that the model fits the data.
"""
# Define all parameters except gamma and jitters
(period, ttran, ecosomega, esinomega, K) = p[0:5]
# Determine the number of spectra observers
numSpec = len(t)
# Define a list of gamma parameters
gammas = p[5:5+numSpec]
# Define jitter squared parameter
jitterSqrd = p[5+numSpec]
#Define list of error multipliers
errorMult = p[5+numSpec+1:len(p)]
# Check observations are consistent
if ( len(t) != len(RV) or len(t) != len(RVerr) or len(RV) != len(RVerr) ):
print "Error! Mismatched number of spectra!"
# Compute RV model light curve for the first all spectra without gamma added
models = []
for ii in range(0, numSpec):
models.append(RV_model(t[ii], period, ttran, ecosomega, esinomega, K, gammas[ii]))
# compute loglikelihood for model
# Eastman et al., 2013 equation
# Christiansen et al., 2017 sec. 3.2 eq. 1
totchisq = 0
loglikelihood = 0
for ii in range(0, numSpec):
totchisq += np.sum((RV[ii]-models[ii])**2. / ( errorMult[ii] * (RVerr[ii]**2. + jitterSqrd) ))
loglikelihood += -np.sum(
(RV[ii]-models[ii])**2. / ( 2. * errorMult[ii] * (RVerr[ii]**2. + jitterSqrd) ) +
np.log(np.sqrt(2. * np.pi * errorMult[ii] * (RVerr[ii]**2. + jitterSqrd)))
)
# If we want to minimize chisQ, return it now
if chisQ:
return totchisq
# Else return log likelihood
return loglikelihood
def logprior(p, numSpec):
"""
Priors on the input parameters.
Input
-----
p : ndarray
Orbital parameters. RV_model for the order.
numSpec : int
Number of individual sets of spectra observations
Returns
-------
prior : float
Log likelihood of this set of input parameters based on the
priors.
"""
# Define all parameters except gamma and jitters
(period, ttran, ecosomega, esinomega, K) = p[0:5]
# Define a list of gamma parameters
gammas = p[5:5+numSpec]
# Define jitter squared parameter
jitterSqrd = p[5+numSpec]
#Define list of error multipliers
errorMult = p[5+numSpec+1:len(p)]
e = np.sqrt(ecosomega**2. + esinomega**2.)
omega = np.arctan2(esinomega, ecosomega)
#If any parameters not physically possible, return negative infinity
if (period < 0. or e < 0. or e >= 1.):
return -np.inf
# If jitter squared term not physically possible or too large (> 1 km/s), return negative infinity
if (jitterSqrd**.5 > 1 or jitterSqrd < 0):
return -np.inf
#Set a uniform prior on error multiplier from 0 to 10
for ii in range(0, numSpec):
if (errorMult[ii] > 10 or errorMult[ii] < 0):
return -np.inf
totchisq = 0.
# otherwise return a uniform prior (except modify the eccentricity to
# ensure the prior is uniform in e)
return -totchisq / 2. - np.log(e)
def logprob(p, t, RV, RVerr):
"""
Get the log probability of the data given the priors and the model.
See loglikeli for the input parameters.
Returns
-------
prob : float
Log likelihood of the model given the data and priors, up to a
constant.
"""
numSpec = len(t)
lp = logprior(p, numSpec)
llike = loglikelihood(p, t, RV, RVerr)
if not np.isfinite(lp):
return -np.inf
if not np.isfinite(llike):
return -np.inf
return lp + llike
def readObservations(filename, sepSpectra=False):
"""
Take data as output from TRES and turn into parsable t, RV, and RVerr np.arrays
Input
-----
filename : string
Name of file containing data
sepSpectra : boolean
default = False
if True will seperate spectra by built in tags
Returns
------
[t, RV, RVerr] : list of ndarrays
times, RV, and RV errors of the data.
"""
# Read data from the observing output file
# Each sub list contains [time in JD, RV, IGNORE, RVerr]
observations = []
with open(filename, "rb") as f:
reader = csv.reader(f)
for row in reader:
observations.append(row)
# Remove first and last discriptionary rows
observations = observations[1:len(observations)-1]
# Seperate out individual rows
tStart = []
tEnd = []
rvStart = []
rvEnd = []
specStart = []
specEnd = []
rvErrStart = []
rvErrEnd = []
pointer = 'tStart'
for observation in observations:
for index in range(0, len(observation[0])):
element = observation[0][index]
if pointer == 'tStart':
if element != ' ':
tStart.append(index)
pointer = 'tEnd'
elif pointer == 'tEnd':
if element == ' ':
tEnd.append(index-1)
pointer = 'rvStart'
elif pointer == 'rvStart':
if element != ' ':
rvStart.append(index)
pointer = 'rvEnd'
elif pointer == 'rvEnd':
if element == ' ':
rvEnd.append(index-1)
pointer = 'specStart'
elif pointer == 'specStart':
if element != ' ':
specStart.append(index)
pointer ='specEnd'
elif pointer == 'specEnd':
if element == ' ':
specEnd.append(index-1)
pointer = 'rvErrStart'
elif pointer == 'rvErrStart':
if element != ' ':
rvErrStart.append(index)
rvErrEnd.append(len(observation[0])-1)
pointer = 'tStart'
break
t = []
rv = []
rvErr = []
spectra = []
for obsNum in range(0, len(observations)):
t.append(float(observations[obsNum][0][tStart[obsNum]:tEnd[obsNum] + 1]) )
rv.append(float(observations[obsNum][0][rvStart[obsNum]:rvEnd[obsNum] + 1]) )
rvErr.append(float(observations[obsNum][0][rvErrStart[obsNum]:rvErrEnd[obsNum] + 1]) )
if sepSpectra:
#wds04342
spectra.append(int(observations[obsNum][0][specStart[obsNum]:specEnd[obsNum] + 1]))
#hd102509
#spectra.append(int(observations[obsNum][0][specStart[obsNum] + 4:specEnd[obsNum] + 1]))
tSep = []
rvSep = []
rvErrSep = []
tFinal = []
rvFinal = []
rvErrFinal = []
if sepSpectra:
currentSpec = 0
for spec in range(0, len(spectra)):
if spectra[spec] == currentSpec:
tSep.append(t[spec])
rvSep.append(rv[spec])
rvErrSep.append(rvErr[spec])
else:
tSep = np.array(tSep)
rvSep = np.array(rvSep)
rvErrSep = np.array(rvErrSep)
tFinal.append(tSep)
rvFinal.append(rvSep)
rvErrFinal.append(rvErrSep)
tSep = []
tSep.append(t[spec])
rvSep = []
rvSep.append(rv[spec])
rvErrSep = []
rvErrSep.append(rvErr[spec])
currentSpec = spectra[spec]
if spec == len(spectra)-1:
tSep = np.array(tSep)
rvSep = np.array(rvSep)
rvErrSep = np.array(rvErrSep)
tFinal.append(tSep)
rvFinal.append(rvSep)
rvErrFinal.append(rvErrSep)
tSep = []
tSep.append(t[spec])
rvSep = []
rvSep.append(rv[spec])
rvErrSep = []
rvErrSep.append(rvErr[spec])
currentSpec = spectra[spec]
return [tFinal, rvFinal, rvErrFinal]