So, I'm supposed to write a function normpdf(x , avg, std)
that returns the Gaussian probability density function of x
for a normal distribution with mean avg
and standard deviation std
, with avg = 0
and std = 1
.
This is what I got so far, but when I click run, I get this message:
Input In [95]
return pdf
^
SyntaxError: invalid syntax
I'm confused on what I did wrong on that part.
import numpy as np
import math
def normpdf(x, avg=0, std=1) :
# normal distribution eq
exponent = math.exp(-0.5 * ((x - avg) / std) ** 2)
pdf = (1 / (std * math.sqrt(2 * math.pi)) * exponent)
return pdf
# set x values
x = np.linspace(1, 50)
normpdf(x, avg, std)
I added the parenthesis here and math.sqrt
:
pdf = (1 / (std * math.sqrt(2 * math.pi)) * exponent)
... but then I got this message:
TypeError Traceback (most recent call last)
Input In [114], in <cell line: 11>()
9 pdf = (1/(std*math.sqrt(2*math.pi))*exponent)
10 return pdf
---> 11 normpdf(x, avg, std)
Input In [114], in normpdf(x, avg, std)
6 def normpdf(x, avg=0, std=1) :
7 #normal distribution eq
----> 8 exponent = math.exp(-0.5*((x-avg)/std)**2)
9 pdf = (1/(std*math.sqrt(2*math.pi))*exponent)
10 return pdf
TypeError: only size-1 arrays can be converted to Python scalars
CodePudding user response:
You don't need the math
module. Use just numpy
functions:
import numpy as np
def normpdf(x, avg=0, std=1):
exp = np.exp(-0.5 * ((x - avg) / std) ** 2)
pdf = (1 / (std * np.sqrt(2 * np.pi)) * exp)
return pdf
x = np.linspace(1, 50)
print(normpdf(x))
The code above will result in:
[2.41970725e-001 5.39909665e-002 4.43184841e-003 1.33830226e-004
1.48671951e-006 6.07588285e-009 9.13472041e-012 5.05227108e-015
1.02797736e-018 7.69459863e-023 2.11881925e-027 2.14638374e-032
7.99882776e-038 1.09660656e-043 5.53070955e-050 1.02616307e-056
7.00418213e-064 1.75874954e-071 1.62463604e-079 5.52094836e-088
6.90202942e-097 3.17428155e-106 5.37056037e-116 3.34271444e-126
7.65392974e-137 6.44725997e-148 1.99788926e-159 2.27757748e-171
9.55169454e-184 1.47364613e-196 8.36395161e-210 1.74636626e-223
1.34141967e-237 3.79052640e-252 3.94039628e-267 1.50690472e-282
2.12000655e-298 1.09722105e-314 0.00000000e 000 0.00000000e 000
0.00000000e 000 0.00000000e 000 0.00000000e 000 0.00000000e 000
0.00000000e 000 0.00000000e 000 0.00000000e 000 0.00000000e 000
0.00000000e 000 0.00000000e 000]
CodePudding user response:
Your implementation of the normal probability density function is almost correct. The issue you're encountering is that you're trying to evaluate the function for an array of values, but the math operations you're using only work with scalar values (single numbers).
To fix this, you can either evaluate the function for each value in the array separately, or you can use numpy functions that can operate on arrays. Here's an example of how you could do this using numpy:
import numpy as np
def normpdf(x, avg=0, std=1):
# Compute the exponent
exponent = np.exp(-0.5 * ((x - avg) / std) ** 2)
# Compute the normalization constant
const = 1 / (std * np.sqrt(2 * np.pi))
# Compute the normal probability density function
pdf = const * exponent
return pdf
# Set x values
x = np.linspace(1, 50)
# Compute the probability density function for the given values of x
pdf = normpdf(x, avg, std)
# Print the result
print(pdf)
Note that in this example I used numpy functions for the exponent and square root, which allows the function to operate on arrays of values instead of just scalars. I also used a numpy array for the x values, which means that the function will return an array of probabilities, one for each value of x.
CodePudding user response:
You are trying to calculate the normal probability density function of an array of values, but the math.exp()
function expects a scalar value as input. To solve this issue, you can use the np.exp()
function from the NumPy library, which can handle arrays as input and will apply the exponent calculation element-wise.
import numpy as np
def normpdf(x, avg=0, std=1):
# normal distribution eq
exponent = np.exp(-0.5 * ((x - avg) / std) ** 2)
pdf = (1 / (std * np.sqrt(2 * np.pi)) * exponent)
return pdf
# set x values
x = np.linspace(1, 50)
normpdf(x)
I replaced the math.exp()
and math.sqrt()
functions with their NumPy equivalents, np.exp()
and np.sqrt()
. These functions can handle arrays as input and will return an array of results.