-
Notifications
You must be signed in to change notification settings - Fork 37
Feature/learn calculus #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d7ddbcd
5874bcd
4a39a0e
25ef2b2
f22ce18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Learn Calculus for Machine Learning | ||
|
||
π§ This collection is a work in progress. Please help us add notebooks! | ||
|
||
This collection of marimo notebooks is designed to teach you the basics of calculus for machine learning. | ||
|
||
Help us build this course! βοΈ | ||
|
||
We're seeking contributors to help us build these notebooks. Every contributor will be acknowledged as an author in this README and in their contributed notebooks. Head over to the [tracking issue](https://github.com/marimo-team/learn/issues/58) to sign up for a planned notebook or propose your own. | ||
|
||
Running notebooks. To run a notebook locally, use | ||
|
||
```python | ||
uvx marimo edit <file_url> | ||
``` | ||
|
||
You can also open notebooks in our online playground by appending marimo.app/ to a notebook's URL. | ||
|
||
Thanks to all our notebook authors! | ||
|
||
[ghimiresunil](https://github.com/ghimiresunil) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Look at how the window functions notebook walks through examples - it's much more conversational and builds understanding progressively. Consider a similar approach:
Do you need any guidance on how to structure the rest of the notebook (after Differentiability)? Happy to chat about what sections would be most valuable to include. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,277 @@ | ||||||
# /// script | ||||||
# requires-python = ">=3.12" | ||||||
# dependencies = [ | ||||||
# "marimo==0.13.15", | ||||||
# "numpy"="2.2.6", | ||||||
# "plotly"="0.1.2", | ||||||
# "sympy"="1.14.0", | ||||||
# ] | ||||||
# /// | ||||||
|
||||||
import marimo | ||||||
|
||||||
__generated_with = "0.13.15" | ||||||
app = marimo.App(width="medium") | ||||||
|
||||||
|
||||||
@app.cell(hide_code=True) | ||||||
def _(mo): | ||||||
mo.md( | ||||||
r""" | ||||||
# Derivatives and Critical Values | ||||||
|
||||||
_By [ghimiresunil](https://github.com/ghimiresunil) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Welcome to this tutorial on **Derivatives and Critical Values**βa key turning point in our exploration of calculus. In the previous module, we learned how to describe the *rate of change* of a function. In this notebook, we take that understanding further to analyze how functions behave, where they reach their highest or lowest values, and how we can use calculus to find those points. | ||||||
|
||||||
--- | ||||||
|
||||||
## Quick Recap: Rates of change | ||||||
|
||||||
In the **Rates of Change** notebook, we explored two foundational ideas: | ||||||
|
||||||
- **Average Rate of Change**: | ||||||
This is the change in a functionβs output relative to the change in input over an interval. It gives us a *global* view of how a function behaves between two points. | ||||||
|
||||||
\[ \text{Average Rate of Change} = \frac{f(b) - f(a)}{b - a} \] | ||||||
|
||||||
- **Instantaneous Rate of Change**: | ||||||
As the interval becomes infinitesimally small, the average rate of change approaches the **derivative**βa *local* measure of how the function is changing at a specific point. | ||||||
|
||||||
|
||||||
\[f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \] | ||||||
|
||||||
This transition from global to local change is where derivatives begin to reveal the fine structure of functions, helping us uncover turning points, slopes, and trends. | ||||||
|
||||||
--- | ||||||
|
||||||
## What You'll Learn in This Notebook | ||||||
|
||||||
In this notebook, we will focus on: | ||||||
|
||||||
- What *critical values* are and how to find them | ||||||
- How to use the **first derivative** to identify increasing/decreasing behavior | ||||||
- How to apply the **second derivative** to determine concavity and locate maxima or minima | ||||||
- How these tools help us analyze and sketch functions | ||||||
- How this foundation prepares us for solving real-world **optimization problems** | ||||||
|
||||||
By the end of this module, youβll be able to use derivatives not just to describe change, but to *predict, optimize, and explain* key behaviors in a variety of systemsβfrom economics to physics to machine learning. | ||||||
|
||||||
Letβs get started... | ||||||
Comment on lines
+25
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intro section is quite verbose and reads a bit formal/AI-generated (especially with the |
||||||
""" | ||||||
) | ||||||
return | ||||||
|
||||||
|
||||||
@app.cell(hide_code=True) | ||||||
def _(mo): | ||||||
mo.md( | ||||||
r""" | ||||||
## Rules of Differentiation | ||||||
|
||||||
Learn the essential rules for differentiating mathematical functions, from basic polynomials to complex compositions. Master the power, product, quotient, and chain rules, and understand how to compute higher-order derivatives used in analysis and modeling. | ||||||
|
||||||
| **Rule** | **What It Is** | **Where Itβs Used** | **How To Use** | **Practice Questions** | | ||||||
|----------|----------------|---------------------|----------------|-------------------------| | ||||||
| **Addition & Subtraction Rule** | The derivative of a sum or difference is the sum or difference of the derivatives | When multiple terms are combined in expressions | \( \frac{d}{dx} [f(x) \pm g(x)] = f'(x) \pm g'(x) \) | 1. \( \frac{d}{dx}(x^3 + \sin x) \) <br> 2. \( \frac{d}{dx}(e^x - x^2) \) | | ||||||
| **Power Rule** | A rule for differentiating functions of the form \( x^n \) | Polynomial functions, roots, rational powers | \( \frac{d}{dx} x^n = n x^{n-1} \) | 1. \( \frac{d}{dx}(x^4) \) <br> 2. \( \frac{d}{dx}(x^{-2}) \) <br> 3. \( \frac{d}{dx}(\sqrt{x}) \) | | ||||||
| **Product Rule** | Differentiates the product of two functions | Used in physics and economics: force Γ distance, price Γ quantity | \( \frac{d}{dx}(u \cdot v) = u'v + uv' \) | 1. \( \frac{d}{dx}(x^2 \sin x) \) <br> 2. \( \frac{d}{dx}(e^x \ln x) \) | | ||||||
| **Quotient Rule** | For differentiating one function divided by another | Rational expressions, modeling change ratios | \( \frac{d}{dx} \left( \frac{u}{v} \right) = \frac{u'v - uv'}{v^2} \) | 1. \( \frac{d}{dx} \left( \frac{x^2}{x+1} \right) \) <br> 2. \( \frac{d}{dx} \left( \frac{\sin x}{x^2} \right) \) | | ||||||
| **Chain Rule** | Used for differentiating composite (nested) functions | Growth/decay models, trig and log functions | \( \frac{d}{dx}(g(h(x))) = g'(h(x)) \cdot h'(x) \) | 1. \( \frac{d}{dx}(\sin(x^2)) \) <br> 2. \( \frac{d}{dx}(e^{3x^2 + 2}) \) <br> 3. \( \frac{d}{dx}(\ln(\sqrt{x})) \) | | ||||||
| **Higher-Order Derivatives** | Derivatives of derivatives, e.g., \( f''(x), f^{(3)}(x) \) | Physics: velocity, acceleration, concavity in graphs | Compute derivatives multiple times: \( f''(x) = \frac{d^2f}{dx^2} \) | 1. \( f(x) = x^3 - 3x + 2 \Rightarrow f''(x) \)? <br> 2. \( \frac{d^3}{dx^3}(\sin x) \)? | | ||||||
""" | ||||||
) | ||||||
return | ||||||
Comment on lines
+66
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel that the table is quite comprehensive/exhaustive. Consider breaking this into smaller sections or making it more interactive. |
||||||
|
||||||
|
||||||
@app.cell(hide_code=True) | ||||||
def _(mo): | ||||||
mo.md(r"""## Import Necessary Libraries""") | ||||||
return | ||||||
|
||||||
|
||||||
@app.cell | ||||||
def _(): | ||||||
import marimo as mo | ||||||
import numpy as np | ||||||
import plotly.graph_objects as go | ||||||
import sympy as sp | ||||||
|
||||||
return go, mo, np, sp | ||||||
|
||||||
|
||||||
@app.cell | ||||||
def _(go, np, sp): | ||||||
def best_differentiation_plot(): | ||||||
x = sp.Symbol("x") | ||||||
Comment on lines
+93
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider breaking these down into smaller, digestible cells (easier on the eyes = readability). Look at how other notebooks tell a story - they introduce one concept, show an example, then move to the next. Another option: Moving it to an appendix section as helper code. |
||||||
|
||||||
expressions = { | ||||||
"Addition Rule": sp.sympify("x**2 + sin(x)"), | ||||||
"Subtraction Rule": sp.sympify("x**3 - cos(x)"), | ||||||
"Power Rule": sp.sympify("x**4"), | ||||||
"Product Rule": sp.sympify("x**2 * sin(x)"), | ||||||
"Quotient Rule": sp.sympify("x**2 / (x + 1)"), | ||||||
"Chain Rule": sp.sympify("sin(x**2)"), | ||||||
} | ||||||
|
||||||
x_vals = np.linspace(-10, 10, 1000) | ||||||
traces = [] | ||||||
visibilities = [] | ||||||
annotations = [] | ||||||
|
||||||
for i, (rule_name, expr) in enumerate(expressions.items()): | ||||||
derivative = sp.diff(expr, x) | ||||||
|
||||||
# Prepare numerical functions | ||||||
f = sp.lambdify(x, expr, "numpy") | ||||||
f_prime = sp.lambdify(x, derivative, "numpy") | ||||||
|
||||||
try: | ||||||
y_vals = f(x_vals) | ||||||
y_prime_vals = f_prime(x_vals) | ||||||
except Exception as e: | ||||||
print(f"Error with {rule_name}: {e}") | ||||||
continue | ||||||
|
||||||
# Plot traces (function and derivative) | ||||||
func_trace = go.Scatter( | ||||||
x=x_vals, | ||||||
y=y_vals, | ||||||
mode="lines", | ||||||
line=dict(color="blue"), | ||||||
name="f(x)", | ||||||
hovertemplate="x: %{x:.2f}<br>f(x): %{y:.2f}", | ||||||
visible=(i == 0), | ||||||
) | ||||||
deriv_trace = go.Scatter( | ||||||
x=x_vals, | ||||||
y=y_prime_vals, | ||||||
mode="lines", | ||||||
line=dict(color="red", dash="dash"), | ||||||
name="f'(x)", | ||||||
hovertemplate="x: %{x:.2f}<br>f'(x): %{y:.2f}", | ||||||
visible=(i == 0), | ||||||
) | ||||||
|
||||||
traces.extend([func_trace, deriv_trace]) | ||||||
|
||||||
# Visibility map | ||||||
vis = [False] * len(expressions) * 2 | ||||||
vis[2 * i] = True | ||||||
vis[2 * i + 1] = True | ||||||
visibilities.append(vis) | ||||||
|
||||||
# Annotations in the lower right corner | ||||||
annotations.append( | ||||||
[ | ||||||
dict( | ||||||
text=f" $f(x) = {sp.latex(expr)}$", | ||||||
xref="paper", | ||||||
yref="paper", | ||||||
x=0.95, | ||||||
y=0.15, | ||||||
showarrow=False, | ||||||
font=dict(size=14), | ||||||
align="left", | ||||||
bgcolor="rgba(255,255,255,0.7)", | ||||||
), | ||||||
dict( | ||||||
text=f"f'(x) = $f'(x) = {sp.latex(derivative)}$", | ||||||
xref="paper", | ||||||
yref="paper", | ||||||
x=0.95, | ||||||
y=0.05, | ||||||
showarrow=False, | ||||||
font=dict(size=14), | ||||||
align="left", | ||||||
bgcolor="rgba(255,255,255,0.7)", | ||||||
), | ||||||
] | ||||||
) | ||||||
|
||||||
# Create figure and add all traces | ||||||
fig = go.Figure(data=traces) | ||||||
|
||||||
# Dropdown menu for rule selection | ||||||
dropdown_buttons = [] | ||||||
Comment on lines
+195
to
+196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the usage of the plotly dropdown; but just incase you hadn't consider playing around w/marimo UI elements like rule_selector = mo.ui.dropdown(["Power Rule", "Product Rule", "Chain Rule"], label="Select Rule") Then use rule_selector.value in later cells to show specific examples or practice problems for that rule. |
||||||
for i, (rule_name, vis) in enumerate(zip(expressions.keys(), visibilities)): | ||||||
dropdown_buttons.append( | ||||||
dict( | ||||||
label=rule_name, | ||||||
method="update", | ||||||
args=[ | ||||||
{"visible": vis}, | ||||||
{ | ||||||
"title": f"π {rule_name}: Function & Derivative", | ||||||
"annotations": annotations[i], | ||||||
}, | ||||||
], | ||||||
) | ||||||
) | ||||||
|
||||||
# Layout | ||||||
fig.update_layout( | ||||||
title="π Addition Rule: Function & Derivative", | ||||||
updatemenus=[ | ||||||
dict( | ||||||
buttons=dropdown_buttons, | ||||||
active=0, | ||||||
x=0.5, | ||||||
y=1.18, | ||||||
xanchor="center", | ||||||
yanchor="top", | ||||||
direction="down", | ||||||
showactive=True, | ||||||
) | ||||||
], | ||||||
annotations=annotations[0], | ||||||
width=1100, | ||||||
height=600, | ||||||
template="plotly_white", | ||||||
hovermode="closest", | ||||||
margin=dict( | ||||||
t=100, r=100 | ||||||
), # Reduced right margin since annotations are now inside | ||||||
legend=dict( | ||||||
x=0.11, | ||||||
y=0.99, | ||||||
xanchor="right", | ||||||
yanchor="top", | ||||||
bgcolor="rgba(255,255,255,0.6)", # Optional: background for clarity | ||||||
bordercolor="gray", # Optional: border for legend box | ||||||
borderwidth=1, | ||||||
), | ||||||
) | ||||||
|
||||||
fig.show() | ||||||
|
||||||
# Run it | ||||||
best_differentiation_plot() | ||||||
return | ||||||
|
||||||
|
||||||
@app.cell | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Make sure to hide your markdown cells (shortcut - |
||||||
def _(mo): | ||||||
mo.md( | ||||||
r""" | ||||||
## Differentiability and Continuity | ||||||
|
||||||
**Objective**: Understand the relationship between differentiability and continuity, identify where a function fails to be differentiable, and visualize these concepts graphically. | ||||||
|
||||||
**Key Questions**: | ||||||
|
||||||
- What does it mean for a function to be differentiable? | ||||||
- How is differentiability related to continuity? | ||||||
- What are the common cases where a function is not differentiable? | ||||||
|
||||||
### Differentiability vs. Continuity | ||||||
#### Definitions | ||||||
- Continuity: A function f(x) is continuous at x = a if: | ||||||
- (No jumps, holes, or asymptotes at x = a.) | ||||||
""" | ||||||
) | ||||||
return | ||||||
|
||||||
|
||||||
if __name__ == "__main__": | ||||||
app.run() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey! First off, sorry for the late review on this - appreciate your initiative on picking up this topic.
For overall structure and consistency, take a look at other notebooks in the repo (like the reactive_plots one or the probability notebooks). You'll notice they follow a similar template/structure pattern that would be good to match here (to maintain a sense of consistency throughout the repo notebooks).