Skip to content

Instantly share code, notes, and snippets.

@mtholder
Created April 21, 2018 13:40
Show Gist options
  • Select an option

  • Save mtholder/5abcf23ecaf8a48a17e70b30d55d0b78 to your computer and use it in GitHub Desktop.

Select an option

Save mtholder/5abcf23ecaf8a48a17e70b30d55d0b78 to your computer and use it in GitHub Desktop.
simple demo of using bracket and brent to optimize a single-variable function
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from scipy.optimize import bracket, brent
from scipy.stats import binom
from math import log, isnan
import sys
_inf = float('inf')
def main():
n = 100
for k in range(1, n):
def f(x, *args):
p = binom.pmf(k, n, x)
try:
assert not isnan(p)
neg_ln_l = -1 * log(p)
except:
neg_ln_l = _inf
return neg_ln_l
brac_res = bracket(f, .5)
brent_res = brent(f, brack=brac_res[:3])
print("{}\t{}".format(k / n, brent_res))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment