original_docstring:  Approximate the distribution of both classes
  
              :param deci: List of decision made for each sample.
              :type  deci: list of float
              :param label: List of labels from the previous samples.
              :type  label: list of bool
              :param prior1: Number of samples of class 1
              :type  prior1: int
              :param prior0: Number of samples of class 0
              :type  prior0: int
              :rtype: [float, float] - ([A, B] - parameters of sigmoid)
          
docstring: Approximate the distribution of both classes
docstring_tokens: Approximate the distribution of both classes
code: def approximate_AB_for_plat_prob(self, deci, label, prior1, prior0):
          """ Approximate the distribution of both classes
  
              :param deci: List of decision made for each sample.
              :type  deci: list of float
              :param label: List of labels from the previous samples.
              :type  label: list of bool
              :param prior1: Number of samples of class 1
              :type  prior1: int
              :param prior0: Number of samples of class 0
              :type  prior0: int
              :rtype: [float, float] - ([A, B] - parameters of sigmoid)
          """
          # Parameter setting
          maxiter = 100
          # Maximum number of iterations
          minstep = 1e-10
          # Minimum step taken in line search
          sigma = 1e-12
          # Set to any value > 0
          # Construct initial values:    target support in array t,
          #                              initial function value in fval
          hiTarget = (prior1 + 1.0) / (prior1 + 2.0)
          loTarget = 1 / (prior0 + 2.0)
          length = prior1 + prior0  # Total number of data
          t = numpy.zeros(length)
          for i in numpy.arange(length):
              if label[i] > 0:
                  t[i] = hiTarget
              else:
                  t[i] = loTarget
  
          A = 0.0
          B = numpy.log((prior0 + 1.0) / (prior1 + 1.0))
          fval = 0.0
          for i in numpy.arange(length):
              fApB = deci[i] * A + B
              if fApB >= 0:
                  fval += t[i] * fApB + numpy.log(1 + numpy.exp(-fApB))
              else:
                  fval += (t[i] - 1) * fApB + numpy.log(1 + numpy.exp(fApB))
  
          for it in numpy.arange(maxiter):
              # Update Gradient and Hessian (use H' = H + sigma 1)
              h11 = h22 = sigma
              h21 = g1 = g2 = 0.0
              for i in numpy.arange(length):
                  fApB = deci[i] * A + B
                  if fApB >= 0:
                      p = numpy.exp(-fApB) / (1.0 + numpy.exp(-fApB))
                      q = 1.0 / (1.0 + numpy.exp(-fApB))
                  else:
                      p = 1.0 / (1.0 + numpy.exp(fApB))
                      q = numpy.exp(fApB) / (1.0 + numpy.exp(fApB))
                  d2 = p * q
                  h11 += deci[i] * deci[i] * d2
                  h22 += d2
                  h21 += deci[i] * d2
                  d1 = t[i] - p
                  g1 += deci[i] * d1
                  g2 += d1
              if abs(g1) < 1e-5 and abs(g2) < 1e-5:  # Stopping criteria
                  break
              # Compute modified Newton directions
              det = h11 * h22 - h21 * h21
              dA = -(h22 * g1 - h21 * g2) / det
              dB = -(-h21 * g1 + h11 * g2) / det
              gd = g1 * dA + g2 * dB
              stepsize = 1
              while stepsize >= minstep:  # Line search
                  newA = A + stepsize * dA
                  newB = B + stepsize * dB
                  newf = 0.0
                  for i in numpy.arange(length):
                      fApB = deci[i] * newA + newB
                      if fApB >= 0:
                          newf += t[i] * fApB + numpy.log(1 + numpy.exp(-fApB))
                      else:
                          newf += (t[i] - 1) * fApB + \
                              numpy.log(1 + numpy.exp(fApB))
                  if newf < fval + 0.0001 * stepsize * gd:
                      A = newA
                      B = newB
                      fval = newf
                      break  # Sufficient decrease satisfied
                  else:
                      stepsize /= 2.0
              if stepsize < minstep:
                  self._log(
                      "Line search fails. A= " + str(A) + " B= " + str(B),
                      level=logging.WARNING)
                  break
          if it >= maxiter:
              self._log("Reaching maximal iterations", level=logging.WARNING)
  
          return [A, B]
code_tokens: def approximate_AB_for_plat_prob ( self , deci , label , prior1 , prior0 ) : maxiter = 100 minstep = 1e-10 sigma = 1e-12 hiTarget = ( prior1 + 1.0 ) / ( prior1 + 2.0 ) loTarget = 1 / ( prior0 + 2.0 ) length = prior1 + prior0 t = numpy . zeros ( length ) for i in numpy . arange ( length ) : if label [ i ] > 0 : t [ i ] = hiTarget else : t [ i ] = loTarget A = 0.0 B = numpy . log ( ( prior0 + 1.0 ) / ( prior1 + 1.0 ) ) fval = 0.0 for i in numpy . arange ( length ) : fApB = deci [ i ] * A + B if fApB >= 0 : fval += t [ i ] * fApB + numpy . log ( 1 + numpy . exp ( - fApB ) ) else : fval += ( t [ i ] - 1 ) * fApB + numpy . log ( 1 + numpy . exp ( fApB ) ) for it in numpy . arange ( maxiter ) : h11 = h22 = sigma h21 = g1 = g2 = 0.0 for i in numpy . arange ( length ) : fApB = deci [ i ] * A + B if fApB >= 0 : p = numpy . exp ( - fApB ) / ( 1.0 + numpy . exp ( - fApB ) ) q = 1.0 / ( 1.0 + numpy . exp ( - fApB ) ) else : p = 1.0 / ( 1.0 + numpy . exp ( fApB ) ) q = numpy . exp ( fApB ) / ( 1.0 + numpy . exp ( fApB ) ) d2 = p * q h11 += deci [ i ] * deci [ i ] * d2 h22 += d2 h21 += deci [ i ] * d2 d1 = t [ i ] - p g1 += deci [ i ] * d1 g2 += d1 if abs ( g1 ) < 1e-5 and abs ( g2 ) < 1e-5 : break det = h11 * h22 - h21 * h21 dA = - ( h22 * g1 - h21 * g2 ) / det dB = - ( - h21 * g1 + h11 * g2 ) / det gd = g1 * dA + g2 * dB stepsize = 1 while stepsize >= minstep : newA = A + stepsize * dA newB = B + stepsize * dB newf = 0.0 for i in numpy . arange ( length ) : fApB = deci [ i ] * newA + newB if fApB >= 0 : newf += t [ i ] * fApB + numpy . log ( 1 + numpy . exp ( - fApB ) ) else : newf += ( t [ i ] - 1 ) * fApB + numpy . log ( 1 + numpy . exp ( fApB ) ) if newf < fval + 0.0001 * stepsize * gd : A = newA B = newB fval = newf break else : stepsize /= 2.0 if stepsize < minstep : self . _log ( "Line search fails. A= " + str ( A ) + " B= " + str ( B ) , level = logging . WARNING ) break if it >= maxiter : self . _log ( "Reaching maximal iterations" , level = logging . WARNING ) return [ A , B ]
short_docstring: Approximate the distribution of both classes
short_docstring_tokens: Approximate the distribution of both classes
comment: """ Approximate the distribution of both classes
  
              :param deci: List of decision made for each sample.
              :type  deci: list of float
              :param label: List of labels from the previous samples.
              :type  label: list of bool
              :param prior1: Number of samples of class 1
              :type  prior1: int
              :param prior0: Number of samples of class 0
              :type  prior0: int
              :rtype: [float, float] - ([A, B] - parameters of sigmoid)
          """$SEP$# Parameter setting$SEP$# Maximum number of iterations$SEP$# Minimum step taken in line search$SEP$# Set to any value > 0$SEP$# Construct initial values:    target support in array t,$SEP$#                              initial function value in fval$SEP$# Total number of data$SEP$# Update Gradient and Hessian (use H' = H + sigma 1)$SEP$# Stopping criteria$SEP$# Compute modified Newton directions$SEP$# Line search$SEP$# Sufficient decrease satisfied