Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALInference 4.1.9.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
cbcBayesDIEvidence.py
Go to the documentation of this file.
1##python
2# -*- coding: utf-8 -*-
3#
4# cbcBayesDIEvidence.py: compute the direct-integration evidence
5# from a file of posterior samples. See arXiv:0911.1777 for the
6# algorithm.
7#
8# Copyright 2011
9# Will M. Farr <will.farr@ligo.org>
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24# MA 02110-1301, USA.
25#
26#
27
28import numpy as np
29from lalinference import bayespputils as bp
30from lalinference import git_version
31from optparse import OptionParser
32
33__author__="Will M. Farr <will.farr@ligo.org>"
34__version__="git id %s"%git_version.id
35__date__=git_version.date
36
37if __name__=='__main__':
38 parser=OptionParser()
39 parser.add_option("--data",dest="data",action="store",help="file of posterior samples",metavar="FILE")
40 parser.add_option("--Nboxing",dest="Nboxing",action="store",default=64,type="int",metavar="N")
41 parser.add_option("--bootstrap", dest='bootstrap',action='store',default=1,type='int',metavar='N')
42 parser.add_option('--output',dest='output',action='store',default=None,metavar='FILE')
43
44 (opts,args)=parser.parse_args()
45
46 pos_parser=bp.PEOutputParser('common')
47
48 f=open(opts.data, "r")
49 try:
50 pos=bp.Posterior(pos_parser.parse(f))
51 finally:
52 f.close()
53
54 outfile=None
55 if opts.output:
56 outfile=open(opts.output,'w')
57 try:
58 for i in range(opts.bootstrap):
59 if i == 0:
60 log_ev=pos.di_evidence(boxing=opts.Nboxing)
61 else:
62 log_ev=pos.bootstrap().di_evidence(boxing=opts.Nboxing)
63 print('log(evidence) with Nboxing = %d is %.1f (evidence is %g)'%(opts.Nboxing,log_ev,np.exp(ev)))
64 if outfile:
65 outfile.write('%g\n'%log_ev)
66 finally:
67 if outfile:
68 outfile.close()