In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [3]:
import random

In [37]:
def multiply(a, b):
    return a * b

def add(a, b):
    return a + b

def circut(a, b, c):
    d = add(a, b)
    return multiply(d, c)

In [27]:
def random_max(fn, a, b, niter=100):
    tweak_amount = 0.01
    best_out = None
    best_a = a
    best_b = b
    values = []
    for i in xrange(niter):
        na = a + tweak_amount + (random.random() * 2 - 1)
        nb = b + tweak_amount + (random.random() * 2 - 1)
        out = fn(na, nb)
        values.append(out)
        if best_out is None or out > best_out:
            best_out = out
            best_a = na
            best_b = nb
    return values, best_out, best_a, best_b

In [31]:
values, best, a, b = random_max(multiply, -2, 3)
plot(values)


Out[31]:
[<matplotlib.lines.Line2D at 0x1062192d0>]

In [32]:
def agradient_max(fn, a, b, niter=100):
    tweak_amount = 0.01
    best_out = None
    best_a = a
    best_b = b
    values = []
    for i in xrange(niter):
        ga = b
        gb = a
        a += tweak_amount * ga
        b += tweak_amount * gb
        out = fn(a, b)
        values.append(out)
        if best_out is None or out > best_out:
            best_out = out
            best_a = a
            best_b = b
    return values, best_out, best_a, best_b

In [34]:
values, best, a, b = agradient_max(multiply, -2, 3)
plot(values)


Out[34]:
[<matplotlib.lines.Line2D at 0x106352e50>]

In [39]:
f = circut(-2, 5, -4)
print f


-12

In [ ]: