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

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:...
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) =
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            ...
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.
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?
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;}
Write a python function that will return the total length of line that passes through any...
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 the first point, otherwise start adding distances...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT