Question

Python An average is a number that shows a middle or normal value for a set...

Python

An average is a number that shows a middle or normal value for a set of data. It is calculated by adding all the data points then dividing the total by the number of data points. A running average is an average that continually changes as more data points are collected.

In plain language, if there are n data points, we sum over those data points and divide by n. As soon as a new data point is collected, the number of data points now become n+1 and we again take the sum of these data points and divide by n+1. This turns out to be an inefficient way as we keep on summing over all the data points as soon as a new data point is added.

A more efficient way to calculate the new average is using the below formula:

new_average = (old_average*n + new_data_point)/(n+1)

Write the recursive function calc running average(), which takes three arguments: 1. data points: a list consisting of numbers (data points) collected over time. 2. data points till now: an integer which determines till what data point the current running average is being calculated for. This is the same as n in the above mentioned formula. 3. old average: a floating point number which determines the running average till the previous data point (i.e., for data points 0 through data points till now-1).

The function takes a list of data points, an index to keep track of which data points to consider and a variable to keep track of the old average.

The following rules apply: 1. For a list of data points of length n, there are n running averages. 2. After calculating average over some i data points and storing in the result list, you need to propagate this as the variable old average to calculate average over i+1 data points. 3. It is guaranteed that at there is at least 1 data point.

The function returns a list of floating point numbers, of same length as the list data points where the value at index i represents the running average over i+1 data points.

Remember: A list starts from index 0 but we count data points from index 1.

Clearly we need to use the same formula over and over again, each time incrementing the value of n and making the new average calculated as the old average for the next application of the formula.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

First of all let me explain you the approach, as you know recursion works with some terminating condition ,so here we will terminate our recursion when we reach the last element.

see for example --

if we have the data_points as [1,2,3]

When we reach the last item 3 ,it will return a list containing average 2.0 (the average from beginning to that point) , then we come back to 2 from the recursion ,we shall append the average till 2 from the beginning (i.e 1.5) to the front of the list ,the new list becomes [1.5,2.0] , then again it comes back to 1 and here we append average till 1 from the beginning (i.e 1) to the front of the list ,the new list becomes [1,1.5,2.0].

Code --

def func(data_points,ind,old_av):
new_av=(old_av*ind+data_points[ind])/(ind+1)
if(ind==(len(data_points)-1)):
return [new_av]
else:
to_ret=func(data_points,ind+1,new_av)
to_ret=[new_av]+to_ret
return to_ret

The snippet is attached so you do not have any problem with indentation

Variables--

data_points--the list containing data points

ind--the current index to be processed(or we can say n i.e number of data points processed)

old_av-- the old average

new_av --the new average

to_ret-- the list that is to be returned

Code explanation--

line 1-- it is the function definition taking 3 parameters as mentioned data_points,ind,old_av

line 2--we calculate the new average using the formula given

line 3,4-- terminating condition ,if it is the last element return the list with single element containing average till last item

line 5,6,7-- if we have not reached the terminating condition we call the function on next data point ,here ind parameter is incremented to ind+1 and in place of old_av ,we pass new_av(because this will be the old average for the next data point) .Next we append the current running average to the front of the list returned (like we appended 1 to [1.5,2.0] in the explanation) and return this list.

note-- when calling the function , set data_points to your list ,ind=0 ,old_av=0

For example----

  func([1,2,3],0,0)

Add a comment
Know the answer?
Add Answer to:
Python An average is a number that shows a middle or normal value for a set...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a PYTHON program that will approximate the value of π. You can do this by...

    Write a PYTHON program that will approximate the value of π. You can do this by computing π to be the ratio of the area of a circle to the area of the square that bounds that circle. Assume a circle of radius 0.5 enclosed by a 1x1 square. The area of the circle then is πr^2=π/4, since r=0.5=1/2 and the area of the square is 1. To approximate the ratio, take a large number of uniformly distributed random points....

  • Python Problem: Variance Explanation: Problem 1: Computing variance For this problem, you will write a function...

    Python Problem: Variance Explanation: Problem 1: Computing variance For this problem, you will write a function variance that takes a list whose elements are numbers (floats or ints), and returns their variance, a single number. (If you don't remember how to compute variance, check the lecture notebook; it's one of the intermediate steps in computing standard deviation.) You should not use NumPy or any other Python libraries for this problem. For now, worry only about correctness, not efficiency. Passing an...

  • DO #1 Real GDP is intended to measure the real value of goods and services produced,...

    DO #1 Real GDP is intended to measure the real value of goods and services produced, not the stock of money, or the balance in your bank account, or whether you are in debt or not. To do this, we may use information on total expenditures - measured in dollars - but the purpose is always to recover an index of real output. For this homework, you need the following information to calculate real GDP over several periods. First, you...

  • DO#3 Real GDP is intended to measure the real value of goods and services produced, not...

    DO#3 Real GDP is intended to measure the real value of goods and services produced, not the stock of money, or the balance in your bank account, or whether you are in debt or not. To do this, we may use information on total expenditures - measured in dollars - but the purpose is always to recover an index of real output. For this homework, you need the following information to calculate real GDP over several periods. First, you know...

  • DO #2 Real GDP is intended to measure the real value of goods and services produced,...

    DO #2 Real GDP is intended to measure the real value of goods and services produced, not the stock of money, or the balance in your bank account, or whether you are in debt or not. To do this, we may use information on total expenditures - measured in dollars - but the purpose is always to recover an index of real output. For this homework, you need the following information to calculate real GDP over several periods. First, you...

  • Create a NOTEPAD or PDF file that restates the problem in your own words, specifies what input is needed, w...

    Create a NOTEPAD or PDF file that restates the problem in your own words, specifies what input is needed, what output is expected, the step by step process (algorithm) to get the output from the input, and test data (input for which you know the expected output) for each of the 3 problems given below. You should not write any actual C++ code. Make sure the problem statement is in your own words and is descriptive enough so someone not...

  • Coding for Python – don’t need it to be complex: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is locate...

    Coding for Python – don’t need it to be complex: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. This type of problem is common in many health disciplines. For example, identifying treatment pathways. The goal is to detect whether a certain pattern appears in the data series. In the following example, the pattern to be...

  • 6.2 all parts please Problems Consider an array of nine drives in which X0 through X7...

    6.2 all parts please Problems Consider an array of nine drives in which X0 through X7 contain data an parity disk. Provide an equation that describes the relationship between data and par- ity bits. 6.1 d X8 is the 62 Consider a disk with N tracks numbered from 0 to (N 1) and assume that requested sectors are distributed randomly and evenly over the disk. We want to calculate the average number of tracks traversed by a seek. a. First,...

  • using python Type: pip3 install matplotlib This installs the matplotlib and you should be able to...

    using python Type: pip3 install matplotlib This installs the matplotlib and you should be able to run the code listed below in line_graph_example.py. The original x_coords list had n+1 entries where n = 4. Make a new x_coords list have 2*n +1 elements [0, 1, ..., 2*n] and augment the y_coords by adding (using insert) a new value between any two old elements. The new value should be the (average of neighbors +1). Then plot the new data. Line graph...

  • Python please help! Thanks you Write a code to get an unlimited number of grades from...

    Python please help! Thanks you Write a code to get an unlimited number of grades from the user (the user can press enter to finish the grades input, or use a sentinel, for example-1), and then calculate the GPA of all the grades and displays the GPA To do this you might need some help. Try to follow the following process (and check your progress by printing different values to make sure they are as they supposed to be): 1-...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT