### Computing for Mathematics - Mock individual coursework

This jupyter notebook contains questions that will resemble the questions in your individual coursework.

**Important** Do not delete the cells containing: 

```
### BEGIN SOLUTION

### END SOLUTION
```

write your solution attempts in those cells.

**If you would like to** submit this notebook:

- Change the name of the notebook from `main` to: `<student_number>`. For example, if your student number is `c1234567` then change the name of the notebook to `c1234567`.
- Write all your solution attempts in the correct locations;
- Save the notebook (`File>Save As`);
- Follow the instructions given in class/email to submit.

#### Question 1

Output the evaluation of the following expressions exactly.

a. \\(\frac{(9a^2bc^4) ^ {\frac{1}{2}}}{6ab^{\frac{3}{2}}c}\\)


```python
### BEGIN SOLUTION
import sympy as sym
a, b, c = sym.Symbol("a"), sym.Symbol("b"), sym.Symbol("c")

sym.expand((9 * a ** 2 * b * c ** 4) ** (sym.S(1) / 2) / (6 * a * b ** (sym.S(3) / 2) * c))
### END SOLUTION
```




$\displaystyle \frac{\sqrt{a^{2} b c^{4}}}{2 a b^{\frac{3}{2}} c}$




```python
q1_a_answer = _
feedback_text = """Your output is not a symbolic expression.

You are expected to use sympy for this question.
"""
try:
    assert q1_a_answer.expand(), feedback_text
except AttributeError:
    assert False, feedback_text
```


```python
import sympy as sym
a, b, c = sym.Symbol("a"), sym.Symbol("b"), sym.Symbol("c")

expected_answer = (9 * a ** 2 * b * c ** 4) ** (sym.S(1) / 2) / (6 * a * b ** (sym.S(3) / 2) * c)
feedback_text = f"""Your answer is not correct.

The expected answer is {expected_answer}."""
assert sym.simplify(q1_a_answer - expected_answer) == 0, feedback_text
```

b. \\((2 ^ {\frac{1}{2}} + 2) ^ 2 - 2 ^ {\frac{5}{2}}\\)


```python
### BEGIN SOLUTION
(sym.S(2) ** (sym.S(1) / 2) + 2) ** 2 - 2 ** (sym.S(5) / 2)
### END SOLUTION
```




$\displaystyle - 4 \sqrt{2} + \left(\sqrt{2} + 2\right)^{2}$




```python
q1_b_answer = _
feedback_text = """Your output is not a symbolic expression.

You are expected to use sympy for this question.
"""
try:
    assert q1_b_answer.expand(), feedback_text
except AttributeError:
    assert False, feedback_text
```


```python
x = sym.Symbol("x")
expected_answer = 6
feedback_text = f"""Your answer is not correct.

The expected answer is {expected_answer}."""
assert sym.expand(q1_b_answer - expected_answer) == 0, feedback_text
```

3. \\((\frac{1}{8}) ^ {\frac{4}{3}}\\)


```python
### BEGIN SOLUTION
(sym.S(1) / 8) ** (sym.S(4) / 3)
### END SOLUTION
```




$\displaystyle \frac{1}{16}$




```python
q1_c_answer = _
feedback_text = """Your output is not a symbolic expression.

You are expected to use sympy for this question.
"""
try:
    assert q1_c_answer.expand(), feedback_text
except AttributeError:
    assert False, feedback_text
```


```python
x = sym.Symbol("x")
expected_answer = sym.S(1) / 16
feedback_text = f"""Your answer is not correct.

The expected answer is {expected_answer}."""
assert q1_c_answer == expected_answer, feedback_text
```

### Question 2

Write a function `expand` that takes a given mathematical expression and returns the expanded expression.


```python
def expand(expression):
    ### BEGIN SOLUTION
    """
    Take a symbolic expression and expands it.
    """
    return sym.expand(expression)
    ### END SOLUTION
```


```python
feedback_text = """You did not include a docstring. This is important to help document your code.


It is done  using triple quotation marks. For example:

def get_remainder(m, n):
    \"\"\"
    This function returns the remainder of m when dividing by n
    \"\"\"
    …

Using that it's possible to access the docstring,
one way to do this is to type: `get_remainder?`
(which only works in Jupyter) or help(get_remainder).

We can also comment code using `#` but this is completely
ignored by Python so cannot be accessed in the same way.

"""
try:
    assert  expand.__doc__ is not None, feedback_text
except NameError:
    assert False, "You did not create a function called `expand`"
```


```python
expression = x * (x  + 1)
assert expand(expression) == x ** 2 + x, f"Your function failed for {expression}"
expression = x * (x  + 1) - x ** 2
assert expand(expression) == x, f"Your function failed for {expression}"
expression = x ** 2 + 1
assert expand(expression) == x ** 2 + 1, f"Your function failed for {expression}"
```

### Question 3

The matrix \\(D\\) is given by \\(D = \begin{pmatrix} 1& 2 & a\\ 3 & 1 & 0\\ 1 & 1 & 1\end{pmatrix}\\) where \\(a\ne 2\\).

a. Create a variable `D` which has value the matrix \\(D\\).


```python
### BEGIN SOLUTION
a = sym.Symbol("a")
D = sym.Matrix([[1, 2, a], [3, 1, 0], [1, 1, 1]])
### END SOLUTION
```


```python
expected_D = sym.Matrix([[1, 2, a], [3, 1, 0], [1, 1, 1]])
feedback_text = f"The expected value of `D` is {expected_D}."
try:
    assert sym.simplify(sym.expand(sym.simplify(D) - expected_D)) == sym.Matrix([[0, 0, 0] for _ in range(3)]), feedback_text
except NameError:
    assert False, "You did not create a variable `D`"
```

b. Create a variable `D_inv` with value the inverse of \\(D\\).


```python
### BEGIN SOLUTION
D_inv = D.inv()
### END SOLUTION
```


```python
expected_D_inv = expected_D.inv()
feedback_text = f"The expected value of `D_inv` is {expected_D_inv}."
assert sym.simplify(sym.expand(sym.simplify(D_inv) - expected_D_inv)) == sym.Matrix([[0, 0, 0] for _ in range(3)]), feedback_text
```

c. Using `D_inv` **output** the solution of the following system of equations:

\\[
\begin{array}{r}
    x + 2y + 4z = 3\\
    3x + y = 4\\
    x + y + z = 1\\
\end{array}
\\]


```python
### BEGIN SOLUTION
b = sym.Matrix([[3], [4], [1]])
sym.simplify(D.inv() @ b).subs({a: 4})
### END SOLUTION
```




$\displaystyle \left[\begin{matrix}\frac{7}{3}\\-3\\\frac{5}{3}\end{matrix}\right]$




```python
answer_q3_c = _
expected_b = sym.Matrix([[3], [4], [1]])
expected_answer = sym.simplify(expected_D_inv @ expected_b).subs({a: 4})
feedback_text = f"The expected solution is {expected_answer}."
assert sym.expand(expected_answer - answer_q3_c) == sym.Matrix([[0], [0], [0]]), feedback_text
```

### Question 4

During a game of frisbee between a handler and their dog the handler chooses to randomly select if they throw using a backhand or a forehand: 25% of the time they will throw a backhand.

Because of the way their dog chooses to approach a flying frisbee they catch it with the following probabilities:

- 80% of the time when it is thrown using a backhand
- 90% of the time when it is thrown using a forehand

a. Write a function `sample_experiment()` that simulates a given throw and returns the throw type (as a string with value `"backhand"` or `"forehand"`) and whether it was caught (as a boolean: either `True` or `False`).


```python
import random


def sample_experiment():
    """
    Returns the throw type and whether it was caught
    """
    ### BEGIN SOLUTION
    if random.random() < .25:
        throw = "backhand"
        probability_of_catch = .8
    else:
        throw = "forehand"
        probability_of_catch = .9
                
    caught = random.random() < probability_of_catch
    ### END SOLUTION
    return throw, caught
```


```python
feedback_text = """You did not include a docstring. This is important to help document your code.


It is done  using triple quotation marks. For example:

def get_remainder(m, n):
    \"\"\"
    This function returns the remainder of m when dividing by n
    \"\"\"
    …

Using that it's possible to access the docstring,
one way to do this is to type: `get_remainder?`
(which only works in Jupyter) or help(get_remainder).

We can also comment code using `#` but this is completely
ignored by Python so cannot be accessed in the same way.

"""
try:
    assert  sample_experiment.__doc__ is not None, feedback_text
except NameError:
    assert False, "You did not create a variable called `sample_experiment`"
```


```python
try:
    random.seed(0)
    throw, caught = sample_experiment()
    assert throw in ["forehand", "backhand"], "Your function did not give a throw with seed=0"
    assert caught in [True, False], "Your function did not give a valid coin with seed=0"

    random.seed(1)
    throw, caught = sample_experiment()
    assert throw in ["forehand", "backhand"], "Your function did not give a valid throw with seed=0"
    assert caught in [True, False], "Your function did not give a valid coin with seed=0"
except NameError:
    assert False, "You did not create a function called `sample_experiment` or there is an error in your function."
```


```python
repetitions = 10_000
random.seed(0)
feedback_text = f"""Your function did not give a selection of forehand throws within acceptable error bounds.

Out of {repetitions} repetitions you got less than 5500 or more than 9500 forehand throw.
"""
throws = [sample_experiment()[0] for _ in range(repetitions)]
assert 5_500 <= throws.count("forehand") <= 9_500, feedback_text
```

b. Using 1,000,000 samples create a variable `probability_of_catch` which has value an estimate for the probability of the frisbee being caught.


```python
### BEGIN SOLUTION
number_of_repetitions = 1_000_000
random.seed(0)
samples = [sample_experiment() for repetition in range(number_of_repetitions)]
probability_of_catch = sum(catch is True for throw, catch in samples) / number_of_repetitions
### END SOLUTION
```


```python
assert type(probability_of_catch) is float, "You did not return a float"
```


```python
expected_answer = sym.S(1) / (4) * sym.S(8) / 10 + sym.S(3) / (4) * sym.S(9) / 10
feedback_text = f"""The expected value is: {expected_answer}

Your value was not within 20% of the expected answer.
"""
assert expected_answer * .8 <= probability_of_catch <= expected_answer * 1.2, feedback_text
```

c. Using the above, create a variable `probability_of_forehand_given_drop` which has value an estimate for the probability of the frisbee being thrown with a forehand given that it was not caught.


```python
### BEGIN SOLUTION
samples_with_drop = [(throw, catch) for throw, catch in samples if catch is False]
number_of_drops = len(samples_with_drop)
probability_of_forehand_given_drop = sum(throw == "forehand" for throw, catch in samples_with_drop) / number_of_drops
### END SOLUTION
```


```python
assert type(probability_of_forehand_given_drop) is float, "You did not return a float"
```


```python
expected_answer = (sym.S(1) / (10) * sym.S(75) / 100) / (1 - (sym.S(1) / (4) * sym.S(8) / 10 + sym.S(3) / (4) * sym.S(9) / 10))
feedback_text = f"""The expected value is: {expected_answer}

Your value was not within 20% of the expected answer.
"""
assert expected_answer * .8 <= probability_of_forehand_given_drop <= expected_answer * 1.2, feedback_text
```

fd08acd332ffd33ee59b69ebc98707cb6a3a25dd
ipynb
Jupyter Notebook
assets/assessment/mock/main.ipynb
drvinceknight/cfm
06977f5c1ba37590b17a8d2a22f57e3575875b3b
MIT
2016-08-25T01:05:20.000Z
2021-07-04T16:17:06.000Z
assets/assessment/mock/main.ipynb
drvinceknight/cfm
06977f5c1ba37590b17a8d2a22f57e3575875b3b
MIT
2016-08-24T20:08:15.000Z
2021-12-19T23:26:14.000Z
assets/assessment/mock/main.ipynb
drvinceknight/cfm
06977f5c1ba37590b17a8d2a22f57e3575875b3b
MIT
2016-09-22T12:36:03.000Z
2019-03-13T13:21:30.000Z
Qwen/Qwen-72B
1. YES
2. YES