Skip to content

Instantly share code, notes, and snippets.

@CountChu
Last active April 23, 2025 04:03
Show Gist options
  • Save CountChu/097b8c1193ac1a31f9b76d6fadcdac63 to your computer and use it in GitHub Desktop.
Save CountChu/097b8c1193ac1a31f9b76d6fadcdac63 to your computer and use it in GitHub Desktop.
#
# Draw the Hoeffding's Inequality with different epsilons, 0.1, 0.3, 0.5, and 1.
#
from scipy import signal
import numpy as np
import math
def hoeffding (x, epsilon):
y = 2.0 * math.exp (-2*epsilon*epsilon*x)
return y
#x = np.arange (0, 100, 0.01)
x = np.arange (0, 10, 0.01)
y1 = [hoeffding (x, 0.1) for x in x]
y2 = [hoeffding (x, 0.3) for x in x]
y3 = [hoeffding (x, 0.5) for x in x]
y4 = [hoeffding (x, 1) for x in x]
import matplotlib.pyplot as plt
plt.xlabel ('N')
plt.ylabel ('P')
latex1 = r'P\leq2e^{\left( -2\varepsilon ^{2}N\right)}'
plt.title (r"Hoeffding's Inequality: $ %s $" % latex1)
latext2 = r'\varepsilon'
plt.plot (x, y1, label=r'$%s$ = 0.1' % latext2)
plt.plot (x, y2, label='$%s$ = 0.3' % latext2)
plt.plot (x, y3, label='$%s$ = 0.5' % latext2)
plt.plot (x, y4, label='$%s$ = 1' % latext2)
plt.legend ()
plt.show ()
@tonussi
Copy link

tonussi commented Apr 20, 2025

Thanks

For Python 3.12

numpy
matplotlib
scipy
PyQt6

and

sudo apt install libxcb-cursor0

Further

Vapnik Chervonenskis

import matplotlib.pyplot as plt
import numpy as np

def vapnik_chervonenskis(x, delta, h):
    y = np.sqrt((8.0 / x) * np.log((4.0 * h * 2 * x) / delta))
    return y

# x = np.arange (0, 100, 0.01)
x = np.arange(0, 10, 0.01)

h = 1

y1 = [vapnik_chervonenskis(x, 0.1, h) for x in x]
y2 = [vapnik_chervonenskis(x, 0.3, h) for x in x]
y3 = [vapnik_chervonenskis(x, 0.5, h) for x in x]
y4 = [vapnik_chervonenskis(x, 1, h) for x in x]

plt.xlabel("N")
plt.ylabel("P")
latex1 = r"P \leq \sqrt { \frac{8}{N} \ln { \dfrac{4mh(2N)}{\delta} } }"
plt.title(r"Vapnik-Chervonenkis's Inequality: $ {latex1} $ (h={h})".format(latex1=latex1, h=h))

latext2 = r"\delta"
plt.plot(x, y1, label=r"$%s$ = 0.1" % latext2)
plt.plot(x, y2, label="$%s$ = 0.3" % latext2)
plt.plot(x, y3, label="$%s$ = 0.5" % latext2)
plt.plot(x, y4, label="$%s$ = 1" % latext2)

plt.legend()
plt.show()

@CountChu
Copy link
Author

Thank you for your update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment