Skip to content

Instantly share code, notes, and snippets.

@kgori
Last active October 11, 2020 11:54
Show Gist options
  • Save kgori/95f604131ce92ec15f4338635a86dfb9 to your computer and use it in GitHub Desktop.
Save kgori/95f604131ce92ec15f4338635a86dfb9 to your computer and use it in GitHub Desktop.
How to discretize a gamma or lognormal distribution, for modelling rates over sites in a phylogenetic model
import scipy
import scipy.stats as ss
import numpy as np
def discretize(alpha, ncat, dist=ss.gamma):
if dist == ss.gamma:
dist = dist(alpha, scale=1 / alpha)
elif dist == ss.lognorm:
dist = dist(s=alpha, scale=np.exp(0.5 * alpha**2))
quantiles = dist.ppf(np.arange(0, ncat) / ncat)
rates = np.zeros(ncat, dtype=np.double)
for i in range(ncat-1):
rates[i] = ncat * scipy.integrate.quad(lambda x: x * dist.pdf(x),
quantiles[i], quantiles[i+1])[0]
rates[ncat-1] = ncat * scipy.integrate.quad(lambda x: x * dist.pdf(x),
quantiles[ncat-1], np.inf)[0]
return rates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment