HI can you help with question A and question B, python files named Q5 and for import stats tma02_stats as reference for

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

HI can you help with question A and question B, python files named Q5 and for import stats tma02_stats as reference for

Post by answerhappygod »

HI can you help with question A and question B, python filesnamed Q5 and for importstats tma02_stats as reference forquestion.
here is given question:
Hi Can You Help With Question A And Question B Python Files Named Q5 And For Import Stats Tma02 Stats As Reference For 1
Hi Can You Help With Question A And Question B Python Files Named Q5 And For Import Stats Tma02 Stats As Reference For 1 (73.89 KiB) Viewed 41 times
PYthon file Q5:
""" TM112 22D TMA02 Q5"""
from tma02_stats import medianfrom tma02_stats import meanfrom tma02_stats import corr_coef
""" You can use one of two approaches:1) add suitable code below and then run this file2) run this file first then do the calculation inthe Python interactive shell."""
# Estimated risk of infection following vaccination bysingle year of age, UK# 1 December 2020 to 31 May 2021
# Office for National Statistics – Coronavirus(COVID-19) Infection Survey
age = [16, 17, 18, 19, 20, 21, 22, 23, 24, 35, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 95]
# Estimated risk of infection followingvaccination
risk = [1.93, 1.88, 1.82, 1.77, 1.73, 1.68, 1.63, 1.59,1.54, 1.5, 1.46, 1.42, 1.38, 1.35, 1.31, 1.28,1.25, 1.23, 1.2, 1.18, 1.16, 1.15, 1.13, 1.12, 1.12, 1.11,1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.11, 1.11, 1.11, 1.11, 1.11, 1.1,1.1, 1.09, 1.07, 1.06, 1.04, 1.02, 1, 0.98, 0.96, 0.94, 0.92,0.91, 0.89, 0.88, 0.87, 0.86, 0.85, 0.85, 0.84, 0.84, 0.83,0.83, 0.82, 0.82, 0.81, 0.81, 0.81, 0.8, 0.8, 0.79, 0.79,0.79]
PYTHON file tma02_stats:
"" TM112 Library code for TMA02 Q5.08/10/2017"""import math
def median(alist): """ Calculates the median of a list of numbers.The list must not be empty."""
number_of_values = len(alist) sorted_list = sorted(alist)
# Two cases, depending on whether the number ofvalues is odd or even. quotient = number_of_values // 2 remainder = number_of_values % 2 if (remainder == 1): result = sorted_list[quotient] else: result = (sorted_list[quotient - 1] +sorted_list[quotient]) / 2 return resultdef test_median(): assert median([2]) == 2 assert median([4, 3]) == 3.5 assert median([3, 1, 8, 4, 7, 6, 4, 2, 5, 9]) ==4.5 assert median([7, 2, 6, 2, 5, 3, 1, 0, 8, 6, 6, 4,9]) == 5
# Unit testtest_median()
def mean(list): """Return mean of list""" sum = 0 count = 0 for item in list: sum = sum + item count = count + 1 return sum / count
def test_mean(): list = [1, 2, 3, 4, 5] assert(mean(list) == 3)# Unit testtest_mean()
def corr_coef(list_x, list_y): """ Return correlation between values in list_x andlist_y.
Lists must be of equal length. """ x_bar = mean(list_x) y_bar = mean(list_y) sxy = 0 sxx = 0 syy = 0 for index in range(len(list_x)): x = list_x[index] y = list_y[index] sxy = sxy + (x - x_bar) * (y -y_bar) sxx = sxx + (x - x_bar) * (x -x_bar) syy = syy + (y - y_bar) * (y -y_bar) return sxy / math.sqrt(sxx * syy)
def test_corr_coef(): # Data from M140 Unit 9 Example 5 list1 = [78.9, 75.8, 77.3, 74.2, 78.1, 72.8, 77.6,77.9] list2 = [56.7, 53.1, 56.1, 55.9, 54.1, 48.6, 59.4,54.0] assert round(corr_coef(list1, list2), 2) ==0.64# Unit testtest_corr_coef()
Estimated risk (as %) of infection fo 1.50 1.00 0.50 0.00 0 10 20 40 50 Age in years (4 marks) Figure 2 Estimated risk of infection following vaccination by single year of age, UK. (December 2020 to May 2021). Note that single year of age (SYOA) demographics are single year of age population (e.g. age 1, age 2, etc.) 90 a. 95.py imports the Python function mean() you used in Block 2 Part 5 to calculate the median of a list of numbers. Use this function to find the mean of age. In your Solution document give the mean. Also provide the Python code you used for calling the mean() function and explain how you executed it. b. The Python file q5.py imports the function corr_coef () which you used in Section 5.4 of Block 2 Part 5 to calculate the correlation coefficient between two lists. i. Use this function to calculate the correlation coefficient between age and risk. In your Solution document, provide the resulting figure rounded manually to two decimal places. Solutions which use Python to round the result will not gain credit. Also provide the Python code you used for calling the corr_coef() function and explain how you executed it. ii. Give the level of correlation, following Table 5.4 (Page 290) in Block 2 Part 5. iii. Briefly discuss whether any effect you have found is likely to be causal. Give one argument for it being causal and one against.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply