C3W1 Assignment: 4.1 Uniform PDF

I’m having a lot of trouble determining what is being asked for Exercise 4.1 where we are def pdf_uniform(x, a, b) and determining what the pdf input variable should be. I tried using norm.pdf(x) but that was not giving the expected output. Also tried using uniform.pdf(x) and np.linspace(uniform.ppf(a), uniform.ppf(b), x) but the latter is giving me the error 'float' object cannot be interpreted as an integer. Can someone at least point me in the direction I should pursue? Thanks in advance!

1 Like

4.1 Uniform PDF

If X \sim \text{Uniform}(a,b), then the PDF for X is given by:

f_X(x) =\begin{cases}\frac{1}{b-a}, \quad \text{if } x \in [a,b]. \\ 0, \quad \text{otherwise.} \end{cases}

You are given all of the components x, a, b, just plug it in and you’ll have the PDF.

Thanks! But I literally don’t know how to put this into python. Was also trying to work with uniform(a,b).pdf(x) and getting closer but still not right.

The purpose of this exercise is to create the pdf function from scratch on your own, hence the provided equation above.

Please refer to this scipy documentation, specifically in the 2nd paragraph where it says:

In the standard form, the distribution is uniform on [0, 1]. Using the parameters loc and scale, one obtains the uniform distribution on [loc, loc + scale].

In other words, you would have to set the loc and scale arguments within the uniform.pdf() function. Let me know if it works.

The equation has two conditions, thus I highly recommend you to construct an if statement. In the first case, x is in the range of a and b. For the second case you can just use else.

thank you for what you have already posted

1 Like

I believe your equation for calculating the pdf is correct. Would you mind removing it so there won’t be any violiation of the code of honor? :slightly_smiling_face:

Regarding the usage of the uniform.pdf function, please read this quoted line mentioned above:

thank you for your response
I removed the post.
My apologies for the error in posting.

Hello @Tom_Pham , how would one go about setting these within the uniform.pdf() ?

Have you had success following the instructions from this?

No. I can’t relate these steps to the exercise? @Tom_Pham