Exemple #1
0
def plot_distrib_coord(list_values, name, dt, xmin, xmax, ymax, color):

    #print(list_values)

    fig = matplotlib.pyplot.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.set_xlim([xmin, xmax])
    n, bins, patches = matplotlib.pyplot.hist(list_values,
                                              50,
                                              normed=1,
                                              facecolor=color,
                                              alpha=0.5)

    param = norm.fit(sorted(list_values))
    pdf_fitted = norm.pdf(sorted(list_values), loc=param[0], scale=param[1])

    mean_norm = norm.mean(loc=param[0], scale=param[1])
    std_norm = norm.std(loc=param[0], scale=param[1]) / math.sqrt(
        float(len(list_values)))

    print('mean = ' + str(mean_norm))
    print('std error = ' + str(std_norm))

    #chi_value = chisquare(sorted(list_values), f_exp=pdf_fitted)
    #print('chi_square = '+str(chi_value[0]))
    #print('p_value = '+str(chi_value[1]))

    matplotlib.pyplot.plot(sorted(list_values), pdf_fitted, 'r-')

    matplotlib.pyplot.savefig(str(name) + '_' + str(dt) + '.svg')
    #matplotlib.pyplot.show()
    matplotlib.pyplot.close()
    return (mean_norm, std_norm)
Exemple #2
0
def gen(mu, sigma, name):
    """Generate fixture data and write to file.

    # Arguments

    * `mu`: mean parameter
    * `sigma`: standard deviation
    * `name::str`: output filename

    # Examples

    ``` python
    python> mu = rand(1000) * 10.0 - 5.0
    python> sigma = rand(1000) * 10.0 + 1.0
    python> gen(mu, sigma, './data.json')
    ```
    """
    y = list()
    for a, b in np.nditer([mu, sigma]):
        y.append(norm.std(a, b))

    # Store data to be written to file as a dictionary:
    data = {"mu": mu.tolist(), "sigma": sigma.tolist(), "expected": y}

    # Based on the script directory, create an output filepath:
    filepath = os.path.join(DIR, name)

    # Write the data to the output filepath as JSON:
    with open(filepath, "w") as outfile:
        json.dump(data, outfile)
    def test_independent_gaussian(self):
        num_vars = 2
        num_samples = 3

        x_marginal_cdfs = [normal_rv.cdf] * num_vars
        x_marginal_inv_cdfs = [normal_rv.ppf] * num_vars
        x_marginal_means = np.asarray([normal_rv.mean()] * num_vars)
        x_marginal_stdevs = np.asarray([normal_rv.std()] * num_vars)

        x_covariance = np.eye(num_vars)

        x_samples = np.random.normal(0., 1., (num_vars, num_samples))
        u_samples = nataf_transformation(x_samples, x_covariance,
                                         x_marginal_cdfs, x_marginal_inv_cdfs,
                                         x_marginal_means, x_marginal_stdevs)

        assert np.allclose(u_samples, x_samples)
    def test_correlated_gaussian(self):
        num_vars = 2
        num_samples = 10

        x_marginal_cdfs = [normal_rv.cdf] * num_vars
        x_marginal_inv_cdfs = [normal_rv.ppf] * num_vars
        x_marginal_means = np.asarray([normal_rv.mean()] * num_vars)
        x_marginal_stdevs = np.asarray([normal_rv.std()] * num_vars)

        x_correlation = np.array([[1, 0.5], [0.5, 1]])
        x_covariance = x_correlation.copy()  # because variances are 1.0
        x_covariance_chol_factor = np.linalg.cholesky(x_covariance)
        iid_x_samples = np.random.normal(0., 1., (num_vars, num_samples))
        x_samples = np.dot(x_covariance_chol_factor, iid_x_samples)

        u_samples = nataf_transformation(x_samples, x_covariance,
                                         x_marginal_cdfs, x_marginal_inv_cdfs,
                                         x_marginal_means, x_marginal_stdevs)

        assert np.allclose(u_samples, iid_x_samples)
Exemple #5
0
 def std(self, dist):
     return norm.std(*self._get_params(dist))
Exemple #6
0
# -*- coding: utf-8 -*-
#https://docs.scipy.org/doc/scipy/reference/stats.html
#https://docs.scipy.org/doc/scipy/reference/tutorial/stats.html
#import scipy
#scipy.stats package is imported as
from scipy import stats

# individual objects are imported as
from scipy.stats import norm

norm.cdf(0)
norm.cdf([-1., 0, 1])
import numpy as np
norm.cdf(np.array([-1., 0, 1]))
norm.mean(), norm.std(), norm.var()
norm.stats(moments="mv")
norm.ppf(0.5)
norm.rvs(size=3)  #random nos

#drawing random numbers relies on generators from numpy.random package. In the example above, the specific stream of random numbers is not reproducible across runs. To achieve reproducibility, you can explicitly seed a global variable
np.random.seed(1234)
#Relying on a global state is not recommended though. A better way is to use the random_state parameter which accepts an instance of numpy.random.RandomState class, or an integer which is then used to seed an internal RandomState object:
norm.rvs(size=5, random_state=1234)
norm.rvs(5)  #one no only

#Shifting and Scaling¶
#All continuous distributions take loc and scale as keyword parameters to adjust the location and scale of the distribution, e.g. for the standard normal distribution the location is the mean and the scale is the standard deviation.
norm.stats(loc=3, scale=4, moments="mv")

#uniform distribution
from scipy.stats import uniform


#############################################################################
## Sampling Importance Resampling (SIR) #####################################
#############################################################################


#suppose that we have a posterior with a Gamma(3,3) format, but we dont know.
#use gaussian in the sampling step
#loc1, scale1 are from the Gaussian
loc1=0
scale1=500 #scale is standard deviation
norm.mean(loc=loc1, scale=scale1)
norm.var(loc=loc1, scale=scale1)
norm.std(loc=loc1, scale=scale1)

np.random.seed(seed=123)
rv1 = np.random.normal(loc=loc1, scale=scale1, size=10000)
rv1[0:10]
np.mean(rv1)
np.std(rv1)
np.var(rv1)
np.max(rv1)
np.min(rv1)

b1, b2 = -5,5
x = np.linspace(b1,b2,100)
y = norm.pdf(x=x, loc=loc1, scale=scale1)
fig, ax = plt.subplots(1, 1) #it creates another plot
ax.plot(x, y,'b-', lw=2, alpha=0.6, label='Gaussian pdf')
Exemple #8
0
def length(mask, estSize, returnAll=False, showSteps=False, pdf=None):
    """
    Calculates the average between node ligament length and standard deviation
    of the passed mask.  Only meaningful on the fully connected phase.

    Parameters
    ----------
    mask : 2D array of bool
        Mask showing the area of one phase. 
    estSize : float64
        The estimated diameter.  Used to set some parameters.
    returnAll : bool, optional
        If set to true, a list of all the measurements will be returned.
    showSteps : bool, optional
        If set to true, figures will be displayed that shows the results of
        intermediate steps when calculating the diameter.
    pdf : matplotlib.backends.backend_pdf.PdfPages, optional
        If provided, information will be output to the pdf file.
        
    Returns
    -------
    logavg : float64
        Average ligament length.  (Assumes lognormal distribution.)
    logstd : float64
        Standard deviation of ligament diameters.
    allMeasurements: 1D array of float64, optional
        A list of all measured ligament lengths.
    """

    # Debug mode
    debug = False
    if debug:
        showSteps = True

    # Adjustable parameters.
    # These have been highly tested and changing these may throw off results.
    small_lig = 0.6 * estSize  # Ligaments smaller than this are within a node.
    filter_out_mult = 0.4  # This * estSize is ignored in final calculation.

    # Return 0 if the mask is not fully connected.
    if not isConnected(mask):
        if returnAll:
            return 0, 0, 0
        else:
            return 0, 0

    rows, cols = mask.shape
    skel = skeleton(mask, estSize, removeEdge=True, times=3)

    if debug:
        display.showSkel(skel, mask)

    # get ligament and node labels
    nodes = np.copy(skel)
    ligaments = np.copy(skel)
    neighbors = convolve2d(skel, np.ones((3, 3)), mode='same')
    neighbors = neighbors * skel
    nodes[neighbors < 4] = 0
    ligaments[neighbors > 3] = 0
    ligaments = label(ligaments, background=0)
    nodes = binary_dilation(nodes, selem=disk(3))
    nodes = label(nodes, background=0)

    # get a list of ligaments connected to each node
    node_to_lig = [
    ]  # node_to_lig[n] is an array of each ligament label that is connected to node n
    unodes = np.unique(nodes)
    for n in unodes:
        node_to_lig.append(np.unique(ligaments[nodes == n]))

    # get a list of nodes connected to each ligament
    lig_to_node = [
    ]  # lig_to_node[l] is an array of each node label that is connected to ligament l
    uligs = np.unique(ligaments)
    for l in uligs:
        lig_to_node.append(np.unique(nodes[ligaments == l]))

    # Get the length of each ligament between nodes.
    lengths = np.bincount(ligaments.flatten())

    # Add ligaments that are within a single node to connected ligaments.
    small = int(round(small_lig))
    too_small = []
    for l in uligs:
        if lengths[l] <= small:
            too_small.append(l)  #keep track of ligaments that are too small
            for connected in lig_to_node[l]:
                if connected > 0 and connected != l:
                    # add half the small ligament to the connected ligaments.
                    lengths[connected] += int(round(lengths[l] / 2, 0))

    # Set to True to show which ligaments are considered to be within a
    # single node.
    if showSteps:
        ligaments_small = ligaments > 0
        ligaments_small = ligaments_small.astype('uint8')
        ligaments_small[ligaments_small == 1] = 2
        for i in too_small:
            ligaments_small[ligaments == i] = 1
        display.showSkel(ligaments_small, mask, dialate=False,
                title=("Green = within node ligaments, " \
                       "White = between node ligaments"))

    # filter out background and extra small lengths
    lengths[0] = 0
    allMeasurements = lengths[lengths > 0]
    lengths = lengths[lengths > estSize * filter_out_mult]

    if len(lengths) == 0:
        if returnAll:
            return 0, 0, 0
        else:
            return 0, 0

    # Get a lognormal fit.
    fit = lognorm.fit(lengths.flatten(), floc=0)
    pdf_fitted = lognorm.pdf(lengths.flatten(),
                             fit[0],
                             loc=fit[1],
                             scale=fit[2])

    # Get gaussian fit.
    gfit = norm.fit(lengths.flatten())

    # Get average and standard deviation for lognormal fit.
    logaverage = lognorm.mean(fit[0], loc=fit[1], scale=fit[2])
    logstd = lognorm.std(fit[0], loc=fit[1], scale=fit[2])

    # Get average and standard deviation for normal fit.
    average = norm.mean(gfit[0], gfit[1])
    std = norm.std(gfit[0], gfit[1])
    numbMeasured = lengths.size

    if showSteps:
        display.showSkel(ligaments, mask, dialate=False)

    if showSteps:
        display.showHist(lengths,
                         gauss=True,
                         log=True,
                         title='Ligament Lengths',
                         xlabel='Ligament lengths [pixels]')

    if pdf is not None:
        inout.pdfSaveSkel(pdf, ligaments, mask, dialate=True)
        inout.pdfSaveHist(pdf,
                          lengths,
                          gauss=True,
                          log=True,
                          title='Ligament Lengths',
                          xlabel='Ligament lengths [pixels]')

    if returnAll:
        # Change to return average, std... for mean and SD of normal PDF.
        return logaverage, logstd, allMeasurements
    else:
        return logaverage, logstd
Exemple #9
0
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm

np.random.seed(5)

# Normal Distribution
mean, var = norm.stats(moments='mv')
std = norm.std()

fig, ax = plt.subplots(1, 1)

x = np.linspace(norm.ppf(0.05), norm.ppf(0.95))
ax.plot(x, norm.pdf(x), 'b-', lw=3, alpha=0.6, label='Gaussian')

q1 = norm.ppf(.25)
median = norm.ppf(.5)
q3 = norm.ppf(.75)

plt.title(
    'Gaussian Distribution ($\mu$: {:.2f}, $\sigma$: {:.2f}, $\sigma^2$: {:.2f})'
    .format(mean, std, var),
    size='xx-large')
plt.xlabel('X', size='large')
plt.ylabel('P(X)', size='large')

# Quartile lines
ax.axvline(x=q1, linewidth=3, alpha=0.6, color='black', linestyle='dashed')
ax.axvline(x=median, linewidth=3, alpha=0.6, color='black', linestyle='dashed')
ax.axvline(x=q3, linewidth=3, alpha=0.6, color='black', linestyle='dashed')
# =================================================================
# Class_Ex12:
# Use scipy statistics methods on randomly created array (use Scipy)
# PDF, CDF (CUMsum), Mean, Std, Histogram
# ----------------------------------------------------------------

from scipy.stats import norm
from scipy import stats

a12 = np.random.randint(10, 50, 25)

norm.pdf(a12)
norm.cdf(a12)

norm.mean(a12)
norm.std(a12)
stats.rv_histogram(np.histogram(norm.rvs(a12)))

print('#', 50 * "-")
# =================================================================
# Class_Ex13:
# USe hypothesise testing  if two datasets of (independent) random variables
# comes from the same distribution (use Scipy)
# Calculate p values.
# ----------------------------------------------------------------

rvs1 = norm.rvs(size=100)
rvs2 = norm.rvs(size=150)
print(stats.ks_2samp(rvs1, rvs2))

print('#', 50 * "-")
Exemple #11
0
 def std(self, n, p):
     std = norm.std(self, n, p)
     return std
Exemple #12
0
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from scipy.stats import norm

print(norm.mean(), norm.std(), norm.var())

r = norm.rvs(size=100)
print(r)

print(r.mean(), r.std(), r.var())
Exemple #13
0
a = np.array([[1, 2], [3, 4]])
print(a)
b = np.array([[5], [6]])
print(b)

print(linalg.inv(a).dot(b))
print(a.dot(linalg.inv(a).dot(b)) - b)
print(np.linalg.solve(a, b))
print(a.dot(np.linalg.solve(a, b)) - b)
print('-----')

# Step 4. Common Methods in stats
print(norm.cdf(0))
print(norm.cdf([-1., 0, 1]))
print(norm.cdf(np.array([-1., 0, 1])))
print(norm.mean(), norm.std(), norm.var())
print(norm.stats(moments="mv"))
print(norm.ppf(0.5))
print(norm.rvs(size=3))
print(np.random.seed(1234))
print(norm.rvs(size=5, random_state=1234))
print(norm.rvs(5))
print('-----')

# Step 5. Broadcasting
print(stats.t.isf([0.1, 0.05, 0.01], [[10], [11]]))
print(stats.t.isf([0.1, 0.05, 0.01], 10))
print(stats.t.isf([0.1, 0.05, 0.01], [10, 11, 12]))
print('-----')

# Step 6. Anlysing one sample