Skip to content

Conversation

ghimiresunil
Copy link

@ghimiresunil ghimiresunil commented Jun 5, 2025

📝 Summary

This pull request adds a new Marimo notebook for the "Derivatives and Critical Values" topic, part of the Calculus for Machine Learning course (Issue #58).

The goal of this notebook is to build a foundational understanding of derivatives through visual explanations and real-world applications relevant to machine learning, such as gradient-based optimization.

✅ Course Content Development Checklist

  • Introduction to Derivatives

    • Derivative as a rate of change
    • Notation and interpretation (Leibniz, Lagrange, etc.)
    • Graphical intuition (slope of the tangent)
  • Rules of Differentiation

    • Power rule
    • Product rule
    • Quotient rule
    • Chain rule
    • Higher-order derivatives
  • Differentiability and Continuity

    • Conditions for differentiability
    • Visual examples of non-differentiable points

📋 Checklist

  • Included all necessary package dependencies using --sandbox for reproducibility
  • Added a README.md file for the topic
  • Used clear, beginner-friendly language throughout the notebook
  • Included visualizations and outputs for better understanding
  • Structured the content progressively for logical flow

@Haleshot Haleshot requested review from Haleshot and Copilot June 5, 2025 09:37
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds a new Marimo notebook focused on teaching derivatives and critical values for the Calculus for Machine Learning course. The update introduces interactive cells demonstrating differentiation rules and dynamic plots, and it also includes an updated README for course introduction and contributor information.

  • Added a new notebook (derivatives_and_critical_values.py) with interactive markdown explanations and plots.
  • Updated the README.md with course details and contribution guidance.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
calculus/derivatives_and_critical_values.py New interactive notebook with cells explaining rules of differentiation.
calculus/README.md Updated documentation and instructions for the Calculus course.
Comments suppressed due to low confidence (2)

calculus/derivatives_and_critical_values.py:164

  • The derivative annotation string redundantly includes 'f'(x) =' twice. Consider revising it to display the label only once, e.g., "f'(x) = {sp.latex(derivative)}".
text=f"f'(x) = $f'(x) = {sp.latex(derivative)}$",

calculus/derivatives_and_critical_values.py:57

  • [nitpick] The use of '_' as the function name for multiple cell definitions is ambiguous. Using descriptive function names could improve code readability and maintainability.
def _(mo):

Copy link
Collaborator

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).

r"""
# Derivatives and Critical Values

_By [ghimiresunil](https://github.com/ghimiresunil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_By [ghimiresunil](https://github.com/ghimiresunil)
_By [ghimiresunil](https://github.com/ghimiresunil)_

Comment on lines +25 to +60
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...
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 --- separation lines). Look at how other notebook authors write their explanations (reactive plots) or (UDF notebook) - they tend to be more conversational and direct. You could shorten this significantly while keeping the key points

return


@app.cell
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@app.cell
@app.cell(hide_code=True)

Make sure to hide your markdown cells (shortcut - Ctrl + H) so only the rendered text shows, not the underlying markdown syntax. I see you have it for the first markdown block.

Comment on lines +66 to +84
@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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Comment on lines +93 to +106
@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")
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Comment on lines +195 to +196
# Dropdown menu for rule selection
dropdown_buttons = []
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 mo.ui.dropdown or mo.ui.radio. This would let you use the selected values in subsequent cells for further analysis.
Check out this notebook for inspiration: https://marimo.io/p/@marimo/embedding-visualizer
You could do something 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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:
Start with a simple example ("Let's say we want to find where a function reaches its peak...")

  • Show the derivative step by step
  • Build up to relevant interactive elements

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants