forked from agabrown/AstroStats-II
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluminositycalibrationmodels.py
316 lines (266 loc) · 15.4 KB
/
luminositycalibrationmodels.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
__all__ = ['UniformSpaceDensityGaussianLFBook', 'UniformSpaceDensityGaussianLF',
'UniformSpaceDensityGaussianLFemcee']
"""
Provide classes and methods the represent luminosity calibration models for use with PyMC.
Anthony Brown 2011-2013
"""
import numpy as np
from pymc import Uniform, deterministic, Normal, Model, InverseGamma
from extrastochastics import OneOverXFourth, OneOverX
from agabutils import inverseVariance
from scipy.stats import gamma
class UniformSpaceDensityGaussianLFBook:
"""
A model for luminosity classification of stars. The stars are all assumed to be of a single class
characterized by a mean absolute magnitude <M> and the variance on this magnitude sigma^2_M.
Furthermore the stars are assumed to be distributed uniformly in the space around the sun. The upper
and lower distance limits are fixed and the survey is assumed to be volume complete.
The prior on the mean absolute magnitude is flat and the prior on the inverse variance tau of the
absolute magnitude distribution is f(tau)=1/tau (i.e., f(sigma^2)=1/sigma^2, with tau=1/sigma^2). This
is an appropriate non-informative prior for a scale parameter.
"""
def __init__(self, simulatedSurvey, lowParallax, upParallax, minMeanAbsoluteMagnitude,
maxMeanAbsoluteMagnitude, minTau, maxTau):
"""
simulatedSurvey - a simulated survey object generated with one of the UniverseModels classes.
lowParallax - assumed lower limit on parallaxes (mas)
upParallax - assumed upper limit on parallaxes (mas)
minMeanAbsoluteMagnitude - lower limit on prior distribution of mean absolute magnitude
maxMeanAbsoluteMagnitude - upper limit on prior distribution of mean absolute magnitude
minTau - lower limit on prior distribution of inverse variance
maxTau - upper limit on prior distribution of inverse variance
"""
self.simulatedSurvey=simulatedSurvey
self.numberOfStarsInSurvey=self.simulatedSurvey.numberOfStarsInSurvey
self.lowParallax=lowParallax
self.upParallax=upParallax
self.minMeanAbsoluteMagnitude=minMeanAbsoluteMagnitude
self.maxMeanAbsoluteMagnitude=maxMeanAbsoluteMagnitude
self.minTau=minTau
self.maxTau=maxTau
self.pyMCModel=Model(self._buildModel())
def _buildModel(self):
# Lower and upper parallax limits are fixed
@deterministic(plot=False)
def minParallax():
"""Lower limit on true parallax values [mas]."""
return self.lowParallax
@deterministic(plot=False)
def maxParallax():
"""Upper limit on true parallax values [mas]."""
return self.upParallax
# Calculate initial guesses for the true parallaxes and absolute magnitudes.
clippedObservedParallaxes=self.simulatedSurvey.observedParallaxes.clip(minParallax.value,
maxParallax.value)
initialAbsMagGuesses = (self.simulatedSurvey.observedMagnitudes +
5.0*np.log10(clippedObservedParallaxes) - 10.0)
meanAbsoluteMagnitudeGuess=initialAbsMagGuesses.mean()
# Prior on mean absolute magnitude
expMagInit=(self.maxMeanAbsoluteMagnitude-self.minMeanAbsoluteMagnitude)/2.0
meanAbsoluteMagnitude = Uniform('meanAbsoluteMagnitude', lower=self.minMeanAbsoluteMagnitude,
upper=self.maxMeanAbsoluteMagnitude, value=expMagInit)
# Prior on absolute magnitude variance. Use f(tau)=1/tau, i.e., f(sigma^2)=1/sigma^2 (tau=1/sigma^2).
# This non-informative prior is appropriate for a scale parameter.
expTauInit=(self.maxTau-self.minTau)/(np.log(self.maxTau)-np.log(self.minTau))
tauAbsoluteMagnitude = OneOverX('tauAbsoluteMagnitude', lower=self.minTau, upper=self.maxTau,
value=expTauInit)
# Prior on parallax. Uniform distribution of stars in space around the Sun.
priorParallaxes=OneOverXFourth('priorParallaxes', lower=minParallax, upper=maxParallax,
size=self.simulatedSurvey.numberOfStarsInSurvey)
# Prior on absolute magnitude
priorAbsoluteMagnitudes=Normal('priorAbsoluteMagnitudes', mu=meanAbsoluteMagnitude,
tau=tauAbsoluteMagnitude, size=self.simulatedSurvey.numberOfStarsInSurvey)
# Apparent magnitudes depend on the parallax and absolute magnitude.
@deterministic(plot=False)
def apparentMagnitudes(parallaxes=priorParallaxes, absoluteMagnitudes=priorAbsoluteMagnitudes):
return absoluteMagnitudes-5.0*np.log10(parallaxes)+10.0
# The likelihood of the data
predictedParallaxes=Normal('predictedParallaxes',mu=priorParallaxes,
tau=inverseVariance(self.simulatedSurvey.parallaxErrors),
value=self.simulatedSurvey.observedParallaxes, observed=True)
predictedApparentMagnitudes=Normal('predictedApparentMagnitudes',mu=apparentMagnitudes,
tau=inverseVariance(self.simulatedSurvey.magnitudeErrors),
value=self.simulatedSurvey.observedMagnitudes, observed=True)
return locals()
#self.pyMCModel=Model(set([minParallax, maxParallax, meanAbsoluteMagnitude, tauAbsoluteMagnitude,
# priorParallaxes, priorAbsoluteMagnitudes, apparentMagnitudes, predictedParallaxes,
# predictedApparentMagnitudes]))
class UniformSpaceDensityGaussianLF:
"""
A model for luminosity classification of stars. The stars are all assumed to be of a single class
characterized by a mean absolute magnitude <M> and the variance on this magnitude sigma^2_M.
Furthermore the stars are assumed to be distributed uniformly in the space around the sun. The upper
and lower distance limits are fixed and the survey is assumed to be volume complete.
The prior on the mean absolute magnitude is flat and the prior on the inverse variance tau of the
absolute magnitude distribution is f(tau)=InvGamma(tau,shape=alpha,scale=beta), i.e.
f(sigma^2)=Gamma(sigma^2, shape=alpha, scale=1/beta), with tau=1/sigma^2.
"""
def __init__(self, simulatedSurvey, lowParallax, upParallax, minMeanAbsoluteMagnitude,
maxMeanAbsoluteMagnitude, shapeTau, scaleTau):
"""
simulatedSurvey - a simulated survey object generated with one of the UniverseModels classes.
lowParallax - assumed lower limit on parallaxes (mas)
upParallax - assumed upper limit on parallaxes (mas)
minMeanAbsoluteMagnitude - lower limit on prior distribution of mean absolute magnitude
maxMeanAbsoluteMagnitude - upper limit on prior distribution of mean absolute magnitude
shapeTau - shape parameter of Inverse Gamma prior distribution of inverse variance
scaleTau - scale parameter of Inverse Gamma prior distribution of inverse variance
"""
self.simulatedSurvey=simulatedSurvey
self.numberOfStarsInSurvey=self.simulatedSurvey.numberOfStarsInSurvey
self.lowParallax=lowParallax
self.upParallax=upParallax
self.minMeanAbsoluteMagnitude=minMeanAbsoluteMagnitude
self.maxMeanAbsoluteMagnitude=maxMeanAbsoluteMagnitude
self.shapeTau=shapeTau
self.scaleTau=scaleTau
self.pyMCModel=Model(self._buildModel())
def _buildModel(self):
# Lower and upper parallax limits are fixed
@deterministic(plot=False)
def minParallax():
"""Lower limit on true parallax values [mas]."""
return self.lowParallax
@deterministic(plot=False)
def maxParallax():
"""Upper limit on true parallax values [mas]."""
return self.upParallax
# Calculate initial guesses for the true parallaxes and absolute magnitudes.
clippedObservedParallaxes=self.simulatedSurvey.observedParallaxes.clip(minParallax.value,
maxParallax.value)
initialAbsMagGuesses = (self.simulatedSurvey.observedMagnitudes +
5.0*np.log10(clippedObservedParallaxes) - 10.0)
meanAbsoluteMagnitudeGuess=initialAbsMagGuesses.mean()
# Prior on mean absolute magnitude
expMagInit=(self.maxMeanAbsoluteMagnitude-self.minMeanAbsoluteMagnitude)/2.0
meanAbsoluteMagnitude = Uniform('meanAbsoluteMagnitude', lower=self.minMeanAbsoluteMagnitude,
upper=self.maxMeanAbsoluteMagnitude, value=expMagInit)
# Prior on absolute magnitude variance.
tauInit=self.scaleTau/(self.shapeTau+1)
tauAbsoluteMagnitude = InverseGamma('tauAbsoluteMagnitude', self.shapeTau, self.scaleTau, value=tauInit)
# Prior on parallax. Uniform distribution of stars in space around the Sun.
priorParallaxes=OneOverXFourth('priorParallaxes', lower=minParallax, upper=maxParallax,
size=self.simulatedSurvey.numberOfStarsInSurvey)
# Prior on absolute magnitude
priorAbsoluteMagnitudes=Normal('priorAbsoluteMagnitudes', mu=meanAbsoluteMagnitude,
tau=tauAbsoluteMagnitude, size=self.simulatedSurvey.numberOfStarsInSurvey)
# Apparent magnitudes depend on the parallax and absolute magnitude.
@deterministic(plot=False)
def apparentMagnitudes(parallaxes=priorParallaxes, absoluteMagnitudes=priorAbsoluteMagnitudes):
return absoluteMagnitudes-5.0*np.log10(parallaxes)+10.0
# The likelihood of the data
predictedParallaxes=Normal('predictedParallaxes',mu=priorParallaxes,
tau=inverseVariance(self.simulatedSurvey.parallaxErrors),
value=self.simulatedSurvey.observedParallaxes, observed=True)
predictedApparentMagnitudes=Normal('predictedApparentMagnitudes',mu=apparentMagnitudes,
tau=inverseVariance(self.simulatedSurvey.magnitudeErrors),
value=self.simulatedSurvey.observedMagnitudes, observed=True)
return locals()
def UniformSpaceDensityGaussianLFBookemcee(posWalker, posteriorDict, observations, observationalErrors):
"""
A model for luminosity classification of stars. The stars are all assumed to be of a single class
characterized by a mean absolute magnitude <M> and the variance on this magnitude sigma^2_M.
Furthermore the stars are assumed to be distributed uniformly in the space around the sun. The upper
and lower distance limits are fixed and the survey is assumed to be volume complete.
The prior on the mean absolute magnitude is flat and the prior on the variance var of the absolute
magnitude distribution is f(var)=1/var. This is an appropriate non-informative prior for a scale
parameter.
**** This model is for the emcee python package. ****
Parameters
----------
posWalker - The Emcee walker position. The first two entries are the hyper-parameters, mu = Mean
Absolute Magnitude and var = Variance, the next set of parameters are the true parallaxes
and absolute magnitudes, respectively, of the stars in the survey (so 2 times the number of
stars in the survey.)
posteriorDict - Dictionary with posterior probability density function parameters:
minParallax : Lower limit on parallax distribution
maxParallax : Upper limit on parallax distribution
muLow : Lower limit of uniform prior on mu.
muHigh : Upper limit of uniform prior on mu.
varLow : Lower limit for the 1/x prior on the variance.
varHigh : Upper limit for the 1/x prior on the variance.
observations - Vector of observed parallaxes and apparent magnitudes (in that order).
observationalErrors - Vector of errors (as inverse variances) on observed parallaxes and apparent
magnitudes (in that order).
Returns
-------
Natural logarithm of the posterior probability density at the position of the walker.
"""
lnPosterior=0.0
numStarsInSurvey=(len(posWalker)-2)/2
meanAbsMag=posWalker[0]
if (meanAbsMag<posteriorDict['muLow'] or meanAbsMag>posteriorDict['muHigh']):
return -np.inf
lnPosterior=lnPosterior-np.log(posteriorDict['muHigh']-posteriorDict['muLow'])
variance=posWalker[1]
if (variance<posteriorDict['varLow'] or variance>posteriorDict['varHigh']):
return -np.inf
lnPosterior=lnPosterior - np.log(variance)
parallaxes=posWalker[2:numStarsInSurvey+2]
if (np.any(parallaxes < posteriorDict['minParallax']) or np.any(parallaxes >
posteriorDict['maxParallax'])):
return -np.inf
lnPosterior = lnPosterior - 4.0*np.sum(np.log(parallaxes))
absoluteMagnitudes=posWalker[numStarsInSurvey+2:]
apparentMagnitudes=absoluteMagnitudes-5.0*np.log10(parallaxes)+10.0
y=np.concatenate((parallaxes,apparentMagnitudes,absoluteMagnitudes))
inv_var=np.diagflat(np.concatenate((observationalErrors, 1.0/np.repeat(variance,numStarsInSurvey))))
means=np.concatenate((observations, np.repeat(meanAbsMag,numStarsInSurvey)))
diff=y-means
lnPosterior = lnPosterior - 0.5*np.dot(diff,np.dot(inv_var,diff))
lnPosterior = lnPosterior - 0.5*numStarsInSurvey*np.log(variance)
return lnPosterior
def UniformSpaceDensityGaussianLFemcee(posWalker, posteriorDict, observations, observationalErrors):
"""
A model for luminosity classification of stars. The stars are all assumed to be of a single class
characterized by a mean absolute magnitude <M> and the variance on this magnitude sigma^2_M.
Furthermore the stars are assumed to be distributed uniformly in the space around the sun. The upper
and lower distance limits are fixed and the survey is assumed to be volume complete.
The prior on the mean absolute magnitude is flat and the prior on the variance var of the
absolute magnitude distribution is f(var)=Gamma(var|shape=k,scale=theta).
**** This model is for the emcee python package. ****
Parameters
----------
posWalker - The Emcee walker position. The first two entries are the hyper-parameters, mu = Mean
Absolute Magnitude and var = Variance, the next set of parameters are the true parallaxes
and absolute magnitudes, respectively, of the stars in the survey (so 2 times the number of
stars in the survey.)
posteriorDict - Dictionary with posterior probability density function parameters:
minParallax : Lower limit on parallax distribution
maxParallax : Upper limit on parallax distribution
muLow : Lower limit of uniform prior on mu.
muHigh : Upper limit of uniform prior on mu.
varShape : Shape parameter for Gamma prior on the variance.
varScale : Scale parameter for Gamma prior on the variance.
observations - Vector of observed parallaxes and apparent magnitudes (in that order).
observationalErrors - Vector of errors (as inverse variances) on observed parallaxes and apparent
magnitudes (in that order).
Returns
-------
Natural logarithm of the posterior probability density at the position of the walker.
"""
lnPosterior=0.0
numStarsInSurvey=(len(posWalker)-2)/2
meanAbsMag=posWalker[0]
if (meanAbsMag<posteriorDict['muLow'] or meanAbsMag>posteriorDict['muHigh']):
return -np.inf
lnPosterior=lnPosterior-np.log(posteriorDict['muHigh']-posteriorDict['muLow'])
variance=posWalker[1]
if (variance<0.0):
return -np.inf
lnPosterior=lnPosterior + gamma.logpdf(variance, posteriorDict['varShape'],
scale=posteriorDict['varScale'])
parallaxes=posWalker[2:numStarsInSurvey+2]
if (np.any(parallaxes < posteriorDict['minParallax']) or np.any(parallaxes >
posteriorDict['maxParallax'])):
return -np.inf
lnPosterior = lnPosterior - 4.0*np.sum(np.log(parallaxes))
absoluteMagnitudes=posWalker[numStarsInSurvey+2:]
apparentMagnitudes=absoluteMagnitudes-5.0*np.log10(parallaxes)+10.0
y=np.concatenate((parallaxes,apparentMagnitudes,absoluteMagnitudes))
inv_var=np.diagflat(np.concatenate((observationalErrors, 1.0/np.repeat(variance,numStarsInSurvey))))
means=np.concatenate((observations, np.repeat(meanAbsMag,numStarsInSurvey)))
diff=y-means
lnPosterior = lnPosterior - 0.5*np.dot(diff,np.dot(inv_var,diff))
lnPosterior = lnPosterior - 0.5*numStarsInSurvey*np.log(variance)
return lnPosterior