Пример #1
0
def main():
    fig, ax = plt.subplots(figsize=gfs)

    xs = np.linspace(0., 10., 500)
    rvs = stats.lomax(c).pdf(xs)
    ax.plot(xs, rvs, label='$\lambda(x) = c/x$')

    rvs = stats.expon(scale=1 / c).pdf(xs)
    ax.plot(xs, rvs, label='$\lambda(x) = c$')

    rvs = stats.rayleigh(scale=np.sqrt(1 / c)).pdf(xs)
    ax.plot(xs, rvs, label='$\lambda(x) = cx$')

    ax.set_xlim(0, 10)
    ax.set_ylim(0, 2)
    ax.set_xlabel('$X$ (failure rv)', fontsize=fs)
    ax.set_ylabel('$p(x)$', fontsize=fs)
    ax.tick_params(labelsize=fs)
    ax.legend(fontsize=fs)

    plt.tight_layout()
    plt.savefig('../hf2pdf-examples.png', bbox_inches='tight')
    plt.close()

    # logspace now
    fig, ax = plt.subplots(figsize=gfs)

    xs = np.linspace(0.1, 100., 1000)
    rvs = stats.lomax(c).pdf(xs)
    ax.plot(xs, rvs, label='$\lambda(x) = c/x$')

    rvs = stats.expon(scale=1 / c).pdf(xs)
    ax.plot(xs, rvs, label='$\lambda(x) = c$')

    rvs = stats.rayleigh(scale=np.sqrt(1 / c)).pdf(xs)
    ax.plot(xs, rvs, label='$\lambda(x) = cx$')

    ax.set_xlabel('$X$ (failure rv)', fontsize=fs)
    ax.set_ylabel('$p(x)$', fontsize=fs)
    ax.tick_params(labelsize=fs)
    ax.legend(fontsize=fs)
    ax.set_ylim(10**-9, )
    ax.set_yscale('log')
    ax.set_xscale('log')

    plt.tight_layout()
    plt.savefig('../hf2pdf-examples-loglog.png', bbox_inches='tight')
    plt.close()
Пример #2
0
def diagnostic_plot(x, y, flag=None):

    if flag is None:
        flag = np.zeros(len(x))
    idx = flag.astype(bool)

    # Turned off outlier detection because it doesn't work well.
    # idx = find_outliers(x, y, initial_clip=idx, threshold=1e-5)
    mu_x = np.mean(x[~idx])
    mu_y = np.mean(y[~idx])
    sma, smi = compute_eigen_vectors(x[~idx], y[~idx])

    plt.clf()
    plt.gcf().set_size_inches((10, 8))
    plt.plot(x, y, "ko", mec="w", label="Centroids", zorder=+5)
    if np.any(idx):
        plt.plot(x[idx],
                 y[idx],
                 "o",
                 color="pink",
                 label="Outliers",
                 zorder=+6)

    # prob = compute_prob_of_points(x, y, sma, smi)
    for i in range(len(x)):
        plt.text(x[i], y[i], " %i" % (i), zorder=+5)

    sigma_a = np.linalg.norm(sma)
    sigma_b = np.linalg.norm(smi)
    angle_deg = np.degrees(np.arctan2(sma[1], sma[0]))

    ax = plt.gca()
    if 1:
        for p in [0.68, 0.95, 0.997]:
            scale = spstats.rayleigh().isf(1 - p)
            width = 2 * sigma_a * scale
            height = 2 * sigma_b * scale
            ell = Ellipse(
                [mu_x, mu_y],
                width=width,
                height=height,
                angle=angle_deg,
                color="gray",
                alpha=0.2,
                label="%g%% Prob" % (100 * p),
            )
            ax.add_patch(ell)

    plt.axhline(0)
    plt.axvline(0)
    plt.plot(0, 0, "*", color="c", ms=28, mec="w", mew=2)
    plt.xlabel("Column shift (pixels)")
    plt.ylabel("Row shift (pixels)")
    plt.axis("equal")
    plt.legend()

    offset, signif = compute_offset_and_signif(x[~idx], y[~idx])
    msg = "Offset %i pixels\nProb Transit on Target: %.0e" % (offset, signif)
    plt.title(msg)
    return plt.gcf()
def rayleigh(x):
    #f = get_fading(num_channels)
    f = ss.rayleigh().pdf(
        ss.rayleigh.rvs(size=2))  # hardcoded in order to save checkpoints
    f = f * 0.631  # to correct for skewness
    x = x - f
    return x
Пример #4
0
    def test_2pl_mml_eap_method(self):
        """Testing the 2PL EAP/MML Method."""
        np.random.seed(618331)

        n_items = 5
        n_people = 150
        difficulty = stats.norm(0, 1).rvs(n_items)
        discrimination = stats.rayleigh(loc=0.25, scale=.8).rvs(n_items)
        thetas = np.random.randn(n_people)
        syn_data = create_synthetic_irt_dichotomous(difficulty, discrimination,
                                                    thetas)

        result = twopl_mml_eap(syn_data, {'hyper_quadrature_n': 21})

        # Smoke Tests / Regression Tests
        expected_difficulty = np.array(
            [-0.2436698, 0.66299148, 1.3451037, -0.68059041, 0.40516614])
        expected_discrimination = np.array(
            [1.99859796, 0.67420679, 1.18591025, 1.60937911, 1.19672389])
        expected_rayleigh_scale = 0.9106036068099617

        np.testing.assert_allclose(result['Difficulty'],
                                   expected_difficulty,
                                   atol=1e-3,
                                   rtol=1e-3)
        np.testing.assert_allclose(result['Discrimination'],
                                   expected_discrimination,
                                   atol=1e-3,
                                   rtol=1e-3)
        self.assertAlmostEqual(result['Rayleigh_Scale'],
                               expected_rayleigh_scale, 3)
Пример #5
0
    def test_2pl_mml_eap_method_csirt(self):
        """Testing the 2PL EAP/MML Method with CSIRT."""
        np.random.seed(779841)

        n_items = 10
        n_people = 300
        difficulty = stats.norm(0, 1).rvs(n_items)
        discrimination = stats.rayleigh(loc=0.25, scale=.8).rvs(n_items)
        thetas = np.random.randn(n_people)
        syn_data = create_synthetic_irt_dichotomous(difficulty, discrimination,
                                                    thetas)

        result = twopl_mml_eap(syn_data, {'estimate_distribution': True})

        # Smoke Tests / Regression Tests
        expected_difficulty = np.array([
            -0.91561408, 1.29631473, 1.01751178, -0.10536047, -0.02235909,
            -0.56510317, -1.67564893, -1.45646904, 1.89544833, -0.78602385
        ])
        expected_discrimination = np.array([
            0.9224411, 0.88102312, 0.86716565, 1.38012222, 0.67176012,
            1.84035622, 1.58453053, 1.11488035, 1.07633054, 1.44767879
        ])
        expected_rayleigh_scale = 0.7591785686427288

        np.testing.assert_allclose(result['Difficulty'],
                                   expected_difficulty,
                                   atol=1e-3,
                                   rtol=1e-3)
        np.testing.assert_allclose(result['Discrimination'],
                                   expected_discrimination,
                                   atol=1e-3,
                                   rtol=1e-3)
        self.assertAlmostEqual(result['Rayleigh_Scale'],
                               expected_rayleigh_scale, 3)
Пример #6
0
    def __init__(self, ecc_prior=True, p_prior=True, inc_prior=True,
                 m1_prior=True, m2_prior=True, para_prior=True,
                 para_est=0, para_err=0, m1_est=0, m1_err=0, m2_est=0, m2_err=0,
                 ecc_beta_a=0.867, ecc_beta_b=3.03,
                 ecc_J08_sig=0.3, ecc_Rexp_lamda=5.12, ecc_Rexp_a=0.781,
                 ecc_Rexp_sig=0.272, ecc_ST08_a=4.33, ecc_ST08_k=0.2431,p_gamma=-0.7,
                 mins_ary=[],maxs_ary=[]):
        ## check min and max range arrays
        if (len(mins_ary)>1) and (len(maxs_ary)>1):
            self.mins_ary = mins_ary
            self.maxs_ary = maxs_ary
        else:
            raise IOError('\n\n No min/max ranges were provided to the priors object!!')
        ## push in two manual constants
        self.days_per_year = 365.2422
        self.sec_per_year = 60*60*24*self.days_per_year
        ## choices
        ## choices:'2e', 'ST08','J08', 'RayExp', 'beta', 'uniform'. Default is 'beta'.
        self.e_prior = ecc_prior
        ## `best-fit' alpha and beta from Kipping+2013
        self.ecc_beta = stats.beta(ecc_beta_a,ecc_beta_b) #https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.beta.html
        ## 'best-fit' for the basic Rayleigh from Juric+2008
        self.ecc_J08_sig = ecc_J08_sig      ### Put this into the advanced settings ???
        ## `best-fit' alpha, lambda and sig from Kipping+2013
        self.ecc_Rexp_lamda = ecc_Rexp_lamda  ### Put this into the advanced settings ???
        self.ecc_Rexp_a = ecc_Rexp_a     ### Put this into the advanced settings ???
        self.ecc_Rexp_sig = ecc_Rexp_sig ### Put this into the advanced settings ???
        #https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rayleigh.html#scipy.stats.rayleigh
        self.ecc_R = stats.rayleigh() 
        #https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.expon.html
        self.ecc_exp = stats.expon()  
        ## `best-fit' for the Shen&Turner 2008 pdf
        self.ecc_ST08_a = ecc_ST08_a     ### Put this into the advanced settings ???
        self.ecc_ST08_k = ecc_ST08_k     ### Put this into the advanced settings ???

        #self.ecc_norm = stats.norm
        #self.ecc_norm_mean = ##
        #self.ecc_norm_sig = ##
        #self.ecc_norm.pdf(ecc,loc=self.ecc_norm_mean, scale=self.ecc_norm_sig)

        ## choices: power-law, Jeffrey's
        self.p_prior = p_prior
        self.p_gamma = p_gamma
        ## choices:sin, cos
        self.inc_prior = inc_prior
        ## choices:IMF, PDMF
        self.m1_prior = m1_prior
        ## choices:CMF, IMF, PDMF
        self.m2_prior = m2_prior
        ## choices:gauss
        self.para_prior = para_prior
        ## values necessary to calculate priors
        self.para_est = para_est
        self.para_err = para_err
        self.m1_est = m1_est
        self.m1_err = m1_err
        self.m2_est = m2_est
        self.m2_err = m2_err
Пример #7
0
 def test_rayleigh_random(self, sigma):
     """
     测试瑞利分布
     """
     randoms = [self.gen_rayleigh_random(sigma) for i in range(self.N)]
     print("==========瑞利分布==========\n", stats.rayleigh.fit(randoms), "\n")
     t1 = np.linspace(0, 10, 101)
     p, t2 = np.histogram(randoms, bins=100, normed=True)
     t2 = (t2[:-1] + t2[1:]) / 2
     plt.plot(t2, p, "ro-", t1, stats.rayleigh(loc=0, scale=sigma).pdf(t1))
Пример #8
0
def plot_maxwell(vel, label=None, draw=True):
  speed = (vel*vel).sum(1)**0.5
  loc, scale = rayleigh.fit(speed, floc=0)
  dist = rayleigh(scale=scale)
  if draw:
    plt.hist(speed, 20, density=True)
    x = np.linspace(dist.ppf(0.01), dist.ppf(0.99), 1000)
    plt.plot(x, dist.pdf(x), label=label)
    if label:
      plt.legend()
  return kstest(speed, dist.cdf)[0]
def rayleigh_dist(sc):
	"""Rayleigh Distribution"""
	#mean, variance, skew, kurt = st.rayleigh.stats(moments='mvsk')
	x = np.linspace(0,10,100)
	#print mean, variance, skew, kurt
	for i in range(len(sc)):
		rv = st.rayleigh(loc=0,scale=sc[i])
		plt.plot(x,rv.pdf(x), linewidth=4, label='$\sigma=%0.1f$' %sc[i])
	plt.title('Rayleigh Distribution', fontsize=26)
	plt.xlabel('X', fontsize=22)
	plt.ylabel('Probability', fontsize=22)
	plt.legend()
	plt.show()
Пример #10
0
def compute_prob_of_points(x, y, sma_vec, smi_vec, cent_vec=None):
    """Compute the probability of observing points as far away as (x,y) for
    a given ellipse.

    For the ellipse described by centroid, semi-major and semi-minor axes
    `sma_vec` and `smi_vec`, compute the
    probability of observing points at least as far away as x,y in
    the direction of that point.

    If no cent_vec supplied, it is computed as the centroid of the
    input points.

    Inputs
    ---------
    x, y
        (1d numpy arrays). x and y coordinates of points to fit.
    sma_vec
        (2 elt numpy array) Vector describing the semi-major axis
    smi_vec
        (2 elt numpy array) Vector describing the semi-minor axis
    cent_vec
        (2 elt numpy array) Vector describing centroid of ellipse.
        If **None**, is set to the centroid of the input points.

    Returns
    --------
    1d numpy array of the probabilities for each point.
    """
    if cent_vec is None:
        cent_vec = get_centroid_point(x, y)

    assert len(x) == len(y)
    assert len(cent_vec) == 2
    assert len(sma_vec) == 2
    assert len(smi_vec) == 2

    xy = np.vstack([x, y]).transpose()
    rel_vec = xy - cent_vec

    # The covector of a vector **v** is defined here as a vector that
    # is parallel to **v**, but has length :math:`= 1/|v|`
    # Multiplying a vector by the covector of the semi-major axis
    # gives the projected distance of that vector along that axis.
    sma_covec = sma_vec / np.linalg.norm(sma_vec)**2
    smi_covec = smi_vec / np.linalg.norm(smi_vec)**2
    coeff1 = np.dot(rel_vec, sma_covec)  # Num sigma along major axis
    coeff2 = np.dot(rel_vec, smi_covec)  # Num sigma along minor axis

    dist_sigma = np.hypot(coeff1, coeff2)
    prob = spstats.rayleigh().sf(dist_sigma)
    return prob
Пример #11
0
    def __init__(self, scale):
        if scale is None:
            self.scale = 1.0
        else:
            self.scale = scale

        self.bounds = np.array([0.999, np.inf])
        if self.scale < 0:
            raise ValueError(
                'Invalid parameters in Rayleigh distribution. Scale should be positive.'
            )
        self.parent = rayleigh(scale=self.scale)
        self.mean, self.variance, self.skewness, self.kurtosis = self.parent.stats(
            moments='mvsk')
        self.x_range_for_pdf = np.linspace(0.0, 8.0 * self.scale,
                                           RECURRENCE_PDF_SAMPLES)
Пример #12
0
    def __init__(self, name, mean, stdv, input_type=None, startpoint=None):

        self.dist_type = "ShiftedRayleigh"

        if input_type is None:
            a = stdv / ((2 - np.pi * 0.5) ** 0.5)
            x_zero = mean - stdv * (np.pi / (4 - np.pi)) ** 0.5
        else:
            a = mean
            x_zero = stdv

        # use scipy to do the heavy lifting
        self.dist_obj = rayleigh(loc=x_zero, scale=a)

        super().__init__(
            name=name, dist_obj=self.dist_obj, startpoint=startpoint,
        )
Пример #13
0
    def __init__(self, scale):
        """
        Parameters
        ----------
        scale : float, positive
            Scale parameter
        """
        assert scale > 0, "scale parameter must be positive"

        self.scale = scale
        self.sigma = scale

        # Scipy backend
        self.sp = rayleigh(scale=scale)

        # Initialize super
        super().__init__(2, scale * 2**0.5)
Пример #14
0
    def test_grm_mml_eap_method(self):
        """Testing the GRM EAP/MML Method."""
        np.random.seed(99854)

        n_items = 10
        n_people = 300
        difficulty = stats.norm(0, 1).rvs(n_items * 3).reshape(n_items, -1)
        difficulty = np.sort(difficulty, axis=1)
        discrimination = stats.rayleigh(loc=0.25, scale=.8).rvs(n_items)
        thetas = np.random.randn(n_people)
        syn_data = create_synthetic_irt_polytomous(difficulty, discrimination,
                                                   thetas)

        result = grm_mml_eap(syn_data, {'hyper_quadrature_n': 21})

        # Smoke Tests / Regression Tests
        expected_difficulty = np.array(
            [[-0.68705911, 0.25370937, 0.62872705],
             [-1.81331475, -1.52607597, 0.01957819],
             [-2.16305964, -0.51648053, 0.20447022],
             [-1.51064069, -1.18709807, 1.74368598],
             [-2.44714587, -1.01438472, -0.44406173],
             [-1.38622596, -0.11417447, 1.14001425],
             [-0.92724279, -0.11335446, 1.30273993],
             [-0.55972331, -0.28527674, 0.01131112],
             [-1.72941028, -0.34732405, 1.17681916],
             [-1.73346085, -0.12292641, 0.91797906]])
        expected_discrimination = np.array([
            1.35572245, 0.77018004, 0.92848851, 1.6339604, 0.79229545,
            2.35881697, 0.64452994, 1.86795956, 1.56986454, 1.93426233
        ])
        expected_rayleigh_scale = 0.9161607303681261

        np.testing.assert_allclose(result['Difficulty'],
                                   expected_difficulty,
                                   atol=1e-3,
                                   rtol=1e-3)
        np.testing.assert_allclose(result['Discrimination'],
                                   expected_discrimination,
                                   atol=1e-3,
                                   rtol=1e-3)
        self.assertAlmostEqual(result['Rayleigh_Scale'],
                               expected_rayleigh_scale, 3)
Пример #15
0
def Rayleigh(mean=1.):
    """
   A Rayleigh distribution function that returns a frozen distribution of the `scipy.stats.rv_continuous <http://http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.rayleigh.html#scipy.stats.rayleigh.html>`_ class.

    :param mean: mean value
    :type mean: float
    :rtype: scipy.stats.rv_continuous instance


    >>> import compmod 
    >>> ray = compmod.distributions.Rayleigh
    >>> ray = compmod.distributions.Rayleigh(5.)
    >>> ray.rvs(15)
    array([ 4.46037568,  4.80288465,  5.37309281,  4.80523501,  5.39211872,
        4.50159587,  4.99945365,  4.96324001,  5.48935765,  6.3571905 ,
        5.01412849,  4.37768037,  5.99915989,  4.71909481,  5.25259294])
        
        
    .. plot:: example_code/distributions/rayleigh.py
     :include-source:
   """
    return stats.rayleigh(scale=mean * (2. / np.pi)**.5)
Пример #16
0
def Rayleigh(mean = 1):
   """
   A Rayleigh distribution function that returns a frozen distribution of the `scipy.stats.rv_continuous <http://http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.rayleigh.html#scipy.stats.rayleigh.html>`_ class.

    :param mean: mean value
    :type mean: float
    :rtype: scipy.stats.rv_continuous instance


    >>> import compmod 
    >>> ray = compmod.distributions.Rayleigh
    >>> ray = compmod.distributions.Rayleigh(5.)
    >>> ray.rvs(15)
    array([ 4.46037568,  4.80288465,  5.37309281,  4.80523501,  5.39211872,
        4.50159587,  4.99945365,  4.96324001,  5.48935765,  6.3571905 ,
        5.01412849,  4.37768037,  5.99915989,  4.71909481,  5.25259294])
        
        
    .. plot:: example_code/distributions/rayleigh.py
     :include-source:
   """
   return stats.rayleigh(mean-1)
Пример #17
0
class TestUtil:

    def test_execute_direct(self):
        out,err = util.execute('echo test')
        assert out=='test\n' and err==''

    def test_execute_env(self):
        out,err = util.execute('sh -c "echo $test_for_execute"',env={'test_for_execute':'test'})
        assert out=='test\n' and err==''

    def test_croak(self):
        util.croak('Burp!')

    @pytest.mark.parametrize('input,output',
                            [
                            ([0,-2],[0,-1]),
                            ([-0.5,0.5],[-1,1]),
                            ([1./2.,1./3.],[3,2]),
                            ([2./3.,1./2.,1./3.],[4,3,2]),
                            ])

    def test_scale2coprime(self,input,output):
        assert np.allclose(util.scale_to_coprime(np.array(input)),
                                                 np.array(output).astype(int))

    def test_lackofprecision(self):
        with pytest.raises(ValueError):
            util.scale_to_coprime(np.array([1/333.333,1,1]))


    @pytest.mark.parametrize('rv',[stats.rayleigh(),stats.weibull_min(1.2),stats.halfnorm(),stats.pareto(2.62)])
    def test_hybridIA(self,rv):
        bins = np.linspace(0,10,100000)
        centers = (bins[1:]+bins[:-1])/2
        N_samples = bins.shape[0]-1000
        dist = rv.pdf(centers)
        selected = util.hybrid_IA(dist,N_samples)
        dist_sampled = np.histogram(centers[selected],bins)[0]/N_samples*np.sum(dist)
        assert np.sqrt(((dist - dist_sampled) ** 2).mean()) < .025 and selected.shape[0]==N_samples
Пример #18
0
def grm_mml_eap(dataset, options=None):
    """Estimate parameters for graded response model.

    Estimate the discrimination and difficulty parameters for
    a graded response model using a mixed Bayesian / Marginal Maximum
    Likelihood algorithm, good for small sample sizes

    Args:
        dataset: [n_items, n_participants] 2d array of measured responses
        options: dictionary with updates to default options

    Returns:
        results_dictionary:
        * Discrimination: (1d array) estimate of item discriminations
        * Difficulty: (2d array) estimates of item difficulties by item thresholds
        * LatentPDF: (object) contains information about the pdf
        * Rayleigh_Scale: (int) Rayleigh scale value of the discrimination prior
        * AIC: (dictionary) null model and final model AIC value
        * BIC: (dictionary) null model and final model BIC value

    Options:
        * estimate_distribution: Boolean    
        * number_of_samples: int >= 5    
        * max_iteration: int
        * distribution: callable
        * quadrature_bounds: (float, float)
        * quadrature_n: int
        * hyper_quadrature_n: int
    """
    options = validate_estimation_options(options)

    cpr_result = condition_polytomous_response(dataset, trim_ends=False)
    responses, item_counts, valid_response_mask = cpr_result
    invalid_response_mask = ~valid_response_mask

    n_items = responses.shape[0]

    # Only use LUT
    _integral_func = _solve_integral_equations_LUT
    _interp_func = create_beta_LUT((.15, 5.05, 500), (-6, 6, 500), options)

    # Quadrature Locations
    latent_pdf = LatentPDF(options)
    theta = latent_pdf.quadrature_locations

    # Compute the values needed for integral equations
    integral_counts = list()
    for ndx in range(n_items):
        temp_output = _solve_for_constants(responses[ndx,
                                                     valid_response_mask[ndx]])
        integral_counts.append(temp_output)

    # Initialize difficulty parameters for estimation
    betas = np.full((item_counts.sum(), ), -10000.0)
    discrimination = np.ones_like(betas)
    cumulative_item_counts = item_counts.cumsum()
    start_indices = np.roll(cumulative_item_counts, 1)
    start_indices[0] = 0

    for ndx in range(n_items):
        end_ndx = cumulative_item_counts[ndx]
        start_ndx = start_indices[ndx] + 1
        betas[start_ndx:end_ndx] = np.linspace(-1, 1, item_counts[ndx] - 1)
    betas_roll = np.roll(betas, -1)
    betas_roll[cumulative_item_counts - 1] = 10000

    # Set invalid index to zero, this allows minimal
    # changes for invalid data and it is corrected
    # during integration
    responses[invalid_response_mask] = 0

    # Prior Parameters
    ray_scale = 1.0
    eap_options = {
        'distribution': stats.rayleigh(loc=.25, scale=ray_scale).pdf,
        'quadrature_n': options['hyper_quadrature_n'],
        'quadrature_bounds': (0.25, 5)
    }
    prior_pdf = LatentPDF(eap_options)
    alpha_evaluation = np.zeros((eap_options['quadrature_n'], ))

    # Meta-Prior Parameter
    hyper_options = {
        'distribution': stats.lognorm(loc=0, s=0.25).pdf,
        'quadrature_n': options['hyper_quadrature_n'],
        'quadrature_bounds': (0.1, 5)
    }
    hyper_pdf = LatentPDF(hyper_options)
    hyper_evaluation = np.zeros((hyper_options['quadrature_n'], ))
    base_hyper = (hyper_pdf.weights *
                  hyper_pdf.null_distribution).astype('float128')
    linear_hyper = base_hyper * hyper_pdf.quadrature_locations

    for iteration in range(options['max_iteration']):
        previous_discrimination = discrimination.copy()
        previous_betas = betas.copy()
        previous_betas_roll = betas_roll.copy()

        # Quadrature evaluation for values that do not change
        # This is done during the outer loop to address rounding errors
        partial_int = np.ones((responses.shape[1], theta.size))
        for item_ndx in range(n_items):
            partial_int *= _graded_partial_integral(
                theta, betas, betas_roll, discrimination, responses[item_ndx],
                invalid_response_mask[item_ndx])

        # Estimate the distribution if requested
        distribution_x_weight = latent_pdf(partial_int, iteration)
        partial_int *= distribution_x_weight

        # Update the lookup table if necessary
        if (options['estimate_distribution'] and iteration > 0):
            new_options = dict(options)
            new_options.update({'distribution': latent_pdf.cubic_splines[-1]})

            _interp_func = create_beta_LUT((.15, 5.05, 500), (-6, 6, 500),
                                           new_options)

        # EAP Discrimination Parameter
        discrimination_pdf = stats.rayleigh(loc=0.25, scale=ray_scale).pdf
        base_alpha = (prior_pdf.weights * discrimination_pdf(
            prior_pdf.quadrature_locations)).astype('float128')
        linear_alpha = (base_alpha *
                        prior_pdf.quadrature_locations).astype('float128')

        for item_ndx in range(n_items):
            # pylint: disable=cell-var-from-loop

            # Indices into linearized difficulty parameters
            start_ndx = start_indices[item_ndx]
            end_ndx = cumulative_item_counts[item_ndx]

            old_values = _graded_partial_integral(
                theta, previous_betas, previous_betas_roll,
                previous_discrimination, responses[item_ndx],
                invalid_response_mask[item_ndx])
            partial_int /= old_values

            def _local_min_func(estimate):
                # Solve integrals for diffiulty estimates
                new_betas = _integral_func(estimate, integral_counts[item_ndx],
                                           distribution_x_weight, theta,
                                           _interp_func)

                betas[start_ndx + 1:end_ndx] = new_betas
                betas_roll[start_ndx:end_ndx - 1] = new_betas
                discrimination[start_ndx:end_ndx] = estimate

                new_values = _graded_partial_integral(
                    theta, betas, betas_roll, discrimination,
                    responses[item_ndx], invalid_response_mask[item_ndx])

                new_values *= partial_int
                otpt = np.sum(new_values, axis=1)

                return np.log(otpt.clip(1e-313, np.inf)).sum()

            # Mean Discrimination Value
            for ndx, disc_location in enumerate(
                    prior_pdf.quadrature_locations):
                alpha_evaluation[ndx] = _local_min_func(disc_location)

            alpha_evaluation -= alpha_evaluation.max()
            total_probability = np.exp(alpha_evaluation.astype('float128'))
            numerator = np.sum(total_probability * linear_alpha)
            denominator = np.sum(total_probability * base_alpha)

            alpha_eap = numerator / denominator

            # Reset the Value the updated discrimination estimation
            _local_min_func(alpha_eap.astype('float64'))

            new_values = _graded_partial_integral(
                theta, betas, betas_roll, discrimination, responses[item_ndx],
                invalid_response_mask[item_ndx])

            partial_int *= new_values

        # Compute the Hyper prior mean value
        for ndx, scale_value in enumerate(hyper_pdf.quadrature_locations):
            temp_distribution = stats.rayleigh(loc=0.25, scale=scale_value).pdf
            hyper_evaluation[ndx] = np.log(
                temp_distribution(discrimination) + 1e-313).sum()

        hyper_evaluation -= hyper_evaluation.max()
        hyper_evaluation = np.exp(hyper_evaluation.astype('float128'))
        ray_scale = (np.sum(hyper_evaluation * linear_hyper) /
                     np.sum(hyper_evaluation * base_hyper)).astype('float64')

        # Check Termination Criterion
        if np.abs(previous_discrimination - discrimination).max() < 1e-3:
            break

    # Recompute partial int for later calculations
    partial_int = np.ones((responses.shape[1], theta.size))
    for item_ndx in range(n_items):
        partial_int *= _graded_partial_integral(
            theta, betas, betas_roll, discrimination, responses[item_ndx],
            invalid_response_mask[item_ndx])

    # Trim difficulties to conform to standard output
    # TODO:  look where missing values are and place NAN there instead
    # of appending them to the end
    output_betas = np.full((n_items, item_counts.max() - 1), np.nan)
    for ndx, (start_ndx,
              end_ndx) in enumerate(zip(start_indices,
                                        cumulative_item_counts)):
        output_betas[ndx, :end_ndx - start_ndx - 1] = betas[start_ndx +
                                                            1:end_ndx]

    # Compute statistics for final iteration
    null_metrics = latent_pdf.compute_metrics(
        partial_int, latent_pdf.null_distribution * latent_pdf.weights, 0)
    full_metrics = latent_pdf.compute_metrics(partial_int,
                                              distribution_x_weight,
                                              latent_pdf.n_points - 3)

    # Ability estimates
    eap_abilities = _ability_eap_abstract(partial_int, distribution_x_weight,
                                          theta)

    return {
        'Discrimination': discrimination[start_indices],
        'Difficulty': output_betas,
        'Ability': eap_abilities,
        'LatentPDF': latent_pdf,
        'Rayleigh_Scale': ray_scale,
        'AIC': {
            'final': full_metrics[0],
            'null': null_metrics[0],
            'delta': null_metrics[0] - full_metrics[0]
        },
        'BIC': {
            'final': full_metrics[1],
            'null': null_metrics[1],
            'delta': null_metrics[1] - full_metrics[1]
        }
    }
for j in range(1,len(gauss_mean)):
	ax1.plot(gauss_x,mlab.normpdf(gauss_x, gauss_mean[j], gauss_sigma[1]), linewidth=4, label='$\mu=%d, \sigma^2=%0.1f$' %(gauss_mean[j], gauss_variance[1]))
ax1.set_title('Gaussian Distribution', fontsize=26)
#ax1.set_xlabel('X', fontsize=22)
ax1.set_ylabel('Probability', fontsize=22)
ax1.set_yscale('log')
ax1.legend(loc='best')


# Rayleigh Distribution
rayl_scale = [0.5, 1, 2, 3, 4]
#mean, variance, skew, kurt = st.rayleigh.stats(moments='mvsk')
rayl_x = np.linspace(0,10,100)
#print mean, variance, skew, kurt
for i in range(len(rayl_scale)):
	rv = st.rayleigh(loc=0,scale=rayl_scale[i])
	ax2.plot(rayl_x,rv.pdf(rayl_x), linewidth=4, label='$\sigma=%0.1f$' %rayl_scale[i])
ax2.set_title('Rayleigh Distribution', fontsize=26)
#ax2.set_xlabel('X', fontsize=22)
ax2.set_ylabel('Probability', fontsize=22)
ax2.set_yscale('log')
ax2.legend(loc='best')


# Gamma Distribution
gamma_shape = [0.5, 1, 2, 3, 5, 7.5, 9]
gamma_scale = [1, 2, 2, 2, 1, 1, 0.5]
gamma_x = np.linspace(0,30,100)
for i in range(len(gamma_scale)):
	rv = st.gamma(gamma_shape[i], loc=0, scale=gamma_scale[i])
	ax3.plot(rv.pdf(gamma_x), linewidth=4, label='$k=%0.1f, \Theta=%0.1f $' %(gamma_shape[i], gamma_scale[i]))
Пример #20
0
def main(argv):
    # Get and parse the command line arguments
    image_loc, target_name = get_arguments(argv)

    # Read the input image
    img = cv2.imread(image_loc, 0)

    # Check if image exists or not
    if (img is None):
        print ("Cannot open {} image".format(image_loc))
        print ("Make sure you provide the correct image path")
        sys.exit(2)

    # Calculate the input images' histogram
    input_hist = cv2.calcHist([img], [0], None, [256], [0,256])

    # Normalize the input histogram
    total = sum(input_hist)
    input_hist /= total

    # Calculate the cumulative input histogram
    cum_input_hist = []
    cum = 0.0
    for i in range(len(input_hist)):
        cum += input_hist[i][0]
        cum_input_hist.append(cum)

    # Calculate the variance of the image
    input_img_var = np.var(img)

    # Calculate the variance of the image square
    input_img_sqr_var = np.var(img**2)

    # Calculate the target dist for diff dist's
    target_dist = []
    target_hist = None
    if (target_name == "uniform"):
        # Import the package of the target distribution
        from scipy.stats import uniform

        # Create uniform distribution object
        unif_dist = uniform(0, 246)

        # Calculate the target distribution
        for i in range(0, 246):
            x = unif_dist.pdf(i)
            target_dist.append(x)
        for i in range(246, 256):
            target_dist.append(0)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

    elif (target_name == "normal"):
        # Import the package of the target distribution
        from scipy.stats import norm

        # Create standard normal distribution object
        norm_dist = norm(0, 1)

        # Calculate the target distribution
        for i in range(0, 256):
            x = norm_dist.pdf(i/42.0 - 3)
            target_dist.append(x)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    elif (target_name == "rayleigh"):
        # Import the package of the target distribution
        from scipy.stats import rayleigh

        # Create rayleigh distribution object
        rayleigh_dist = rayleigh(0.5)

        # Calculate the target distribution
        for i in range(0, 256):
            x = rayleigh_dist.pdf(i/128.0)
            target_dist.append(x)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    elif (target_name == "gamma"):
        # Import the package of the target distribution
        from scipy.stats import gamma

        # Create gamma distribution object
        gamma_dist = gamma(0.5, 0, 1.0)

        # Calculate the target distribution
        target_dist.append(1)
        for i in range(1, 256):
            x = gamma_dist.pdf(i/256.0)
            target_dist.append(x)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    elif (target_name == "weibull"):
        # Import the package of the target distribution
        from scipy.stats import weibull_min

        # Create weibull distribution object
        weibull_dist = weibull_min(c=1.4, scale=input_img_var)

        # Calculate the target distribution
        for i in range(0, 256):
            x = weibull_dist.pdf(i/256.0)
            target_dist.append(x)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    elif (target_name == "beta1"):
        # Import the package of the target distribution
        from scipy.stats import beta

        # Create beta distribution object
        beta_dist = beta(0.5, 0.5)

        # Calculate the target distribution
        target_dist.append(6)
        for i in range(1, 255):
            x = beta_dist.pdf(i/256.0)
            target_dist.append(x)
        target_dist.append(6)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    elif (target_name == "beta2"):
        # Import the package of the target distribution
        from scipy.stats import beta

        # Create beta distribution object
        beta_dist = beta(5, 1)

        # Calculate the target distribution
        for i in range(0, 255):
            x = beta_dist.pdf(i/256.0)
            target_dist.append(x)
        target_dist.append(6)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    elif (target_name == "lognorm"):
        # Import the package of the target distribution
        from scipy.stats import lognorm

        # Create lognorm distribution object
        lognorm_dist = lognorm(1)

        # Calculate the target distribution
        for i in range(0, 256):
            x = lognorm_dist.pdf(i/100.0)
            target_dist.append(x)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    elif (target_name == "laplace"):
        # Import the package of the target distribution
        from scipy.stats import laplace

        # Create lognorm distribution object
        laplace_dist = laplace(4)

        # Calculate the target distribution
        target_dist.append(0)
        for i in range(1, 256):
            x = laplace_dist.pdf(i/256.0)
            target_dist.append(x)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    elif (target_name == "beta3"):
        # Import the package of the target distribution
        from scipy.stats import beta

        # Create beta distribution object
        beta_dist = beta(8, 2)

        # Calculate the target distribution
        for i in range(0, 255):
            x = beta_dist.pdf(i/256.0)
            target_dist.append(x)
        target_dist.append(0)

        # Calculate the target histogram
        target_hist = np.ndarray(shape=(256,1))
        for i in range(0,256):
            target_hist[i][0] = target_dist[i]

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    else: # Image itself is a target distribution case
        # Read the image
        target_dist = cv2.imread(target_name, 0)

        # Check if image is read or not
        if (target_dist is None):
            print ("{} is not a valid target name (or) image does not exist".format(target_name))
            print ("Make sure you give correct target name or correct target image location")
            sys.exit(2)

        # Create target histogram from the image
        target_hist = cv2.calcHist([target_dist], [0], None, [256], [0,256])

        # Normalize the target histogram
        total = sum(target_hist)
        target_hist /= total

    # Calculate the cumulative target histogram
    cum_target_hist = []
    cum = 0.0
    for i in range(len(target_hist)):
        cum += target_hist[i][0]
        cum_target_hist.append(cum)

    # Obtain the mapping from the input hist to target hist
    lookup = {}
    for i in range(len(cum_input_hist)):
        min_val = abs(cum_target_hist[0] - cum_input_hist[i])
        min_j = 0

        for j in range(1, len(cum_target_hist)):
            val = abs(cum_target_hist[j] - cum_input_hist[i])
            if (val < min_val):
                min_val = val
                min_j = j

        lookup[i] = min_j

    # Create the transformed image using the img's pixel values and the lookup table
    trans_img = img.copy()
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            trans_img[i][j] = lookup[img[i][j]]

    # Write the transformed image to a png file
    cv2.imwrite('images/transformed.png', trans_img)

    # Plot the input image and the target image in one plot
    input_img_resized = cv2.resize(img, (0,0), None, 0.25, 0.25)
    trans_img_resized = cv2.resize(trans_img, (0,0), None, 0.25, 0.25)
    numpy_horiz = np.hstack((input_img_resized, trans_img_resized))
    cv2.imshow('Input image ------------------------ Trans image', numpy_horiz)
    cv2.waitKey(25)

    # Calculate the transformed image's histogram
    trans_hist = cv2.calcHist([trans_img], [0], None, [256], [0,256])

    # Normalize the transformed image's histogram
    total = sum(trans_hist)
    trans_hist /= total

    # Convert cum_input_hist to matrix for plotting
    cum_input_hist_matrix = np.ndarray(shape=(256,1))
    for i in range(0,256):
        cum_input_hist_matrix[i][0] = cum_input_hist[i]

    # Calculate the cum transformed histogram for plotting
    cum_trans_hist = np.ndarray(shape=(256,1))
    cum = 0.0
    for i in range(0,256):
        cum += trans_hist[i][0]
        cum_trans_hist[i][0] = cum

    # Convert cum_target_hist to matrix for plotting
    cum_target_hist_matrix = np.ndarray(shape=(256,1))
    for i in range(0,256):
        cum_target_hist_matrix[i][0] = cum_target_hist[i]

    plt.subplot(2, 3, 1)
    plt.title('Original hist')
    plt.plot(input_hist)

    plt.subplot(2, 3, 2)
    plt.title('Original cdf')
    plt.plot(cum_input_hist_matrix)

    plt.subplot(2, 3, 3)
    plt.title('Target pdf')
    plt.plot(target_hist)

    plt.subplot(2, 3, 4)
    plt.title('Transformed hist')
    plt.plot(trans_hist)

    plt.subplot(2, 3, 5)
    plt.title('Transformed cdf')
    plt.plot(cum_trans_hist)

    plt.subplot(2, 3, 6)
    plt.title('Target cdf')
    plt.plot(cum_target_hist_matrix)
    plt.show()
Пример #21
0
    # noise parameters
    mean_noise = 0
    std_noise = np.sqrt(1 / (2 * R * snr))
    #print("\nVariance: ", std_noise)
    noise = mean_noise + std_noise * np.random.randn(
        size_test_data, M)  # randn => standard normal distribution

    ################################################################################
    ################################################################################
    # HEY JOHN, HERE'S WHERE I NEED YOUR HELP
    # fading
    # https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rayleigh.html

    #fading = ss.rayleigh().pdf(ss.rayleigh.rvs(size=M))
    fading = ss.rayleigh().pdf(
        np.linspace(ss.rayleigh.ppf(0.01), ss.rayleigh.ppf(0.99), M))
    fading = fading * 0.631  # correct for skewness
    signal = signal - fading

    ################################################################################
    ################################################################################

    # construct signal
    signal = signal + noise

    signal = np.round(signal)

    # compute errors
    errors = np.not_equal(signal, test_data)  # boolean test
    ber[i] = np.mean(errors)
Пример #22
0
 def test_Exponential_to_Rayleigh(self):
     X = RV(Exponential(rate=5))
     sims = sqrt(X).sim(Nsim)
     cdf = stats.rayleigh(scale=1 / sqrt(2 * 5)).cdf
     pval = stats.kstest(sims, cdf).pvalue
     self.assertTrue(pval > .01)
Пример #23
0
def rayleigh():
    return stats.rayleigh()
Пример #24
0
def make_rayleigh_rand_var_of_abs_fft_noise(squared_energy_of_window, variance_of_noise_in_time_domain):
    rayleigh_scale_parameter_abs_fft_noise = calc_rayleigh_scale_parameter_abs_fft_noise(
        squared_energy_of_window, variance_of_noise_in_time_domain
    )
    return rayleigh(loc=0.0, scale=rayleigh_scale_parameter_abs_fft_noise)
Пример #25
0
def run():

    telescope_area = np.pi * (1 * u.m)**2
    total_efficiency = 0.7
    observation_cadence = 5 * u.s
    observation_duration = 2 * u.hr
    telescope_random_seed = 99
    approximate_num_flares = 60

    # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
    #  Create an observation target:
    spectral_band = eep.simulate.spectral.JohnsonPhotometricBand('U')
    emission_type = eep.simulate.spectral.SpectralEmitter(spectral_band,
                                                          magnitude=16)

    star_radius = 1. * u.R_sun
    star_rotation_rate = 2 * pi_u / observation_duration  # Spin quickly for demo purposes

    MyStar = eep.simulate.Star(radius=star_radius,
                               spectral_type=emission_type,
                               rotation_rate=star_rotation_rate,
                               limb=eep.simulate.Limb(
                                   'nonlinear', coeffs=[0.5, 0.1, 0.1, -0.1]),
                               name="My Star",
                               point_color='saddlebrown')

    MyStar.set_view_from_earth(0 * u.deg, 90 * u.deg)

    # eep.visualize.render_3d_planetary_system(MyStar)

    #  =============================================================  #

    #  Delta-function-like region centered on equator:
    long_1 = pi_u
    lat_1 = pi_u / 2
    region_1 = eep.simulate.active_regions.Region(long_1, lat_1)
    region_1_name = 'DelFlares Reg1'

    #  Approx. max flare intensity:
    max_intensity = MyStar.get_flux()
    min_intensity = max_intensity / 10

    #  To explicitly specify units, use a tuple of (pdf, unit):
    delta_flare_intensities = ([min_intensity,
                                max_intensity], max_intensity.unit)

    MyDeltaFlareActivity = eep.simulate.active_regions.FlareActivity(
        eep.simulate.flares.DeltaFlare,
        intensity_pdf=delta_flare_intensities,
        label=region_1_name)

    ActiveRegion1 = eep.simulate.active_regions.ActiveRegion(
        flare_activity=MyDeltaFlareActivity,
        occurrence_freq_pdf=approximate_num_flares / 2 / observation_duration,
        region=region_1)

    MyStar.add_active_regions(ActiveRegion1)

    #  =============================================================  #
    MyTelescope = eep.simulate.Telescope(collection_area=telescope_area,
                                         efficiency=total_efficiency,
                                         cadence=observation_cadence,
                                         observation_target=MyStar,
                                         random_seed=telescope_random_seed,
                                         name="My First Telescope")

    MyTelescope.prepare_continuous_observational_run(observation_duration,
                                                     print_report=True)
    MyTelescope.collect_data(save_diagnostic_data=True)

    eep.visualize.render_telescope_lightcurve(MyTelescope,
                                              flare_color='orange')

    # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
    print("""
    Now we'll create a second active regions on opposite side of the star, 
    and give it different phenomenology to make it distinct.  This active region will have a wider range
    of flare locations, so it won't disappear behind the star quite as cleanly.
    """)

    #  Hemispherical region:
    min_long_2 = -pi_u / 2
    max_long_2 = pi_u / 2
    #  Center around equator, but do not extend all the way to the poles:
    min_lat_2 = pi_u / 12
    max_lat_2 = pi_u - min_lat_2
    region_2 = eep.simulate.active_regions.Region([min_long_2, max_long_2],
                                                  [min_lat_2, max_lat_2])
    region_2_name = 'ExpFlares Reg2'

    #  To allow default units of ph/s-m², do not specify a unit (will throw a warning as a reminder):
    exponential_flare_intensities = stats.expon(scale=max_intensity)

    MyExpFlareActivity = eep.simulate.active_regions.FlareActivity(
        eep.simulate.flares.ParabolicRiseExponentialDecay,
        intensity_pdf=exponential_flare_intensities,
        onset_pdf=[1, 4] * u.s,
        decay_pdf=(stats.rayleigh(scale=10), u.s),
        label=region_2_name)

    ActiveRegion2 = eep.simulate.active_regions.ActiveRegion(
        flare_activity=MyExpFlareActivity,
        occurrence_freq_pdf=approximate_num_flares / 2 / observation_duration,
        region=region_2)

    MyStar.add_active_regions(ActiveRegion2)

    #  =============================================================  #

    #  Telescope is already defined and already knows changes in star, just need to re-prepare observations:

    MyTelescope.prepare_continuous_observational_run(observation_duration,
                                                     print_report=True)
    MyTelescope.collect_data(save_diagnostic_data=True)

    eep.visualize.render_telescope_lightcurve(MyTelescope,
                                              flare_color={
                                                  region_1_name: "orange",
                                                  region_2_name: "b"
                                              })

    #  =============================================================  #
    MyTelescope.prepare_continuous_observational_run(observation_duration * 5)
    MyTelescope.collect_data(save_diagnostic_data=True)

    print("""
    Upon close inspection of the lightcurve, you can see the general trend of alternating between 
    delta-like flares and exponential flares, which matches the star's rotation."""
          )

    eep.visualize.interactive_lightcurve(MyTelescope._time_domain,
                                         MyTelescope._pure_lightcurve)
Пример #26
0
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 05 13:24:41 2015

@author: rarossi
"""
import scipy as sp
from scipy import stats
def printppf(model, rv):
    print '%s\t%.2f\t%.2f\t%.2f\t%.2f' % (model, rv.ppf(0.9),rv.ppf(0.99),rv.ppf(0.999),rv.ppf(0.9999))
    
data = sp.loadtxt('data25h.txt',skiprows=1, usecols= (1,))
data -= data.mean()

print 'Model\t90%\t99%\t99.9%\t99.99%'

l, s = stats.rayleigh.fit(data)
ray = stats.rayleigh(scale=s, loc=l)
printppf('Rayleigh', ray)

sp, l, s = stats.weibull_max.fit(data)
wei = stats.weibull_max(scale=s, loc=l, c=sp)
printppf('Weibull', wei)

l, s = stats.gumbel_r.fit(data)
gum = stats.gumbel_r(scale=s, loc=l)
printppf('Gumbel',gum)
Пример #27
0
# Calculate a few first moments:

mean, var, skew, kurt = rayleigh.stats(moments='mvsk')

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

x = np.linspace(rayleigh.ppf(0.01), rayleigh.ppf(0.99), 100)
ax.plot(x, rayleigh.pdf(x), 'r-', lw=5, alpha=0.6, label='rayleigh 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 = rayleigh()
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

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

vals = rayleigh.ppf([0.001, 0.5, 0.999])
np.allclose([0.001, 0.5, 0.999], rayleigh.cdf(vals))
# True

# Generate random numbers:

r = rayleigh.rvs(size=1000)

# And compare the histogram:

ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
Пример #28
0
class TestUtil:

    def test_execute_direct(self):
        out,err = util.execute('echo test')
        assert out=='test\n' and err==''

    def test_execute_env(self):
        out,err = util.execute('sh -c "echo $test_for_execute"',env={'test_for_execute':'test'})
        assert out=='test\n' and err==''

    def test_execute_runtime_error(self):
        with pytest.raises(RuntimeError):
            util.execute('false')

    @pytest.mark.parametrize('input,output',
                            [
                            ([0,-2],[0,-1]),
                            ([-0.5,0.5],[-1,1]),
                            ([1./2.,1./3.],[3,2]),
                            ([2./3.,1./2.,1./3.],[4,3,2]),
                            ])

    def test_scale2coprime(self,input,output):
        assert np.allclose(util.scale_to_coprime(np.array(input)),
                                                 np.array(output).astype(int))

    def test_lackofprecision(self):
        with pytest.raises(ValueError):
            util.scale_to_coprime(np.array([1/333.333,1,1]))


    @pytest.mark.parametrize('rv',[stats.rayleigh(),stats.weibull_min(1.2),stats.halfnorm(),stats.pareto(2.62)])
    def test_hybridIA(self,rv):
        bins = np.linspace(0,10,100000)
        centers = (bins[1:]+bins[:-1])/2
        N_samples = bins.shape[0]-1000
        dist = rv.pdf(centers)
        selected = util.hybrid_IA(dist,N_samples)
        dist_sampled = np.histogram(centers[selected],bins)[0]/N_samples*np.sum(dist)
        assert np.sqrt(((dist - dist_sampled) ** 2).mean()) < .025 and selected.shape[0]==N_samples

    @pytest.mark.parametrize('point,direction,normalize,keepdims,answer',
                             [
                              ([1,0,0],'z',False,True, [1,0,0]),
                              ([1,0,0],'z',True, False,[1,0]),
                              ([0,1,1],'z',False,True, [0,0.5,0]),
                              ([0,1,1],'y',True, False,[0.41421356,0]),
                              ([1,1,0],'x',False,False,[0.5,0]),
                              ([1,1,1],'y',True, True, [0.3660254, 0,0.3660254]),
                             ])
    def test_project_stereographic(self,point,direction,normalize,keepdims,answer):
        assert np.allclose(util.project_stereographic(np.array(point),direction=direction,
                                                      normalize=normalize,keepdims=keepdims),answer)

    @pytest.mark.parametrize('fro,to,mode,answer',
                             [
                              ((),(1,),'left',(1,)),
                              ((1,),(7,),'right',(1,)),
                              ((1,2),(1,1,2,2),'right',(1,1,2,1)),
                              ((1,2),(1,1,2,2),'left',(1,1,1,2)),
                              ((1,2,3),(1,1,2,3,4),'right',(1,1,2,3,1)),
                              ((10,2),(10,3,2,2,),'right',(10,1,2,1)),
                              ((10,2),(10,3,2,2,),'left',(10,1,1,2)),
                              ((2,2,3),(2,2,2,3,4),'left',(1,2,2,3,1)),
                              ((2,2,3),(2,2,2,3,4),'right',(2,2,1,3,1)),
                             ])
    def test_shapeshifter(self,fro,to,mode,answer):
        assert util.shapeshifter(fro,to,mode) == answer

    @pytest.mark.parametrize('fro,to,mode',
                             [
                              ((10,3,4),(10,3,2,2),'left'),
                              ((2,3),(10,3,2,2),'right'),
                             ])
    def test_invalid_shapeshifter(self,fro,to,mode):
        with pytest.raises(ValueError):
            util.shapeshifter(fro,to,mode)

    @pytest.mark.parametrize('a,b,answer',
                             [
                              ((),(1,),(1,)),
                              ((1,),(),(1,)),
                              ((1,),(7,),(1,7)),
                              ((2,),(2,2),(2,2)),
                              ((1,2),(2,2),(1,2,2)),
                              ((1,2,3),(2,3,4),(1,2,3,4)),
                              ((1,2,3),(1,2,3),(1,2,3)),
                             ])
    def test_shapeblender(self,a,b,answer):
        assert util.shapeblender(a,b) == answer

    @pytest.mark.parametrize('style',[util.emph,util.deemph,util.warn,util.strikeout])
    def test_decorate(self,style):
        assert 'DAMASK' in style('DAMASK')

    @pytest.mark.parametrize('complete',[True,False])
    def test_D3D_base_group(self,tmp_path,complete):
        base_group = ''.join(random.choices('DAMASK', k=10))
        with h5py.File(tmp_path/'base_group.dream3d','w') as f:
            f.create_group(os.path.join(base_group,'_SIMPL_GEOMETRY'))
            if complete:
                f[os.path.join(base_group,'_SIMPL_GEOMETRY')].create_dataset('SPACING',data=np.ones(3))

        if complete:
            assert base_group == util.DREAM3D_base_group(tmp_path/'base_group.dream3d')
        else:
            with pytest.raises(ValueError):
                util.DREAM3D_base_group(tmp_path/'base_group.dream3d')

    @pytest.mark.parametrize('complete',[True,False])
    def test_D3D_cell_data_group(self,tmp_path,complete):
        base_group = ''.join(random.choices('DAMASK', k=10))
        cell_data_group = ''.join(random.choices('KULeuven', k=10))
        cells = np.random.randint(1,50,3)
        with h5py.File(tmp_path/'cell_data_group.dream3d','w') as f:
            f.create_group(os.path.join(base_group,'_SIMPL_GEOMETRY'))
            f[os.path.join(base_group,'_SIMPL_GEOMETRY')].create_dataset('SPACING',data=np.ones(3))
            f[os.path.join(base_group,'_SIMPL_GEOMETRY')].create_dataset('DIMENSIONS',data=cells[::-1])
            f[base_group].create_group(cell_data_group)
            if complete:
                f[os.path.join(base_group,cell_data_group)].create_dataset('data',shape=np.append(cells,1))

        if complete:
            assert cell_data_group == util.DREAM3D_cell_data_group(tmp_path/'cell_data_group.dream3d')
        else:
            with pytest.raises(ValueError):
                util.DREAM3D_cell_data_group(tmp_path/'cell_data_group.dream3d')


    @pytest.mark.parametrize('full,reduced',[({},                           {}),
                                             ({'A':{}},                     {}),
                                             ({'A':{'B':{}}},               {}),
                                             ({'A':{'B':'C'}},)*2,
                                             ({'A':{'B':{},'C':'D'}},       {'A':{'C':'D'}})])
    def test_prune(self,full,reduced):
        assert util.dict_prune(full) == reduced


    @pytest.mark.parametrize('full,reduced',[({},                           {}),
                                             ({'A':{}},                     {}),
                                             ({'A':'F'},                    'F'),
                                             ({'A':{'B':{}}},               {}),
                                             ({'A':{'B':'C'}},              'C'),
                                             ({'A':1,'B':2},)*2,
                                             ({'A':{'B':'C','D':'E'}},      {'B':'C','D':'E'}),
                                             ({'B':'C','D':'E'},)*2,
                                             ({'A':{'B':{},'C':'D'}},       {'B':{},'C':'D'})])
    def test_flatten(self,full,reduced):
        assert util.dict_flatten(full) == reduced


    def test_double_Bravais_to_Miller(self):
        with pytest.raises(KeyError):
            util.Bravais_to_Miller(uvtw=np.ones(4),hkil=np.ones(4))

    def test_double_Miller_to_Bravais(self):
        with pytest.raises(KeyError):
            util.Miller_to_Bravais(uvw=np.ones(4),hkl=np.ones(4))


    @pytest.mark.parametrize('vector',np.array([
                                                [1,0,0],
                                                [1,1,0],
                                                [1,1,1],
                                                [1,0,-2],
                                               ]))
    @pytest.mark.parametrize('kw_Miller,kw_Bravais',[('uvw','uvtw'),('hkl','hkil')])
    def test_Miller_Bravais_Miller(self,vector,kw_Miller,kw_Bravais):
        assert np.all(vector == util.Bravais_to_Miller(**{kw_Bravais:util.Miller_to_Bravais(**{kw_Miller:vector})}))

    @pytest.mark.parametrize('vector',np.array([
                                                [1,0,-1,2],
                                                [1,-1,0,3],
                                                [1,1,-2,-3],
                                                [0,0,0,1],
                                               ]))
    @pytest.mark.parametrize('kw_Miller,kw_Bravais',[('uvw','uvtw'),('hkl','hkil')])
    def test_Bravais_Miller_Bravais(self,vector,kw_Miller,kw_Bravais):
        assert np.all(vector == util.Miller_to_Bravais(**{kw_Miller:util.Bravais_to_Miller(**{kw_Bravais:vector})}))
Пример #29
0
 def exp_to_scipy_distribution(self, p: RayleighEP) -> Any:
     return ss.rayleigh(scale=np.sqrt(p.chi / 2.0))
Пример #30
0
    plt.xlabel("Datos")
    plt.ylabel("Frecuencia de aparición")
    plt.show()
    """
    5) Con los datos contenidos en datos.csv, encontrar la mejor curva de ajuste y 
      graficar la curva de ajuste encontrada
    """
    #Gráfica todas las curvas.
    plt.hist(DATOS, bins=BINS, alpha=0.7, color="lightblue", normed=True)

    xt = plt.xticks()[0]
    xmin, xmax = 0, max(xt)
    lnspc = np.linspace(xmin, xmax, len(DATOS))

    c, d = rayleigh.fit(DATOS)
    modelo = rayleigh(c, d)
    pdf_g = rayleigh.pdf(lnspc, c, d)
    plt.plot(lnspc, pdf_g, 'k-', lw=5, alpha=1, color="blue", label='Rayleigh')

    e, f = norm.fit(DATOS)
    pdf_g = norm.pdf(lnspc, e, f)
    plt.plot(lnspc, pdf_g, 'r-', lw=2, alpha=0.5, color="red", label='Normal')

    g, h = uniform.fit(DATOS)
    pdf_g = uniform.pdf(lnspc, g, h)
    plt.plot(lnspc,
             pdf_g,
             'r-',
             lw=2,
             alpha=0.5,
             color="black",
Пример #31
0
def debug_sampler_and_plot():

    sampler = Basic_Sampler('gpu')

    # gamma
    output = sampler.gamma(np.ones(1000)*4.5, 5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 100, 100), stats.gamma.pdf(np.linspace(0, 100, 100), 4.5, scale=5))
    plt.title('gamma(4.5, 5)')
    plt.show()

    # standard_gamma
    output = sampler.standard_gamma(np.ones(1000)*4.5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 20, 100), stats.gamma.pdf(np.linspace(0, 20, 100), 4.5))
    plt.title('standard_gamma(4.5)')
    plt.show()

    # dirichlet
    output = sampler.dirichlet(np.ones(1000)*4.5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    # x = np.linspace(np.min(output), np.max(output), 100)
    # plt.plot(x, stats.dirichlet.pdf(x, alpha=np.ones(100)*4.5))
    plt.title('dirichlet(4.5)')
    plt.show()

    # beta
    output = sampler.beta(np.ones(1000)*0.5, 0.5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 1, 100), stats.beta.pdf(np.linspace(0, 1, 100), 0.5, 0.5))
    plt.title('beta(0.5, 0.5)')
    plt.show()

    # beta(2, 5)
    output = sampler.beta(np.ones(1000)*2, 5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 1, 100), stats.beta.pdf(np.linspace(0, 1, 100), 2, 5))
    plt.title('beta(2, 5)')
    plt.show()

    # normal
    output = sampler.normal(np.ones(1000)*5, np.ones(1000)*2)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(-2, 13, 100), stats.norm.pdf(np.linspace(-2, 13, 100), 5, scale=2))
    plt.title('normal(5, 2)')
    plt.show()

    # standard_normal
    output = sampler.standard_normal(1000)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(-3, 3, 100), stats.norm.pdf(np.linspace(-3, 3, 100)))
    plt.title('standard_normal()')
    plt.show()

    # uniform
    output = sampler.uniform(np.ones(1000)*(-2), np.ones(1000)*5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(-3, 6, 100), stats.uniform.pdf(np.linspace(-3, 6, 100), -2, 7))
    plt.title('uniform(-2, 5)')
    plt.show()

    # standard_uniform
    output = sampler.standard_uniform(1000)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(-0.3, 1.3, 100), stats.uniform.pdf(np.linspace(-0.3, 1.3, 100)))
    plt.title('standard_uniform()')
    plt.show()

    # binomial
    output = sampler.binomial(np.ones(1000)*10, np.ones(1000)*0.5)
    plt.figure()
    plt.hist(output, bins=np.max(output)-np.min(output), density=True, range=(np.min(output)-0.5, np.max(output)-0.5))
    # plt.scatter(np.arange(10), stats.binom._pmf(np.arange(10), 10, 0.5), c='orange', zorder=10)
    plt.title('binomial(10, 0.5)')
    plt.show()

    # negative_binomial
    output = sampler.negative_binomial(np.ones(1000)*10, 0.5)
    plt.figure()
    plt.hist(output, bins=np.max(output)-np.min(output), density=True, range=(np.min(output)-0.5, np.max(output)-0.5))
    plt.scatter(np.arange(30), stats.nbinom._pmf(np.arange(30), 10, 0.5), c='orange', zorder=10)
    plt.title('negative_binomial(10, 0.5)')
    plt.show()

    # multinomial
    output = sampler.multinomial(5, [0.8, 0.2], 1000)
    # output = sampler.multinomial([10]*4, [[0.8, 0.2]]*4, 3)
    plt.figure()
    plt.hist(output[0], bins=10, density=True)
    plt.title('multinomial(5, [0.8, 0.2])')
    plt.show()

    a = np.array([np.array([[i] * 6 for i in range(6)]).reshape(-1), np.array(list(range(6)) * 6)]).T
    output = stats.multinomial(n=5, p=[0.8, 0.2]).pmf(a)
    sns.heatmap(output.reshape(6, 6), annot=True)
    plt.ylabel('number of the 1 kind(p=0.8)')
    plt.xlabel('number of the 2 kind(p=0.2)')
    plt.title('stats.multinomial(n=5, p=[0.8, 0.2])')
    plt.show()

    # poisson
    output = sampler.poisson(np.ones(1000)*10)
    plt.figure()
    plt.hist(output, bins=22, density=True, range=(-0.5, 21.5))
    plt.scatter(np.arange(20), stats.poisson.pmf(np.arange(20), 10), c='orange', zorder=10)
    plt.title('poisson(10)')
    plt.show()

    # cauchy
    output = sampler.cauchy(np.ones(1000)*1, 0.5)
    plt.figure()
    plt.hist(output, bins=20, density=True, range=(-5, 7))
    plt.plot(np.linspace(-5, 7, 100), stats.cauchy.pdf(np.linspace(-5, 7, 100), 1, 0.5))
    plt.title('cauchy(1, 0.5)')
    plt.show()

    # standard_cauchy
    output = sampler.standard_cauchy(1000)
    plt.figure()
    plt.hist(output, bins=20, density=True, range=(-7, 7))
    plt.plot(np.linspace(-7, 7, 100), stats.cauchy.pdf(np.linspace(-7, 7, 100)))
    plt.title('standard_cauchy()')
    plt.show()

    # chisquare
    output = sampler.chisquare(np.ones(1000)*10)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 30, 100), stats.chi2.pdf(np.linspace(0, 30, 100), 10))
    plt.title('chisquare(10)')
    plt.show()

    # noncentral_chisquare
    output = sampler.noncentral_chisquare(np.ones(1000)*10, 5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    # nocentral_chi2 = scale^2 * (chi2 + 2*loc*chi + df*loc^2)
    # E(Z) = nonc + df
    # Var(Z) = 2(df+2nonc)
    plt.title('noncentral_chisquare(df=10, nonc=5)')
    plt.show()

    # exponential
    lam = 0.5
    output = sampler.exponential(np.ones(1000)*lam)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0.01, 4, 100), stats.expon.pdf(np.linspace(0.01, 4, 100), scale=0.5))
    plt.title('exponential(0.5)')
    plt.show()

    # standard_exponential
    output = sampler.standard_exponential(1000)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0.01, 8, 100), stats.expon.pdf(np.linspace(0.01, 8, 100)))
    plt.title('standard_exponential()')
    plt.show()

    # f
    output = sampler.f(np.ones(1000)*10, 10)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 8, 100), stats.f.pdf(np.linspace(0, 8, 100), 10, 10))
    plt.title('f(10, 10)')
    plt.show()

    # noncentral_f
    output = sampler.noncentral_f(np.ones(1000)*10, 10, 5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    # E(F) = (m+nonc)*n / (m*(n-2)), n>2.
    # Var(F) = 2*(n/m)**2 * ((m+nonc)**2 + (m+2*nonc)*(n-2)) / ((n-2)**2 * (n-4))
    plt.title('noncentral_f(dfnum=10, dfden=10, nonc=5)')
    plt.show()

    # geometric
    output = sampler.geometric(np.ones(1000)*0.1)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.scatter(np.arange(50), stats.geom.pmf(np.arange(50), p=0.1), c='orange', zorder=10)
    plt.title('geometric(0.1)')
    plt.show()

    # gumbel
    output = sampler.gumbel(np.ones(1000)*5, np.ones(1000)*2)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 20, 100), stats.gumbel_r.pdf(np.linspace(0, 20, 100)+0.01, 5, scale=2))
    plt.title('gumbel(5, 2)')
    plt.show()
    np.random.gumbel()

    # hypergeometric
    output = sampler.hypergeometric(np.ones(1000)*5, 10, 10)
    plt.figure()
    plt.hist(output, bins=np.max(output)-np.min(output), density=True, range=(np.min(output)+0.5, np.max(output)+0.5))
    plt.scatter(np.arange(10), stats.hypergeom(15, 5, 10).pmf(np.arange(10)), c='orange', zorder=10)  # hypergeom(M, n, N), total, I, tiems
    plt.title('hypergeometric(5, 10, 10)')
    plt.show()

    # laplace
    output = sampler.laplace(np.ones(1000)*5, np.ones(1000)*2)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(-10, 20, 100), stats.laplace.pdf(np.linspace(-10, 20, 100), 5, scale=2))
    plt.title('laplace(5, 2)')
    plt.show()

    # logistic
    output = sampler.logistic(np.ones(1000)*5, np.ones(1000)*2)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(-10, 20, 100), stats.logistic.pdf(np.linspace(-10, 20, 100), 5, scale=2))
    plt.title('logistic(5, 2)')
    plt.show()

    # power
    output = sampler.power(np.ones(1000)*0.5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 1.5, 100), stats.powerlaw.pdf(np.linspace(0, 1.5, 100), 0.5))
    plt.title('power(0.5)')
    plt.show()

    # zipf
    output = sampler.zipf(np.ones(1000)*1.1)
    counter = Counter(output)
    filter = np.array([[key, counter[key]] for key in counter.keys() if key < 50])
    plt.figure()
    plt.scatter(filter[:, 0], filter[:, 1] / 1000)
    plt.plot(np.arange(1, 50), stats.zipf(1.1).pmf(np.arange(1, 50)))
    plt.title('zipf(1.1)')
    plt.show()

    # pareto
    output = sampler.pareto(np.ones(1000) * 2, np.ones(1000) * 5)
    plt.figure()
    count, bins, _ = plt.hist(output, bins=50, density=True, range=(np.min(output), 100))
    a, m = 2., 5.  # shape and mode
    fit = a * m ** a / bins ** (a + 1)
    plt.plot(bins, max(count) * fit / max(fit), linewidth=2, color='r')
    plt.title('pareto(2, 5)')
    plt.show()

    # rayleigh
    output = sampler.rayleigh(np.ones(1000)*2.0)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 8, 100), stats.rayleigh(scale=2).pdf(np.linspace(0, 8, 100)))
    plt.title('rayleigh(2)')
    plt.show()

    # t
    output = sampler.t(np.ones(1000)*2.0)
    plt.figure()
    plt.hist(output, bins=20, density=True, range=(-6, 6))
    plt.plot(np.linspace(-6, 6, 100), stats.t(2).pdf(np.linspace(-6, 6, 100)))
    plt.title('t(2)')
    plt.show()

    # triangular
    output = sampler.triangular(np.ones(1000)*0.0, 0.3, 1)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 1, 100), stats.triang.pdf(np.linspace(0, 1, 100), 0.3))
    plt.title('triangular(0, 0.3, 1)')
    plt.show()

    # weibull
    output = sampler.weibull(np.ones(1000)*4.5, 5)
    plt.figure()
    plt.hist(output, bins=20, density=True)
    plt.plot(np.linspace(0, 10, 100), stats.weibull_min.pdf(np.linspace(0, 10, 100), 4.5, scale=5))
    plt.title('weibull(4.5, 5)')
    plt.show()
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as ss
'''

r = np.random.rayleigh(0.05, (100,))
print(r)

'''

x = []

t = ss.rayleigh().pdf(
    np.linspace(ss.rayleigh.ppf(0.01), ss.rayleigh.ppf(0.99), 100))
print(t)
print(t.shape)

sign = np.random.randint(0, 2, 100)

for i in range(len(sign)):
    if sign[i] == 0:
        sign[i] = -1

print(sign)

w = np.multiply(sign, t)
print(w)
Пример #33
0
    
    
potentialC = np.array( [0.1, 0.5, 1, 2, 3, 3.5] )
potentialCprime = np.linspace( 2.5, 3.55, 20)
estimates = np.zeros_like( potentialCprime)
for i,c in enumerate(potentialCprime):
    #sampling_dist = stats.norm( loc = c, scale= 5 )
    sampling_dist = stats.gamma(1, loc=Q )
    target_c = lambda x: target(x,c)
    mci = q2.MCIntegrator( target_c, interval =interval, b_antithetic = False, sampling_dist = sampling_dist, N=100000, verbose= False )
    estimates[i] = mci.estimate()
    
    


#3.0526315789473681 is about best
c_opt = 3.0526

def rayleigh(u):
    return 2*u*np.exp(-u**2)

    
def target(u):
    return ( u > Q)*rayleigh(u)/rayleigh(u-c_opt)
sampling_dist = stats.rayleigh( loc = c_opt, scale=1./np.sqrt(2))
mci = q2.MCIntegrator( target, interval =interval, b_antithetic = False, sampling_dist = sampling_dist, N=100000, verbose= False )
print mci.estimate()
# estimate: 1.13859704116e-06


Пример #34
0
def all_dists():
    # dists param were taken from scipy.stats official
    # documentaion examples
    # Total - 89
    return {
        "alpha":
        stats.alpha(a=3.57, loc=0.0, scale=1.0),
        "anglit":
        stats.anglit(loc=0.0, scale=1.0),
        "arcsine":
        stats.arcsine(loc=0.0, scale=1.0),
        "beta":
        stats.beta(a=2.31, b=0.627, loc=0.0, scale=1.0),
        "betaprime":
        stats.betaprime(a=5, b=6, loc=0.0, scale=1.0),
        "bradford":
        stats.bradford(c=0.299, loc=0.0, scale=1.0),
        "burr":
        stats.burr(c=10.5, d=4.3, loc=0.0, scale=1.0),
        "cauchy":
        stats.cauchy(loc=0.0, scale=1.0),
        "chi":
        stats.chi(df=78, loc=0.0, scale=1.0),
        "chi2":
        stats.chi2(df=55, loc=0.0, scale=1.0),
        "cosine":
        stats.cosine(loc=0.0, scale=1.0),
        "dgamma":
        stats.dgamma(a=1.1, loc=0.0, scale=1.0),
        "dweibull":
        stats.dweibull(c=2.07, loc=0.0, scale=1.0),
        "erlang":
        stats.erlang(a=2, loc=0.0, scale=1.0),
        "expon":
        stats.expon(loc=0.0, scale=1.0),
        "exponnorm":
        stats.exponnorm(K=1.5, loc=0.0, scale=1.0),
        "exponweib":
        stats.exponweib(a=2.89, c=1.95, loc=0.0, scale=1.0),
        "exponpow":
        stats.exponpow(b=2.7, loc=0.0, scale=1.0),
        "f":
        stats.f(dfn=29, dfd=18, loc=0.0, scale=1.0),
        "fatiguelife":
        stats.fatiguelife(c=29, loc=0.0, scale=1.0),
        "fisk":
        stats.fisk(c=3.09, loc=0.0, scale=1.0),
        "foldcauchy":
        stats.foldcauchy(c=4.72, loc=0.0, scale=1.0),
        "foldnorm":
        stats.foldnorm(c=1.95, loc=0.0, scale=1.0),
        # "frechet_r": stats.frechet_r(c=1.89, loc=0.0, scale=1.0),
        # "frechet_l": stats.frechet_l(c=3.63, loc=0.0, scale=1.0),
        "genlogistic":
        stats.genlogistic(c=0.412, loc=0.0, scale=1.0),
        "genpareto":
        stats.genpareto(c=0.1, loc=0.0, scale=1.0),
        "gennorm":
        stats.gennorm(beta=1.3, loc=0.0, scale=1.0),
        "genexpon":
        stats.genexpon(a=9.13, b=16.2, c=3.28, loc=0.0, scale=1.0),
        "genextreme":
        stats.genextreme(c=-0.1, loc=0.0, scale=1.0),
        "gausshyper":
        stats.gausshyper(a=13.8, b=3.12, c=2.51, z=5.18, loc=0.0, scale=1.0),
        "gamma":
        stats.gamma(a=1.99, loc=0.0, scale=1.0),
        "gengamma":
        stats.gengamma(a=4.42, c=-3.12, loc=0.0, scale=1.0),
        "genhalflogistic":
        stats.genhalflogistic(c=0.773, loc=0.0, scale=1.0),
        "gilbrat":
        stats.gilbrat(loc=0.0, scale=1.0),
        "gompertz":
        stats.gompertz(c=0.947, loc=0.0, scale=1.0),
        "gumbel_r":
        stats.gumbel_r(loc=0.0, scale=1.0),
        "gumbel_l":
        stats.gumbel_l(loc=0.0, scale=1.0),
        "halfcauchy":
        stats.halfcauchy(loc=0.0, scale=1.0),
        "halflogistic":
        stats.halflogistic(loc=0.0, scale=1.0),
        "halfnorm":
        stats.halfnorm(loc=0.0, scale=1.0),
        "halfgennorm":
        stats.halfgennorm(beta=0.675, loc=0.0, scale=1.0),
        "hypsecant":
        stats.hypsecant(loc=0.0, scale=1.0),
        "invgamma":
        stats.invgamma(a=4.07, loc=0.0, scale=1.0),
        "invgauss":
        stats.invgauss(mu=0.145, loc=0.0, scale=1.0),
        "invweibull":
        stats.invweibull(c=10.6, loc=0.0, scale=1.0),
        "johnsonsb":
        stats.johnsonsb(a=4.32, b=3.18, loc=0.0, scale=1.0),
        "johnsonsu":
        stats.johnsonsu(a=2.55, b=2.25, loc=0.0, scale=1.0),
        "ksone":
        stats.ksone(n=1e03, loc=0.0, scale=1.0),
        "kstwobign":
        stats.kstwobign(loc=0.0, scale=1.0),
        "laplace":
        stats.laplace(loc=0.0, scale=1.0),
        "levy":
        stats.levy(loc=0.0, scale=1.0),
        "levy_l":
        stats.levy_l(loc=0.0, scale=1.0),
        "levy_stable":
        stats.levy_stable(alpha=0.357, beta=-0.675, loc=0.0, scale=1.0),
        "logistic":
        stats.logistic(loc=0.0, scale=1.0),
        "loggamma":
        stats.loggamma(c=0.414, loc=0.0, scale=1.0),
        "loglaplace":
        stats.loglaplace(c=3.25, loc=0.0, scale=1.0),
        "lognorm":
        stats.lognorm(s=0.954, loc=0.0, scale=1.0),
        "lomax":
        stats.lomax(c=1.88, loc=0.0, scale=1.0),
        "maxwell":
        stats.maxwell(loc=0.0, scale=1.0),
        "mielke":
        stats.mielke(k=10.4, s=3.6, loc=0.0, scale=1.0),
        "nakagami":
        stats.nakagami(nu=4.97, loc=0.0, scale=1.0),
        "ncx2":
        stats.ncx2(df=21, nc=1.06, loc=0.0, scale=1.0),
        "ncf":
        stats.ncf(dfn=27, dfd=27, nc=0.416, loc=0.0, scale=1.0),
        "nct":
        stats.nct(df=14, nc=0.24, loc=0.0, scale=1.0),
        "norm":
        stats.norm(loc=0.0, scale=1.0),
        "pareto":
        stats.pareto(b=2.62, loc=0.0, scale=1.0),
        "pearson3":
        stats.pearson3(skew=0.1, loc=0.0, scale=1.0),
        "powerlaw":
        stats.powerlaw(a=1.66, loc=0.0, scale=1.0),
        "powerlognorm":
        stats.powerlognorm(c=2.14, s=0.446, loc=0.0, scale=1.0),
        "powernorm":
        stats.powernorm(c=4.45, loc=0.0, scale=1.0),
        "rdist":
        stats.rdist(c=0.9, loc=0.0, scale=1.0),
        "reciprocal":
        stats.reciprocal(a=0.00623, b=1.01, loc=0.0, scale=1.0),
        "rayleigh":
        stats.rayleigh(loc=0.0, scale=1.0),
        "rice":
        stats.rice(b=0.775, loc=0.0, scale=1.0),
        "recipinvgauss":
        stats.recipinvgauss(mu=0.63, loc=0.0, scale=1.0),
        "semicircular":
        stats.semicircular(loc=0.0, scale=1.0),
        "t":
        stats.t(df=2.74, loc=0.0, scale=1.0),
        "triang":
        stats.triang(c=0.158, loc=0.0, scale=1.0),
        "truncexpon":
        stats.truncexpon(b=4.69, loc=0.0, scale=1.0),
        "truncnorm":
        stats.truncnorm(a=0.1, b=2, loc=0.0, scale=1.0),
        "tukeylambda":
        stats.tukeylambda(lam=3.13, loc=0.0, scale=1.0),
        "uniform":
        stats.uniform(loc=0.0, scale=1.0),
        "vonmises":
        stats.vonmises(kappa=3.99, loc=0.0, scale=1.0),
        "vonmises_line":
        stats.vonmises_line(kappa=3.99, loc=0.0, scale=1.0),
        "wald":
        stats.wald(loc=0.0, scale=1.0),
        "weibull_min":
        stats.weibull_min(c=1.79, loc=0.0, scale=1.0),
        "weibull_max":
        stats.weibull_max(c=2.87, loc=0.0, scale=1.0),
        "wrapcauchy":
        stats.wrapcauchy(c=0.0311, loc=0.0, scale=1.0),
    }
Пример #35
0
print(res)

# Crear histograma de Y. RESPUESTA 4
a, bins, c = plt.hist(res, 200, density = 1, facecolor='blue', alpha=0.5)
plt.xlabel('Magnitud')
plt.ylabel('Frecuencia')
plt.grid(True)
plt.title(r'Histograma de mediciones de una variable fisica')
plt.savefig('res4.png')


# RESPUESTA 5
(mu, sigma) = stats.rayleigh.fit(res)
print("mu: ", mu)
print("sigma: ", sigma)
rayleigh = stats.rayleigh(mu, sigma)
print("rayleigh: ", rayleigh)
y = stats.rayleigh.pdf(bins, mu, sigma)
plt.plot(bins, y)
cdf = stats.rayleigh.cdf(bins, mu, sigma)
plt.savefig('res5.png')

# Respuesta 6. Probabilidad en el intervalo [18, 68]

print("La probabilidad en el intervalo [18, 68] es: ", rayleigh.cdf(68) - rayleigh.cdf(18))
contrast = 0
for i in range(0, len(res)):
    if res[i] <= 68 and res[i] >= 18:
        contrast += 1

contraste = float(contrast)/len(res)
Пример #36
0
def Threshold_finder(data,
                     max_population=3,
                     min_population_size=0.2,
                     confidence_interval=0.90,
                     verbose=False):
    import warnings
    warnings.filterwarnings("ignore")
    '''
    data: 1D data array with count numbers
    max_population: Define the maximal number of populations exist in sample datasets
    min_population_size: The smallest population should have at least 20% population
    confidence_interval: if unimodel was used, select the confidence interval for lower bound; 0.90 = 5% confidence one tail test
    '''

    best_population = np.inf
    best_loglike = -np.inf
    best_mdoel = None
    model_kind = 'Gaussian'  # Set Gaussian to be the default model type

    for n_components in [
            n + 1 for n in list(reversed(np.arange(max_population)))
    ]:
        BGM = BayesianGaussianMixture(n_components=n_components,
                                      verbose=0).fit(data)
        # Proceed only if the model can converge
        if BGM.converged_:
            if verbose:
                print('%s populations converged' % str(n_components))
            dict_wp = dict()  # store weighted probability for each population
            for p in np.arange(n_components):
                para = norm.fit(
                    mask_list(data, BGM.predict(data),
                              p))  # fit gaussian model to population p
                dict_wp[p] = norm(para[0], para[1]).pdf(data) * BGM.weights_[p]
            # Compute log likelyhood of prediction
            # wp[0] = norm.pdf(data[i])*weight[0], wp[1] = norm.pdf(data[i])*weight[1] ...
            # log(wp[0]+wp[1]+...) gives total log likelyhood
            loglike = sum([
                np.log(sum([dict_wp[p][i] for p in np.arange(n_components)]))
                for i in np.arange(len(data))
            ])[0]
            if loglike > best_loglike and min(
                    BGM.weights_) > min_population_size:  # minimal
                best_loglike = loglike
                best_population = n_components
                best_mdoel = BGM
            if verbose:
                print('%s model with %s population has log likelyhood of %s ' %
                      (model_kind, n_components, loglike))
        else:
            if verbose:
                print('%s populations not converged' % str(n_components))

        if n_components == 1:  # A gaussian model may not best fit one distribution; Other models should also being tested to decide if better 1 model fit exist

            para = rayleigh.fit(data)
            loglike = sum(np.log(rayleigh(para[0], para[1]).pdf(data)))[0]
            if loglike > best_loglike:
                best_loglike = loglike
                best_population = 1
                best_mdoel = rayleigh(para[0], para[1])
                model_kind = 'Rayleigh'
                if verbose:
                    print(
                        '%s model with %s population has log likelyhood of %s '
                        % (model_kind, n_components, loglike))

    if best_mdoel == None:  # nither Gaussian nor Rayleight could fit the data
        para = chi2.fit(data)
        loglike = sum(np.log(chi2(para[0], para[1], para[2]).pdf(data)))[0]
        if loglike > best_loglike:
            best_loglike = loglike
            best_population = 1
            best_mdoel = chi2(para[0], para[1], para[2])
            model_kind = 'Chi-square'
            if verbose:
                print('%s model with %s population has log likelyhood of %s ' %
                      (model_kind, n_components, loglike))

    if best_population > 1:
        p = list(best_mdoel.means_).index(
            min(best_mdoel.means_
                ))  # Get the population id that represent negatives
        threshold = max(mask_list(data, best_mdoel.predict(data), p))[0]
    else:
        if model_kind == 'Rayleigh' or model_kind == 'Chi-square':
            threshold = min(1,
                            abs(best_mdoel.interval(confidence_interval)[0]))
        else:
            para = norm.fit(data)
            threshold = min(
                1,
                abs(
                    norm(data, para[0],
                         para[1]).interval(confidence_interval)[0]))

    print(
        'Best model with %s distribution has %s populations with threshold at %s'
        % (model_kind, best_population, threshold))

    return threshold, model_kind, best_mdoel, best_population