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.
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) =
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 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.
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 function in python that takes in an integer n and computes the left hand...
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1]. Hint: compute the error from the true answer
Suppose samples of size 100 are drawn randomly from a population of size 1000 and the...
Suppose samples of size 100 are drawn randomly from a population of size 1000 and the population has a mean of 20 and a standard deviation of 5. What is the probability of observing a sample mean equal to or greater than 21?
Python: How would I write a function that takes a directory and a size in bytes,...
Python: How would I write a function that takes a directory and a size in bytes, and returns a list of files in the directory or below that are larger than the size. For example, I can use this function to look for files larger than 1 Meg below my Home directory.
Write the body of the function getFileSize to return the size of an opened file. #include...
Write the body of the function getFileSize to return the size of an opened file. #include <stdio.h> #include <stdlib.h> #include <unistd.h> long getFileSize (FILE * f) { } int main () { FILE * file = fopen (“myfile.txt”,”r”); long fsize = getFileSize(file); printf ("File Size : %lu\n",fsize); return 0;}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT