Skip to content

Conversation

k-anisha
Copy link

📝 Summary

Adding rates of change notebook for Calculus in ML

📋 Checklist

  • I have included package dependencies in the notebook file using --sandbox
  • If adding a course, include a README.md
  • Keep language direct and simple.

@Haleshot Haleshot requested a review from Copilot May 30, 2025 07:28
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 PR adds a new Marimo notebook covering rates of change in calculus for machine learning and provides a README for the Calculus module.

  • Introduces RatesOfChange.py, a Marimo notebook demonstrating derivatives, gradient descent, kinematics, and an economics example
  • Adds README.md with course description and instructions for running notebooks

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
calculus_for_ML/RatesOfChange.py New Marimo notebook on rates of change concepts
calculus_for_ML/README.md README for the Calculus for ML course, with setup and contribution info
Comments suppressed due to low confidence (2)

calculus_for_ML/RatesOfChange.py:1

  • [nitpick] Module filename uses CamelCase; per PEP8 it may be clearer to rename the file to lowercase 'rates_of_change.py'.
import marimo

calculus_for_ML/RatesOfChange.py:39

  • Consider adding unit tests for this f(x) function (and deriv_f) to verify correctness of outputs.
def f(x):


@app.cell
def _(np, plt):
# First, let's start by definiting a simple function.
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

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

Typo in comment: 'definiting' should be 'defining'.

Suggested change
# First, let's start by definiting a simple function.
# First, let's start by defining a simple function.

Copilot uses AI. Check for mistakes.

def f(x):
return x ** 2

# Now, we can plot the function.abs
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

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

Remove the stray '.abs' in the comment, e.g. 'plot the function.'

Suggested change
# Now, we can plot the function.abs
# Now, we can plot the function.

Copilot uses AI. Check for mistakes.

def _(mo):
mo.md(
r"""
We can use another example in economics. In economics, there is a term known as 'Price Sensitivity,' wjich essentially measures the rate of change in the quantity demanded or supplied of a good. The price elasticity is given by the formula:
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

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

Typo in preceding line: 'wjich' should be 'which'.

Suggested change
We can use another example in economics. In economics, there is a term known as 'Price Sensitivity,' wjich essentially measures the rate of change in the quantity demanded or supplied of a good. The price elasticity is given by the formula:
We can use another example in economics. In economics, there is a term known as 'Price Sensitivity,' which essentially measures the rate of change in the quantity demanded or supplied of a good. The price elasticity is given by the formula:

Copilot uses AI. Check for mistakes.

Comment on lines +221 to +230
@app.cell
def _():
return


@app.cell
def _():
return


Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

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

[nitpick] This empty cell appears unused; consider removing redundant stub cells at the end of the notebook.

Suggested change
@app.cell
def _():
return
@app.cell
def _():
return

Copilot uses AI. Check for mistakes.

@Haleshot
Copy link
Collaborator

Relevant issue #58

Copy link
Collaborator

@Haleshot Haleshot left a comment

Choose a reason for hiding this comment

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

The math content throughout the notebook is solid - the derivatives, the GD implementation & physics relationships you've laid out are accurate. The economics example at the end also makes good sense and ties things together nicely.

For styling, you could polish this up with some markdown formatting. Using backticks around function names like f(x) and f'(x) would make them stand out better.

Overall, I really like how you build things step-by-step (the flow). Would recommend improving the above PR description to be more descriptive and capture the contents of the notebook accurately (refer to previous PRs).

Also, a nice summary at the end of the notebook going over the contents of the notebook (what was covered, etc.). would be nice. Usage of appropriate markdown extensions &/or admonitions would also make it stand out better.

Comment on lines +17 to +22
@app.cell
def _(mo):
mo.md(
r"""
In this notebook, we'll learn about rates of change in Machine Learning.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd start the notebook with the title being mentioned (h1 tag) followed by the author's name. Something like

# Rates of Change in Machine Learning

*By [YourName](https://github.com/k-anisha)*

In this notebook, we'll learn about...

Comment on lines +7 to +14
@app.cell
def _():
import marimo as mo
import numpy as np
import matplotlib.pyplot as plt
from sympy import symbols
from sympy import diff
return diff, mo, np, plt, symbols
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd move this cell to the bottom (better to directly start with the notebook topic - also consistent and followed in most notebooks in the learn repo).

Comment on lines +39 to +48
def f(x):
return x ** 2

# Now, we can plot the function.abs

x = np.linspace(-10, 10, 500)
y = f(x)

plt.plot(x, y, label=r'$f(x) = x^2$')
plt.title('Graph of f(x) = x^2')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of a static plot, consider adding sliders to explore different functions:

# Could use mo.ui.slider to let users change the coefficient
coeff = mo.ui.slider(1, 5, value=1, label="Coefficient")
def f(x):
    return coeff.value * x**2

This would let users see how changing the coefficient affects the parabola (+ also adds to the interactivity of the notebook).

Comment on lines +105 to +120
mo.md(r"""Now, let's get into gradient descent. Below is an example on a quadratic function.""")
return


@app.cell
def _(deriv_f, f, np, plt):
# Our parameters below

learning_rate = 0.1
initial = 8
iterations = 20

current = initial
history = [current]

for i in range(iterations):
Copy link
Collaborator

Choose a reason for hiding this comment

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

The learning rate, initial point, & iterations are hardcoded. Consider making these interactive:

learning_rate = mo.ui.slider(0.01, 0.5, value=0.1, step=0.01, label="Learning Rate")
initial = mo.ui.slider(-10, 10, value=8, label="Starting Point") 
iterations = mo.ui.slider(5, 50, value=20, label="Iterations")

Users could experiment with different values and see how it affects convergence.


@app.cell
def _(np, plt):
# First, let's start by definiting a simple function.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Btw, I'd recommend checking out our plotting tutorial: https://links.marimo.app/tutorial-plotting
Has a good example where various functions are visualized iteratively.

return diff, mo, np, plt, symbols


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

Would recommend doing this (hiding markdown cell blocks) throughout the notebook

Comment on lines +23 to +32
In simple terms, the rate of change measures how one quantity changes as we adjust its inputs or parameters. The rate of change can represent how a model's output changes as we adjust the input or parameters.

In calculus, the rate of change of a function is given by calculating the derivative at a certain point. So, if f(x) is a function, then f'(x) is the rate of change.

In the context of Machine Learning, rates of change are especially useful when working with optimization problems - for example, minimizing a loss function, or using gradient descent to try and find optimal parameters and minimize error.

Let's get into rates of change!
"""
)
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

One thing that would help connect this to the issue description - consider adding a section before you get into derivatives directly, that shows the difference between:

  • Average rate of change: (f(b) - f(a)) / (b - a) (slope between two points)
  • Instantaneous rate of change: f'(x) (slope at exactly one point)

You could use your f(x) = x^2 function (or the plotting notebook) to show a line (secant?) b/w two points, then demonstrate how as the points get closer, it approaches the tangent line.

Something like picking points at x=2 & x=4, then x=2 & x=3, then x=2 & x=2.1, showing how the slopes approach the derivative value at x=2.

@Haleshot
Copy link
Collaborator

Hey @k-anisha; just wanted to follow-up if you had a chance to revisit this PR and the review comments posted above.

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