3

I'm using kernel density estimation in order to compute probability density function for item occurrence.

Using this output, i want to find all the local minims and maxims. I'm interested in different methods for local-min/max extraction.

1 Answers1

1

One way to do it is to calculate first derivative (difference in discrete domain) and find where there is a change in the sign. That indicates the existence of a local minimum or maximum. For example:

from numpy import diff, sign, cos, pi, arange
import matplotlib.pyplot as plt
t = arange(0,2,0.01)
x = cos(2*pi*t)
# Find derivative of x
first_derivative = diff(x)

# Calc. sign difference
sign_diff = sign(first_derivative[1:]) - sign(first_derivative[:-1])

# Find local min and max
local_max_index = [i for i,k in enumerate(sign_diff) if k == -2]
local_min_index = [i for i,k in enumerate(sign_diff) if k == 2]

# plot results
plt.figure()
plt.plot(t,x)
plt.plot(t[local_max_index],x[local_max_index], 'ro')
plt.plot(t[local_min_index],x[local_min_index], 'ro')

enter image description here

Hope it helps!

Giannis Krilis
  • 501
  • 2
  • 7
  • This is only if you have a function right? What if you don't? – Carlos Mougan Jan 15 '20 at 10:39
  • Could you explain more what do you mean "when you have a funtion"? This works with descrete data points, you do not need to know the function that the data came from, if this is what you meant. – Giannis Krilis Jan 15 '20 at 15:13