feat: math chapter done + editorconfig + remove pycache

This commit is contained in:
2025-11-17 02:33:06 +01:00
parent cb3915b405
commit ac1ddfb98b
6 changed files with 93 additions and 47 deletions

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -1,8 +1,14 @@
## This module represents the first chapter of the book
## "Essential Math for Data Science" - Thomas Nield
## Chaper 1 - Basic Math and Calculus Review
from cmath import log as complex_log # used for complex numbers
from math import e, exp, log
from numpy import arange
# Sympy is powerful since it does not make aproximations, it keeps every primitive as it is, this means that it's slower but more precisse
from sympy import diff, limit, oo, symbols
from sympy import diff, limit, oo, symbols, integrate
def t_summ():
@@ -71,3 +77,59 @@ def t_build_derivate(f):
def t_build_partial_derivate(f,symbol):
return diff(f, symbol)
def t_approximate_integral(f, init, end, precission):
integral = 0
for i in arange(init, end, precission):
w = i + precission
h = f(w) # or f(i) to estimate down
integral += (h * precission)
return integral
def t_calculate_integral(f, init, end, symbol):
return integrate(f, (symbol, init, end))
def test_math_module():
t_exponent(2,8)
print(t_compound_interest(100, 20 / 100, 2, 12))
print(t_compound_interest_algorigthm(100, 20 / 100, 2, 12))
print(t_compound_interest_continious(100, 20 / 100, 2))
print(t_compound_interest_continious_exp(100, 20 / 100, 2))
print(t_calculate_e(1000000000000))
print(t_logarithm_natural(10))
print(t_limit())
print(t_calculate_e_limit())
print(t_calculate_e_limit().evalf())
x = symbols("x")
derivative = t_build_derivate(x ** 2)
value_on_two = derivative.subs(x,2)
print(derivative, value_on_two)
# Partial derivates are applied to one variable at a time
y = symbols("y")
z = symbols("z")
partial_derivative_y = t_build_partial_derivate((3 * y ** 3) + 2 * z ** 2, y)
partial_derivative_z = t_build_partial_derivate((3 * y ** 3) + 2 * z ** 2, z)
print(partial_derivative_y, partial_derivative_z)
print(t_build_derivative_from_limit(x ** 2))
print(t_calculate_derivative_limit(x ** 2, 2))
## Chain rule
# for a given function y (lets say y = x ** 2 + 1) and another z (lets say z = y ** 3 - 2), we can find a derivativeof z with respect to x by multiplying the derivatives
# normal derivative calc
x = symbols("x")
z = (x ** 2 + 1) ** 3 - 2
print(t_build_derivate(z))
# chain rule calc
x, y, z = symbols("x y z")
y_f = x ** 2 + 1
z_f = y ** 3 - 2
print(t_build_derivate(y_f), t_build_derivate(z_f))
## Integrals
def foo(x):
return x ** 2 + 1
print(t_approximate_integral(foo, 0, 1, 0.001))
x = symbols("x")
f = x ** 2 + 1
print(t_calculate_integral(f, 0, 1, x))

View File

@@ -0,0 +1,10 @@
## This module represents the second chapter of the book
## "Essential Math for Data Science" - Thomas Nield
## Chaper 2 - Probability
# 2.0 odds means than an events has twice the probabilities to happen than not
def p_odds_to_probability(o):
return (o / (1 + o))
def p_probability_to_odds(p):
return (p / (1 - p))