def move(self):
     is_modified = self.modify()
     if (not is_modified):
         return
     score = self.target()  # metropolis criterion
     if (score < self.score):
         self.score = score
         self.n_accept += 1
         self.commit_changes()
         if (score < self.lowest_score):
             self.lowest_score = self.score
             self.best_model = self.working_model.deep_copy()
             self.best_i = self.calc_i.deep_copy()
             self.best_nlm_coefs = self.nlm_coefs.deep_copy()
     else:
         delta = (self.score - score) / self.T
         delta = smath.exp(delta)
         if (random.random() < delta):
             #print "Delta, T", delta, self.T, score, self.score
             self.revoke_changes()
             self.n_reject += 1
         else:
             self.score = score
             self.n_accept += 1
             self.commit_changes()
     return
Example #2
0
def eps_eq_core(a1, a2, eps, out, prefix):
  if (hasattr(a1, "__len__")): # traverse list
    assert len(a1) == len(a2)
    for i in xrange(len(a1)):
      if (not eps_eq_core(a1[i], a2[i], eps, out, prefix+"  ")):
        return False
    return True
  if (isinstance(a1, complex)): # complex numbers
    if (not eps_eq_core(a1.real, a2.real, eps, out, prefix+"real ")):
      return False
    if (not eps_eq_core(a1.imag, a2.imag, eps, out, prefix+"imag ")):
      return False
    return True
  ok = True
  if (a1 == 0 or a2 == 0):
    if (abs(a1-a2) > eps):
      ok = False
  else:
    l1 = round(math.log(abs(a1)))
    l2 = round(math.log(abs(a2)))
    m = math.exp(-max(l1, l2))
    if (abs(a1*m-a2*m) > eps):
      ok = False
  if (out is not None):
    annotation = ""
    if (not ok):
      annotation = " eps_eq ERROR"
    print >> out, prefix + str(a1) + annotation
    print >> out, prefix + str(a2) + annotation
    print >> out, prefix.rstrip()
    return True
  return ok
Example #3
0
def eps_eq_core(a1, a2, eps, out, prefix):
  if (hasattr(a1, "__len__")): # traverse list
    assert len(a1) == len(a2)
    for i in xrange(len(a1)):
      if (not eps_eq_core(a1[i], a2[i], eps, out, prefix+"  ")):
        return False
    return True
  if (isinstance(a1, complex)): # complex numbers
    if (not eps_eq_core(a1.real, a2.real, eps, out, prefix+"real ")):
      return False
    if (not eps_eq_core(a1.imag, a2.imag, eps, out, prefix+"imag ")):
      return False
    return True
  ok = True
  if (a1 == 0 or a2 == 0):
    if (abs(a1-a2) > eps):
      ok = False
  else:
    l1 = round(math.log(abs(a1)))
    l2 = round(math.log(abs(a2)))
    m = math.exp(-max(l1, l2))
    if (abs(a1*m-a2*m) > eps):
      ok = False
  if (out is not None):
    annotation = ""
    if (not ok):
      annotation = " eps_eq ERROR"
    print >> out, prefix + str(a1) + annotation
    print >> out, prefix + str(a2) + annotation
    print >> out, prefix.rstrip()
    return True
  return ok
Example #4
0
 def q2(self,rmax):
   a = 3.019
   b = -1.28945
   x = float(rmax)
   if x<10:
     x = 10.0
   if x > 200:
     x = 200.0
   x = smath.log(x)
   y = a+b*x
   return smath.exp(y)
Example #5
0
 def Dnmk(self,n,m,k):
   tmp1 = long(1.0)
   if k%2 != 0:
     tmp1=-1.0
   tmp2   = self.lgf.log_fact(n-k)
   tmp3_1 = self.lgf.log_fact(k)
   tmp3_2 = self.lgf.log_fact( (n+m)/2-k)
   tmp3_3 = self.lgf.log_fact( (n-m)/2-k)
   tmp3 = tmp3_1+tmp3_2+tmp3_3
   result = tmp1*smath.exp(tmp2-tmp3)
   return result
Example #6
0
 def q1(self,rmax):
   a=-3.724
   b=1.269
   c=-0.2639
   x = float(rmax)
   if x<10:
     x = 10.0
   if x > 200:
     x = 200.0
   x = smath.log(x)
   y = a+b*x+c*x*x
   return smath.exp(y)
 def FindReflectionPt(self,kk):
   reflect_matrix=[]
   reflect_value=[]
   self.alpha=random.random()*0.2+0.9
   for ii in range(self.dimension+1-kk, self.dimension+1):
     reflectionPt=(self.centroid*(1.0+self.alpha) - self.alpha*self.matrix[ii])
     reflect_matrix.append(reflectionPt)
     reflect_value.append(self.function(reflectionPt))
   self.reflectionPtValue=min(reflect_value)
   if(self.reflectionPtValue > self.simplexValue[0]):
     p=math.exp(-(self.reflectionPtValue-self.simplexValue[0])/self.T)
     #print p
     if(p >= random.random()):
       self.ReplacePt(kk, reflect_matrix, reflect_value)
   else:
     self.ReplacePt(kk, reflect_matrix, reflect_value)
 def FindReflectionPt(self, kk):
     reflect_matrix = []
     reflect_value = []
     self.alpha = random.random() * 0.2 + 0.9
     for ii in range(self.dimension + 1 - kk, self.dimension + 1):
         reflectionPt = (self.centroid * (1.0 + self.alpha) -
                         self.alpha * self.matrix[ii])
         reflect_matrix.append(reflectionPt)
         reflect_value.append(self.function(reflectionPt))
     self.reflectionPtValue = min(reflect_value)
     if (self.reflectionPtValue > self.simplexValue[0]):
         p = math.exp(-(self.reflectionPtValue - self.simplexValue[0]) /
                      self.T)
         #print p
         if (p >= random.random()):
             self.ReplacePt(kk, reflect_matrix, reflect_value)
     else:
         self.ReplacePt(kk, reflect_matrix, reflect_value)
def sanity_test():
  (pi, e) = ext.test_boost_thread()
  assert approx_equal(pi, math.pi, eps=5), pi
  assert approx_equal(e, math.exp(1), eps=5), e
Example #10
0
def sanity_test():
    (pi, e) = ext.test_boost_thread()
    assert approx_equal(pi, math.pi, eps=5), pi
    assert approx_equal(e, math.exp(1), eps=5), e