コード例 #1
0
    def perturb_detector(self, params, which):
        logH = 0.0

        if which == ba_idx: #lets call this phi
           params[which] += (tf_phi_max + np.pi/2)*dnest4.randh()
           params[which] = dnest4.wrap(params[which], -np.pi/2, tf_phi_max)
        elif which == c_idx: #call it omega
            params[which] += 0.1*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.07, 0.2)
        elif which == dc_idx: #d
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.7, 0.9)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == aliasrc_idx:
          params[which] += 9.9*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 0.1, 10)

        elif which == grad_idx:
          logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
          params[which] += prior_vars[which] *dnest4.randh()
          params[which] = dnest4.wrap(params[which], detector.gradList[0], detector.gradList[-1])
          logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == imp_avg_idx:
          logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
          params[which] += prior_vars[which] *dnest4.randh()
          params[which] = dnest4.wrap(params[which], detector.impAvgList[0], detector.impAvgList[-1])
          logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == trap_idx:
            params[which] += (1000 - traprc_min)*dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000)

        elif which >= velo_first_idx and which < velo_first_idx+4:
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 1, priors[which]*10)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == velo_first_idx+4 or which == velo_first_idx+5:
          params[which] += (beta_lims[1] - beta_lims[0])  *dnest4.randh()
          params[which] = dnest4.wrap(params[which], beta_lims[0], beta_lims[1])

        else: #velocity or rc params: cant be below 0, can be arb. large
            print ("which value %d not supported" % which)
            exit(0)

        return logH
コード例 #2
0
def random_position(r, z):
    r_init, z_init = r, z
    r += dnest4.randh() * 0.1
    z += dnest4.randh() * 0.1

    r = dnest4.wrap(r, 0, detector.detector_radius)
    z = dnest4.wrap(z, 0, detector.detector_length)

    if not detector.IsInDetector(r, 0.1, z):
        #    print "not in detector..."
        return random_position(r_init, z_init)
    else:
        return (r, z)
コード例 #3
0
def random_position(r, z):
  r_init,z_init = r,z
  r += dnest4.randh()*0.1
  z += dnest4.randh()*0.1

  r = dnest4.wrap(r, 0, detector.detector_radius)
  z = dnest4.wrap(z, 0, detector.detector_length)

  if not detector.IsInDetector(r, 0.1, z):
#    print "not in detector..."
    return random_position(r_init,z_init)
  else:
    return (r,z)
コード例 #4
0
ファイル: Model.py プロジェクト: benshanks/mjd-analysis
    def get_new_rad(self, rad, theta):
        detector = self.detector
        #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
        theta_eq = np.arctan(detector.detector_length /
                             detector.detector_radius)
        theta_taper = np.arctan(detector.taper_length /
                                detector.detector_radius)
        if theta <= theta_taper:
            z = np.tan(theta) * (detector.detector_radius -
                                 detector.taper_length) / (1 - np.tan(theta))
            max_rad = z / np.sin(theta)
        elif theta <= theta_eq:
            max_rad = detector.detector_radius / np.cos(theta)
        else:
            theta_comp = np.pi / 2 - theta
            max_rad = detector.detector_length / np.cos(theta_comp)

        #AND THE MINIMUM (from PC dimple)
        #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

        min_rad = np.amax([detector.pcRad, detector.pcLen])

        new_rad = rad + (max_rad - min_rad) * dnest4.randh()
        new_rad = dnest4.wrap(new_rad, min_rad, max_rad)
        return new_rad
コード例 #5
0
    def perturb(self, params):
        """
        Perturb the current parameters by proposing a new position. This takes a numpy array of
        parameters as input, and modifies it in-place. In just perturbs one parameter at a time.

        Args:
            params (:class:`np.ndarray`): the current parameters (this is modified by the function)

        Returns:
            float: the natural logarithm of the Metropolis-Hastings proposal ratio.
        """
        logH = 0.0  # log of the Metropolis-Hastings prior x proposal ratio

        # randomly choose which parameter to perturb
        which = np.random.randint(len(params))
        mmu = 0.
        msigma = 10.
        if which == 0:
            # update H for Gaussian prior
            logH -= -0.5 * ((params[which] - mmu) / msigma)**2
        params[which] += 1. * randh(
        )  # scale factor of 1. (randh is a heavy-tailed distribution to occasionally sample distant values, although this isn't really required in this case)

        if which == 0:
            # update H for Gaussian prior
            logH += -0.5 * ((params[which] - mmu) / msigma)**2
        else:
            # wrap c value so that it stays within the prior range
            params[which] = wrap(params[which], cmin, cmax)

        return logH
コード例 #6
0
    def perturb(self, params):
        """
        Perturb the current parameters by proposing a new position. This takes a numpy array of
        parameters as input, and modifies it in-place. In just perturbs one parameter at a time.

        Args:
            params (:class:`np.ndarray`): the current parameters (this is modified by the function)

        Returns:
            float: the natural logarithm of the Metropolis-Hastings proposal ratio.
        """
        logH = 0.0 # log of the Metropolis-Hastings prior x proposal ratio
        
        # randomly choose which parameter to perturb 
        which = np.random.randint(len(params))
        mmu = 0.
        msigma = 10.
        if which == 0:
            # update H for Gaussian prior
            logH -= -0.5*((params[which]-mmu)/msigma)**2
        params[which] += 1.*randh() # scale factor of 1. (randh is a heavy-tailed distribution to occasionally sample distant values, although this isn't really required in this case)

        if which == 0:
            # update H for Gaussian prior
            logH += -0.5*((params[which]-mmu)/msigma)**2
        else:
            # wrap c value so that it stays within the prior range
            params[which] = wrap(params[which], cmin, cmax)

        return logH
コード例 #7
0
ファイル: Model.py プロジェクト: benshanks/mjd-analysis
    def get_new_theta(self,rad,theta):
        detector = self.detector
        if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
            max_val = np.pi/2
            min_val = 0
        else:
            if rad < detector.detector_radius - detector.taper_length:
                #can't possibly hit the taper
                min_val = 0
            elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                #low enough that it could hit the taper region
                a = detector.detector_radius - detector.taper_length
                z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                min_val = np.arcsin(z/rad)
            else:
                #longer than could hit the taper
                min_val = np.arccos(detector.detector_radius/rad)

            if rad < detector.detector_length:
                max_val = np.pi/2
            else:
                max_val = np.pi/2 - np.arccos(detector.detector_length/rad)

        new_theta = theta + (max_val - min_val)*dnest4.randh()
        new_theta = dnest4.wrap(new_theta, min_val, max_val)
        return new_theta
コード例 #8
0
ファイル: Model.py プロジェクト: benshanks/mjd-analysis
    def get_new_theta(self, rad, theta):
        detector = self.detector
        if rad < np.amin([
                detector.detector_radius - detector.taper_length,
                detector.detector_length
        ]):
            max_val = np.pi / 2
            min_val = 0
        else:
            if rad < detector.detector_radius - detector.taper_length:
                #can't possibly hit the taper
                min_val = 0
            elif rad < np.sqrt(detector.detector_radius**2 +
                               detector.taper_length**2):
                #low enough that it could hit the taper region
                a = detector.detector_radius - detector.taper_length
                z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                min_val = np.arcsin(z / rad)
            else:
                #longer than could hit the taper
                min_val = np.arccos(detector.detector_radius / rad)

            if rad < detector.detector_length:
                max_val = np.pi / 2
            else:
                max_val = np.pi / 2 - np.arccos(detector.detector_length / rad)

        new_theta = theta + (max_val - min_val) * dnest4.randh()
        new_theta = dnest4.wrap(new_theta, min_val, max_val)
        return new_theta
コード例 #9
0
ファイル: straightline.py プロジェクト: eggplantbren/DNest4
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(3)

        if which == 0 or which == 1:
            logH -= -0.5*(params[which]/1E3)**2
            params[which] += 1E3*dnest4.randh()
            logH += -0.5*(params[which]/1E3)**2
        else:
            log_sigma = np.log(params[2])
            log_sigma += 20*dnest4.randh()
            # Note the difference between dnest4.wrap in Python and
            # DNest4::wrap in C++. The former *returns* the wrapped value.
            log_sigma = dnest4.wrap(log_sigma, -10.0, 10.0)
            params[2] = np.exp(log_sigma)

        return logH
コード例 #10
0
ファイル: _parameterbase.py プロジェクト: zachh12/waffle
    def perturb_gaussian_parameter(self, parameter):
        mu = self.mean
        var = self.variance

        logH = 0
        logH -= -0.5*((parameter - mu)/var)**2
        parameter += var*dnest4.randh()
        if (self.lim_lo > -max_float) or (self.lim_hi<np.inf):
            parameter = dnest4.wrap(parameter, self.lim_lo, self.lim_hi)
        logH += -0.5*((parameter - mu)/var)**2

        return (logH, parameter)
コード例 #11
0
ファイル: straightline.py プロジェクト: zachh12/DNest4
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(3)

        if which == 0 or which == 1:
            logH -= -0.5 * (params[which] / 1E3)**2
            params[which] += 1E3 * dnest4.randh()
            logH += -0.5 * (params[which] / 1E3)**2
        else:
            log_sigma = np.log(params[2])
            log_sigma += 20 * dnest4.randh()
            # Note the difference between dnest4.wrap in Python and
            # DNest4::wrap in C++. The former *returns* the wrapped value.
            log_sigma = dnest4.wrap(log_sigma, -10.0, 10.0)
            params[2] = np.exp(log_sigma)

        return logH
コード例 #12
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0

        which = rng.randint(len(params))

        if which == 0:
            params[which] += (detector.detector_radius) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0,
                                        detector.detector_radius)
        elif which == 1:
            max_val = np.pi / 4
            params[which] += np.pi / 4 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, max_val)
            if params[which] < 0 or params[which] > np.pi / 4:
                print "wtf phi"
            #params[which] = np.clip(params[which], 0, max_val)
        elif which == 2:
            params[which] += (detector.detector_length) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0,
                                        detector.detector_length)

        elif which == 3:  #scale
            min_scale = wf.wfMax - 0.01 * wf.wfMax
            max_scale = wf.wfMax + 0.005 * wf.wfMax
            params[which] += (max_scale - min_scale) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], min_scale, max_scale)
        #   print "  adjusted scale to %f" %  ( params[which])

        elif which == 4:  #t0
            params[which] += 1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
        elif which == 5:  #smooth
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            # elif which == 6: #wf baseline slope (m)
            #     logH -= -0.5*(params[which]/1E-4)**2
            #     params[which] += 1E-4*dnest4.randh()
            #     logH += -0.5*(params[which]/1E-4)**2
            # elif which == 7: #wf baseline incercept (b)
            #     logH -= -0.5*(params[which]/1E-2)**2
            #     params[which] += 1E-2*dnest4.randh()
            #     logH += -0.5*(params[which]/1E-2)**2

            #   params[which] += 0.01*dnest4.randh()
            #   params[which]=dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)

        return logH
コード例 #13
0
    def perturb_detector(self, params):
        which = rng.randint(len(priors))

        if which == ba_idx:  #b over a
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, 15)
        elif which == c_idx:  #b over a
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, -0.7)
        elif which == dc_idx:  #b over a
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1.05, -0.975)
        elif which == rc1_idx:
            params[which] += 30 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 60, 90)
        elif which == rc2_idx:
            params[which] += 5 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 5)
        elif which == rcfrac_idx:
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.9, 1)
        elif which == grad_idx:
            params[which] += (len(detector.gradList) - 1) * dnest4.randh()
            params[which] = np.int(
                dnest4.wrap(params[which], 0,
                            len(detector.gradList) - 1))

        elif which >= velo_first_idx and which < velo_first_idx + 6:
            params[which] += (velo_width * priors[which] -
                              1 / velo_width * priors[which]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.1 * priors[which],
                                        (10.) * priors[which])
        elif which == trap_idx:
            log_traprc = np.log(params[which])
            log_traprc += 20 * dnest4.randh()
            log_traprc = dnest4.wrap(log_traprc, 0., 20.0)
            params[which] = np.exp(log_traprc)

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)
コード例 #14
0
    def perturb_detector(self, params):
        which = rng.randint(len(priors))

        if which == ba_idx: #b over a
          params[which] += 0.1*dnest4.randh()
          params[which] = dnest4.wrap(params[which], -0.9, 15)
        elif which == c_idx: #b over a
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, -0.7)
        elif which == dc_idx: #b over a
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1.05, -0.975)
        elif which == rc1_idx:
          params[which] += 30*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 60, 90)
        elif which == rc2_idx:
          params[which] += 5*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 0, 5)
        elif which == rcfrac_idx:
          params[which] += 0.1*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 0.9, 1)
        elif which == grad_idx:
          params[which] += (len(detector.gradList)-1)*dnest4.randh()
          params[which] = np.int(dnest4.wrap(params[which], 0, len(detector.gradList)-1))

        elif which >= velo_first_idx and which < velo_first_idx+6:
            params[which] += (velo_width*priors[which] - 1/velo_width*priors[which])  *dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.1*priors[which], (10.)*priors[which])
        elif which == trap_idx:
            log_traprc = np.log(params[which])
            log_traprc += 20*dnest4.randh()
            log_traprc = dnest4.wrap(log_traprc, 0., 20.0)
            params[which] = np.exp(log_traprc)

        else: #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)
コード例 #15
0
ファイル: Model.py プロジェクト: benshanks/mjd-analysis
    def get_new_rad(self,rad, theta):
          detector = self.detector
          #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
          theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
          theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
          if theta <= theta_taper:
             z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
             max_rad = z / np.sin(theta)
          elif theta <= theta_eq:
              max_rad = detector.detector_radius / np.cos(theta)
          else:
              theta_comp = np.pi/2 - theta
              max_rad = detector.detector_length / np.cos(theta_comp)

          #AND THE MINIMUM (from PC dimple)
          #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

          min_rad = np.amax([detector.pcRad, detector.pcLen])

          new_rad = rad + (max_rad - min_rad)*dnest4.randh()
          new_rad = dnest4.wrap(new_rad, min_rad, max_rad)
          return new_rad
コード例 #16
0
 def perturb(self, params):
     """
     Each step we perturb all the parameters which is more effective from
     computation point of view.
     """
     logH = 0.0
     parDict = self.__model.get_modelParDict()
     pIndex = 0
     for modelName in self.__model._modelList:
         parFitDict = parDict[modelName]
         for parName in parFitDict.keys():
             parRange = parFitDict[parName]["range"]
             parType = parFitDict[parName]["type"]
             if parType == "c":
                 #print "[DN4M]: continual"
                 r1, r2 = parRange
                 params[pIndex] += (
                     r2 - r1) * dnest4.randh()  #Uniform distribution
                 if (params[pIndex] < r1) or (params[pIndex] > r2):
                     #print "[DNest4Model]: perturb out boundary!"
                     logH -= np.inf
             elif parType == "d":
                 #print "[DN4M]: discrete"
                 rangeLen = len(parRange)
                 iBng = -1 * parRange.index(params[pIndex])
                 iPar = iBng + rng.randint(rangeLen)
                 params[pIndex] = parRange[iPar]
                 if not params[pIndex] in parRange:
                     #print "[DNest4Model]: perturb out boundary!"
                     logH -= np.inf
             else:
                 raise TypeError(
                     "The parameter type '{0}' is not recognised!".format(
                         parType))
             parFitDict[parName]["value"] = params[pIndex]
             pIndex += 1
     return logH
コード例 #17
0
ファイル: _parameterbase.py プロジェクト: zachh12/waffle
 def perturb_uniform_parameter(self, parameter):
     logH = 0
     parameter += (self.lim_hi - self.lim_lo)  *dnest4.randh()
     parameter = dnest4.wrap(parameter, self.lim_lo, self.lim_hi)
     return (logH, parameter)
コード例 #18
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0

        reps = 1;
        if(rng.rand() < 0.5):
            reps += np.int(np.power(100.0, rng.rand()));

        # print "going to perturb %d reps" % reps

        for i in range(reps):
            # print "   rep iteration %d" % i
            which = rng.randint(len(params))

            if which == 0:
              rad_idx = 0
              theta_idx =  2

              theta = params[theta_idx]

              #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
              theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
              theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
            #   print "theta: %f pi" % (theta / np.pi)
              if theta <= theta_taper:
                 z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
                 max_rad = z / np.sin(theta)
              elif theta <= theta_eq:
                  max_rad = detector.detector_radius / np.cos(theta)
                #   print "max rad radius: %f" %  max_rad
              else:
                  theta_comp = np.pi/2 - theta
                  max_rad = detector.detector_length / np.cos(theta_comp)
                #   print "max rad length: %f" %  max_rad

              #AND THE MINIMUM (from PC dimple)
              #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

              min_rad = np.amax([detector.pcRad, detector.pcLen])

              total_max_rad = np.sqrt(detector.detector_length**2 + detector.detector_radius**2 )

              params[which] += total_max_rad*dnest4.randh()
              params[which] = dnest4.wrap(params[which] , min_rad, max_rad)

            elif which ==2: #theta
              rad_idx = 0
              rad = params[rad_idx]

            #   print "rad: %f" % rad
              if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
                  max_val = np.pi/2
                  min_val = 0
                #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
              else:
                  if rad < detector.detector_radius - detector.taper_length:
                      #can't possibly hit the taper
                    #   print "less than taper adjustment"
                      min_val = 0
                  elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                      #low enough that it could hit the taper region
                    #   print "taper adjustment"
                      a = detector.detector_radius - detector.taper_length
                      z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                      min_val = np.arcsin(z/rad)
                  else:
                      #longer than could hit the taper
                    #   print  " longer thantaper adjustment"
                      min_val = np.arccos(detector.detector_radius/rad)

                  if rad < detector.detector_length:
                      max_val = np.pi/2
                  else:
                      max_val = np.pi/2 - np.arccos(detector.detector_length/rad)
                #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

              params[which] += np.pi/2*dnest4.randh()
              params[which] = dnest4.wrap(params[which], min_val, max_val)

            # if which == 0:
            #     params[which] += (detector.detector_radius)*dnest4.randh()
            #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
            elif which == 1:
                max_val = np.pi/4
                params[which] += np.pi/4*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi/4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)
            # elif which == 2:
            #     params[which] += (detector.detector_length)*dnest4.randh()
            #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

            elif which == 3: #scale
                min_scale = wf.wfMax - 0.01*wf.wfMax
                max_scale = wf.wfMax + 0.005*wf.wfMax
                params[which] += (max_scale-min_scale)*dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_scale, max_scale)
            #   print "  adjusted scale to %f" %  ( params[which])

            elif which == 4: #t0
              params[which] += 1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
            elif which == 5: #smooth
              params[which] += 0.1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], 0, 25)
                #   print "  adjusted smooth to %f" %  ( params[which])

                # elif which == 6: #wf baseline slope (m)
                #     logH -= -0.5*(params[which]/1E-4)**2
                #     params[which] += 1E-4*dnest4.randh()
                #     logH += -0.5*(params[which]/1E-4)**2
                # elif which == 7: #wf baseline incercept (b)
                #     logH -= -0.5*(params[which]/1E-2)**2
                #     params[which] += 1E-2*dnest4.randh()
                #     logH += -0.5*(params[which]/1E-2)**2

                #   params[which] += 0.01*dnest4.randh()
                #   params[which]=dnest4.wrap(params[which], -1, 1)
                #   print "  adjusted b to %f" %  ( params[which])

            else: #velocity or rc params: cant be below 0, can be arb. large
                print "which value %d not supported" % which
                exit(0)


        return logH
コード例 #19
0
ファイル: Model.py プロジェクト: benshanks/mjd-analysis
    def perturb_detector(self, params, which):
        logH = 0.0
        detector = self.detector
        num_waveforms = self.num_waveforms
        priors = self.priors
        prior_vars = self.prior_vars

        if which == phi_idx:  #lets call this phi
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -np.pi, 0)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
        elif which == omega_idx:  #call it omega
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, np.pi)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
        elif which == d_idx:  #d
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.5, 0.999)
            # params[which] = dnest4.wrap(params[which], 0, 5000)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            if which == rcfrac_idx:
                params[which] = dnest4.wrap(params[which], 0, 1)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
        #
        # elif which == num0_idx:
        #   sig = 3*timeStepMult
        #   logH -= -0.5*((params[which] - 0.)/sig)**2
        #   params[which] += sig*dnest4.randh()
        #   logH += -0.5*((params[which] - 0.)/sig)**2

        # elif which == aliasrc_idx:
        #     params[which] += 19.9*dnest4.randh()
        #     params[which] = dnest4.wrap(params[which], 0.1, 20)

        elif which == grad_idx:
            # logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            # params[which] += prior_vars[which] *dnest4.randh()
            # params[which] = dnest4.wrap(params[which], detector.gradList[0], detector.gradList[-1])
            # logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

            paramlist = detector.gradList
            params[which] += (paramlist[-1] - paramlist[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0],
                                        paramlist[-1])

        elif which == imp_avg_idx:
            # logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            # params[which] += prior_vars[which] *dnest4.randh()
            # params[which] = dnest4.wrap(params[which], detector.impAvgList[0], detector.impAvgList[-1])
            # logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
            paramlist = detector.impAvgList
            params[which] += (paramlist[-1] - paramlist[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0],
                                        paramlist[-1])

        elif which == pcrad_idx:
            paramlist = detector.pcRadList
            params[which] += (paramlist[-1] - paramlist[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0],
                                        paramlist[-1])

        elif which == pclen_idx:
            paramlist = detector.pcLenList
            params[which] += (paramlist[-1] - paramlist[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0],
                                        paramlist[-1])

        elif which == trap_idx:
            # let's go with log10-uniform on this
            # log_trap = np.log10(params[which])
            # log_trap += 3*dnest4.randh()
            # log_trap = dnest4.wrap(log_trap, 1, 4)
            # params[which] = 10**(log_trap)

            # p_guess = 350
            # sig = 50
            # logH -= -0.5*((params[which] - p_guess )/sig)**2
            # params[which] += sig*dnest4.randh()
            # params[which] = dnest4.wrap(params[which], 100, 600)
            # logH += -0.5*((params[which] - p_guess)/sig)**2

            traprc_min = 10
            params[which] += (1000 - traprc_min) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000)

        elif which == untrap_idx:
            traprc_min = 1000
            params[which] += (1000000 - traprc_min) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000000)
        elif which == energy_idx:
            wf_guess = self.conf.energy_guess
            sig = 10

            logH -= -0.5 * ((params[which] - wf_guess) / sig)**2
            params[which] += sig * dnest4.randh()
            params[which] = dnest4.wrap(params[which], wf_guess - 30,
                                        wf_guess + 30)
            logH += -0.5 * ((params[which] - wf_guess) / sig)**2

        elif which >= velo_first_idx and which < velo_first_idx + 4:
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 1, priors[which] * 10)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == velo_first_idx + 4 or which == velo_first_idx + 5:
            params[which] += (self.conf.beta_lims[1] -
                              self.conf.beta_lims[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], self.conf.beta_lims[0],
                                        self.conf.beta_lims[1])

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print("which value %d not supported" % which)
            exit(0)

        return logH
