示例#1
0
 def interpret(parameters: np.ndarray, classes: np.ndarray, interval: float):
     n_samples, n_components, n_classes = classes.shape
     assert parameters.ndim == 3
     assert parameters.shape == (n_samples, SkewNormal.N_PARAMETERS + 1, n_components)
     shapes = np.expand_dims(parameters[:, 0, :], 2).repeat(n_classes, 2)
     locations = np.expand_dims(parameters[:, 1, :], 2).repeat(n_classes, 2)
     scales = np.expand_dims(relu(parameters[:, 2, :]), 2).repeat(n_classes, 2)
     proportions = np.expand_dims(softmax(parameters[:, 3, :], axis=1), 1)
     components = skewnorm.pdf(classes, shapes, loc=locations, scale=scales) * interval
     m, v, s, k = skewnorm.stats(shapes[:, :, 0], loc=locations[:, :, 0], scale=scales[:, :, 0], moments="mvsk")
     return proportions, components, (m, np.sqrt(v), s, k)
def test_skewdist():
    sd = SkewDist(num_interp=500)
    a = random.choice([2., 3., 4.])
    xs = skewnorm.rvs(a, size=1000)

    for x in xs:
        sd.update(value=x)

    percentiles = [0.25, 0.5, 0.75]
    skew_quantiles = [sd.inv_cdf(p) for p in percentiles]
    actual_quantiles = [np.quantile(xs, q) for q in percentiles]
    assert all(abs(z1 - z2) < 2.0 for z1, z2 in zip(skew_quantiles, actual_quantiles))

    sd_stats = [sd.state.mean, sd.state.var(), sd.state.skewness(), sd.state.kurtosis()]
    moment_comparison = list(zip(sd_stats, skewnorm.stats(a, moments='mvsk')))
    pass
#The skewed normal distributions are fitted through Bayesian optimization using the scipy module
plt.figure(3)
plt.xlabel('distance bin')
plt.ylabel('sizes (m)')
n = 1
x = np.arange(0, 200,
              0.1)  #range needed to create probability distribution function
skewed_mean_distances = []
skewed_std_distances = []
skewed_variance_distances = []
skewed_kurtosis_distances = []
skewed_skew_distances = []
for bins in size_nest_distances:
    if len(bins) > 10:
        a, loc, scale = skewnorm.fit(bins)  #I fit all distance bins
        mean, var, skew, kurt = skewnorm.stats(a, loc, scale, moments='mvsk')
        skewed_mean_distances.append(mean)
        skewed_std_distances.append(skewnorm.std(
            a, loc, scale))  #Calculate the std of all distributions
        skewed_variance_distances.append(
            var)  #Calcualate variance if interesting
        skewed_skew_distances.append(skew)  #calculate skewness if interesting
        skewed_kurtosis_distances.append(
            kurt)  #calculate kurtosis if interesting
        pdf = skewnorm.pdf(x, a, loc,
                           scale)  #create probability distribution function
        #I now plot all of the fitted distributions with their respective histograms
        plt.subplot(1, len(size_nest_distances), n)
        plt.hist(bins, bins=50, normed=True)
        plt.plot(x, pdf)
        n += 1
示例#4
0
from scipy.stats import skewnorm
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

# Calculate a few first moments:

a = 4
mean, var, skew, kurt = skewnorm.stats(a, moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(skewnorm.ppf(0.01, a),
                skewnorm.ppf(0.99, a), 100)
ax.plot(x, skewnorm.pdf(x, a),
       'r-', lw=5, alpha=0.6, label='skewnorm pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = skewnorm(a)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = skewnorm.ppf([0.001, 0.5, 0.999], a)
np.allclose([0.001, 0.5, 0.999], skewnorm.cdf(vals, a))
# True
示例#5
0
 def get_moments(*args) -> dict:
     assert len(args) == len(SkewNormalDistribution.get_parameter_names())
     m, v, s, k = skewnorm.stats(*args, moments="mvsk")
     std = np.sqrt(v)
     moments = dict(mean=m, std=std, skewness=s, kurtosis=k)
     return moments