1

I wonder, if someone could please check/help me with this simple code:

import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
    var = -0.1
    shift = 10
    return np.exp(var*(x - 100)) / (1 + np.exp(var*(x - 100) ))
    # return 1 / (1 + np.exp(-x))

def derivative(x, step):
    return -5.0 * (sigmoid(x+step) - sigmoid(x)) / step

x = np.linspace(0, 200, 1000)

y1 = sigmoid(x)
y2 = derivative(x, 0.0000000000001)

plt.plot(x, y1, label='reverse sigmoid')
plt.plot(x, y2, label='derivative')
plt.legend(loc='upper right')
plt.show()

It is my basic attempt to plot the reverse sigmoid and its derivative inspired by other posts. The results look like so:

enter image description here

The derivative looks odd and ideally I want to inflate the height of its peak. Any help would be very much appreciated. Thanks!

cs0815
  • 1,294
  • 18
  • 30
  • 2
    this doesn't look wrong – Aksakal Nov 23 '21 at 16:19
  • 2
    A quick visual check suggests that near the middle, the blue curve is dropping a distance of $0.5$ as it descends from a height of $0.75$ (near a value of $95$ or so--it's hard to read precisely) to a height of $0.25$ (near a value of $105$ or so). That would be an average slope of $-0.5/(105-95) = -0.05$ over that distance, quite in line with what the orange curve is depicting--*except the sign is different.* Of course, to inflate the height of the orange curve's peak all you have to do is multiply the function by a constant greater than $1.$ Whether that is meaningful is questionable. – whuber Nov 23 '21 at 20:26
  • thanks all. I overlooked the step=0.0000000000001 in the code, which made the derivative curve look jittery (if there is such a word). – cs0815 Nov 23 '21 at 20:30

1 Answers1

2

You can compute the derivative of a sigmoid in closed form. If you have the function $$ f(x) = \dfrac{1}{1 + \exp{(-\beta(x-\mu))}} $$ (where in your case $\mu=100$ and $\beta = -0.1$ then its derivative is

$$f'(x) = \dfrac{-\beta\exp{(-\beta(x-\mu))}}{(1 + \exp{(-\beta(x-\mu))})^2}$$ This is found by simple application of the quotient rule

I'm not sure what you mean by "the derivative looks odd" (its actually quite "even", haha that is a math joke). The derivaitve is flat and close to zero where $f$ is not changing very much and it peaks at the steepest part of $f$ (that is when $x=\mu$).

bdeonovic
  • 8,507
  • 1
  • 24
  • 49