Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_08/extra-materials/e_log_examples.ipynb
1904 views
Kernel: Python 3

Exponential functions and logarithms

import math import numpy as np

Compounding

If I have a loan and I have to pay 50% a year interest, payable on the last day of the year, I repay 1.5 * the original loan amount

original_amt = 100 interest = .50 repayment_amnt = 100 *(1+interest) repayment_amnt
150.0

If that loan compounds every 6 months, I repay a little more than 1.5 * the original amount

amt_after_6_months = original_amt * (1+interest/2) final_repayment_amnt = amt_after_6_months * (1+interest/2) final_repayment_amnt
156.25

If that loan compounds every month, I have to pay back even more.

months = range(1,12) payment_after_first_month = original_amt * (1+interest/12) for i in months: payment_after_first_month = payment_after_first_month * (1+interest/12) payment_after_first_month
163.20941327229255

And the number keeps getting higher the more you compound... until you reach e!

Exponential functions

What is e? It is simply a number (known as Euler's number):

math.e
2.718281828459045

The np.exp() function raises e to some power

math.e**2
7.3890560989306495
np.exp(2)
7.38905609893065

e is a significant number, because it is the base rate of growth shared by all continually growing processes.

For example, if I have 10 dollars, and it grows 100% in 1 year (compounding continuously), I end up with 10*e^1 dollars:

# 100% growth for 1 year 10 * np.exp(1)
27.18281828459045
# 100% growth for 2 years 10 * np.exp(2)
73.89056098930651

Side note: When e is raised to a power, it is known as the exponential function. Technically, any number can be the base, and it would still be known as an exponential function (such as 2^5). But in our context, the base of the exponential function is assumed to be e.

Anyway, what if I only have 20% growth instead of 100% growth?

# 20% growth for 1 year 10 * np.exp(0.20)
12.214027581601698
# 20% growth for 2 years 10 * np.exp(0.20 * 2)
14.918246976412703

Logarithms

Logarithms invert raising something to a power. Here is an example:

powers = [10, 10**2, 10**3] powers
[10, 100, 1000]
np.log10(powers)
array([1., 2., 3.])

Another example

powers2 = [2, 2**2, 2**3, 2**4, 2**5, 2**6] powers2
[2, 4, 8, 16, 32, 64]
np.log2(powers2)
array([1., 2., 3., 4., 5., 6.])

Natural Logarithm -- Inverting e

The 'natural logarithm' inverts e. Becuase it is so common, you don't need to specify the base in numpy.

One interpretation of the (natural) logarithm is it gives you the time needed to reach a certain level of growth. For example, if I want growth by a factor of 2.718, it will take me 1 unit of time (assuming a 100% growth rate):

np.log(np.exp(5))
5.0
np.exp(np.log(5))
4.999999999999999
# time needed to grow 1 unit to 2.718 units np.log(2.718)
0.999896315728952

If I want growth by a factor of 7.389, it will take me 2 units of time:

# time needed to grow 1 unit to 7.389 units np.log(7.389)
1.9999924078065106

If I want growth by a factor of 1, it will take me 0 units of time:

# time needed to grow 1 unit to 1 unit np.log(1)
0.0