Calmcode - diseases: interact

Interact

1 2 3 4 5 6 7

You can introduce a slider in jupyter to interact with the chart. Here's the code you'd need.

from ipywidgets import interact

import numpy as np
import matplotlib.pylab as plt

@interact(p=(0.001, 0.2, 0.001))
def f(p):
    g = np.linspace(1, 200, 500)
    prob_group_safe = (1-p)**g
    plt.figure(figsize=(12, 3))
    plt.plot(g, g*prob_group_safe, label=f"p={np.round(p, 3)}")
    plt.xlabel("number of people in a group")
    plt.ylabel("E(groups off the hook)")
    plt.legend();

Extra

You can also run this snippet of code if you'd like to compare two different curves based on different probabilities.

from ipywidgets import interact

import numpy as np
import matplotlib.pylab as plt

@interact(p=(0.001, 0.2, 0.001))
def g(p):
    g = np.linspace(1, 200, 500)
    plt.figure(figsize=(12, 3))
    plt.plot(g, g*(1-0.02)**g, label="p=0.02")
    plt.plot(g, g*(1-p)**g, label=f"p={np.round(p, 3)}")
    plt.xlabel("number of people in a group")
    plt.legend();

Bonus

This is a tricky question. But the code below shows charts that are a bit different. Can you guess what this chart might represent?

from ipywidgets import interact

import numpy as np
import matplotlib.pylab as plt

@interact(p=(0.001, 0.2, 0.001))
def f(p):
    g = np.linspace(1, 200, 500)
    plt.figure(figsize=(12, 3))
    plt.plot(g, g*0.02*(1-0.02)**g, label="p=0.02")
    plt.plot(g, g*p*(1-p)**g, label=f"p={np.round(p, 3)}")
    plt.xlabel("number of people in a group")
    plt.legend();