示例#1
0
bins = 30

img_dir = "/mnt/c/Users/ltray/Documents/Cambridge/3F3/img/"

# Plot normal distribution
x = np.random.randn(1000)  # randn is standard normal distribution
x_values = np.linspace(-5., 5., 100)

ks_density = ksdensity(x, width=0.4)
scale = lambda el: el * N / bins

fig = plt.figure()

plt.title('Histogram of gaussian distribution (N={})'.format(N))
plt.hist(x, bins=bins, density=True, label="normalised frequency")
plt.plot(x_values, n_pdf(x_values), linestyle='dashed', label="gaussian pdf")
plt.xlabel('x')
plt.ylabel('Normalised frequency')
plt.legend()

#plt.savefig(img_dir + "gaussian_hist.png")

fig = plt.figure()
plt.title('Kernel smoothed gaussian distribution (N={}, sigma={})'.format(N, sigma))
ks_density = ksdensity(x, width=sigma)
plt.plot(x_values, ks_density(x_values) / sigma, label="kernel smoothed")
plt.plot(x_values, n_pdf(x_values), linestyle='dashed', label="gaussian pdf")
plt.xlabel('x')
plt.ylabel('probability density')
plt.legend()
示例#2
0
from pdfs import n_pdf
from transforms import *

transform = 2
J = 30
N = 1000
img_name = 'transform{}.png'.format(transform)

x_rand = np.random.randn(N)
if transform == 1:
    # y = ax + b
    a = 2
    b = 4
    f = t1(a, b)
    x_lin = np.linspace(-(10 * a) / 2 + b, (10 * a) / 2 + b, N)
    y = n_pdf(x_lin, b, a)

elif transform == 2:
    # y = x^2
    f = t2()
    x_lin = np.linspace(-4, 3, N)
    y = t2_plot(x_lin)

plt.plot(x_lin,
         y,
         color='darkorange',
         ls='--',
         label='Exact Transformed Dist.')
plt.hist(f(x_rand),
         bins=J,
         color='cornflowerblue',
示例#3
0
 def ksd(x_axis):
     prob = [n_pdf(x_i, data, width) for x_i in x_axis]
     pdf = [np.average(pr) for pr in prob]  # each row is one x value
     return np.array(pdf)
示例#4
0
import numpy as np
import matplotlib.pyplot as plt
from pdfs import n_pdf, u_pdf

dist = 'u'
J = 20
N = 1000
img_name = 'uniform.png'

if dist == 'n':
    # Plot normal distribution
    x_rand = np.random.randn(N)
    x_lin = np.linspace(-5, 5, N)
    plt.hist(x_rand, bins=J, color='cornflowerblue', density=True, label='Histogram Approximation')
    plt.plot(x_lin, n_pdf(x_lin), color='darkorange', ls='--', label='Exact Normal Dist.') # normal plot

elif dist == 'u':
    # Plot uniform distribution
    a = 0
    b = 1
    p = u_pdf(a, b)
    x_rand = np.random.rand(N)
    x_lin = np.linspace(-0.5, 1.5, N)
    plt.hist(x_rand, bins=J, color='cornflowerblue', density=True, label='Histogram Approximation')
    range = b - a
    plt.plot([a, b], [p, p], color='darkorange', ls='--', label='Exact Uniform Dist.')
    plt.plot([a - range / 4, a], [0, 0], color='darkorange', ls='--')
    plt.plot([b, b + range / 4], [0, 0], color='darkorange', ls='--')
    plt.plot([a, a], [0, p], color='darkorange', ls='--')
    plt.plot([b, b], [0, p], color='darkorange', ls='--')
    #plt.plot(x_lin, n_pdf(x_lin), color='red', ls='--') # normal plot
示例#5
0

N = 100000
bins = 30
a = 1.4
b = 3

img_dir = "/mnt/c/Users/ltray/Documents/Cambridge/3F3/img/"

# Plot normal distribution
x = np.random.randn(1000)  # randn is standard normal distribution
x_values = np.linspace(-5., 8., 100)

fig, ax = plt.subplots(3, sharex="col")
plt.title('Transform Gaussian')

ax[0].set_title('Standard Gaussian, (mu=0, sigma=1)')
ax[0].hist(x, bins=bins, density=True)
ax[0].plot(x_values, n_pdf(x_values), linestyle='dashed')

ax[1].set_title('f(x) = ax + b,  (a={}, b={})'.format(a, b))
ax[1].hist(f_1(x, a, b), bins=bins, density=True)
ax[1].plot(x_values, n_pdf(x_values, mu=b, sigma=a), linestyle='dashed')

ax[2].set_title('f(x) = x^2')
ax[2].hist(f_2(x), bins=bins, density=True)
ax[2].plot(x_values, pdf_2(x_values), linestyle='dashed')

plt.savefig(img_dir + "transformed_hist.png")
plt.show()
示例#6
0
N = 1000
width = 0.1

img_name = 'smoothed_normal.png'

if dist == 'n':
    # Plot normal distribution
    x_rand = np.random.randn(N)
    x_lin = np.linspace(-5, 5, N)
    ks_density = ksdensity(x_rand, width=width)
    plt.plot(x_lin,
             ks_density(x_lin),
             color='cornflowerblue',
             label='Smoothed Approximation')
    plt.plot(x_lin,
             n_pdf(x_lin),
             color='darkorange',
             ls='--',
             label='Exact Normal Dist.')  # normal plot
    plt.legend()

elif dist == 'u':
    # Plot uniform distribution
    a = 0
    b = 1
    p = u_pdf(a, b)
    x_rand = np.random.rand(N)
    x_lin = np.linspace(-0.5, 1.5, N)
    ks_density = ksdensity(x_rand, width=width)
    plt.plot(x_lin,
             ks_density(x_lin),