Math for Data Science

Solutions - Review Days 1-3

Author

Joanna Bieri
DATA100

Important Information

Math Fundamentals + Python

So far in this class we have worked to review some mathematics fundamentals and to introduce you to the ways that Python can help you do the calculations and check your answers. The goal of this notebook is to give you answers to all the review problems and to be your cheat sheet for what we have done so far.

Rounding

# What will the code do?
'''
my_number = 14.9858187509
decimal_place = 0

rounded_number = round(my_number,decimal_place)

print(rounded_number)
'''

print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!

Answer:

This code takes the number 14.9858187509 and rounds it right at the decimal place. The result will be 15.

Exponential Notation

# What will the code do?
'''
my_number = 135726384769325
my_format = ".2e"
scientific_notation = format(my_number, my_format)
print(scientific_notation)
'''

print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!

Answer:

This code will take the number 135726384769325 and reformat it in scientific notation, with just one number before the decimal point. To do this we have to move the decimal point to the left 14 times. Giving us \(1.35726384769325 \times 10^{14}\). The we round to two places after the decimal point giving us \(1.36 \times 10^{14}\) or in Python notation 1.36e+14.

Calculator Calculations

# What will the code do?
a = 10
b = 3
c = 27
d = 2
e = -4

'''
first_answer = a**d/e
print(first_answer)
'''
print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!

Answer:

This code first sets some variables so that the different numbers are stored in the variables a-e. The we see that we are taking a raised to the power of d and then dividing by e:

\[ \frac{10^2}{-4} = \frac{100}{-4} = -25\]

so now first_answer = -25

Chaining Together Calculator Calculations

# What will the code do?
'''
second_answer = first_answer - c
print(second_answer)
'''
print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!

Answer:

Since we stored the first_answer = -25 above. When we run this code it will continue the calculation:

\[ -25 - 27 = -52 \]

so now second_answer = -52

Using numpy to Access Functions

# What will the code do?
import numpy as np

'''
first_answer = np.sin(b)
second_answer = np.sqrt(first_answer)
print(second_answer)
'''
print('Make a guess (do the calculation) before you run the code!')
Make a guess (do the calculation) before you run the code!

Answer:

First we have to import numpy so that python has access to the extra functions. Then when we do np.sin(b) this is calculating the trigonometric function (sine):

first_answer \(=\sin(3)=0.1411200080598672\).

After we figure out the first_answer then we are taking the square root of it.

second_answer \(=\sqrt{0.1411200080598672}=0.3756594309475901\).

Using sympy to Write Fractions

print('How would you write the fraction 5/7 using sp.Rational(a,b)?')
How would you write the fraction 5/7 using sp.Rational(a,b)?

Answer:

# import sympy
import sympy as sp

# create the variable for the fraction
my_fraction = sp.Rational(5,7)

# show the answer
my_fraction

\(\displaystyle \frac{5}{7}\)

Fraction Calculations

First calculate by hand:

\[\frac{\frac{2}{3}+\frac{5}{4}}{\frac{3}{2}}\]

Then use Python and Sympy to confirm your answer.

Answer:

By hand we need to get a common denominator for the numerator, before we can divide the denominator. A common denminator between \(3\) and \(4\) is \(12\). I see I need to multiply \(3\times4=12\). In each case I multiply by a fancy one:

\[\frac{\frac{4}{4}\frac{2}{3}+\frac{3}{3}\frac{5}{4}}{\frac{3}{2}}\]

\[\frac{\frac{8}{12}+\frac{15}{12}}{\frac{3}{2}} = \frac{\frac{23}{12}}{\frac{3}{2}}\]

Now to divide by a fraction I can multiply by the reciprocal.

\[\frac{\frac{23}{12}}{\frac{3}{2}} = \frac{23}{12}\frac{2}{3}\]

When I multiply I just multiply all the numbers in the numerator and all the numbers in the denominator

\[\frac{23}{12}\frac{2}{3} = \frac{46}{36}\]

Then I simplify. I can take \(2\) out of both the numerator and denominator.

\[\frac{46}{36} = \frac{23}{18}\]

In Python

frac1 = sp.Rational(2,3)
frac2 = sp.Rational(5,4)
frac3 = sp.Rational(3,2)

answer = (frac1 + frac2)/frac3
answer

\(\displaystyle \frac{23}{18}\)

Simplifying Exponents with numbers

First simplify by hand

\[ (2^4+(4^{1/2})^4) \]

Then use Python and Sympy or Numpy to confirm your answer

Answer:

By hand we will start by simplifying the exponent raised to a power using the rule \((x^a)^b=a^{ab}\)

\[ (2^4+(4^{1/2})^4) = (2^4+4^2)\]

Then I will calculate the powers inside the parenthesis

\[(2^4+4^2) = (16+16) = 32 \]

In Python

# Just do the calculation in numpy
(2**4+(4**(1/2))**4)
32.0

Simplifying Exponents with x

First simplify by hand

\[ \frac{(x^5+x^2x^3)^2}{x^2} \]

Then use Python and Sympy to confirm your answer

Answer:

By hand we start with simplifying the numerator. I will multiply the term \(x^2x^3\) to get \(x^5\)

\[ \frac{(x^5+x^2x^3)^2}{x^2} = \frac{(x^5+x^5)^2}{x^2}\]

and then add together the two terms that have the same exponent

\[ \frac{(x^5+x^5)^2}{x^2} =\frac{(2x^5)^2}{x^2} \]

now we can apply the exponent in the numerator. Here we have to do the exponent of each term.

\[ \frac{(2x^5)^2}{x^2} = \frac{4x^{10}}{x^2} \]

finally we can divide out the denominator

\[ \frac{4x^{10}}{x^2} = 4x^8 \]

In Python

# We already have sympy loaded above

# Define the symbols
x = sp.symbols('x')

# Enter the equation
my_expr = ((x**5+x**2*x**3)**2)/x**2

# Show the answer... it is already simplified
my_expr

\(\displaystyle 4 x^{8}\)

Simplifying Logarithms and Exponents

First simplify by hand

\[ \ln((e^{x^2})^2) \]

Then use Python and Sympy to confirm your answer

Answer:

By hand we will first deal with the stuff inside the natural log. Here we see and exponent raised to a power so we will use the rule \((x^a)^b=a^{ab}\) again

\[ \ln((e^{x^2})^2) = \ln(e^{2x^2})\]

then we know that the natural log and the exponent base e are inverses.

\[\ln(e^{2x^2}) = 2x^2\]

In Python

# We already have sympy loaded above

# Define the symbols
x = sp.symbols('x')

# Enter the equation
my_expr = sp.log(sp.exp(x**2)**2)

my_expr

\(\displaystyle \log{\left(e^{2 x^{2}} \right)}\)

# If we want it to simplify more, we have to give it more information about x

x = sp.symbols('x',real=True, positive=True)
my_expr = sp.log(sp.exp(x**2)**2)
my_expr

\(\displaystyle 2 x^{2}\)

More Review Problems (from Day3)

For these ones I will show you the python code:

  1. Add the fractions: \[ \frac{x}{2}+\frac{x}{3}+\frac{x}{4}\]
# Define the pieces
x = sp.symbols('x')

a = x/2
b=x/3
c=x/4

# Do the calculation
answer = x+b+c

# Show the answer
answer

\(\displaystyle \frac{19 x}{12}\)

  1. Solve for \(x\): \[3x+2-5=4^2\]
# Define the pieces
x = sp.symbols('x')

# Enter the equation with everything solved to one side
my_eqn = 3*x+2-4-4**2
display(my_eqn) # This shows the equation


# Solve for x
sp.solve(my_eqn,x)

\(\displaystyle 3 x - 18\)

[6]
  1. Simplify the expression: \[2^33-3\]
# Just enter the numbers
2**3*3-3
21
  1. Simplify the expression: \[\ln(e^{\frac{4-2}{3^3}})\]
# Just enter the numbers - you can use numpy or sympy

#numpy
print(np.log( np.exp((4-2)/3**3) ))

#sympy
display(sp.log( sp.exp((4-2)/3**3) ))
0.07407407407407406

\(\displaystyle 0.0740740740740741\)

  1. Solve for \(x\): \[\log_4(x)=2\]
# Define the pieces
x = sp.symbols('x')

# Enter the equation with everything solved to one side
my_eqn = sp.log(x,4) - 2
display(my_eqn)

# Solve for x
sp.solve(my_eqn,x)

\(\displaystyle \frac{\log{\left(x \right)}}{\log{\left(4 \right)}} - 2\)

[16]

Linear systems and plotting the solution.

  1. \[7x+2y=3\] \[4x+3y=11\]
# Define the pieces
x,y = sp.symbols('x,y')

# Enter the equations with everything solved to one side
eqn1 = 7*x+2*y-3
eqn2 = 4*x+3*y-11

# Solve the system
sp.solve([eqn1,eqn2], [x,y])
{x: -1, y: 5}
# To plot we need to enter the equations as y = f(x)
y_eq1 = (3-7*x)/2
y_eq2 = (11-4*x)/3

# Then plot - the should intersect at the correct points. In this case (-1,5)
plt = sp.plot(y_eq1,y_eq2)

  1. \[\frac{x}{2}-\frac{y}{5}=-4\] \[3x+\frac{y}{2}=-7\]
# Define the pieces
x,y = sp.symbols('x,y')

# Enter the equations with everything solved to one side
eqn1 = x/2-y/5+4
eqn2 = 3*x+y/2+7

# Solve the system
sp.solve([eqn1,eqn2], [x,y])
{x: -4, y: 10}
# To plot we need to enter the equations as y = f(x)
y_eq1 = (4+x/2)*5
y_eq2 = (-7-3*x)*2

# Then plot - the should intersect at the correct points. In this case (-4,10)
plt = sp.plot(y_eq1,y_eq2)

Linear Systems from Word Problems

Problem 1

  1. You run an accounting business that specializes in auditing (verifying accounting records). One of your auditors is working on the payroll records for a company with 75 employees. Some are part-time and some are full-time. After working for three days, your auditor tells you that the audit is completed for half of the full-time employees, but there are still 50 employee records to audit. Find out how many of the employees are full time and how many are part-time.

Answer

First you need to turn the words into math. Here is the set of questions I ask:

  1. What are we trying to solve for?

    • In this case we want to know how many full time and how many part time employees there are. So we can let:
    • \(x=\)full time
    • \(y=\)part time
  2. Now what do we know about combinations of these variables:

    • The company has 75 employees: \(x+y=75\)
    • The auditor has finished half of the full time employees \(\frac{x}{2}\) and none of the part time \(y\). Since they have 50 records left this means that \(\frac{x}{2}+y = 50\)
  3. Solve the system of two equations for two unknowns

\[x+y=75\] \[\frac{x}{2}+y = 50\]

# Define the pieces
x,y = sp.symbols('x,y')

# Enter the equations with everything solved to one side
eqn1 = x+y-75
eqn2 = x/2+y-50

# Solve the system
sp.solve([eqn1,eqn2], [x,y])
{x: 50, y: 25}
  1. Interpret your result: There are 50 full time employees and 25 part time.

Problem 2

  1. A financial planner wants to invest \$8000, some in stocks earning 15% annually and the rest in bonds earning 6% annually. How much should be invested at each rate to get a return of $930 annually from the two investments?

First you need to turn the words into math. Here is the set of questions I ask:

  1. What are we trying to solve for?

    • In this case we want to know how many stocks and how bonds there are. So we can let:
    • \(x=\)money in stocks
    • \(y=\)money in bonds
  2. Now what do we know about combinations of these variables:

    • The total amount invested is \(x+y=8000\)
    • The stocks earn 15% and the bonds earn 6% and we want a total return of $930: \(.15x+.06y=930\)
  3. Solve the system of two equations for two unknowns

\[x+y=8000\] \[0.15x+0.06y = 930\]

# Define the pieces
x,y = sp.symbols('x,y')

# Enter the equations with everything solved to one side
eqn1 = x+y-8000
eqn2 = 0.15*x+0.06*y-930

# Solve the system
sp.solve([eqn1,eqn2], [x,y])
{x: 5000.00000000000, y: 3000.00000000000}
  1. Interpret your results: we should invest $5000 in stocks and $3000 in bonds.

Factoring expressions in Sympy

  1. \[x^2+9x\]
  2. \[x^2+9x-1\]

Answers:

# Problem 1
# Define the pieces
x = sp.symbols('x')

# Enter the expression
my_expr = x**2+9*x
display(my_expr)

# Use sympy to factor
sp.factor(my_expr)

\(\displaystyle x^{2} + 9 x\)

\(\displaystyle x \left(x + 9\right)\)

# Problem 2
# Define the pieces
x = sp.symbols('x')

# Enter the expression
my_expr = x**2+9*x-1
display(my_expr)

# Use sympy to factor
sp.factor(my_expr)

\(\displaystyle x^{2} + 9 x - 1\)

\(\displaystyle x^{2} + 9 x - 1\)

What happened here? Well this expression was not able to be nicely factored!

Solving expressions in Sympy

  1. \[x^2-16=0\]
  2. \[x^2+9x-1=0\]
# Problem 1
# Define the pieces
x = sp.symbols('x')

# Enter the expression - always make sure there is a zero on the right hand side
my_expr = x**2-16
display(my_expr)

# Use sympy to factor
display(sp.factor(my_expr))

# Use sympy to solve
sp.solve(my_expr)

\(\displaystyle x^{2} - 16\)

\(\displaystyle \left(x - 4\right) \left(x + 4\right)\)

[-4, 4]
# Problem 2
# Define the pieces
x = sp.symbols('x')

# Enter the expression - always make sure there is a zero on the right hand side
my_expr = x**2+9*x-1
display(my_expr)

# Use sympy to factor
display(sp.factor(my_expr))

# Use sympy to solve
sp.solve(my_expr)

\(\displaystyle x^{2} + 9 x - 1\)

\(\displaystyle x^{2} + 9 x - 1\)

[-9/2 + sqrt(85)/2, -sqrt(85)/2 - 9/2]

Here we see that the result is what would come out of the quadratic formula!

Expanding expressions using sympy.

  1. \[ (x+4)^2 \]
  2. \[ \]

remember if you are doing these by hand you have to FOIL the polynomials together!

# Problem 1
# Define the pieces
x = sp.symbols('x')

# Enter the expression - always make sure there is a zero on the right hand side
my_expr = (x+4)**2
display(my_expr)

# Use sympy to expand
display(sp.expand(my_expr))

\(\displaystyle \left(x + 4\right)^{2}\)

\(\displaystyle x^{2} + 8 x + 16\)

# Problem 2
# Define the pieces
x = sp.symbols('x')

# Enter the expression - always make sure there is a zero on the right hand side
my_expr = x*(x-3)*(x+2)
display(my_expr)

# Use sympy to expand
display(sp.expand(my_expr))

\(\displaystyle x \left(x - 3\right) \left(x + 2\right)\)

\(\displaystyle x^{3} - x^{2} - 6 x\)