Esempio n. 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)
Esempio n. 2
0
 def __init__(self, mode=0, elem=None, sample=None):
     if mode == 0:
         self.mu = elem[0]
         self.sigma = elem[1]
     else:
         self.mu, self.sigma = norm.fit(sample)
     self.math_average = norm.mean(loc=self.mu, scale=self.sigma)
     self.dispersion = norm.var(loc=self.mu, scale=self.sigma)
    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)
	ax.set_xticks(list_xticks)
	ax.set_xticklabels(list_labels)

	



figure.savefig("Resultats/angle_.svg", dpi=200)


F = open("Resultats/angles_mean_totalLength.dat","w") 

for i in range(0,len(liste_y1_global)):
	param = norm.fit(sorted(liste_y1_global[i]))
	x_list1 = numpy.arange(0,max_list1,0.01)
	pdf_fitted = norm.pdf(x_list1,loc=param[0],scale=param[1])
	mean = norm.mean(loc=param[0],scale=param[1])
	meanAT=list_labels[i]+' meanAT = '+str(mean)+"\n"
 
	F.write(meanAT)

	param = norm.fit(sorted(liste_y2_global[i]))
	x_list2 = numpy.arange(0,max_list2,0.01)
	pdf_fitted = norm.pdf(x_list2,loc=param[0],scale=param[1])
	mean = norm.mean(loc=param[0],scale=param[1]) 
	meanCG=list_labels[i]+' meanCG = '+str(mean)+"\n"
	F.write(meanCG)
	F.write("\n")

F.close() 
Esempio n. 6
0
 def mean(self, dist):
     return norm.mean(*self._get_params(dist))
Esempio n. 7
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
#estimation for credible interval
np.quantile(rv2,[.05,.95])



#############################################################################
## 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)
Esempio n. 9
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
Esempio n. 10
0
dist_continu = [d for d in dir(stats) if isinstance(getattr(stats, d), stats.rv_continuous)]
dist_discrete = [d for d in dir(stats) if isinstance(getattr(stats, d), stats.rv_discrete)]



n = np.empty(1000)

for i in range(1000):
    n[i] = norm.rvs()

plt.hist(n, bins = 50, density = 1)
plt.hist(norm.pdf(n), bins = 50, density = 1)
plt.hist(norm.cdf(n), bins = 50, density = 1)
plt.hist(norm.moment(n.any()), bins = 50, density = 1)
norm.mean()
norm.var()
norm.ppf(0.5) # median of 0-1 normal distribution
norm.rvs(size = 3)

norm.rvs(size = 3, random_state = 1234)
norm.rvs(3) # the first arguement is the loc (mean, in case of normal distrubution) parameter, not size


# Shifting and scaling
x = norm.rvs(size = 5, loc = 3, scale = 4)
norm.stats(x)

norm.stats(loc = 3, scale = 4, moments = "mv")

np.mean(norm.rvs(5, size = 500))
print('#', 50 * "-")
# =================================================================
# 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))
Esempio n. 12
0
#coding:utf-8
from scipy.stats import norm
import numpy as np

list_num = [1, 2, 2, 1, 2, 3, 0, 1, 2, 4, 0]
list_num2 = [2, 4, 6, 3, 1, 5, 7, 4, 2, 4, 1]
print(norm.mean(list_num))
print(norm.mean(list_num2))
print('均值:')
print(np.mean(list_num))
print(np.mean(list_num2))
print('方差:')
print(np.var(list_num))
print(np.var(list_num2))
print('标准差:')
print(np.std(list_num))
print(np.std(list_num2))
print('协方差:')
print(np.cov(list_num, list_num2))
print(np.cov(list_num2, list_num))
print('相关系数:')
print(np.correlate(list_num, list_num2))
print(np.correlate(list_num2, list_num))
print(np.correlate(list_num2, list_num2))
print(np.correlate(list_num, list_num))
print('中位数')
print(np.median(list_num))
print(np.median(list_num2))
print('四分位数')
print(np.percentile(list_num, 25))
print(np.percentile(list_num2, 25))
Esempio n. 13
0
#(Normal distribution, applied)

# The grades from an exam roughly follow a Gaussian distribution of mean 5 and variance 2.
# Using the scipy.stats.norm package, determine what percentage of students will pass the exam, if the minimum score is 3.

from statistics import NormalDist
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt

mu = 5
sigma = 2
x = np.linspace(-20, 20)
pdf_X = norm.pdf(x, mu, sigma)

mean = norm.mean(mu, sigma)

print('mean is', mean)

fig, ax = plt.subplots(1, 1)
ax.plot(x, pdf_X)
ax.vlines(mean, 0, max(pdf_X), colors='pink')
plt.ylabel("pdf")
plt.xlabel("x")
plt.title("Probability density function")
plt.show()

#print(NormalDist(mu=5, sigma=2).pdf(3))
#print(NormalDist(mu=5, sigma=2).cdf(3))

percentage = 1 - NormalDist(mu=5, sigma=2).cdf(3)
Esempio n. 14
0
 def mean(self, n, p):
     mu = norm.mean(self, n, p)
     return mu
Esempio n. 15
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())
Esempio n. 16
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