gstlal  0.8.1
 All Classes Namespaces Files Functions Variables Pages
misc.py
1 #
2 # Copyright (C) 2010,2011 Kipp Cannon <kipp.cannon@ligo.org>
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation; either version 2 of the License, or (at your
7 # option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 # Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #
18 
19 
20 #
21 # =============================================================================
22 #
23 # Preamble
24 #
25 # =============================================================================
26 #
27 
28 
29 from scipy import optimize
30 import numpy, scipy
31 import sys
32 
33 
34 #
35 # import all symbols from _misc
36 #
37 
38 
39 from _misc import *
40 
41 
42 #
43 # =============================================================================
44 #
45 # Extras
46 #
47 # =============================================================================
48 #
49 
50 
51 #
52 # inverse of cdf_weighted_chisq_P()
53 #
54 
55 
56 def cdf_weighted_chisq_Pinv(A, noncent, dof, var, P, lim, accuracy):
57  func = lambda x: cdf_weighted_chisq_P(A, noncent, dof, var, x, lim, accuracy) - P
58  lo = 0.0
59  hi = 1.0
60  while func(hi) < 0:
61  lo = hi
62  hi *= 2
63  print >>sys.stderr, lo, hi
64  return optimize.brentq(func, lo, hi, xtol = accuracy * 4)
65 
66 #
67 # Function to compute the threshold at a fixed FAR for weighted \chi^2
68 #
69 
70 
71 def max_stat_thresh(coeffs, fap, samp_tol=100.0):
72  num = int(samp_tol/ fap)
73  out = numpy.zeros(num)
74  for c in coeffs: out += c*scipy.randn(num)**2
75  out.sort()
76  return out[-int(samp_tol)]
77 
78 
79 #
80 # Function to compute the optimal quadratic statistic coefficients given
81 # singular values S and a desired signal size amp
82 #
83 
84 
85 def ss_coeffs(S, amp=5.5):
86  return S**2 / (S**2 + len(S) / amp**2 )