Question

In: Computer Science

(Python) a) Using the the code below write a function that takes the list xs as...

(Python)

a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing.

Hint: list slicing capabilities can be useful in implementing this function.

from random import random as rnd
ns = 100
nr = 10
xs = [None] * ns

def nsums(ns, nr):
for n in range(ns):
xsum = 0.
for i in range(nr):
xd = rnd() - 0.5
xsum += xd
xs[n] = xsum / nr # mean of the nr random numbers
return (xs)

b) Keeping the number of samples ns fixed, generate the means and standard deviations lists using the function in part a for several nrs values - for example, for nrs = 10, 20, 200. Then rescale the means by dividing each mean in the list by its error, si / √nrs, where si is the standard deviation of the i-th chunk.

Solutions

Expert Solution

Python code follows:

#Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks 
#(where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) 
#of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing.

import statistics
import math

from random import random as rnd
ns = 100
nr = 10
xs = [None] * ns

# Returns a list of length ns with each item as the average of nr random numbers
def nsums(ns, nr):
    for n in range(ns):
        xsum = 0.
        for i in range(nr):
            xd = rnd() - 0.5
            xsum += xd
        xs[n] = xsum / nr # mean of the nr random numbers
    return (xs)
    
#PART A solution follows
def chunker (xs, nrs):
    ns = len (xs)
    splitlst = [xs [i:i+nrs] for i in range(0, ns, nrs)]
    meanlst = [statistics.mean (l) for l in splitlst]
    stdevlst = [statistics.stdev (l) for l in splitlst]
    return meanlst, stdevlst

#Part B, rescaler function
def rescaler (meanlist, stdevlist, nrs):
    sqrtnrs = math.sqrt (nrs)
    l = [meanlist[i] / stdevlist [i] * sqrtnrs for i in range (len (meanlist))]
    return l
    
# Part B, calling with different nrs values 10, 20, 30
l = nsums (ns, nr)
m, s = chunker (l, 10)
print ("Means 10:", m,"\nStandard deviations 10:", s)
m, s = chunker (l, 20)
print ("Means 20:", m,"\nStandard deviations 20:", s)
m, s = chunker (l, 30)
print ("Means 30:", m,"\nStandard deviations 30:", s)

print ("\n\nRescaled means list:")
print (rescaler (m, s, 30))

Related Solutions

USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
In python I want to create a function that takes in a linked list. Using recursion...
In python I want to create a function that takes in a linked list. Using recursion only, I want to check if the linked list is sorted. How do i do this?
All functions are written in python Write a function cube_evens_lc(values) that takes as input a list...
All functions are written in python Write a function cube_evens_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the even numbers in values (i.e., the even numbers raised to the third power). For example: >>> cube_evens_lc([2, 5, 6, 4, 1]) result: [8, 216, 64] This version of the function may not use recursion. Write a function cube_evens_rec(values) that takes as input a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT