Example #1
0
 def pnsig(self, n=None):
     """
     The probability that n events are from the signal source.
     
     :Parameters:
     
       n : int
         A number of events <= on_cts
     
     :Returns:
     
       p : float OR float array
         If n is provided, the probability that n of the on_cts are from
         the signal source is returned; with no arguments, an array of
         length (on_cts+1) is returned so that p[n] is the probability
         that n counts are from the signal source.
     """
     # Make sure we've calculate an array of signal count probabilities.
     if self.p_signal is None:
         self.p_signal = sigctp(self.on_cts, self.on_intvl,
                                self.off_cts, self.off_intvl)
     if n:
         if n<0 or n>self.on_cts:
             raise ValueError('Number of source counts must be <= on_cts!')
         return self.p_signal[n]
     else:
         return self.p_signal[:]  # return copy so cache can't be altered
Example #2
0
 def sigmp(self, s):
     """
     Marginal posterior density for scalar or vector signal rate(s).
     
     The calculation is exact (to machine precision), and requires a sum
     with a number of terms equal to the number of on-source counts.  In
     many cases with a large number of counts, many of the terms will be
     negligible.  The marginal likelihood method, sigml, can neglect small
     terms and may be preferable to sigmp in these cases; its results
     will have to be explicitly normalized to produce a posterior pdf.
     
     :Parameters:
     
       s : float OR float array
         Signal rate(s) (counts per unit interval)
     
     :Returns:
     
       siglmp : float OR float array
         Natural logarithm of the marginal posterior for the signal rate(s)
     """
     # Make sure we've calculate an array of signal count probabilities.
     if self.p_signal is None:
         self.p_signal = sigctp(self.on_cts, self.on_intvl,
                                self.off_cts, self.off_intvl)
     # Treat array or vector argument cases separately.
     try:
         s = float(s)  # raises TypeError for arrays of length != 1
         return psigc(s, self.on_intvl, self.p_signal)
     except TypeError:
         if len(s.shape) != 1:
             raise ValueError('sigll handles only 1-D arrays!')
         llvals = zeros_like(s)
         for i, sval in enumerate(s):
             llvals[i] = psigc(sval, self.on_intvl, self.p_signal)
         return llvals