0

I want to plot the points of intersection in the following graphs.

Forming concave envelope to sigmoidal functions

The idea is to highlight the point of intersection of the line and sigmoid function and show that we have to reduce the value of upper bound such that the point of intersection and upper bound coincide. This gives the tightest concave envelope.

Therefore, I want to highlight the point of intersection. I looked up Intersection of two graphs in Python, find the x value, but the solutions here require same length of f and g so they can compute np.argwhere(np.diff(np.sign(f - g))).flatten() I generate a line by simply joining end points and sigmoid by generating many points. My code is appended below.

import matplotlib.pyplot as plt
import numpy as np

a1 = 5
a2 = 2.5
a3 = 1.75
x = np.linspace(-5, 5, 100)
z = 1 / (1 + np.exp(-x))
x1 = [-5, a1]
y1 = [1 / (1 + np.exp(5)), 1 / (1 + np.exp(-a1))]

x2 = [-5, a2]
y2 = [1 / (1 + np.exp(5)), 1 / (1 + np.exp(-a2))]

x3 = [-5, a3]
y3 = [1 / (1 + np.exp(5)), 1 / (1 + np.exp(-a3))]

plt.figure(figsize=(15,5))
plt.subplot(1,3,1)
plt.plot(x, z, '-b', label='Sigmoid function')
plt.plot(x1, y1, 'o--g', label='Concave envelope', alpha= 0.75)
plt.xlabel("x")
plt.ylabel("Sigmoid(x)")
plt.legend()
plt.grid()

plt.subplot(1,3,2)
plt.plot(x, z, '-b', label='Sigmoid function')
plt.plot(x2, y2, 'o--g', label='Concave envelope', alpha= 0.75)
plt.xlabel("x")
plt.grid()

plt.subplot(1,3,3)
plt.plot(x, z, '-b', label='Sigmoid function')
plt.plot(x3, y3, 'o--g', label='Concave envelope', alpha= 0.75)
plt.xlabel("x")
plt.grid()


#plt.savefig('Sigmoid_ex.png', transparent=True, dpi = 144)
plt.show()

Can anyone suggest a workaround this?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
madhafakha
  • 23
  • 4
  • Is this of any help? https://stackoverflow.com/questions/69234771/finding-intersection-of-two-graphs-with-different-numpy-sizes – fdireito May 18 '23 at 08:01
  • @fdireito, Thanks for the post, but this does not help much because it approximates the function in between, although in my case, since it is a line, it would help. I turned to math for help. – madhafakha May 18 '23 at 08:34

1 Answers1

0

I could not find the solution quickly so I turned to math. Basically solved the equation of line computed from points of sigmoid and solved it with sigmoid numerically.

Numerically solved line with sigmoid on WolframAlpha

Might not be a programming hack, but it works for me here. If anyone finds a better solution that does this, do post it.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
madhafakha
  • 23
  • 4
  • if you have a solution, post the code and not the pictures of code as users might be inclined to downvote. – D.L May 18 '23 at 15:10
  • @D.L There is no code, I just solved the underlying math equation. Basically did it manually and added a point in array : ) – madhafakha May 19 '23 at 16:36