You can use inverse transform sampling on the spectral and permutation entropy functions.
Here is a pseudo code for the spectral entropy:
import numpy as np
import scipy.stats
import spectral_entropy
from numpy.random import random
from scipy import interpolate
import matplotlib.pyplot as plt
import cProfile
#Spectral entropy function
def f(x):
# does not need to be normalized
entropy = []
for i in range(0,len(x)):
spectrum = spectral_entropy.clean_spectrum(x[i])
entropy.append(scipy.stats.entropy(spectrum[:, 1]))
return entropy
def sample(g):
x = [[-2,2,5],[3,5,9],[1,-3,6]]
y = g(x) # probability density function, pdf
cdf_y = np.cumsum(y) # cumulative distribution function, cdf
cdf_y = cdf_y/cdf_y.max() # takes care of normalizing cdf to 1.0
inverse_cdf = interpolate.interp1d(cdf_y,x) # this is a function
return inverse_cdf
def return_samples(N=1e6):
# let's generate some samples according to the chosen pdf, f(x)
uniform_samples = random(int(N))
required_samples = sample(f)(uniform_samples)
return required_samples
cProfile.run('return_samples()')
## plot
x = np.linspace(-3,3,1e4)
fig,ax = plt.subplots()
ax.set_xlabel('x')
ax.set_ylabel('probability density')
ax.plot(x,f(x)/np.sum(f(x)*(x[1]-x[0])) )
ax.hist(return_samples(1e6),bins='auto',normed=True,range=(x.min(),x.max()))
plt.show()
Please let me know if it works.
Source: https://gist.github.com/amarvutha/c2a3ea9d42d238551c694480019a6ce1