2

Issue:

Prevent scipy brute optimization routine (or any opt routine for that matter) from evaluating a function with negative integers. The overall function I am trying to optimize uses a gamma function which is undefined for negative integers.

Background:

I am utilizing various optimization routines from scipy (including brute and fmin) to optimize a function I wrote which contains a gamma function.

import scipy.optimize as sopt
param = [a, b, c]
param = sopt.brute(function_I_wrote, ((-2.0, 2.0, 0.25), (0.00, 0.25, 0.05),
                (0, 4, 0.2)),finish=None)

The gamma function utilizes the first parameter and the range of values you see (-2.0, 2.0, 0.25). The problem I am running into is whenever it evaluates the function at a negative integer the routine is killed since it is undefined for gamma(neg.integer).

part of function where the issue is:

           math.gamma(-a)

To trick it I shifted everything 0.1 so that the increments are (-2.1, 1.9, 0.25) so presumably now it will not land on a negative integer (or zero).

Is there a more formal way to specify that the optimization routine must avoid negative integers for that first parameter? I want it to evaluate at numbers between ~-2 and ~2 but not at negative integers.

ChrisP
  • 5,812
  • 1
  • 33
  • 36
Doug
  • 209
  • 2
  • 9
  • 19
  • 2
    Wrap the gamma function inside a function that returns a large (positive or negative) number when x is close to a nonpositive integer. – Erwin Kalvelagen Apr 26 '16 at 10:37
  • You should be able to return `np.inf` and the brute comparisons will still very likely work. – Andi May 27 '16 at 13:20

0 Answers0