CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

Public worksheets for UCLA's Mathematics for Life Scientists course

Views: 10255

Note: To run any of the interactives in this worksheet, make sure you're signed in to your CoCalc account, and click “Open in CoCalc” at the top right of this window.

A “saturating” function

It increases at first, but only up to a maximum (saturation) level. f(X)=mXh+X f(X) = m \cdot \frac{X}{h + X}

Parameters:

  • m=m = the saturation level, the value that f(X)f(X) approaches as XX gets large (think mm for maximum)

  • h=h = the half-saturation point, the XX value at which f(X)=12mf(X) = \frac{1}{2}m


Example with m=6m = 6 and h=2h = 2:

# An interactive allowing you to manipulate the values of m and h: xmax=15 @interact def saturating( m=slider(1, 8, 1, default=1, label="$m$"), h=slider(1, 10, 1, default=1, label="$h$"), show_m=checkbox(False, label="Show $m$"), show_h=checkbox(False, label="Show $h$"), ): f(X) = m * X / (h + X) label = r"%s \cdot \frac{X}{%s + X}" % (m, h) p = plot(f(X), (X, 0, xmax), thickness=2) if show_m: p += line(((0, m), (xmax, m)), color="red", linestyle="dashed") p += text(r"$m$", (0.2, m + 0.3), color="red", fontsize="larger", horizontal_alignment="left") if show_h: p += line(((0, m/2), (h, m/2), (h, 0)), color="green", linestyle="dotted") p += text(r"$X = h$", (h + 0.1, 0.4), color="green", fontsize="larger", horizontal_alignment="left") p += text(r"$\frac{m}{2}$", (0.2, m/2 + 0.5), color="green", fontsize="larger", horizontal_alignment="left") p.show(ymin=0, ymax=10, axes_labels=("$X$", "$f(X) = {}$".format(label)))
Interact: please open in CoCalc

Variation on the above: a sigmoid function

It has an “S” shape (sort of), which is where the term “sigmoid” comes from.

It also increases only up to a maximum (saturation) level, but it starts out flat (horizontal) for low XX values, then curves upward, before leveling off. The exponent nn controls the steepness of the transition from low values to high values. f(X)=mXnhn+Xn f(X) = m \cdot \frac{X^n}{h^n + X^n} Note: Often, we just use h=1h = 1, so that the function simplifies to f(X)=mXn1+Xn f(X) = m \cdot \frac{X^n}{1 + X^n}

Parameters:

  • m=m = the saturation level, the value that f(X)f(X) approaches as XX gets large (think mm for maximum)

  • h=h = the half-saturation point, the XX value at which f(X)=12mf(X) = \frac{1}{2}m

  • n=n = a parameter that controls the steepness


Example with m=4m = 4 and n=15n = 15 (and h=1h = 1):

# An interactive allowing you to manipulate the values of n, m and h: xmax=15 @interact def sigmoid( n=slider(2, 20, 1, default=2, label="$n$"), m=slider(1, 8, 1, default=1, label="$m$"), h=slider(1, 10, 1, default=1, label="$h$"), show_m=checkbox(False, label="Show $m$"), show_h=checkbox(False, label="Show $h$"), ): f(X) = m * X^n / (h^n + X^n) label = r"%s \cdot \frac{X^{%s}}{%s + X^{%s}}" % (m, n, h if n == 1 or h == 1 else "%s^{%s}" % (h, n), n) p = plot(f(X), (X, 0, xmax), thickness=2) if show_m: p += line(((0, m), (xmax, m)), color="red", linestyle="dashed") p += text(r"$m$", (0.2, m + 0.3), color="red", fontsize="larger", horizontal_alignment="left") if show_h: p += line(((0, m/2), (h, m/2), (h, 0)), color="green", linestyle="dotted") p += text(r"$X = h$", (h + 0.1, 0.4), color="green", fontsize="larger", horizontal_alignment="left") p += text(r"$\frac{m}{2}$", (0.2, m/2 + 0.5), color="green", fontsize="larger", horizontal_alignment="left") p.show(ymin=0, ymax=10, axes_labels=("$X$", "$f(X) = {}$".format(label)))
Interact: please open in CoCalc
# A simpler version of the above interactive, with m = h = 1, allowing you to play with the steepness (n): xmax=3 @interact def sigmoid_simple( n=slider(2, 40, 1, default=2, label="$n$"), show_m=checkbox(False, label="Show $m$"), ): m = 1 f(X) = m * X^n / (1 + X^n) label = r"\frac{X^{%s}}{1 + X^{%s}}" % (n, n) p = plot(f(X), (X, 0, xmax), thickness=2) if show_m: p += line(((0, m), (xmax, m)), color="red", linestyle="dashed") p += text(r"$m$", (0.05, m + 0.05), color="red", fontsize="larger", horizontal_alignment="left") p.show(ymin=0, ymax=1.5, axes_labels=("$X$", "$f(X) = {}$".format(label)))
Interact: please open in CoCalc

A “de-saturating” function

It decreases, from its initial (maximum) level, down to near zero. f(X)=mhh+X f(X) = m \cdot \frac{h}{h + X}

Parameters:

  • m=m = the initial (maximum) level, the value of f(X)f(X) at X=0X = 0

  • h=h = the half-saturation point, the XX value at which f(X)=12mf(X) = \frac{1}{2}m

# An interactive allowing you to manipulate the values of m and h: xmax=15 @interact def desaturating( m=slider(1, 8, 1, default=1, label="$m$"), h=slider(1, 10, 1, default=1, label="$h$"), show_h=checkbox(False, label="Show $h$"), ): f(X) = m * h / (h + X) label = r"%s \cdot \frac{%s}{%s + X}" % (m, h, h) p = plot(f(X), (X, 0, xmax), thickness=2) p += text(r"$m$", (0.1, m + 0.3), color="red", fontsize="larger", horizontal_alignment="left") if show_h: p += line(((0, m/2), (h, m/2), (h, 0)), color="green", linestyle="dotted") p += text(r"$X = h$", (h + 0.05, 0.4), color="green", fontsize="larger", horizontal_alignment="left") p += text(r"$\frac{m}{2}$", (0.1, m/2 + 0.5), color="green", fontsize="larger", horizontal_alignment="left") p.show(ymin=0, ymax=10, axes_labels=("$X$", "$f(X) = {}$".format(label)))
Interact: please open in CoCalc

A decreasing sigmoid function

This one has a backwards “S” shape. Like the de-saturating function, it decreases from its initial (maximum) level down to near zero, but it starts out flat (horizontal) for low XX values, then curves downward, before leveling off. The exponent nn controls the steepness of the transition from low values to high values. f(X)=mhnhn+Xn f(X) = m \cdot \frac{h^n}{h^n + X^n} Note: Often, we just use h=1h = 1, so that the function simplifies to f(X)=m11+Xn f(X) = m \cdot \frac{1}{1 + X^n}

Parameters:

  • m=m = the initial (maximum) level, the value of f(X)f(X) at X=0X = 0

  • h=h = the half-saturation point, the XX value at which f(X)=12mf(X) = \frac{1}{2}m

  • n=n = a parameter that controls the steepness

# An interactive allowing you to manipulate the values of n, m and h: xmax=15 @interact def decreasing_sigmoid( n=slider(2, 20, 1, default=2, label="$n$"), m=slider(1, 8, 1, default=1, label="$m$"), h=slider(1, 10, 1, default=1, label="$h$"), show_h=checkbox(False, label="Show $h$"), ): f(X) = m * h^n / (h^n + X^n) h_tothe_n = h if n == 1 or h == 1 else "%s^{%s}" % (h, n) label = r"%s \cdot \frac{%s}{%s + X^{%s}}" % (m, h_tothe_n, h_tothe_n, n) p = plot(f(X), (X, 0, xmax), thickness=2) p += text(r"$m$", (0.2, m + 0.4), color="red", fontsize="larger", horizontal_alignment="left") if show_h: p += line(((0, m/2), (h, m/2), (h, 0)), color="green", linestyle="dotted") p += text(r"$X = h$", (h - 0.1, 0.4), color="green", fontsize="larger", horizontal_alignment="right") p += text(r"$\frac{m}{2}$", (0.2, m/2 + 0.5), color="green", fontsize="larger", horizontal_alignment="left") p.show(ymin=0, ymax=10, axes_labels=("$X$", "$f(X) = {}$".format(label)))
Interact: please open in CoCalc
# A simpler version of the above interactive, with m = h = 1, allowing you to play with the steepness (n): xmax=3 @interact def decreasing_sigmoid_simple( n=slider(2, 40, 1, default=2, label="$n$"), ): m = 1 f(X) = m * 1 / (1 + X^n) label = r"\frac{1}{1 + X^{%s}}" % (n, ) p = plot(f(X), (X, 0, xmax), thickness=2) p.show(ymin=0, ymax=1.5, axes_labels=("$X$", "$f(X) = {}$".format(label)))
Interact: please open in CoCalc