diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e7ef85b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +[*.{md,py}] +charset = utf-8 + +[*.py] +indent_style = tab +indent_size = 4 + +[Makefile] +indent_style = tab diff --git a/src/main.py b/src/main.py index fadbf60..7c9e113 100644 --- a/src/main.py +++ b/src/main.py @@ -1,55 +1,13 @@ from sympy import diff, limit, oo, symbols from modules.math import ( - t_build_derivate, - t_build_derivative_from_limit, - t_build_partial_derivate, - t_calculate_derivative_limit, - t_calculate_e, - t_calculate_e_limit, - t_compound_interest, - t_compound_interest_algorigthm, - t_compound_interest_continious, - t_compound_interest_continious_exp, - t_exponent, - t_limit, - t_logarithm_natural, + test_math_module ) from modules.strings import t_strings if __name__=="__main__": t_strings() - 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)) + print(">> Math module") + test_math_module() + print(">>>>>>>>>>>>>>>>>>>>") diff --git a/src/modules/__pycache__/math.cpython-313.pyc b/src/modules/__pycache__/math.cpython-313.pyc deleted file mode 100644 index 81f90b5..0000000 Binary files a/src/modules/__pycache__/math.cpython-313.pyc and /dev/null differ diff --git a/src/modules/__pycache__/strings.cpython-313.pyc b/src/modules/__pycache__/strings.cpython-313.pyc deleted file mode 100644 index 11854a7..0000000 Binary files a/src/modules/__pycache__/strings.cpython-313.pyc and /dev/null differ diff --git a/src/modules/math.py b/src/modules/math.py index 475affd..063fc52 100644 --- a/src/modules/math.py +++ b/src/modules/math.py @@ -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)) diff --git a/src/modules/probability.py b/src/modules/probability.py new file mode 100644 index 0000000..43f72ab --- /dev/null +++ b/src/modules/probability.py @@ -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))