-
Notifications
You must be signed in to change notification settings - Fork 10
/
PointEstimator.py
executable file
·215 lines (168 loc) · 6.03 KB
/
PointEstimator.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
#!/usr/bin/env python
# ======================================================================
# Globally useful modules:
import matplotlib
# Force matplotlib to not use any Xwindows backend:
matplotlib.use('Agg')
# Fonts, latex:
matplotlib.rc('font',**{'family':'serif', 'serif':['TimesNewRoman']})
matplotlib.rc('text', usetex=True)
import string,numpy,pylab,sys,getopt
import pappy
# ======================================================================
def PointEstimator(argv):
"""
NAME
PointEstimator.py
PURPOSE
Compute a point estimate for a required parameter, given samples
drawn from that parameter's PDF.
COMMENTS
Expected data format is plain text, header marked by # in column 1, with
header lines listing:
1) Parameter names separated by commas. These names will
be used as labels on the plots, and should be in latex.
2) Parameter ranges [min,max,] for plot axes.
Best to use histogram if Neff is small. May be faster to work
directly from samples if Neff is large, but there's probably not
much in it.
USAGE
PointEstimator.py [flags]
FLAGS
-h Print this message [0]
-v Verbose operation [0]
INPUTS
file Name of textfile containing data
OPTIONAL INPUTS
-s Work directly from samples, rather than making a histogram
-n --columns List of columns to plot [all] NB. inputs are one-indexed!
-w wcol Column number of sample weight
-L Lcol Index of column containing likelihood of sample (to be ignored)
-t --terse Terse output (one line per parameter)
-c --credibility cred % of samples within uncertainties
OUTPUTS
stdout Useful information
EXAMPLES
PointEstimator.py -w 1 examples/thetas.cpt
PointEstimator.py -t -w 1 -n 2,3,4 examples/localgroup.cpt,red,shaded
BUGS
HISTORY
2012-05-03 started Marshall (Oxford)
"""
# --------------------------------------------------------------------
try:
opts, args = getopt.getopt(argv, "hvstw:n:c:",["help","verbose","terse","columns","credibility"])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
print PointEstimator.__doc__
return
vb = False
longwinded = True
wcol = -1
Lcol = -1
columns = 'All'
cred = 68
histogram = True
# NB. wcol is assumed to be entered indexed to 1!
for o,a in opts:
if o in ("-v", "--verbose"):
vb = True
elif o in ("-t", "--terse"):
longwinded = False
elif o in ("-n","--columns"):
columns = a
elif o in ("-c", "--credibility"):
cred = a
elif o in ("-w"):
wcol = int(a) - 1
elif o in ("-L"):
Lcol = int(a) - 1
elif o in ("-s"):
histogram = False
elif o in ("-h", "--help"):
print PointEstimator.__doc__
return
else:
assert False, "unhandled option"
# Check for datafiles in array args:
if len(args) == 1:
datafile = args[0]
if vb or longwinded:
print "Point estimate(s) of parameter(s) in file "+datafile
else :
print PointEstimator.__doc__
return
# --------------------------------------------------------------------
# Read in data:
data = numpy.loadtxt(datafile)
# Start figuring out how many parameters we have - index will be a
# list of column numbers containg the parameter values.
# NB. ALL files must have same parameters/weights structure!
npars = data.shape[1]
index = numpy.arange(npars)
# Some cols may not be data, adjust index accordingly.
# Weights/importances:
if wcol >= 0 and wcol < data.shape[1]:
if vb: print "using weight in column: ",wcol
wht = data[:,wcol].copy()
keep = (index != wcol)
index = index[keep]
npars = npars - 1
else:
if (wcol >= data.shape[1]):
print "WARNING: only ",data.shape[1]," columns are available"
if vb: print "Setting weights to 1"
wht = 0.0*data[:,0].copy() + 1.0
# Likelihood values:
if Lcol >= 0:
Lhood = data[:,Lcol].copy()
if ( k == 0 ):
keep = (index != Lcol)
index = index[keep]
npars = npars - 1
if vb: print "using lhood in column: ",Lcol
else:
Lhood = 0.0*data[:,0].copy() + 1.0
# Having done all that, optionally overwrite index with specified list
# of column numbers. Note conversion to zero-indexed python:
if columns != 'All':
pieces = columns.split(',')
index = []
for piece in pieces:
index.append(int(piece) - 1)
npars = len(index)
if vb: print "Only using data in",npars,"columns (",index,"): "
# Now parameter list is in index - which is fixed for other datasets
labels,limits,dummy = pappy.read_header(datafile)
# --------------------------------------------------------------------
# Loop over parameters, doing calculations:
for i in range(npars):
col = index[i]
d = data[:,col].copy()
mean,stdev,Neff,N95 = pappy.meansd(d,wht=wht)
if histogram:
dylimits = numpy.zeros([2])
dylimits[0] = mean - 10*stdev
dylimits[1] = mean + 10*stdev
nbins = 161
bins = numpy.linspace(dylimits[0],dylimits[1],nbins)
h,x = numpy.histogram(d,weights=wht,bins=bins,range=[bins[0],bins[-1]])
norm = numpy.sum(h)
h = h/norm
median,errplus,errminus = pappy.compress_histogram(h,x,ci=cred)
else:
median,errplus,errminus = pappy.compress_samples(d,wht=wht,ci=cred)
estimate = pappy.format_point_estimate(median,errplus,errminus)
if longwinded:
print " Par no.",col+1,":",labels[col].strip(),"=",estimate
else:
print estimate
if vb:
print " Par no.",col+1," mean,stdev,Neff,N95 = ",mean,stdev,Neff,N95
# --------------------------------------------------------------------
return
# ======================================================================
if __name__ == '__main__':
PointEstimator(sys.argv[1:])
# ======================================================================