Skip to content

Instantly share code, notes, and snippets.

@shane5ul
Created July 1, 2015 03:39
Show Gist options
  • Save shane5ul/f89549834f0c3f71c6c7 to your computer and use it in GitHub Desktop.
Save shane5ul/f89549834f0c3f71c6c7 to your computer and use it in GitHub Desktop.
These are helper functions for this blog post.
def integrand(r, f, rmin, rmax, mu, sigma):
"""defines the part inside the integral sign"""
from scipy.stats import truncnorm
a, b = (rmin - mu)/sigma, (rmax - mu) /sigma
rv = truncnorm(a, b, loc=mu, scale=sigma)
return rv.pdf(r) * np.log(1 + f*(r-1))
def logS(f, rmin, rmax, mu, sigma):
"""numerically solves the integral"""
from scipy.integrate import quad
lnS, err = quad(integrand, rmin, rmax, (f, rmin, rmax, mu, sigma))
return lnS
def neglogS(f, rmin, rmax, mu, sigma):
"""scipy has a minimizer, not maximizer"""
return -logS(f, rmin, rmax, mu, sigma)
def optimalAlloc(rmin, rmax, mu, sigma):
"""maximize logS = minimize -logS"""
from scipy.optimize import fminbound
fst = fminbound(neglogS, 0.0001, 0.9999, (rmin, rmax, mu, sigma))
return fst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment