-
Notifications
You must be signed in to change notification settings - Fork 35
Anisha/calc #107
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?
Anisha/calc #107
Conversation
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.
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 (andderiv_f
) to verify correctness of outputs.
def f(x):
|
||
@app.cell | ||
def _(np, plt): | ||
# First, let's start by definiting a simple function. |
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.
Typo in comment: 'definiting' should be 'defining'.
# 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 |
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.
Remove the stray '.abs' in the comment, e.g. 'plot the function.'
# 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: |
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.
Typo in preceding line: 'wjich' should be 'which'.
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.
@app.cell | ||
def _(): | ||
return | ||
|
||
|
||
@app.cell | ||
def _(): | ||
return | ||
|
||
|
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.
[nitpick] This empty cell appears unused; consider removing redundant stub cells at the end of the notebook.
@app.cell | |
def _(): | |
return | |
@app.cell | |
def _(): | |
return |
Copilot uses AI. Check for mistakes.
Relevant issue #58 |
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.
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.
@app.cell | ||
def _(mo): | ||
mo.md( | ||
r""" | ||
In this notebook, we'll learn about rates of change in Machine Learning. | ||
|
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.
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...
@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 |
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.
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).
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') |
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.
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).
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): |
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.
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. |
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.
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 |
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.
@app.cell | |
@app.cell(hide_code=True) |
Would recommend doing this (hiding markdown cell blocks) throughout the notebook
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 |
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.
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.
Hey @k-anisha; just wanted to follow-up if you had a chance to revisit this PR and the review comments posted above. |
📝 Summary
Adding rates of change notebook for Calculus in ML
📋 Checklist
--sandbox
README.md