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...
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...
Using the code below from “LStack.h” file, write the code for a main program that takes...
Using the code below from “LStack.h” file, write the code for a main program that takes as input an arithmetic expression. The program outputs whether the expression contains matching grouping symbols. For example, the arithmetic expressions { 25 + ( 3 – 6 ) * 8 } and 7 + 8 * 2 contains matching grouping symbols. However, the expression 5 + { ( 13 + 7 ) / 8 - 2 * 9 does not contain matching grouping symbols....
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s)...
USE PYTHON-LIST Q1 - You need to keep track of book titles. Write code using function(s) that will allow the user to do the following. 1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. Note: if you wish to display the booklist as a vertical number booklist , that is ok (that is also a hint) 4. Insert book titles anywhere in the list. 5. Clear...
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT