Skip to content

Commit

Permalink
add a misc utils file for common math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ddbourgin committed Jun 21, 2020
1 parent 0bd908a commit 8b3f72e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions numpy_ml/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Utilities module"""

from . import testing
from . import data_structures
from . import distance_metrics
from . import kernels
from . import windows
from . import graphs
from . import misc
24 changes: 24 additions & 0 deletions numpy_ml/utils/misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Miscellaneous utility functions"""
import numpy as np


def logsumexp(log_probs, axis=None):
"""
Redefine scipy.special.logsumexp
see: http://bayesjumping.net/log-sum-exp-trick/
"""
_max = np.max(log_probs)
ds = log_probs - _max
exp_sum = np.exp(ds).sum(axis=axis)
return _max + np.log(exp_sum)


def log_gaussian_pdf(x_i, mu, sigma):
"""Compute log N(x_i | mu, sigma)"""
n = len(mu)
a = n * np.log(2 * np.pi)
_, b = np.linalg.slogdet(sigma)

y = np.linalg.solve(sigma, x_i - mu)
c = np.dot(x_i - mu, y)
return -0.5 * (a + b + c)

0 comments on commit 8b3f72e

Please sign in to comment.