Skip to content

Instantly share code, notes, and snippets.

@huangziwei
Last active August 23, 2017 14:03
Show Gist options
  • Save huangziwei/3803424f3a7ee732162669edca6c1aca to your computer and use it in GitHub Desktop.
Save huangziwei/3803424f3a7ee732162669edca6c1aca to your computer and use it in GitHub Desktop.
porting some some R functions to python
def lfactorial(n):
from scipy.special import gammaln
return gammaln(n+1)
def lchoose(n, k):
from scipy.special import gammaln
return (gammaln(n+1) - gammaln(n-k+1) - gammaln(k+1))
def sample(x, size, replace=False, prob=None):
import numpy as np
if type(x) == int:
a = np.arange(1, x+1)
else:
a = x
return np.random.choice(a, size, replace, prob)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment