AI/ML notes

ch1_basic_math_and_calculus_review

Ch 1. Basic Math and Calculus Review

Number Systems

  • Natural Numbers

    • Numbers: 1, 2, 3, 4, 5, …
    • Only includes positive numbers.
  • Whole Numbers

    • Numbers: Natural numbers + 0.
  • Integers

    • Numbers: Includes positive and negative natural numbers, plus 0.
  • Rational Numbers

    • Definition: Numbers expressible as fractions (e.g., 2/3, 6.87 = 687/100, or 2 = 2/1).
    • Rational numbers are called "ratios" and are useful for measuring non-discrete units like parts of a gallon or fractions of a mile.
  • Irrational Numbers

    • Definition: Numbers that cannot be expressed as fractions (e.g., π, √2, Euler’s number e).
    • These numbers have infinite decimal digits (e.g., π = 3.14159...).
  • Real Numbers

    • Includes both rational and irrational numbers.
    • Used in most practical data science applications involving decimals.
  • Complex and Imaginary Numbers

    • Encountered when taking the square root of a negative number.
    • Relevant in certain advanced mathematical problems but less common in basic data science work.
  • Practical Use in Data Science

    • Most work involves whole numbers, natural numbers, integers, and real numbers.
    • Imaginary numbers are typically used in advanced contexts like matrix decomposition.

Order of Operations

  • Parentheses first

  • Then exponents

  • Followed by multiplication and division (from left to right)

  • Lastly, addition and subtraction (from left to right) parts of a mathematical expression are evaluated.

  • PEMDAS (Please Excuse My Dear Aunt Sally), Mnemonic to remember the order:

    • Parentheses
    • Exponents
    • Multiplication
    • Division
    • Addition
    • Subtraction

Variables and functions

  • Variables

    • Named placeholders for unspecified or unknown numbers.
  • Functions

    • Define relationships between two or more variables.
    • Take input variables (independent or domain variables) and result in an output variable (dependent variable).
  • Cartesian Plane

    • A two-dimensional plane used for plotting variables.
    • Known as the x-y plane or coordinate plane, with one number line for each variable.

SymPy Overview

  • SymPy is a symbolic math library used for graphing and performing algebraic operations.

  • It handles mathematical expressions symbolically rather than numerically.

  • Summation in SymPy

    • The Sum() operator performs summation operations.
    • Syntax: Sum(expression, (variable, start, end))
    • In the example, Sum(2*i, (i, 1, n)) iterates i from 1 to n, multiplies each i by 2, and sums the results.
  • Code Example

    from sympy import *
    i, n = symbols('i n')
    # Iterate each element i from 1 to n,
    # then multiply and sum
    summation = Sum(2*i, (i, 1, n))
    # Specify n as 5,
    # iterating the numbers 1 through 5
    up_to_5 = summation.subs(n, 5)
    print(up_to_5.doit())  # Output: 30
  • Key Points

    • subs() is used to substitute specific values into symbolic expressions. In the example, n is set to 5.
    • doit() function is required to calculate and simplify the summation, as SymPy summations are lazy and don't execute automatically.

Exponent and Logarithm

Operator Exponent Property Logarithm Property
Multiplication $x^m \times x^n = x^{m+n}$ $\log(a \times b) = \log(a) + \log(b)$
Division $\frac{x^m}{x^n} = x^{m-n}$ $\log\left(\frac{a}{b}\right) = \log(a) - \log(b)$
Exponentiation $(x^m)^n = x^{mn}$ $\log(a^n) = n \times \log(a)$
Zero Exponent $x^0 = 1$ $\log(1) = 0$
Inverse $x^{-1} = \frac{1}{x}$ $\log(x^{-1}) = \log\left(\frac{1}{x}\right) = -\log(x)$
  • Euler’s Number (e)

    • A special constant, approximately 2.71828, similar to π.
    • Commonly used because it simplifies many mathematical problems, especially involving exponents and logarithms.
  • Why Euler’s Number is Widely Used

    • The exponential function of e is unique because its derivative is itself.
    • In cases where the base of an exponential or logarithmic function doesn’t matter, e is often chosen for its simplicity in calculus and derivatives.
  • Loan Example with Compound Interest

    • Example: Loan $100 at 20% annual interest, compounded monthly.
      • Monthly interest: $\frac{0.20}{12} = 0.01666$
      • Formula to calculate the balance after time $t$: $ A = P \left(1 + \frac{r}{n}\right)^{nt} $
      • Variables:
        • $A$: Balance
        • $P$: Initial investment
        • $r$: Interest rate
        • $t$: Time in years
        • $n$: Number of compounding periods per year (monthly, daily, hourly).
  • Increasing Compounding Frequency

    • Compounding more frequently (e.g., daily or hourly) yields slightly more interest, but there is a diminishing return as the compounding frequency increases.
    • For continuous compounding, the balance approaches a limit calculated with Euler’s number.
  • Continuous Compounding

    • The balance from continuously compounding interest can be calculated using e.
    • Compounding every minute or hour approaches the value from continuous compounding.
    • Formula for continuous compounding: $ A = P \cdot e^{rt} $
  • $e$ and Exponential Growth

    • The formula $\left(1 + \frac{1}{n}\right)^n$ approaches e as $n$ becomes larger.
    • This expression reflects how e emerges from processes involving continuous growth, and it appears in many fields, including population studies and finance.
  • Use of $e$ in Programming

    • Platforms like Python and Excel use the exp() function to calculate expressions with e as the base.
    • e is the default base in many exponent and logarithmic functions.

Limits

  • Limits describe what happens to a function as the input variable approaches a particular value, often infinity.

  • Example: $f(x) = \frac{1}{x}$

    • As $x$ increases indefinitely, $f(x)$ gets closer to 0 but never actually reaches it.
  • Limit Expression in SymPy

    • SymPy represents infinity using oo.

    • Example Code:

      from sympy import *
      x = symbols('x')
      f = 1 / x
      result = limit(f, x, oo)
      print(result)  # Output: 0
  • Euler’s Number from Limits

    • Euler’s number can be derived as a limit: $ \lim_{{n \to \infty}} \left(1 + \frac{1}{n}\right)^n = e \approx 2.71828 $

    • SymPy automatically recognizes this limit as Euler’s number (E).

    • Example Code:

      from sympy import *
      n = symbols('n')
      f = (1 + (1/n))**n
      result = limit(f, n, oo)
      print(result)  # Output: E
      print(result.evalf())  # Output: 2.71828182845905
  • Key Takeaways

    • Limits are used to describe behavior as inputs grow indefinitely or approach a certain value.
    • Euler’s number, $e$, is derived from a limit that involves continuously increasing $n$.

Derivatives

  • A derivative measures the slope of a function at any given point, indicating the rate of change.

  • Used frequently in machine learning algorithms like gradient descent.

  • When the slope is 0, it marks the minimum or maximum of a function.

  • Using SymPy to Calculate Derivatives

    • SymPy can symbolically calculate derivatives.

    • Example Code:

      from sympy import *
      # Declare 'x' to SymPy
      x = symbols('x')
      # Declare the function f(x) = x^2
      f = x**2
      # Calculate the derivative of the function
      dx_f = diff(f)
      print(dx_f)  # Output: 2*x
      # Calculate the slope at x = 2
      print(dx_f.subs(x, 2))  # Output: 4
  • Partial Derivatives Overview

    • Partial derivatives are derivatives of functions with multiple input variables.
    • They measure the rate of change with respect to one variable, assuming all other variables are held constant.
    • These are particularly useful when dealing with multidimensional functions.
  • Gradient

    • In functions with multiple variables, the partial derivatives with respect to each variable form a gradient, representing slopes in different directions.
  • Example: Function with Two Variables

    • Given $f(x, y) = 2x^3 + 3y^3$,

      • $\frac{d}{dx} f(x, y)$ measures the slope in the x-direction.
      • $\frac{d}{dy} f(x, y)$ measures the slope in the y-direction.
      from sympy import *
      from sympy.plotting import plot3d
      
      # Declare x and y to SymPy
      x, y = symbols('x y')
      
      # Declare the function f(x, y)
      f = 2*x**3 + 3*y**3
      
      # Calculate the partial derivatives for x and y
      dx_f = diff(f, x)
      dy_f = diff(f, y)
      
      print(dx_f)  # Output: 6*x**2
      print(dy_f)  # Output: 9*y**2
      
      # Plot the 3D surface of the function
      plot3d(f)
  • Key Points

    • Partial derivatives provide slope information in different directions on a multidimensional surface.
    • SymPy can be used to symbolically calculate these derivatives and visualize functions in 3D.