Skip to content

Instantly share code, notes, and snippets.

@austensatterlee
Last active March 22, 2017 21:55
Show Gist options
  • Save austensatterlee/7e207ae28cddfeaaf391 to your computer and use it in GitHub Desktop.
Save austensatterlee/7e207ae28cddfeaaf391 to your computer and use it in GitHub Desktop.
Neat-o Mathematics
import numpy as np
# Example kernels
def uniform_kernel(x):
norm_coeff = .5
return .5*np.bitwise_and(-1<x,1>x)
def normal_kernel(x):
norm_coeff = (2*np.pi)**-.5
return .5*np.exp(-.5*x**2)
def KDE(input_samples,kernel,bandwidth=1.0):
"""
Takes in an array of samples, a kernel func (e.g. unigorm_kernel,normal_kernel),
and an optional "bandwidth" smoothing parameter
"""
def f(x):
return 1./(bandwidth*len(input_samples))*kernel(1./bandwidth*(x-input_samples)).sum()
return np.vectorize(f)
###
# Example:
###
# import matplotlib.pyplot as plt
# samples = np.hstack([np.random.normal(0,1,100),np.random.normal(3,.5,100)])
# domain = np.linspace(-5,10,1500)
# f,ax = plt.subplots(2)
# ax[0].hist(samples,normed=True)
# ax[0].plot(domain,KDE(samples,normal_kernel)(domain))
# ax[0].set_title('normal kernel')
# ax[1].hist(samples,normed=True)
# ax[1].plot(domain,KDE(samples,uniform_kernel)(domain))
# ax[1].set_title('uniform kernel')
def logmap(n,x0,r=4):
_logmapcache={}
def _logmap(n):
if n==0:
return x0
xprev=_logmapcache[n] if n in _logmapcache else _logmap(n-1)
return r*xprev*(1-xprev)
return _logmap(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment