0

I want to check if a random point is inside the normal distribution or not. As you can see from my code, I separate them using different colors; however, the result is wrong - the criterion is if a point is inside or outside of the distribution then color it accordingly.

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

mu = 175
sigma = 10
xnormal = np.linspace(-3 * sigma + mu, 3 * sigma + mu, 1000)
xnormal = [int(x) for x in xnormal]

plt.plot(xnormal, stats.norm.pdf(xnormal, mu, sigma))
plt.axhline(y=0.04, color='r', linestyle='-')

N = 100

for i in range(N):
    x = np.random.randint(145, 205)
    y = np.random.uniform(0.0, 0.04)
    if(y <= (xnormal.count(x)/1000)):
        plt.scatter(x,y,None,'orange')
    else:
        plt.scatter(x,y,None,'b')
plt.show()

What the program outputs:
What the program outputs.

What the result should be:
What the result should be.

1 Answer 1

2

Small indexing problem:

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

#just inserted for reproducibility
np.random.seed(123)

mu = 175
sigma = 10
xnormal = np.linspace(-3 * sigma + mu, 3 * sigma + mu, 1000)
xnormal = [int(x) for x in xnormal]
ynormal = stats.norm.pdf(xnormal, mu, sigma)

plt.plot(xnormal, ynormal)
plt.axhline(y=0.04, color='r', linestyle='-')

N = 100

for i in range(N):
    x = np.random.randint(145, 205)
    y = np.random.uniform(0.0, 0.04)
    if(y <= ynormal[xnormal.index(x)]):
        plt.scatter(x,y,None,'orange')
    else:
        plt.scatter(x,y,None,'b')
plt.show()

Sample output: enter image description here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.