Question

In: Computer Science

Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled...

Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled from the distribution P.

* Sample run :

print(average_sample_median(10,[0.2,0.1,0.15,0.15,0.2,0.2]))

print(average_sample_median(10,[0.3,0.4,0.3]))

print(average_sample_median(10,P=[0.99,0.01])


* Expected Output :
3.7855
2.004
1

Solutions

Expert Solution

Here Iam providing the answer.Hope this helps you.If you have any doubts comment me i will clarify you.Please Upvote if you like my work.Hope you will like this.Thank you :- )

We do this problem in 2 ways.Iam providing 2 ways use what you want :- )

1st Way Using Numpy module:

import numpy as np
def average_sample_median(n, P):  
    return np.median( np.random.choice ( np.arange (1, len(P) + 1 ), n, p = P ))
print(average_sample_median(10,[0.2,0.1,0.15,0.15,0.2,0.2]))
print(average_sample_median(10,[0.3,0.4,0.3]))
print(average_sample_median(10,[0.99,0.01]))

2nd Way using random module:-

import random
def average_sample_median(n,P):
    times = 1000
    median_sum = 0
    for i in range(times):
        samples = random.choices(P, k=n)
        sum_p = 0
        for j, p in enumerate(samples):
            sum_p += p
            if sum_p == 0.5:
                median_sum += j + 1.5
                break
            if sum_p > 0.5:
                median_sum += j + 1
                break     
    avg_median = median_sum / times
    return avg_median
print(average_sample_median(10,[0.2,0.1,0.15,0.15,0.2,0.2]))
print(average_sample_median(10,[0.3,0.4,0.3]))
print(average_sample_median(10,[0.99,0.01]))

Output:-


Related Solutions

Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie Germain prime that is greater or equal to n.
In Python Complete the function powersOf5(). Have the function take a parameter n and return a...
In Python Complete the function powersOf5(). Have the function take a parameter n and return a list of the first n powers of 5. One way to calculate powers of 5 is by starting with 1 and then multiplying the previous number by 5. Examples: powersOf5( 1 ) returns [1] powersOf5( 2 ) returns [1, 5] powersOf5( 3 ) returns [1, 5, 25] powersOf5( 6 ) returns [1, 5, 25, 125, 625, 3125] powersOf5( 10 ) returns [1, 5, 25,...
Random samples of size n = 90 were selected from a binomial population with p =...
Random samples of size n = 90 were selected from a binomial population with p = 0.8. Use the normal distribution to approximate the following probability. (Round your answer to four decimal places.) P(p̂ > 0.78) =
Random samples of size n = 60 were selected from a binomial population with p =...
Random samples of size n = 60 were selected from a binomial population with p = 0.2. Use the normal distribution to approximate the following probabilities. (Round your answers to four decimal places.) (a)     P(p̂ ≤ 0.22) = (b)     P(0.18 ≤ p̂ ≤ 0.22) =
Python: Write a recursive function that accepts an integer argument, n. The function should display n...
Python: Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks. Test the function.
This is a python question. Write a function mult_table(n) that consumes a natural number n and...
This is a python question. Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Use accumulative recursion. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof (listof Nat))    Examples:...
Write a Python function that will return the index of an element in a list.1- The...
Write a Python function that will return the index of an element in a list.1- The function will receive a list of at least 5 numbers as a single argument when the function is called. 2. The function will ask the user to input a number and will find and return the index of that number in the list.3. If the number is not an element of the list, the function returns the string "unknown."
To experimentally verify the central limit theorem, take 1000 random samples of size n=33 from a...
To experimentally verify the central limit theorem, take 1000 random samples of size n=33 from a population that is distributed exponentially with λ=3. To this end, you must show The histogram of the distribution is approximately normal. Its expected value is μ Its variance is sigma^2/33 Please complete in Rstudio The center thickness (in mils) of a sample of twenty-five contact lenses are given below 0.3978             0.4019             0.4031             0.4044             0.3984             0.3972             0.3981 0.3947             0.4012             0.4043             0.4051             0.4016             0.3994             0.3999 0.4062            ...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total...
PYTHON! Exercise 3 - Total Line length Write a python function that will return the total length of line that passes through any number of provided points ( (x,y) ). The points should be passed as individual tuples or lists. The function should also have a parameter (True or False) to indicate whether the line should start from the origin, and that parameter should default to False. If True, the returned value should include the distance from the origin to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT