Question

In: Computer Science

Write the python code that generates a normal sample with given μ and σ, and the...

Write the python code that generates a normal sample with given μ and σ, and the code that calculates m (sample mean) and s (sample standard deviation) from the sample.

Solutions

Expert Solution

Code for generate a normal sample:

import numpy as np

def main():
    #get input for mean
    mean = int(input("Please enter mean: "))
    #get input for standard deviation
    stdev = int(input("Please enter standard deviation: "))
    #generate samples with mean and standard deviation
    samples = generateSamples(mean, stdev, 100000)

    print('Normal sample: ', samples)
    print('Mean:', samples.mean())
    print('Standard deviation:', samples.std())
    return

def generateSamples(mean, stdev, n=1):
    # Calculate mu and sigma of normal distribution
    meanSqr = mean ** 2
    phi = (stdev ** 2 + meanSqr) ** 0.5
    sigma = (np.log(phi ** 2 / meanSqr)) ** 0.5
    mu = np.log(meanSqr / phi)
    # Generate normal population
    normal_samples = np.random.lognormal(mu, sigma , n)
    # Convert single sample (if n=1) to a float, otherwise leave as array
    normal_samples = normal_samples[0] if len(normal_samples) == 1 else normal_samples
    return normal_samples

#Call method
main()

Code Screenshot:

Sample output:


Related Solutions

Write the python code that generates a normal sample with given μ and σ, and the...
Write the python code that generates a normal sample with given μ and σ, and the code that calculates m and s from the sample. Do the same using the Bayes’ estimator assuming a prior distribution for μ.
Given a normal distribution with μ=103 and σ=25​, and given you select a sample of n=25​,...
Given a normal distribution with μ=103 and σ=25​, and given you select a sample of n=25​, complete parts​ (a) through​ (d). What is the probability that X is between 91 and 93.5​? ​P(91<X<93.5​)=
1. Given a normal distribution with μ=102 and σ=25, if you select a sample of n=12,...
1. Given a normal distribution with μ=102 and σ=25, if you select a sample of n=12, what is the probability that ?̅ is a. less than 90 ? b. between 90 and 92.5 ? c. above 103.6 ? 2. Given a normal distribution with μ=101 and σ=15, if you select a sample of n=9, what is the probability that ?̅ is a. less than 95 ? b. between 90 and 92.5 ? c. above 101.8 ?
Given a normal distribution with μ=40 and σ =9​, find​ (a) the normal curve area to...
Given a normal distribution with μ=40 and σ =9​, find​ (a) the normal curve area to the right of x=25; (b) the normal curve area to the left of x=29; (c) the normal curve area between x=43 and x=52​; (d) the value of x that has 90​% of the normal curve area to the​ left; and​ (e) the two values of x that contain the middle 70​% of the normal curve area.
Given a normal distribution with μ=40 and σ=66​, find​ (a) the normal curve area to the...
Given a normal distribution with μ=40 and σ=66​, find​ (a) the normal curve area to the right of x=24; (b) the normal curve area to the left of x=29; (c) the normal curve area between x=47 and x=54; ​(d) the value of x that has 70​% of the normal curve area to the​ left; and​ (e) the two values of x that contain the middle 65​% of the normal curve area.
Given that x is a normal variable with mean μ = 49 and standard deviation σ...
Given that x is a normal variable with mean μ = 49 and standard deviation σ = 6.2, find the following probabilities. (Round your answers to four decimal places.) (a)  P(x ≤ 60) (b)  P(x ≥ 50) (c)  P(50 ≤ x ≤ 60)
Given that x is a normal variable with mean μ = 43 and standard deviation σ...
Given that x is a normal variable with mean μ = 43 and standard deviation σ = 6.9, find the following probabilities. (Round your answers to four decimal places.) (a) P(x ≤ 60) (b) P(x ≥ 50) (c) P(50 ≤ x ≤ 60)
Given that x is a normal variable with mean μ = 107 and standard deviation σ...
Given that x is a normal variable with mean μ = 107 and standard deviation σ = 11, find the following probabilities. (Round your answers to four decimal places.) (a)  P(x ≤ 120) (b)  P(x ≥ 80) (c)  P(108 ≤ x ≤ 117)
Given that x is a normal variable with mean μ = 49 and standard deviation σ...
Given that x is a normal variable with mean μ = 49 and standard deviation σ = 6.9, find the following probabilities. (Round your answers to four decimal places.) (a)  P(x ≤ 60) (b)  P(x ≥ 50) (c)  P(50 ≤ x ≤ 60)
Given that x is a normal variable with mean μ = 108 and standard deviation σ...
Given that x is a normal variable with mean μ = 108 and standard deviation σ = 14, find the following probabilities. (Round your answers to four decimal places.) (a) P(x ≤ 120) (b) P(x ≥ 80) (c) P(108 ≤ x ≤ 117)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT