Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem 8 : Weighted Means In this problem, we will create a function named mean ( ) that can be used to calculate either standard
Problem : Weighted Means
In this problem, we will create a function named mean that can be used to calculate either standard means or weighted
means. The function will have two parameters, x and w The parameter x is expected to be a numerical list, and w is
expected to be either a numerical list or to have the value None. If wNone, then the function should simply return the
mean or average of the elements in x If w is a list, then the function should return the weighted mean of the elements in
x using the elements of w as weights.
If the elements of x are denoted by and the elements of w are denoted by then the weighted
mean is calculated as follows:
As an example that you are not being asked to include in your notebook if:
list
list
Then weightedmeanlist list should return:
Define a function named mean with two parameters named x and w The parameter x is expected to be a numerical
list, and w is expected to be either a numerical list or to have the value None. The default value of w should be None. The
function should behave as follows:
If wNone, then the function should return the standard mean average of the values in x You may use the
functions sum and len to calculate this.
If w is a list, then the function should return the weighted mean of the values in x using the values from w as
weights.
In any case, this function should return only a single value.
The function should not print anything. It should not create any new lists, and should alter either of the lists provided
to it as arguments.
We will now test the mean function. Create a new code cell to perform the steps below.
Create the list shown below:
values
weights
Use two calls to the function mean to calculate the standard mean of values and then the weighted mean of
values, using the list weights to supply the weights. Print the results with messages as shown below.
Standard Mean: xxxx
Weighted Mean: xxxx
The call to mean that is used to calculate the standard mean is expected to involve only one argument
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started