Histogram does not appear on the f-distribution plot

I am trying to generate f-distributed random numbers with a given degree of freedom d1 and d2 and plot both histograms with f-distributed random numbers and plot an idealized f-distribution curve, but when I give small df values, the histogram does not appear. I am new to statistics and matplotlib and I couldn't figure out how to deal with this problem. This is my code:

def distF(request, distribution_id):
    dist = get_object_or_404(Distribution, pk=distribution_id)
    dfd = dist.var4
    dfn = dist.var2
    x = np.random.f(dfn, dfd, size = dist.var3)
    num_bins = 50

    fig, ax = plt.subplots()
    print(x)
    # the histogram of the data
    n, bins, patches = ax.hist(x, num_bins, normed=True)
    y = np.linspace(0, 5, 1001)[1:]
    dist = st.f(dfn, dfd, 0)
    #y = np.linspace(st.f.ppf(0.01, dfn, dfd), st.f.ppf(0.99, dfn, dfd), 100)
    ax.plot(y, dist.pdf(y), '--')

    ax.set_xlabel('Smarts')
    ax.set_ylabel('Probability density')
    ax.set_xlim([0, 4])
    ax.set_ylim([0, 3])
    fig.tight_layout()
    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)
    plt.close(fig)
    return response

      

Here's what the plots look like:

F-distribution plot with small df values F distribution plot with small df values

F-distribution plot with large df values F distribution plot with large df values

+3


source to share


1 answer


The problem is that the distribution of f with a dfd

of 1 extends very strongly to large numbers. So, let's say you have values โ€‹โ€‹of 2000 or so in your array x

, but only 50 bits between 0 and 2000. This makes the bits quite large and therefore rather low in height. I would think that if you want to limit your opinion to some low number anyway, it would be better to also limit the histogram to that number.

In the code below, the limit will be 5 and the cell width will be 0.2.



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

dfn = 10
dfd =1
limit = 5

x = np.random.f(dfn, dfd, size = 100)
bins = np.arange(0, limit, 0.2)

fig, ax = plt.subplots()

# the histogram of the data
n, bins, patches = ax.hist(x, bins, normed=True)
y = np.linspace(0, limit, 1001)[1:]
dist = st.f(dfn, dfd, 0)

ax.plot(y, dist.pdf(y), '--')

ax.set_xlabel('Smarts')
ax.set_ylabel('Probability density')
ax.set_xlim([0, limit])

fig.tight_layout()
plt.show()

      

enter image description here

0


source







All Articles