Question: Please help me with this question in Python. #=========================================================== import numpy as np def stat_comp(n=100, mean=1000, deviation=0.32): ''' fill in the gap to fulfill the

Please help me with this question in Python.

Please help me with this question in Python. #=========================================================== import numpy as

#===========================================================

import numpy as np

def stat_comp(n=100, mean=1000, deviation=0.32): ''' fill in the gap to fulfill the above listed tasks, return the answers in a tuple (x, norm1, norm2, sd11, sd12, sd21, sd22) '''

return x, norm1, norm2, sd11, sd12, sd21, sd22

#=========================================================== def verify(x, norm1, norm2, sd11, sd12, sd21, sd22, mean, devia):

n = len(x) if abs(mean - np.sum(x)) > np.sqrt(n)*devia: print('Possible error in construction of x') return False if norm1**2 - norm2**2

tol=1e-8 s = 0.0 for i in range(n): for j in range(i+1, n): s += abs(x[i]*x[j]) #end for j for i diff = abs(norm1**2 - norm2**2 - 2*s) if difform1**2 > tol: print('2nd possible error in norms: relative_diff_norms={:.8e}'.format(difform1**2)) return False

if abs(sd11 - sd12) > tol: print('Possible error in sd11 & sd12') return False if abs(sd21 - sd22) > tol: print('Possible error in sd21 & sd22') return False

if abs(sd11 - sd21) + abs(sd12 - sd22) > tol*10: print('Possible error in sd11 to sd21') return False #this False might be the least reliable one return True

#=========================================================== if __name__=='__main__':

DEBUG=True

n = 1000 #try the default mean and deviation mean=1000; deviation=0.32 x, norm1, norm2, sd11, sd12, sd21, sd22 = stat_comp(n) if verify(x, norm1, norm2, sd11, sd12, sd21, sd22, mean, deviation): print('Test passed ') else: print('***** Need further debug. Possible errors in code***** ')

ipass=0; ifail=0 for mean in range(0, 3000, 200): deviation = 0.1+ np.sqrt(mean/10) if DEBUG: print('mean = {}, deviation={}'.format(mean, deviation))

x, norm1, norm2, sd11, sd12, sd21, sd22 = stat_comp(n, mean, deviation) if verify(x, norm1, norm2, sd11, sd12, sd21, sd22, mean, deviation): print('Test passed '); ipass+=1 else: ifail+=1 print('***** Need further debug. Possible errors in code***** ')

print(" Total {} test: Passed {}".format(ipass+ifail, ipass)) if ifail==0: print(" Congratulations, you passed all tests for Problem 7.")

Prob. 7 Write a function for the following tasks: a) Use numpy to generate a random array x = (,#2, . .. ,xn) of length n, where x's are drawn from normal distribution with mean and standard deviation . (This should be easily done using numpy.) b) Compute the 1-norm and 2-norm of the r generated in 7a) in a vectorized manner, i.e., without using loop. As a reminder, for (21, 22,... ,xn), the 1-norm and 2-norm are defined, respectively, as 7L Using the same r generated in 7a), compute IL where x=- Use both a loop (save the value in sd11) and a vectorized operation (save the value in sd12) d) Using the same x generated in 7a), compute IL Use both a loop (save the value in sd21) and a vectorized operation (save the value in sd22) The interface of your code is the following, where the mean and deviation are made keyword arguments with given default values: l def stat.comp (n, mean 1000, deviation-0.32) fill in the gap to fulfill the above listed tasks return the ans wers in a tuple (x,norml, norm2, sd11, sd12, sd21, sd22) Prob. 7 Write a function for the following tasks: a) Use numpy to generate a random array x = (,#2, . .. ,xn) of length n, where x's are drawn from normal distribution with mean and standard deviation . (This should be easily done using numpy.) b) Compute the 1-norm and 2-norm of the r generated in 7a) in a vectorized manner, i.e., without using loop. As a reminder, for (21, 22,... ,xn), the 1-norm and 2-norm are defined, respectively, as 7L Using the same r generated in 7a), compute IL where x=- Use both a loop (save the value in sd11) and a vectorized operation (save the value in sd12) d) Using the same x generated in 7a), compute IL Use both a loop (save the value in sd21) and a vectorized operation (save the value in sd22) The interface of your code is the following, where the mean and deviation are made keyword arguments with given default values: l def stat.comp (n, mean 1000, deviation-0.32) fill in the gap to fulfill the above listed tasks return the ans wers in a tuple (x,norml, norm2, sd11, sd12, sd21, sd22)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!