コード例 #20
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(len(params))

        if which >= len(priors):
            #this is a waveform variable!
            wf_which = np.floor((which - len(priors)) / num_waveforms)
            # print "which idx is %d, value is %f" % (which, params[which])
            # print "  wf which is %d" % wf_which

            if wf_which == 0 or wf_which == 4:  #radius and t0
                wf_idx = (which - len(priors)) % num_waveforms
                rad_idx = len(priors) + wf_idx
                theta_idx = len(priors) + 2 * num_waveforms + wf_idx
                t0_idx = len(priors) + 4 * num_waveforms + wf_idx

                theta = params[theta_idx]

                #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                theta_eq = np.arctan(detector.detector_length /
                                     detector.detector_radius)
                theta_taper = np.arctan(detector.taper_length /
                                        detector.detector_radius)
                #   print "theta: %f pi" % (theta / np.pi)
                if theta <= theta_taper:
                    z = np.tan(theta) * (detector.detector_radius -
                                         detector.taper_length) / (
                                             1 - np.tan(theta))
                    max_rad = z / np.sin(theta)
                elif theta <= theta_eq:
                    max_rad = detector.detector_radius / np.cos(theta)
                #   print "max rad radius: %f" %  max_rad
                else:
                    theta_comp = np.pi / 2 - theta
                    max_rad = detector.detector_length / np.cos(theta_comp)
                #   print "max rad length: %f" %  max_rad

                #AND THE MINIMUM (from PC dimple)
                #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )
                min_rad = np.amax([detector.pcRad, detector.pcLen])

                mean = [0, 0]
                cov = [[1, -0.8], [-0.8, 1]]
                jumps = np.array((0.1 * dnest4.randh(), 0.1 * dnest4.randh()))
                (r_jump, t0_jump) = np.dot(cov, jumps)

                params[rad_idx] = dnest4.wrap(params[rad_idx] + r_jump,
                                              min_rad, max_rad)
                params[t0_idx] = dnest4.wrap(params[t0_idx] + t0_jump, min_t0,
                                             max_t0)

            elif wf_which == 1:
                max_val = np.pi / 4
                params[which] += np.pi / 4 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi / 4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)

            elif wf_which == 2:  #theta
                wf_idx = (which - len(priors)) % num_waveforms
                rad_idx = len(priors) + wf_idx
                rad = params[rad_idx]
                #   print "rad: %f" % rad
                if rad < np.amin([
                        detector.detector_radius - detector.taper_length,
                        detector.detector_length
                ]):
                    max_val = np.pi / 2
                    min_val = 0
                #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
                else:
                    if rad < detector.detector_radius - detector.taper_length:
                        #can't possibly hit the taper
                        #   print "less than taper adjustment"
                        min_val = 0
                    elif rad < np.sqrt(detector.detector_radius**2 +
                                       detector.taper_length**2):
                        #low enough that it could hit the taper region
                        #   print "taper adjustment"
                        a = detector.detector_radius - detector.taper_length
                        z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                        min_val = np.arcsin(z / rad)
                    else:
                        #longer than could hit the taper
                        #   print  " longer thantaper adjustment"
                        min_val = np.arccos(detector.detector_radius / rad)

                    if rad < detector.detector_length:
                        max_val = np.pi / 2
                    else:
                        max_val = np.pi / 2 - np.arccos(
                            detector.detector_length / rad)
                #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

                params[which] += (max_val - min_val) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_val, max_val)
                #   params[which] = np.clip(params[which], min_val, max_val)
                if params[which] < min_val or params[which] > max_val:
                    print "wtf theta"

            elif wf_which == 3:  #scale
                wf_idx = (which - len(priors)) % num_waveforms
                wf = wfs[wf_idx]
                params[which] += dnest4.randh()
                params[which] = dnest4.wrap(params[which],
                                            wf.wfMax - 10 * wf.baselineRMS,
                                            wf.wfMax + 10 * wf.baselineRMS)
                params[which] = np.clip(params[which],
                                        wf.wfMax - 50 * wf.baselineRMS,
                                        wf.wfMax + 50 * wf.baselineRMS)
            #   print "  adjusted scale to %f" %  ( params[which])
            elif wf_which == 5:  #smooth
                params[which] += 0.1 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            elif wf_which == 6:  #wf baseline slope (m)
                params[which] += 0.0001 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], -0.001, 0.001)
            #   print "  adjusted m to %f" %  ( params[which])
            elif wf_which == 7:  #wf baseline incercept (b)
                params[which] += 0.01 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        elif which == ba_idx:  #b over a
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, 15)
        elif which == c_idx:  #b over a
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.9, -0.7)
        elif which == dc_idx:  #b over a
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1.05, -0.975)

        elif which == rc1_idx:
            space = np.exp(-1. / 90) - np.exp(-1. / 60)
            params[which] += space * dnest4.randh()
            params[which] = dnest4.wrap(params[which], np.exp(-1. / 60),
                                        np.exp(-1. / 90))
        elif which == rc2_idx:
            space = np.exp(-1. / 10) - np.exp(-1. / .01)
            params[which] += space * dnest4.randh()
            params[which] = dnest4.wrap(params[which], np.exp(-1. / 0.01),
                                        np.exp(-1. / 10))
        elif which == rcfrac_idx:
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.9, 1)
        elif which == grad_idx:
            params[which] += (len(detector.gradList) - 1) * dnest4.randh()
            params[which] = np.int(
                dnest4.wrap(params[which], 0,
                            len(detector.gradList) - 1))
        elif which >= velo_first_idx and which < velo_first_idx + 6:
            velo_which = (which - velo_first_idx) % 3
            #TODO: consider long transforming priors on two of these
            if velo_which == 0:  #mu0 parameter
                params[which] += (100E3 - 10E3) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3, 100E3)
            elif velo_which == 1:  #ln(1/beta)
                space = np.log(1 / .1)
                params[which] += space * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, np.log(1 / .1))
            elif velo_which == 2:
                space = 10E3 * 100 - 100E3 * 300
                params[which] += space * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3 * 100,
                                            100E3 * 300)

        elif which == trap_idx:
            space = np.exp(-1. / 5000) - np.exp(-1. / 50)
            params[which] += space * dnest4.randh()
            params[which] = dnest4.wrap(params[which], np.exp(-1. / 50),
                                        np.exp(-1. / 5000))

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)

        return logH
コード例 #21
0
ファイル: dnest4_func.py プロジェクト: exosports/LISA
def perturb(coords, ndim, width):
    i = np.random.randint(ndim)
    coords[i] += (width[i]) * dnest4.randh()
    # Note: use the return value of wrap, unlike in C++
    coords[i] = dnest4.wrap(coords[i], -0.5 * width[i], 0.5 * width[i])
    return 0.0
コード例 #22
0
ファイル: dns_model.py プロジェクト: benshanks/mjd-analysis
    def perturb_wf(self, params, wf_idx, ):
    #do both wf and detector params in case theres strong correlation
        logH = 0.0
        num_waveforms = self.mpi_manager.num_waveforms
        detector = self.mpi_manager.detector

        reps = 1
        if rng.rand() < 0.5:
            reps += np.int(np.power(100.0, rng.rand()));

        for i in range(reps):
            wf_which = rng.randint(6)

            # my_which = rng.randint(len(priors) + 8)

            # if my_which < len(priors):
            #     #detector variable
            #     logH += self.perturb_detector(params, my_which)
            #
            # else:
            if wf_which < 6:
                #this is a waveform variable!
                # wf_which =  np.int(my_which - len(priors))

                #which idx of the global params array
                which = len(priors) + wf_which*num_waveforms + wf_idx

                rad_idx = len(priors) + wf_idx
                theta_idx =  len(priors) + 2*num_waveforms+ wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                  theta = params[theta_idx]

                  #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                  theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
                  theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
                  if theta <= theta_taper:
                     z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
                     max_rad = z / np.sin(theta)
                  elif theta <= theta_eq:
                      max_rad = detector.detector_radius / np.cos(theta)
                  else:
                      theta_comp = np.pi/2 - theta
                      max_rad = detector.detector_length / np.cos(theta_comp)

                  #AND THE MINIMUM (from PC dimple)
                  #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

                  min_rad = np.amax([detector.pcRad, detector.pcLen])

                  total_max_rad = np.sqrt(detector.detector_length**2 + detector.detector_radius**2 )

                  params[which] += total_max_rad*dnest4.randh()
                  params[which] = dnest4.wrap(params[which] , min_rad, max_rad)

                elif wf_which ==2: #theta
                  rad = params[rad_idx]

                  if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
                      max_val = np.pi/2
                      min_val = 0
                  else:
                      if rad < detector.detector_radius - detector.taper_length:
                          #can't possibly hit the taper
                          min_val = 0
                      elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                          #low enough that it could hit the taper region
                          a = detector.detector_radius - detector.taper_length
                          z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                          min_val = np.arcsin(z/rad)
                      else:
                          #longer than could hit the taper
                          min_val = np.arccos(detector.detector_radius/rad)

                      if rad < detector.detector_length:
                          max_val = np.pi/2
                      else:
                          max_val = np.pi/2 - np.arccos(detector.detector_length/rad)

                  params[which] += np.pi/2*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], min_val, max_val)

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi/4
                    params[which] += np.pi/4*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)
                    if params[which] < 0 or params[which] > np.pi/4:
                        print ("wtf phi")

                # elif wf_which == 2:
                #     params[which] += (detector.detector_length)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

                elif wf_which == 3: #scale
                    wf = self.mpi_manager.wfs[wf_idx]
                    min_scale = wf.wfMax - 0.01*wf.wfMax
                    max_scale = wf.wfMax + 0.005*wf.wfMax
                    params[which] += (max_scale-min_scale)*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_scale, max_scale)

                elif wf_which == 4: #t0
                  params[which] += 1*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], self.min_maxt, self.max_maxt)
                elif wf_which == 5: #smooth
                  params[which] += 0.1*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], 0, 25)

                else:
                    print( "wf which value %d (which value %d) not supported" % (wf_which, which) )
                    exit(0)

        return logH
コード例 #23
0
ファイル: Model.py プロジェクト: benshanks/mjd-analysis
    def perturb_wf(
        self,
        params,
        wf_idx,
    ):
        #do both wf and detector params in case theres strong correlation
        logH = 0.0
        num_waveforms = self.num_waveforms
        detector = self.detector
        priors = self.priors

        wf_params = 6

        reps = 1
        if rng.rand() < 0.5:
            reps += np.int(np.power(100.0, rng.rand()))

        for i in range(reps):
            wf_which = rng.randint(wf_params)

            # my_which = rng.randint(len(priors) + 8)

            # if my_which < len(priors):
            #     #detector variable
            #     logH += self.perturb_detector(params, my_which)
            #
            # else:
            if wf_which < wf_params:
                #this is a waveform variable!
                # wf_which =  np.int(my_which - len(priors))

                #which idx of the global params array
                which = len(priors) + wf_which * num_waveforms + wf_idx

                rad_idx = len(priors) + wf_idx
                theta_idx = len(priors) + 2 * num_waveforms + wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                    theta = params[theta_idx]

                    IsInDetector = 0
                    while not (IsInDetector):
                        new_rad = self.get_new_rad(params[which], theta)
                        r = new_rad * np.cos(theta)
                        z = new_rad * np.sin(theta)
                        IsInDetector = detector.IsInDetector(r, 0, z)

                    params[which] = new_rad

                elif wf_which == 2:  #theta
                    rad = params[rad_idx]

                    IsInDetector = 0
                    while not (IsInDetector):
                        new_theta = self.get_new_theta(rad, params[which])
                        r = rad * np.cos(new_theta)
                        z = rad * np.sin(new_theta)
                        IsInDetector = detector.IsInDetector(r, 0, z)
                    params[which] = new_theta

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi / 4
                    params[which] += np.pi / 4 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)

                # elif wf_which == 3: #scale
                #     wf_guess = self.conf.energy_guess
                #     sig = 10
                #
                #     logH -= -0.5*((params[which] - wf_guess  )/sig)**2
                #     params[which] += sig*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which], wf_guess - 30, wf_guess + 30)
                #     logH += -0.5*((params[which] - wf_guess)/sig)**2

                elif wf_which == 3:  #t0
                    #gaussian around 0, sigma... 5?
                    t0_sig = self.maxt_sigma
                    logH -= -0.5 * (
                        (params[which] - self.alignidx_guess) / t0_sig)**2
                    params[which] += t0_sig * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], self.min_maxt,
                                                self.max_maxt)
                    logH += -0.5 * (
                        (params[which] - self.alignidx_guess) / t0_sig)**2

                elif wf_which == 4:  #smooth
                    #gaussian around 10
                    smooth_guess = 20
                    sig = 20 * timeStepMult
                    logH -= -0.5 * (
                        (params[which] - smooth_guess * timeStepMult) / sig)**2
                    params[which] += sig * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 1,
                                                40 * timeStepMult)
                    logH += -0.5 * (
                        (params[which] - smooth_guess * timeStepMult) / sig)**2

                elif wf_which == 5:  #p
                    #gaussian around 10

                    if self.conf.smooth_type == "gen_gaus":
                        p_guess = 2
                        sig = 10
                        logH -= -0.5 * ((params[which] - p_guess) / sig)**2
                        params[which] += sig * dnest4.randh()
                        params[which] = dnest4.wrap(params[which], 1, 20)
                        logH += -0.5 * ((params[which] - p_guess) / sig)**2
                    elif self.conf.smooth_type == "skew":
                        params[which] += 20 * dnest4.randh()
                        params[which] = dnest4.wrap(params[which], -20, 0)

            else:
                print("wf which value %d (which value %d) not supported" %
                      (wf_which, which))
                exit(0)

        return logH
コード例 #24
0
    def perturb_detector(self, params, which):
        logH = 0.0

        if which == ba_idx:  #lets call this phi
            params[which] += (tf_phi_max + np.pi / 2) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -np.pi / 2, tf_phi_max)
        elif which == c_idx:  #call it omega
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.07, 0.2)
        elif which == dc_idx:  #d
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.7, 0.9)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == aliasrc_idx:
            params[which] += 9.9 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.1, 10)

        elif which == grad_idx:
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], detector.gradList[0],
                                        detector.gradList[-1])
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == imp_avg_idx:
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], detector.impAvgList[0],
                                        detector.impAvgList[-1])
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == trap_idx:
            params[which] += (1000 - traprc_min) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000)

        elif which >= velo_first_idx and which < velo_first_idx + 4:
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 1, priors[which] * 10)
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        elif which == velo_first_idx + 4 or which == velo_first_idx + 5:
            params[which] += (beta_lims[1] - beta_lims[0]) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], beta_lims[0],
                                        beta_lims[1])

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print("which value %d not supported" % which)
            exit(0)

        return logH
コード例 #25
0
 def perturb(self, coords):
     i = np.random.randint(self.ndim)
     coords[i] += self.width*dnest4.randh()
     # Note: use the return value of wrap, unlike in C++
     coords[i] = dnest4.wrap(coords[i], -0.5*self.width, 0.5*self.width)
     return 0.0
コード例 #26
0
ファイル: Model.py プロジェクト: benshanks/mjd-analysis
    def perturb_wf(self, params, wf_idx, ):
    #do both wf and detector params in case theres strong correlation
        logH = 0.0
        num_waveforms = self.num_waveforms
        detector = self.detector
        priors = self.priors

        wf_params = 6

        reps = 1
        if rng.rand() < 0.5:
            reps += np.int(np.power(100.0, rng.rand()));

        for i in range(reps):
            wf_which = rng.randint(wf_params)

            # my_which = rng.randint(len(priors) + 8)

            # if my_which < len(priors):
            #     #detector variable
            #     logH += self.perturb_detector(params, my_which)
            #
            # else:
            if wf_which < wf_params:
                #this is a waveform variable!
                # wf_which =  np.int(my_which - len(priors))

                #which idx of the global params array
                which = len(priors) + wf_which*num_waveforms + wf_idx

                rad_idx = len(priors) + wf_idx
                theta_idx =  len(priors) + 2*num_waveforms+ wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                  theta = params[theta_idx]

                  IsInDetector = 0
                  while not(IsInDetector):
                    new_rad = self.get_new_rad(params[which], theta)
                    r = new_rad * np.cos(theta)
                    z = new_rad * np.sin(theta)
                    IsInDetector = detector.IsInDetector(r, 0,z)

                  params[which] = new_rad

                elif wf_which ==2: #theta
                    rad = params[rad_idx]

                    IsInDetector = 0
                    while not(IsInDetector):
                      new_theta = self.get_new_theta(rad, params[which])
                      r = rad * np.cos(new_theta)
                      z = rad * np.sin(new_theta)
                      IsInDetector = detector.IsInDetector(r, 0,z)
                    params[which] = new_theta

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi/4
                    params[which] += np.pi/4*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)

                # elif wf_which == 3: #scale
                #     wf_guess = self.conf.energy_guess
                #     sig = 10
                #
                #     logH -= -0.5*((params[which] - wf_guess  )/sig)**2
                #     params[which] += sig*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which], wf_guess - 30, wf_guess + 30)
                #     logH += -0.5*((params[which] - wf_guess)/sig)**2

                elif wf_which == 3: #t0
                  #gaussian around 0, sigma... 5?
                  t0_sig = self.maxt_sigma
                  logH -= -0.5*((params[which] - self.alignidx_guess )/t0_sig)**2
                  params[which] += t0_sig*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], self.min_maxt, self.max_maxt)
                  logH += -0.5*((params[which] - self.alignidx_guess)/t0_sig)**2

                elif wf_which == 4: #smooth
                  #gaussian around 10
                  smooth_guess = 20
                  sig = 20*timeStepMult
                  logH -= -0.5*((params[which] - smooth_guess*timeStepMult )/sig)**2
                  params[which] += sig*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], 1, 40*timeStepMult)
                  logH += -0.5*((params[which] - smooth_guess*timeStepMult)/sig)**2

                elif wf_which == 5: #p
                    #gaussian around 10

                    if self.conf.smooth_type == "gen_gaus":
                        p_guess = 2
                        sig = 10
                        logH -= -0.5*((params[which] - p_guess )/sig)**2
                        params[which] += sig*dnest4.randh()
                        params[which] = dnest4.wrap(params[which], 1, 20)
                        logH += -0.5*((params[which] - p_guess)/sig)**2
                    elif self.conf.smooth_type == "skew":
                        params[which] += 20*dnest4.randh()
                        params[which] = dnest4.wrap(params[which], -20, 0)

            else:
                print( "wf which value %d (which value %d) not supported" % (wf_which, which) )
                exit(0)

        return logH
コード例 #27
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        self.changed_wfs[:] = 0

        det_or_wf = rng.randint(2)
        if det_or_wf == 0:
            which = rng.randint(len(priors))
        else:
            which = rng.randint(num_waveforms*8) + len(priors)

        if which < len(priors):
            self.changed_wfs[:] = 1

        if which >= len(priors):
            #this is a waveform variable!
            wf_which =  np.int(np.floor((which - len(priors)) / num_waveforms))
            wf_idx = (which - len(priors)) % num_waveforms
            self.changed_wfs[wf_idx] = 1

            if wf_which == 0:
                params[which] += (detector.detector_radius)*dnest4.randh()
                params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
            elif wf_which == 1:
                max_val = np.pi/4
                params[which] += np.pi/4*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi/4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)
            elif wf_which == 2:
                params[which] += (detector.detector_length)*dnest4.randh()
                params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

            elif wf_which == 3: #scale
                wf = wfs[wf_idx]
                min_scale = wf.wfMax - 0.01*wf.wfMax
                max_scale = wf.wfMax + 0.005*wf.wfMax
                params[which] += (max_scale-min_scale)*dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_scale, max_scale)
            #   print "  adjusted scale to %f" %  ( params[which])

            elif wf_which == 4: #t0
              params[which] += 1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
            elif wf_which == 5: #smooth
              params[which] += 0.1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            elif wf_which == 6: #wf baseline slope (m)
                logH -= -0.5*(params[which]/1E-4)**2
                params[which] += 1E-4*dnest4.randh()
                logH += -0.5*(params[which]/1E-4)**2
            elif wf_which == 7: #wf baseline incercept (b)
                logH -= -0.5*(params[which]/1E-2)**2
                params[which] += 1E-2*dnest4.randh()
                logH += -0.5*(params[which]/1E-2)**2

            #   params[which] += 0.01*dnest4.randh()
            #   params[which]=dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        elif which == ba_idx: #lets call this phi
           params[which] += np.pi*dnest4.randh()
           params[which] = dnest4.wrap(params[which], -np.pi/2, np.pi/2)
        elif which == c_idx: #call it omega
            params[which] += 0.1*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.13, 0.14)
        elif which == dc_idx: #d
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.81, 0.82)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == grad_idx:
          params[which] += (len(detector.gradList)-1)*dnest4.randh()
          params[which] = np.int(dnest4.wrap(params[which], 0, len(detector.gradList)-1))
        elif which >= velo_first_idx and which < velo_first_idx+6:
            mu_max = 100E5
            velo_which =  (which - velo_first_idx)%3
            #TODO: consider long transforming priors on two of these
            if velo_which ==0: #mu0 parameter
                params[which] += (mu_max - 10E3)  *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3, mu_max)
            elif velo_which ==1: #ln(1/beta)
                space = np.log(1/.1)
                params[which] += space *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, np.log(1/.1))
            elif velo_which == 2:
                minval, maxval = 5E6, 1E8
                params[which] += (maxval-minval) *dnest4.randh()
                params[which] = dnest4.wrap(params[which], minval, maxval)

        elif which == trap_idx:
          params[which] += prior_vars[trap_idx]*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 50, 1000)

        else: #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)


        return logH
コード例 #28
0
    def perturb(self, params):
        """
         Unlike in C++, this takes a numpy array of parameters as input,
         and modifies it in-place. The return value is still logH.
         """
        logH = 0.0
        which = rng.randint(len(params))

        if which >= len(priors):
            #this is a waveform variable!
            wf_which = np.floor((which - len(priors)) / num_waveforms)
            if wf_which == 0:
                params[which] += (detector.detector_radius) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0,
                                            detector.detector_radius)
            elif wf_which == 1:
                max_val = np.pi / 4
                params[which] += np.pi / 4 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi / 4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)
            elif wf_which == 2:
                params[which] += (detector.detector_length) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0,
                                            detector.detector_length)

            elif wf_which == 3:  #scale
                wf_idx = (which - len(priors)) % num_waveforms
                wf = wfs[wf_idx]
                params[which] += dnest4.randh()
                params[which] = dnest4.wrap(params[which],
                                            wf.wfMax - 10 * wf.baselineRMS,
                                            wf.wfMax + 10 * wf.baselineRMS)
                params[which] = np.clip(params[which],
                                        wf.wfMax - 50 * wf.baselineRMS,
                                        wf.wfMax + 50 * wf.baselineRMS)
            #   print "  adjusted scale to %f" %  ( params[which])

            elif wf_which == 4:  #t0
                params[which] += 1 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
            elif wf_which == 5:  #smooth
                params[which] += 0.1 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            elif wf_which == 6:  #wf baseline slope (m)
                logH -= -0.5 * (params[which] / 1E-4)**2
                params[which] += 1E-4 * dnest4.randh()
                logH += -0.5 * (params[which] / 1E-4)**2
            elif wf_which == 7:  #wf baseline incercept (b)
                logH -= -0.5 * (params[which] / 1E-2)**2
                params[which] += 1E-2 * dnest4.randh()
                logH += -0.5 * (params[which] / 1E-2)**2

            #   params[which] += 0.01*dnest4.randh()
            #   params[which]=dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        elif which == ba_idx:  #b over a
            params[which] += 70 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -50, 20)
        elif which == c_idx:  #b over a
            #this is now 1/gain (so assume it goes from 1 to e^-...7?)
            log_gain = np.log(params[which])
            log_gain += 10 * dnest4.randh()
            log_gain = dnest4.wrap(log_gain, -10, 0.0)
            params[which] = np.exp(log_gain)
            #  params[which] += 4*dnest4.randh()
            #  params[which] = dnest4.wrap(params[which], -2, 2)
        elif which == dc_idx:  #b over a
            params[which] += 1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 1)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2
            params[which] += prior_vars[which] * dnest4.randh()
            logH += -0.5 * (
                (params[which] - priors[which]) / prior_vars[which])**2

        else:  #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)

        return logH
コード例 #29
0
    def perturb_wf(self, params, wf_idx):

        wf_which = rng.randint(8) #8 wf params
        which =  len(priors) + wf_which*num_waveforms + wf_idx

        if wf_which == 0 or wf_which == 4: #radius and t0
          wf_idx = (which - len(priors)) % num_waveforms
          rad_idx = len(priors) + wf_idx
          theta_idx =  len(priors) + 2*num_waveforms+ wf_idx
          t0_idx =  len(priors) + 4*num_waveforms+ wf_idx

          theta = params[theta_idx]

          #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
          theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
          theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
        #   print "theta: %f pi" % (theta / np.pi)
          if theta <= theta_taper:
             z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
             max_rad = z / np.sin(theta)
          elif theta <= theta_eq:
              max_rad = detector.detector_radius / np.cos(theta)
            #   print "max rad radius: %f" %  max_rad
          else:
              theta_comp = np.pi/2 - theta
              max_rad = detector.detector_length / np.cos(theta_comp)
            #   print "max rad length: %f" %  max_rad

          #AND THE MINIMUM (from PC dimple)
          #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )
          min_rad = np.amax([detector.pcRad, detector.pcLen])

          mean = [0, 0]
          cov = [[1, -0.8], [-0.8, 1]]
          jumps = np.array((0.1*dnest4.randh(), 0.1*dnest4.randh()))
          (r_jump, t0_jump) = np.dot(cov, jumps)

          params[rad_idx] = dnest4.wrap(params[rad_idx] + r_jump , min_rad, max_rad)
          params[t0_idx] = dnest4.wrap(params[t0_idx] + t0_jump , min_t0, max_t0)

        elif wf_which == 1:
            max_val = np.pi/4
            params[which] += np.pi/4*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, max_val)
            if params[which] < 0 or params[which] > np.pi/4:
                print "wtf phi"
            #params[which] = np.clip(params[which], 0, max_val)

        elif wf_which ==2: #theta
          wf_idx = (which - len(priors)) % num_waveforms
          rad_idx = len(priors) + wf_idx
          rad = params[rad_idx]
        #   print "rad: %f" % rad
          if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
              max_val = np.pi/2
              min_val = 0
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
          else:
              if rad < detector.detector_radius - detector.taper_length:
                  #can't possibly hit the taper
                #   print "less than taper adjustment"
                  min_val = 0
              elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                  #low enough that it could hit the taper region
                #   print "taper adjustment"
                  a = detector.detector_radius - detector.taper_length
                  z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                  min_val = np.arcsin(z/rad)
              else:
                  #longer than could hit the taper
                #   print  " longer thantaper adjustment"
                  min_val = np.arccos(detector.detector_radius/rad)

              if rad < detector.detector_length:
                  max_val = np.pi/2
              else:
                  max_val = np.pi/2 - np.arccos(detector.detector_length/rad)
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

          params[which] += (max_val-min_val)*dnest4.randh()
          params[which] = dnest4.wrap(params[which], min_val, max_val)
        #   params[which] = np.clip(params[which], min_val, max_val)
          if params[which] < min_val or params[which] > max_val:
            print "wtf theta"

        elif wf_which == 3: #scale
          wf_idx = (which - len(priors)) % num_waveforms
          wf = wfs[wf_idx]
          params[which] += dnest4.randh()
          params[which] = dnest4.wrap(params[which], wf.wfMax - 10*wf.baselineRMS, wf.wfMax + 10*wf.baselineRMS)
          params[which] = np.clip(params[which], wf.wfMax - 50*wf.baselineRMS, wf.wfMax + 50*wf.baselineRMS)
        #   print "  adjusted scale to %f" %  ( params[which])
        elif wf_which == 5: #smooth
          params[which] += 0.1*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 0, 25)
        #   print "  adjusted smooth to %f" %  ( params[which])

        elif wf_which == 6: #wf baseline slope (m)
          params[which] += 0.001*dnest4.randh()
          params[which]=dnest4.wrap(params[which], -0.01, 0.01)
        #   print "  adjusted m to %f" %  ( params[which])
        elif wf_which == 7: #wf baseline incercept (b)
          params[which] += 0.01*dnest4.randh()
          params[which]=dnest4.wrap(params[which], -1, 1)
        #   print "  adjusted b to %f" %  ( params[which])
        else:
            print "unknown wf param number drawn: %d" % wf_which
            exit(0)
コード例 #30
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0

        #this adjusts one wf at a time
        #choose which set of params to adjust (detector, wf 1, wf2...)
        #
        # num_sets = num_waveforms+1
        # set_idx = rng.randint(num_sets)
        #
        # #choose how many t
        # reps = 1;
        # if(rng.rand() < 0.5):
        #     reps += np.int(np.power(100.0, rng.rand()));
        #
        # for i in range(reps):
        #     if set_idx == 0:
        #         #detector param
        #         which = rng.randint(len(priors))
        #     else:
        #         #waveform param (from the one waveform we're adjusting this go-around
        #         which = rng.randint(8) + len(priors) + (set_idx-1)*num_waveforms

        #this adjusts wfs simultaneouslt
        reps = 1;
        if(rng.rand() < 0.5):
            reps += np.int(np.power(100.0, rng.rand()));

        for i in range(reps):
            if(rng.rand() < 0.5):
                #detector param
                which = rng.randint(len(priors))
            else:
                #waveform param (from any of the waveforms)
                which = rng.randint(8*num_waveforms) + len(priors)

            if which >= len(priors):
                #this is a waveform variable!
                wf_which =  np.int(np.floor((which - len(priors)) / num_waveforms))
                wf_idx = (which - len(priors)) % num_waveforms

                wf_idx = (which - len(priors)) % num_waveforms
                rad_idx = len(priors) + wf_idx
                theta_idx =  len(priors) + 2*num_waveforms+ wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                  theta = params[theta_idx]

                  #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                  theta_eq = np.arctan(detector.detector_length/detector.detector_radius)
                  theta_taper = np.arctan(detector.taper_length/detector.detector_radius)
                #   print "theta: %f pi" % (theta / np.pi)
                  if theta <= theta_taper:
                     z = np.tan(theta)*(detector.detector_radius - detector.taper_length) / (1-np.tan(theta))
                     max_rad = z / np.sin(theta)
                  elif theta <= theta_eq:
                      max_rad = detector.detector_radius / np.cos(theta)
                    #   print "max rad radius: %f" %  max_rad
                  else:
                      theta_comp = np.pi/2 - theta
                      max_rad = detector.detector_length / np.cos(theta_comp)
                    #   print "max rad length: %f" %  max_rad

                  #AND THE MINIMUM (from PC dimple)
                  #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

                  min_rad = np.amax([detector.pcRad, detector.pcLen])

                  total_max_rad = np.sqrt(detector.detector_length**2 + detector.detector_radius**2 )

                  params[which] += total_max_rad*dnest4.randh()
                  params[which] = dnest4.wrap(params[which] , min_rad, max_rad)

                elif wf_which ==2: #theta
                  rad = params[rad_idx]

                #   print "rad: %f" % rad
                  if rad < np.amin([detector.detector_radius - detector.taper_length, detector.detector_length]):
                      max_val = np.pi/2
                      min_val = 0
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
                  else:
                      if rad < detector.detector_radius - detector.taper_length:
                          #can't possibly hit the taper
                        #   print "less than taper adjustment"
                          min_val = 0
                      elif rad < np.sqrt(detector.detector_radius**2 + detector.taper_length**2):
                          #low enough that it could hit the taper region
                        #   print "taper adjustment"
                          a = detector.detector_radius - detector.taper_length
                          z = 0.5 * (np.sqrt(2*rad**2-a**2) - a)
                          min_val = np.arcsin(z/rad)
                      else:
                          #longer than could hit the taper
                        #   print  " longer thantaper adjustment"
                          min_val = np.arccos(detector.detector_radius/rad)

                      if rad < detector.detector_length:
                          max_val = np.pi/2
                      else:
                          max_val = np.pi/2 - np.arccos(detector.detector_length/rad)
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

                  params[which] += np.pi/2*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], min_val, max_val)

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi/4
                    params[which] += np.pi/4*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)
                    if params[which] < 0 or params[which] > np.pi/4:
                        print "wtf phi"

                # elif wf_which == 2:
                #     params[which] += (detector.detector_length)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

                elif wf_which == 3: #scale
                    wf = wfs[wf_idx]
                    min_scale = wf.wfMax - 0.01*wf.wfMax
                    max_scale = wf.wfMax + 0.005*wf.wfMax
                    params[which] += (max_scale-min_scale)*dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_scale, max_scale)
                #   print "  adjusted scale to %f" %  ( params[which])

                elif wf_which == 4: #t0
                  params[which] += 1*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
                elif wf_which == 5: #smooth
                  params[which] += 0.1*dnest4.randh()
                  params[which] = dnest4.wrap(params[which], 0, 25)
                #   print "  adjusted smooth to %f" %  ( params[which])

                elif wf_which == 6: #wf baseline slope (m)
                    logH -= -0.5*(params[which]/1E-4)**2
                    params[which] += 1E-4*dnest4.randh()
                    logH += -0.5*(params[which]/1E-4)**2
                elif wf_which == 7: #wf baseline incercept (b)
                    logH -= -0.5*(params[which]/1E-2)**2
                    params[which] += 1E-2*dnest4.randh()
                    logH += -0.5*(params[which]/1E-2)**2
                else:
                    print "wf which value %d (which value %d) not supported" % (wf_which, which)
                    exit(0)

                #   params[which] += 0.01*dnest4.randh()
                #   params[which]=dnest4.wrap(params[which], -1, 1)
                #   print "  adjusted b to %f" %  ( params[which])

            elif which == ba_idx: #lets call this phi
               params[which] += (tf_phi_max + np.pi/2)*dnest4.randh()
               params[which] = dnest4.wrap(params[which], -np.pi/2, tf_phi_max)
            elif which == c_idx: #call it omega
                params[which] += 0.1*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0.13, 0.14)
            elif which == dc_idx: #d
                params[which] += 0.01*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0.81, 0.82)

            elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
                #all normally distributed priors
                logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
                params[which] += prior_vars[which]*dnest4.randh()
                logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

            elif which == grad_idx:
              params[which] += (detector.gradList[-1] - detector.gradList[0])*dnest4.randh()
              params[which] = dnest4.wrap(params[which], detector.gradList[0], detector.gradList[-1])
            elif which == imp_avg_idx:
              params[which] += (detector.impAvgList[-1] - detector.impAvgList[0])*dnest4.randh()
              params[which] = dnest4.wrap(params[which], detector.impAvgList[0], detector.impAvgList[-1])

            elif which >= velo_first_idx and which < velo_first_idx+6:
                mu_max = 100E5
                velo_which =  (which - velo_first_idx)%3
                #TODO: consider long transforming priors on two of these
                if velo_which ==0: #mu0 parameter
                    params[which] += (mu_max - 10E3)  *dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 10E3, mu_max)
                elif velo_which ==1: #ln(1/beta)
                    space = np.log(1/.1)
                    params[which] += space *dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, np.log(1/.1))
                elif velo_which == 2:
                    minval, maxval = 5E6, 1E8
                    params[which] += (maxval-minval) *dnest4.randh()
                    params[which] = dnest4.wrap(params[which], minval, maxval)

            # elif which >= k0_first_idx and which < k0_first_idx+4:
            #     minval, maxval = -50, 50
            #     params[which] += (maxval-minval) *dnest4.randh()
            #     params[which] = dnest4.wrap(params[which], minval, maxval)

            elif which == trap_idx:
              params[which] += (1000 - traprc_min)*dnest4.randh()
              params[which] = dnest4.wrap(params[which], traprc_min, 1000)

            else: #velocity or rc params: cant be below 0, can be arb. large
                print "which value %d not supported" % which
                exit(0)


        return logH
コード例 #31
0
    def perturb_wf(
        self,
        params,
        wf_idx,
    ):
        #do both wf and detector params in case theres strong correlation
        logH = 0.0

        reps = 1
        if rng.rand() < 0.5:
            reps += np.int(np.power(100.0, rng.rand()))

        for i in range(reps):
            wf_which = rng.randint(6)

            # my_which = rng.randint(len(priors) + 8)

            # if my_which < len(priors):
            #     #detector variable
            #     logH += self.perturb_detector(params, my_which)
            #
            # else:
            if wf_which < 6:
                #this is a waveform variable!
                # wf_which =  np.int(my_which - len(priors))

                #which idx of the global params array
                which = len(priors) + wf_which * num_waveforms + wf_idx

                rad_idx = len(priors) + wf_idx
                theta_idx = len(priors) + 2 * num_waveforms + wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                    theta = params[theta_idx]

                    #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                    theta_eq = np.arctan(detector.detector_length /
                                         detector.detector_radius)
                    theta_taper = np.arctan(detector.taper_length /
                                            detector.detector_radius)
                    #   print "theta: %f pi" % (theta / np.pi)
                    if theta <= theta_taper:
                        z = np.tan(theta) * (detector.detector_radius -
                                             detector.taper_length) / (
                                                 1 - np.tan(theta))
                        max_rad = z / np.sin(theta)
                    elif theta <= theta_eq:
                        max_rad = detector.detector_radius / np.cos(theta)
                    #   print "max rad radius: %f" %  max_rad
                    else:
                        theta_comp = np.pi / 2 - theta
                        max_rad = detector.detector_length / np.cos(theta_comp)
                    #   print "max rad length: %f" %  max_rad

                    #AND THE MINIMUM (from PC dimple)
                    #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

                    min_rad = np.amax([detector.pcRad, detector.pcLen])

                    total_max_rad = np.sqrt(detector.detector_length**2 +
                                            detector.detector_radius**2)

                    params[which] += total_max_rad * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_rad,
                                                max_rad)

                elif wf_which == 2:  #theta
                    rad = params[rad_idx]

                    #   print "rad: %f" % rad
                    if rad < np.amin([
                            detector.detector_radius - detector.taper_length,
                            detector.detector_length
                    ]):
                        max_val = np.pi / 2
                        min_val = 0
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
                    else:
                        if rad < detector.detector_radius - detector.taper_length:
                            #can't possibly hit the taper
                            #   print "less than taper adjustment"
                            min_val = 0
                        elif rad < np.sqrt(detector.detector_radius**2 +
                                           detector.taper_length**2):
                            #low enough that it could hit the taper region
                            #   print "taper adjustment"
                            a = detector.detector_radius - detector.taper_length
                            z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                            min_val = np.arcsin(z / rad)
                        else:
                            #longer than could hit the taper
                            #   print  " longer thantaper adjustment"
                            min_val = np.arccos(detector.detector_radius / rad)

                        if rad < detector.detector_length:
                            max_val = np.pi / 2
                        else:
                            max_val = np.pi / 2 - np.arccos(
                                detector.detector_length / rad)
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

                    params[which] += np.pi / 2 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_val,
                                                max_val)

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi / 4
                    params[which] += np.pi / 4 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)
                    if params[which] < 0 or params[which] > np.pi / 4:
                        print("wtf phi")

                # elif wf_which == 2:
                #     params[which] += (detector.detector_length)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

                elif wf_which == 3:  #scale
                    wf = wfs[wf_idx]
                    min_scale = wf.wfMax - 0.01 * wf.wfMax
                    max_scale = wf.wfMax + 0.005 * wf.wfMax
                    params[which] += (max_scale - min_scale) * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_scale,
                                                max_scale)
                #   print "  adjusted scale to %f" %  ( params[which])

                elif wf_which == 4:  #t0
                    params[which] += 1 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_maxt,
                                                max_maxt)
                elif wf_which == 5:  #smooth
                    params[which] += 0.1 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, 25)
                #   print "  adjusted smooth to %f" %  ( params[which])

                # elif wf_which == 6: #wf baseline slope (m)
                #     logH -= -0.5*(params[which]/1E-4)**2
                #     params[which] += 1E-4*dnest4.randh()
                #     logH += -0.5*(params[which]/1E-4)**2
                # elif wf_which == 7: #wf baseline incercept (b)
                #     logH -= -0.5*(params[which]/1E-2)**2
                #     params[which] += 1E-2*dnest4.randh()
                #     logH += -0.5*(params[which]/1E-2)**2
                else:
                    print("wf which value %d (which value %d) not supported" %
                          (wf_which, which))
                    exit(0)

                #   params[which] += 0.01*dnest4.randh()
                #   params[which]=dnest4.wrap(params[which], -1, 1)
                #   print "  adjusted b to %f" %  ( params[which])

        return logH
コード例 #32
0
     def perturb(self, params):
         """
         Unlike in C++, this takes a numpy array of parameters as input,
         and modifies it in-place. The return value is still logH.
         """
         logH = 0.0
         which = rng.randint(len(params))

         if which >= len(priors):
             #this is a waveform variable!
             wf_which = np.floor((which - len(priors)) / num_waveforms)
             if wf_which == 0:
                 params[which] += (detector.detector_radius)*dnest4.randh()
                 params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
             elif wf_which == 1:
                 max_val = np.pi/4
                 params[which] += np.pi/4*dnest4.randh()
                 params[which] = dnest4.wrap(params[which], 0, max_val)
                 if params[which] < 0 or params[which] > np.pi/4:
                     print "wtf phi"
                 #params[which] = np.clip(params[which], 0, max_val)
             elif wf_which == 2:
                 params[which] += (detector.detector_length)*dnest4.randh()
                 params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

             elif wf_which == 3: #scale
               wf_idx = (which - len(priors)) % num_waveforms
               wf = wfs[wf_idx]
               params[which] += dnest4.randh()
               params[which] = dnest4.wrap(params[which], wf.wfMax - 10*wf.baselineRMS, wf.wfMax + 10*wf.baselineRMS)
               params[which] = np.clip(params[which], wf.wfMax - 50*wf.baselineRMS, wf.wfMax + 50*wf.baselineRMS)
             #   print "  adjusted scale to %f" %  ( params[which])

             elif wf_which == 4: #t0
               params[which] += 1*dnest4.randh()
               params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
             elif wf_which == 5: #smooth
               params[which] += 0.1*dnest4.randh()
               params[which] = dnest4.wrap(params[which], 0, 25)
             #   print "  adjusted smooth to %f" %  ( params[which])

             elif wf_which == 6: #wf baseline slope (m)
                 logH -= -0.5*(params[which]/1E-4)**2
                 params[which] += 1E-4*dnest4.randh()
                 logH += -0.5*(params[which]/1E-4)**2
             elif wf_which == 7: #wf baseline incercept (b)
                 logH -= -0.5*(params[which]/1E-2)**2
                 params[which] += 1E-2*dnest4.randh()
                 logH += -0.5*(params[which]/1E-2)**2

             #   params[which] += 0.01*dnest4.randh()
             #   params[which]=dnest4.wrap(params[which], -1, 1)
             #   print "  adjusted b to %f" %  ( params[which])

         elif which == ba_idx: #b over a
           params[which] += 70*dnest4.randh()
           params[which] = dnest4.wrap(params[which], -50, 20)
         elif which == c_idx: #b over a
            #this is now 1/gain (so assume it goes from 1 to e^-...7?)
            log_gain = np.log(params[which])
            log_gain += 10*dnest4.randh()
            log_gain = dnest4.wrap(log_gain, -10, 0.0)
            params[which] = np.exp(log_gain)
            #  params[which] += 4*dnest4.randh()
            #  params[which] = dnest4.wrap(params[which], -2, 2)
         elif which == dc_idx: #b over a
             params[which] += 1*dnest4.randh()
             params[which] = dnest4.wrap(params[which], 0, 1)

         elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
             #all normally distributed priors
             logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
             params[which] += prior_vars[which]*dnest4.randh()
             logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

         else: #velocity or rc params: cant be below 0, can be arb. large
             print "which value %d not supported" % which
             exit(0)


         return logH
コード例 #33
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(len(params))

        if which == 0 or which == 4:  #radius and t0
            #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
            theta_eq = np.arctan(detector.detector_length /
                                 detector.detector_radius)
            theta_taper = np.arctan(detector.taper_length /
                                    detector.detector_radius)
            theta = params[2]
            #   print "theta: %f pi" % (theta / np.pi)
            if theta <= theta_taper:
                z = np.tan(theta) * (detector.detector_radius -
                                     detector.taper_length) / (1 -
                                                               np.tan(theta))
                max_rad = z / np.sin(theta)
            elif theta <= theta_eq:
                max_rad = detector.detector_radius / np.cos(theta)
            #   print "max rad radius: %f" %  max_rad
            else:
                theta_comp = np.pi / 2 - theta
                max_rad = detector.detector_length / np.cos(theta_comp)
            #   print "max rad length: %f" %  max_rad

            #AND THE MINIMUM (from PC dimple)
            #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )
            min_rad = np.amax([detector.pcRad, detector.pcLen])

            mean = [0, 0]
            cov = [[1, -0.8], [-0.8, 1]]
            jumps = np.array((0.1 * dnest4.randh(), 0.1 * dnest4.randh()))
            (r_jump, t0_jump) = np.dot(cov, jumps)
            params[0] = dnest4.wrap(params[0] + r_jump, min_rad, max_rad)
            params[4] = dnest4.wrap(params[4] + t0_jump, min_t0, max_t0)

            if not checkPosition(params):
                print "... in radius step"

        elif which == 1:
            max_val = np.pi / 4
            params[which] += np.pi / 4 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, max_val)
            if params[which] < 0 or params[which] > np.pi / 4:
                print "wtf phi"
            #params[which] = np.clip(params[which], 0, max_val)

        elif which == 2:  #theta
            rad = params[0]
            #   print "rad: %f" % rad
            if rad < np.amin([
                    detector.detector_radius - detector.taper_length,
                    detector.detector_length
            ]):
                max_val = np.pi / 2
                min_val = 0
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
            else:
                if rad < detector.detector_radius - detector.taper_length:
                    #can't possibly hit the taper
                    #   print "less than taper adjustment"
                    min_val = 0
                elif rad < np.sqrt(detector.detector_radius**2 +
                                   detector.taper_length**2):
                    #low enough that it could hit the taper region
                    #   print "taper adjustment"
                    a = detector.detector_radius - detector.taper_length
                    z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                    min_val = np.arcsin(z / rad)
                else:
                    #longer than could hit the taper
                    #   print  " longer thantaper adjustment"
                    min_val = np.arccos(detector.detector_radius / rad)

                if rad < detector.detector_length:
                    max_val = np.pi / 2
                else:
                    max_val = np.pi / 2 - np.arccos(
                        detector.detector_length / rad)
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

            params[which] += (max_val - min_val) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], min_val, max_val)
            #   params[which] = np.clip(params[which], min_val, max_val)
            if params[which] < min_val or params[which] > max_val:
                print "wtf theta"
            if not checkPosition(params):
                print "... in theta step with rad %f" % rad
                print "theta: min %f pi, max %f pi" % (min_val / np.pi,
                                                       max_val / np.pi)

        elif which == 3:  #scale
            params[which] += dnest4.randh()
            params[which] = dnest4.wrap(params[which],
                                        wf.wfMax - 10 * wf.baselineRMS,
                                        wf.wfMax + 10 * wf.baselineRMS)


#        elif which == 4: #t0
#          params[which] += 0.1*dnest4.randh()
#          params[which] = np.clip(params[which], 0, wf.wfLength)
        elif which == 5:  #smooth
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 20)

        elif which == 6 or which == 7:  #m and b, respectively
            #normally distributed, no cutoffs
            params[which] += prior_vars[which] * dnest4.randh()
            if which == 6:
                dnest4.wrap(params[which], -0.01, 0.01)
                params[which] = np.clip(params[which], -0.01, 0.01)
            if which == 7:
                dnest4.wrap(params[which], -01, 01)
                params[which] = np.clip(params[which], -01, 01)
        else:
            print "which value %d not supported" % which
            exit(0)

        return logH
コード例 #34
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0
        which = rng.randint(len(params))

        if which >= len(priors):
            #this is a waveform variable!
            wf_which = np.floor((which - len(priors)) / num_waveforms)

            if wf_which == 0:
                params[which] += (detector.detector_radius)*dnest4.randh()
                params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
            elif wf_which == 1:
                max_val = np.pi/4
                params[which] += np.pi/4*dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, max_val)
                if params[which] < 0 or params[which] > np.pi/4:
                    print "wtf phi"
                #params[which] = np.clip(params[which], 0, max_val)
            elif wf_which == 2:
                params[which] += (detector.detector_length)*dnest4.randh()
                params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

            elif wf_which == 3: #scale
                wf_idx = (which - len(priors)) % num_waveforms
                wf = wfs[wf_idx]
                min_scale = wf.wfMax - 0.01*wf.wfMax
                max_scale = wf.wfMax + 0.005*wf.wfMax
                params[which] += (max_scale-min_scale)*dnest4.randh()
                params[which] = dnest4.wrap(params[which], min_scale, max_scale)
            #   print "  adjusted scale to %f" %  ( params[which])

            elif wf_which == 4: #t0
              params[which] += 1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], min_maxt, max_maxt)
            elif wf_which == 5: #smooth
              params[which] += 0.1*dnest4.randh()
              params[which] = dnest4.wrap(params[which], 0, 25)
            #   print "  adjusted smooth to %f" %  ( params[which])

            elif wf_which == 6: #wf baseline slope (m)
                logH -= -0.5*(params[which]/1E-4)**2
                params[which] += 1E-4*dnest4.randh()
                logH += -0.5*(params[which]/1E-4)**2
            elif wf_which == 7: #wf baseline incercept (b)
                logH -= -0.5*(params[which]/1E-2)**2
                params[which] += 1E-2*dnest4.randh()
                logH += -0.5*(params[which]/1E-2)**2

            #   params[which] += 0.01*dnest4.randh()
            #   params[which]=dnest4.wrap(params[which], -1, 1)
            #   print "  adjusted b to %f" %  ( params[which])

        elif which == ba_idx: #b over a
            params[which] += 600*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -200, 400)
        elif which == c_idx: #b over a
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.82, -0.8)
        elif which == dc_idx: #b over a
            params[which] += 0.01*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1.03, -0.98)

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == grad_idx:
          params[which] += (len(detector.gradList)-1)*dnest4.randh()
          params[which] = np.int(dnest4.wrap(params[which], 0, len(detector.gradList)-1))
        elif which >= velo_first_idx and which < velo_first_idx+6:
            velo_which =  (which - velo_first_idx)%3
            #TODO: consider long transforming priors on two of these
            if velo_which ==0: #mu0 parameter
                params[which] += (500E3 - 10E3)  *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3, 500E3)
            elif velo_which ==1: #ln(1/beta)
                space = np.log(1/.1)
                params[which] += space *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0, np.log(1/.1))
            elif velo_which == 2:
                space = 10E3 * 100 - 500E3 * 300
                params[which] += space *dnest4.randh()
                params[which] = dnest4.wrap(params[which], 10E3 * 100, 500E3 * 300)

        elif which == trap_idx:
          params[which] += prior_vars[trap_idx]*dnest4.randh()
          params[which] = dnest4.wrap(params[which], 50, 1000)

        else: #velocity or rc params: cant be below 0, can be arb. large
            print "which value %d not supported" % which
            exit(0)


        return logH
コード例 #35
0
ファイル: Model.py プロジェクト: benshanks/mjd-analysis
    def perturb_detector(self, params, which):
        logH = 0.0
        detector = self.detector
        num_waveforms =  self.num_waveforms
        priors = self.priors
        prior_vars = self.prior_vars

        if which == phi_idx: #lets call this phi
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], -np.pi, 0)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
        elif which == omega_idx: #call it omega
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, np.pi)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
        elif which == d_idx: #d
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0.5, 0.999)
            # params[which] = dnest4.wrap(params[which], 0, 5000)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
            #all normally distributed priors
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            if which ==  rcfrac_idx:  params[which] = dnest4.wrap(params[which], 0, 1)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
        #
        # elif which == num0_idx:
        #   sig = 3*timeStepMult
        #   logH -= -0.5*((params[which] - 0.)/sig)**2
        #   params[which] += sig*dnest4.randh()
        #   logH += -0.5*((params[which] - 0.)/sig)**2

        # elif which == aliasrc_idx:
        #     params[which] += 19.9*dnest4.randh()
        #     params[which] = dnest4.wrap(params[which], 0.1, 20)

        elif which == grad_idx:
            # logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            # params[which] += prior_vars[which] *dnest4.randh()
            # params[which] = dnest4.wrap(params[which], detector.gradList[0], detector.gradList[-1])
            # logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

            paramlist = detector.gradList
            params[which] += (paramlist[-1] - paramlist[0])*dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0], paramlist[-1])

        elif which == imp_avg_idx:
            # logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            # params[which] += prior_vars[which] *dnest4.randh()
            # params[which] = dnest4.wrap(params[which], detector.impAvgList[0], detector.impAvgList[-1])
            # logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2
            paramlist = detector.impAvgList
            params[which] += (paramlist[-1] - paramlist[0])*dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0], paramlist[-1])

        elif which == pcrad_idx:
            paramlist = detector.pcRadList
            params[which] += (paramlist[-1] - paramlist[0])*dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0], paramlist[-1])

        elif which == pclen_idx:
            paramlist = detector.pcLenList
            params[which] += (paramlist[-1] - paramlist[0])*dnest4.randh()
            params[which] = dnest4.wrap(params[which], paramlist[0], paramlist[-1])

        elif which == trap_idx:
            # let's go with log10-uniform on this
            # log_trap = np.log10(params[which])
            # log_trap += 3*dnest4.randh()
            # log_trap = dnest4.wrap(log_trap, 1, 4)
            # params[which] = 10**(log_trap)

            # p_guess = 350
            # sig = 50
            # logH -= -0.5*((params[which] - p_guess )/sig)**2
            # params[which] += sig*dnest4.randh()
            # params[which] = dnest4.wrap(params[which], 100, 600)
            # logH += -0.5*((params[which] - p_guess)/sig)**2

            traprc_min = 10
            params[which] += (1000 - traprc_min)*dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000)

        elif which == untrap_idx:
            traprc_min = 1000
            params[which] += (1000000 - traprc_min)*dnest4.randh()
            params[which] = dnest4.wrap(params[which], traprc_min, 1000000)
        elif which == energy_idx:
            wf_guess = self.conf.energy_guess
            sig = 10

            logH -= -0.5*((params[which] - wf_guess  )/sig)**2
            params[which] += sig*dnest4.randh()
            params[which] = dnest4.wrap(params[which], wf_guess - 30, wf_guess + 30)
            logH += -0.5*((params[which] - wf_guess)/sig)**2


        elif which >= velo_first_idx and which < velo_first_idx+4:
            logH -= -0.5*((params[which] - priors[which])/prior_vars[which])**2
            params[which] += prior_vars[which]*dnest4.randh()
            params[which] = dnest4.wrap(params[which], 1, priors[which]*10)
            logH += -0.5*((params[which] - priors[which])/prior_vars[which])**2

        elif which == velo_first_idx+4 or which == velo_first_idx+5:
            params[which] += (self.conf.beta_lims[1] - self.conf.beta_lims[0])  *dnest4.randh()
            params[which] = dnest4.wrap(params[which], self.conf.beta_lims[0], self.conf.beta_lims[1])

        else: #velocity or rc params: cant be below 0, can be arb. large
            print ("which value %d not supported" % which)
            exit(0)

        return logH
コード例 #36
0
    def perturb_wf(self, params, wf_idx):

        wf_which = rng.randint(8)  #8 wf params
        which = len(priors) + wf_which * num_waveforms + wf_idx

        if wf_which == 0 or wf_which == 4:  #radius and t0
            wf_idx = (which - len(priors)) % num_waveforms
            rad_idx = len(priors) + wf_idx
            theta_idx = len(priors) + 2 * num_waveforms + wf_idx
            t0_idx = len(priors) + 4 * num_waveforms + wf_idx

            theta = params[theta_idx]

            #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
            theta_eq = np.arctan(detector.detector_length /
                                 detector.detector_radius)
            theta_taper = np.arctan(detector.taper_length /
                                    detector.detector_radius)
            #   print "theta: %f pi" % (theta / np.pi)
            if theta <= theta_taper:
                z = np.tan(theta) * (detector.detector_radius -
                                     detector.taper_length) / (1 -
                                                               np.tan(theta))
                max_rad = z / np.sin(theta)
            elif theta <= theta_eq:
                max_rad = detector.detector_radius / np.cos(theta)
            #   print "max rad radius: %f" %  max_rad
            else:
                theta_comp = np.pi / 2 - theta
                max_rad = detector.detector_length / np.cos(theta_comp)
            #   print "max rad length: %f" %  max_rad

            #AND THE MINIMUM (from PC dimple)
            #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )
            min_rad = np.amax([detector.pcRad, detector.pcLen])

            mean = [0, 0]
            cov = [[1, -0.8], [-0.8, 1]]
            jumps = np.array((0.1 * dnest4.randh(), 0.1 * dnest4.randh()))
            (r_jump, t0_jump) = np.dot(cov, jumps)

            params[rad_idx] = dnest4.wrap(params[rad_idx] + r_jump, min_rad,
                                          max_rad)
            params[t0_idx] = dnest4.wrap(params[t0_idx] + t0_jump, min_t0,
                                         max_t0)

        elif wf_which == 1:
            max_val = np.pi / 4
            params[which] += np.pi / 4 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, max_val)
            if params[which] < 0 or params[which] > np.pi / 4:
                print "wtf phi"
            #params[which] = np.clip(params[which], 0, max_val)

        elif wf_which == 2:  #theta
            wf_idx = (which - len(priors)) % num_waveforms
            rad_idx = len(priors) + wf_idx
            rad = params[rad_idx]
            #   print "rad: %f" % rad
            if rad < np.amin([
                    detector.detector_radius - detector.taper_length,
                    detector.detector_length
            ]):
                max_val = np.pi / 2
                min_val = 0
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
            else:
                if rad < detector.detector_radius - detector.taper_length:
                    #can't possibly hit the taper
                    #   print "less than taper adjustment"
                    min_val = 0
                elif rad < np.sqrt(detector.detector_radius**2 +
                                   detector.taper_length**2):
                    #low enough that it could hit the taper region
                    #   print "taper adjustment"
                    a = detector.detector_radius - detector.taper_length
                    z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                    min_val = np.arcsin(z / rad)
                else:
                    #longer than could hit the taper
                    #   print  " longer thantaper adjustment"
                    min_val = np.arccos(detector.detector_radius / rad)

                if rad < detector.detector_length:
                    max_val = np.pi / 2
                else:
                    max_val = np.pi / 2 - np.arccos(
                        detector.detector_length / rad)
            #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

            params[which] += (max_val - min_val) * dnest4.randh()
            params[which] = dnest4.wrap(params[which], min_val, max_val)
            #   params[which] = np.clip(params[which], min_val, max_val)
            if params[which] < min_val or params[which] > max_val:
                print "wtf theta"

        elif wf_which == 3:  #scale
            wf_idx = (which - len(priors)) % num_waveforms
            wf = wfs[wf_idx]
            params[which] += dnest4.randh()
            params[which] = dnest4.wrap(params[which],
                                        wf.wfMax - 10 * wf.baselineRMS,
                                        wf.wfMax + 10 * wf.baselineRMS)
            params[which] = np.clip(params[which],
                                    wf.wfMax - 50 * wf.baselineRMS,
                                    wf.wfMax + 50 * wf.baselineRMS)
        #   print "  adjusted scale to %f" %  ( params[which])
        elif wf_which == 5:  #smooth
            params[which] += 0.1 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], 0, 25)
        #   print "  adjusted smooth to %f" %  ( params[which])

        elif wf_which == 6:  #wf baseline slope (m)
            params[which] += 0.001 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -0.01, 0.01)
        #   print "  adjusted m to %f" %  ( params[which])
        elif wf_which == 7:  #wf baseline incercept (b)
            params[which] += 0.01 * dnest4.randh()
            params[which] = dnest4.wrap(params[which], -1, 1)
        #   print "  adjusted b to %f" %  ( params[which])
        else:
            print "unknown wf param number drawn: %d" % wf_which
            exit(0)
コード例 #37
0
    def perturb(self, params):
        """
        Unlike in C++, this takes a numpy array of parameters as input,
        and modifies it in-place. The return value is still logH.
        """
        logH = 0.0

        #this adjusts one wf at a time
        #choose which set of params to adjust (detector, wf 1, wf2...)
        #
        # num_sets = num_waveforms+1
        # set_idx = rng.randint(num_sets)
        #
        # #choose how many t
        # reps = 1;
        # if(rng.rand() < 0.5):
        #     reps += np.int(np.power(100.0, rng.rand()));
        #
        # for i in range(reps):
        #     if set_idx == 0:
        #         #detector param
        #         which = rng.randint(len(priors))
        #     else:
        #         #waveform param (from the one waveform we're adjusting this go-around
        #         which = rng.randint(8) + len(priors) + (set_idx-1)*num_waveforms

        #this adjusts wfs simultaneouslt
        reps = 1
        if (rng.rand() < 0.5):
            reps += np.int(np.power(100.0, rng.rand()))

        for i in range(reps):
            if (rng.rand() < 0.5):
                #detector param
                which = rng.randint(len(priors))
            else:
                #waveform param (from any of the waveforms)
                which = rng.randint(8 * num_waveforms) + len(priors)

            if which >= len(priors):
                #this is a waveform variable!
                wf_which = np.int(
                    np.floor((which - len(priors)) / num_waveforms))
                wf_idx = (which - len(priors)) % num_waveforms

                wf_idx = (which - len(priors)) % num_waveforms
                rad_idx = len(priors) + wf_idx
                theta_idx = len(priors) + 2 * num_waveforms + wf_idx
                self.changed_wfs[wf_idx] = 1

                if wf_which == 0:
                    theta = params[theta_idx]

                    #FIND THE MAXIMUM RADIUS STILL INSIDE THE DETECTOR
                    theta_eq = np.arctan(detector.detector_length /
                                         detector.detector_radius)
                    theta_taper = np.arctan(detector.taper_length /
                                            detector.detector_radius)
                    #   print "theta: %f pi" % (theta / np.pi)
                    if theta <= theta_taper:
                        z = np.tan(theta) * (detector.detector_radius -
                                             detector.taper_length) / (
                                                 1 - np.tan(theta))
                        max_rad = z / np.sin(theta)
                    elif theta <= theta_eq:
                        max_rad = detector.detector_radius / np.cos(theta)
                    #   print "max rad radius: %f" %  max_rad
                    else:
                        theta_comp = np.pi / 2 - theta
                        max_rad = detector.detector_length / np.cos(theta_comp)
                    #   print "max rad length: %f" %  max_rad

                    #AND THE MINIMUM (from PC dimple)
                    #min_rad  = 1./ ( np.cos(theta)**2/detector.pcRad**2  +  np.sin(theta)**2/detector.pcLen**2 )

                    min_rad = np.amax([detector.pcRad, detector.pcLen])

                    total_max_rad = np.sqrt(detector.detector_length**2 +
                                            detector.detector_radius**2)

                    params[which] += total_max_rad * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_rad,
                                                max_rad)

                elif wf_which == 2:  #theta
                    rad = params[rad_idx]

                    #   print "rad: %f" % rad
                    if rad < np.amin([
                            detector.detector_radius - detector.taper_length,
                            detector.detector_length
                    ]):
                        max_val = np.pi / 2
                        min_val = 0
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)
                    else:
                        if rad < detector.detector_radius - detector.taper_length:
                            #can't possibly hit the taper
                            #   print "less than taper adjustment"
                            min_val = 0
                        elif rad < np.sqrt(detector.detector_radius**2 +
                                           detector.taper_length**2):
                            #low enough that it could hit the taper region
                            #   print "taper adjustment"
                            a = detector.detector_radius - detector.taper_length
                            z = 0.5 * (np.sqrt(2 * rad**2 - a**2) - a)
                            min_val = np.arcsin(z / rad)
                        else:
                            #longer than could hit the taper
                            #   print  " longer thantaper adjustment"
                            min_val = np.arccos(detector.detector_radius / rad)

                        if rad < detector.detector_length:
                            max_val = np.pi / 2
                        else:
                            max_val = np.pi / 2 - np.arccos(
                                detector.detector_length / rad)
                    #   print "theta: min %f pi, max %f pi" % (min_val, max_val)

                    params[which] += np.pi / 2 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_val,
                                                max_val)

                # if wf_which == 0:
                #     params[which] += (detector.detector_radius)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_radius)
                elif wf_which == 1:
                    max_val = np.pi / 4
                    params[which] += np.pi / 4 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, max_val)
                    if params[which] < 0 or params[which] > np.pi / 4:
                        print "wtf phi"

                # elif wf_which == 2:
                #     params[which] += (detector.detector_length)*dnest4.randh()
                #     params[which] = dnest4.wrap(params[which] , 0, detector.detector_length)

                elif wf_which == 3:  #scale
                    wf = wfs[wf_idx]
                    min_scale = wf.wfMax - 0.01 * wf.wfMax
                    max_scale = wf.wfMax + 0.005 * wf.wfMax
                    params[which] += (max_scale - min_scale) * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_scale,
                                                max_scale)
                #   print "  adjusted scale to %f" %  ( params[which])

                elif wf_which == 4:  #t0
                    params[which] += 1 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], min_maxt,
                                                max_maxt)
                elif wf_which == 5:  #smooth
                    params[which] += 0.1 * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0, 25)
                #   print "  adjusted smooth to %f" %  ( params[which])

                elif wf_which == 6:  #wf baseline slope (m)
                    logH -= -0.5 * (params[which] / 1E-4)**2
                    params[which] += 1E-4 * dnest4.randh()
                    logH += -0.5 * (params[which] / 1E-4)**2
                elif wf_which == 7:  #wf baseline incercept (b)
                    logH -= -0.5 * (params[which] / 1E-2)**2
                    params[which] += 1E-2 * dnest4.randh()
                    logH += -0.5 * (params[which] / 1E-2)**2
                else:
                    print "wf which value %d (which value %d) not supported" % (
                        wf_which, which)
                    exit(0)

                #   params[which] += 0.01*dnest4.randh()
                #   params[which]=dnest4.wrap(params[which], -1, 1)
                #   print "  adjusted b to %f" %  ( params[which])

            elif which == ba_idx:  #lets call this phi
                params[which] += (tf_phi_max + np.pi / 2) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], -np.pi / 2,
                                            tf_phi_max)
            elif which == c_idx:  #call it omega
                params[which] += 0.1 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0.13, 0.14)
            elif which == dc_idx:  #d
                params[which] += 0.01 * dnest4.randh()
                params[which] = dnest4.wrap(params[which], 0.81, 0.82)

            elif which == rc1_idx or which == rc2_idx or which == rcfrac_idx:
                #all normally distributed priors
                logH -= -0.5 * (
                    (params[which] - priors[which]) / prior_vars[which])**2
                params[which] += prior_vars[which] * dnest4.randh()
                logH += -0.5 * (
                    (params[which] - priors[which]) / prior_vars[which])**2

            elif which == grad_idx:
                params[which] += (detector.gradList[-1] -
                                  detector.gradList[0]) * dnest4.randh()
                params[which] = dnest4.wrap(params[which],
                                            detector.gradList[0],
                                            detector.gradList[-1])
            elif which == imp_avg_idx:
                params[which] += (detector.impAvgList[-1] -
                                  detector.impAvgList[0]) * dnest4.randh()
                params[which] = dnest4.wrap(params[which],
                                            detector.impAvgList[0],
                                            detector.impAvgList[-1])

            elif which >= velo_first_idx and which < velo_first_idx + 6:
                mu_max = 100E5
                velo_which = (which - velo_first_idx) % 3
                #TODO: consider long transforming priors on two of these
                if velo_which == 0:  #mu0 parameter
                    params[which] += (mu_max - 10E3) * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 10E3, mu_max)
                elif velo_which == 1:  #ln(1/beta)
                    space = np.log(1 / .1)
                    params[which] += space * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], 0,
                                                np.log(1 / .1))
                elif velo_which == 2:
                    minval, maxval = 5E6, 1E8
                    params[which] += (maxval - minval) * dnest4.randh()
                    params[which] = dnest4.wrap(params[which], minval, maxval)

            # elif which >= k0_first_idx and which < k0_first_idx+4:
            #     minval, maxval = -50, 50
            #     params[which] += (maxval-minval) *dnest4.randh()
            #     params[which] = dnest4.wrap(params[which], minval, maxval)

            elif which == trap_idx:
                params[which] += (1000 - traprc_min) * dnest4.randh()
                params[which] = dnest4.wrap(params[which], traprc_min, 1000)

            else:  #velocity or rc params: cant be below 0, can be arb. large
                print "which value %d not supported" % which
                exit(0)

        return logH