Posted February 16, 2018 #importing from math library from math import pi, sqrt, exp # mean value from user mu = float (input ("Enter the mean : ")) # standard deviation from user sigma = float (input ("Enter the standrad deviation : ")) # value of t from user t = float (input ("Enter the value of t : ")) # creating x for the exponential function x = - (1 / 2) * (t - mu / sigma) ** 2 # compute the gaussian distribution ft = 1 / sigma * sqrt(2 * pi) * exp(x) # print the gaussian distribution print ("The gaussian distribution is %.3f" %(ft)) Hello all, I tried to write the Gaussian distribution equation on python and I think I have it mostly done but when I input the values provided in the question I get the wrong answer. Note: the variable of the function in the equation above is replaced with a 't' instead if an 'x' Input data: - mean: 1.4 - standard deviation: 0.15 - variable of function t: 0.95 Expected output: ft = 1 / sigma * sqrt(2 * pi) * exp(x) - Enter the mean : 1.4 - Enter the standard deviation : 0.15 - Enter the value of t : 0.95 - The gaussian distribution is 0.030 Program produced output: - Enter the mean : 1.4 - Enter the standard deviation : 0.15 - Enter the value of t : 0.95 - The gaussian distribution is 0.000 Anyone knows what's going on or if the equation is right? This is the question that I'm trying to solve, the equation slightly differs from the one given above but I think the one I have is a simplified version of it (in some way or another) Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted February 16, 2018 Do the same equation on Matlab or manually using a TI calculator. Do the outputs match? Create some unit tests with random values and then check on calculator to see if they match Sudo make me a sandwich Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted February 16, 2018 Author 32 minutes ago, wasab said: Do the same equation on Matlab or manually using a TI calculator. Do the outputs match? Create some unit tests with random values and then check on calculator to see if they match I tried it using the calculator that I have and I got 0.0295..., with the values above, which is close to the answer in the question. Yet I get 0.000 when I use my program. I also tried using different values but it still gave me a wrong answer. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted February 16, 2018 Try using more parentheses in your ft assignment, something like (1/(sigma * (sqrt (2*pi))))* exp (x) Id do it like that to ensure it's evaluating the correct order. Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted February 16, 2018 Your x looks wrong to me, you are squaring the result of (t - mu / sigma) instead of those terms separately. I'd try: x = - (t - mu) ** 2 / (2 * sigma ** 2) CPU: 6700k GPU: Zotac RTX 2070 S RAM: 16GB 3200MHz SSD: 2x1TB M.2 Case: DAN Case A4 Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted February 17, 2018 Author 10 hours ago, WiiManic said: Your x looks wrong to me, you are squaring the result of instead of those terms separately. I'd try: x = - (t - mu) ** 2 / (2 * sigma ** 2) There are two equations, one of them has the squaring on both and the other only has it on (t - mu / sigma). I think one of them is a 'simplified' version or something. I'll try it out nonetheless. Thanks. EDIT: I tried it out and got 0.186 instead of 0.030 (with the same values I used above), but that's a start. Thanks Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted February 17, 2018 Author Solution Thanks to @WiiManic and @HeaterUp for helping me solve the question and showing me where I was mistaken. Cheers! Answers: On 2/17/2018 at 12:46 AM, WiiManic said: x = - (t - mu) ** 2 / (2 * sigma ** 2) On 2/17/2018 at 12:42 AM, HeaterUp said: (1/(sigma * (sqrt (2*pi))))* exp (x) Link to comment Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now