Python scipy.stats.kurtosis() Examples

The following are 30 code examples of scipy.stats.kurtosis(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.stats , or try the search function .
Example #1
Source File: univariate.py    From mne-features with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def compute_kurtosis(data):
    """Kurtosis of the data (per channel).

    Parameters
    ----------
    data : ndarray, shape (n_channels, n_times)

    Returns
    -------
    output : ndarray, shape (n_channels,)

    Notes
    -----
    Alias of the feature function: **kurtosis**
    """
    ndim = data.ndim
    return stats.kurtosis(data, axis=ndim - 1, fisher=False) 
Example #2
Source File: test_analytics.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_kurt(self, float_frame_with_na, float_frame, float_string_frame):
        from scipy.stats import kurtosis

        def alt(x):
            if len(x) < 4:
                return np.nan
            return kurtosis(x, bias=False)

        assert_stat_op_calc('kurt', alt, float_frame_with_na)
        assert_stat_op_api('kurt', float_frame, float_string_frame)

        index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
                           codes=[[0, 0, 0, 0, 0, 0],
                                  [0, 1, 2, 0, 1, 2],
                                  [0, 1, 0, 1, 0, 1]])
        df = DataFrame(np.random.randn(6, 3), index=index)

        kurt = df.kurt()
        kurt2 = df.kurt(level=0).xs('bar')
        tm.assert_series_equal(kurt, kurt2, check_names=False)
        assert kurt.name is None
        assert kurt2.name == 'bar' 
Example #3
Source File: test_statistics.py    From ctapipe with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_kurtosis():
    from ctapipe.image.statistics import kurtosis

    np.random.seed(0)
    data = np.random.normal(5, 2, 1000)

    mean = np.mean(data)
    std = np.std(data)

    assert np.isclose(kurtosis(data), scipy.stats.kurtosis(data))
    assert np.isclose(kurtosis(data, mean=mean), scipy.stats.kurtosis(data))
    assert np.isclose(kurtosis(data, std=std), scipy.stats.kurtosis(data))
    assert np.isclose(kurtosis(data, mean=mean, std=std), scipy.stats.kurtosis(data))
    assert np.isclose(
        kurtosis(data, fisher=False), scipy.stats.kurtosis(data, fisher=False)
    ) 
Example #4
Source File: test_correct.py    From abagen with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test__rs(a):
    rs = np.random.RandomState(1234)

    # create an array with a pretty ridiculous outlier effect to try and fix
    y = rs.normal(size=(100, 1000))
    y[0] += 1000
    y[:, 0] += 1000
    out = correct._rs(y, axis=a)

    # max will always be less than one, min will always be greater than zero
    assert np.all(out.max(axis=a) <= 1) and np.all(out.min(axis=a) >= 0)

    # we should have reduced skewness / kurtosis compared to the original
    assert np.all(sstats.skew(out, axis=a) < sstats.skew(y, axis=a))
    assert np.all(sstats.kurtosis(out, axis=a) < sstats.kurtosis(y, axis=a))

    # this is a weird test; we're gonna bin the data at 0.2 intervals and make
    # sure no bins are empty. if one is something probably went wrong, right?
    for low in np.arange(0, 1, 0.2):
        hi = low + 0.2 + np.spacing(1)  # include 1
        assert np.all(np.sum(np.logical_and(out >= low, out < hi), axis=a) > 0) 
Example #5
Source File: test_correct.py    From abagen with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test__srs(a):
    rs = np.random.RandomState(1234)

    # create an array with a pretty ridiculous outlier effect to try and fix
    y = rs.normal(size=(100, 1000))
    y[0] += 1000
    y[:, 0] += 1000
    out = correct._srs(y, axis=a)

    # max will always be one, min will always be zero
    assert np.allclose(out.max(axis=a), 1) and np.allclose(out.min(axis=a), 0)

    # we should have reduced skewness / kurtosis compared to the original
    assert np.all(sstats.skew(out, axis=a) < sstats.skew(y, axis=a))
    assert np.all(sstats.kurtosis(out, axis=a) < sstats.kurtosis(y, axis=a))

    # this is a weird test; we're gonna bin the data at 0.2 intervals and make
    # sure no bins are empty. if one is something probably went wrong, right?
    for low in np.arange(0, 1, 0.2):
        hi = low + 0.2 + np.spacing(1)  # include 1
        assert np.all(np.sum(np.logical_and(out >= low, out < hi), axis=a) > 0) 
Example #6
Source File: calculate_aggregate_statistics.py    From tbp-next-basket with GNU General Public License v3.0 6 votes vote down vote up
def calculate_aggregate(values):
    agg_measures = {
        'avg': np.mean(values),
        'std': np.std(values),
        'var': np.var(values),
        'med': np.median(values),
        '10p': np.percentile(values, 10),
        '25p': np.percentile(values, 25),
        '50p': np.percentile(values, 50),
        '75p': np.percentile(values, 75),
        '90p': np.percentile(values, 90),
        'iqr': np.percentile(values, 75) - np.percentile(values, 25),
        'iqm': interquartile_range_mean(values),
        'mad': mean_absolute_deviation(values),
        'cov': 1.0 * np.mean(values) / np.std(values),
        'gin': gini_coefficient(values),
        'skw': stats.skew(values),
        'kur': stats.kurtosis(values),
        'sum': np.sum(values)
    }

    return agg_measures 
Example #7
Source File: features.py    From crowdai-musical-genre-recognition-starter-kit with MIT License 6 votes vote down vote up
def columns():
    feature_sizes = dict(chroma_stft=12, chroma_cqt=12, chroma_cens=12,
                         tonnetz=6, mfcc=20, rmse=1, zcr=1,
                         spectral_centroid=1, spectral_bandwidth=1,
                         spectral_contrast=7, spectral_rolloff=1)
    moments = ('mean', 'std', 'skew', 'kurtosis', 'median', 'min', 'max')

    columns = []
    for name, size in feature_sizes.items():
        for moment in moments:
            it = ((name, moment, '{:02d}'.format(i+1)) for i in range(size))
            columns.extend(it)

    names = ('feature', 'statistics', 'number')
    columns = pd.MultiIndex.from_tuples(columns, names=names)

    # More efficient to slice if indexes are sorted.
    return columns.sort_values() 
Example #8
Source File: test_statistics.py    From Effective-Quadratures with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_parameter_mc_least_squares_moments(self):
        x1 = Parameter(distribution='uniform',lower =0.0, upper=1.0, order=7)
        x2 = Parameter(distribution='Beta',lower =0.0, upper=1.0, shape_parameter_A = 1.6, shape_parameter_B=3.2 , order=7)
        myparameters = [x1, x2]
        mybasis = Basis('tensor-grid')
        mypoly = Poly(myparameters, mybasis, method='numerical-integration')
        mypoly.set_model(rosenbrock_fun)
        mean, variance = mypoly.get_mean_and_variance()
        skewness, kurtosis = mypoly.get_skewness_and_kurtosis()
        large_number = 3000000
        s = np.random.rand(large_number, 2)
        x1_samples = x1.get_icdf(s[:,0])
        x2_samples = x2.get_icdf(s[:,1])
        s = np.vstack([x1_samples, x2_samples]).T
        model_evals = evaluate_model(s, rosenbrock_fun)
        mean_mc = np.mean(model_evals)
        variance_mc = np.var(model_evals)
        skewness_mc = skew(model_evals)
        np.testing.assert_array_less(np.abs(mean-mean_mc)/mean * 100.0,  1.0)
        np.testing.assert_array_less(np.abs(variance-variance_mc)/variance * 100.0,  5.0)
        np.testing.assert_array_less(np.abs(skewness-skewness_mc)/skewness * 100.0,  5.0) 
Example #9
Source File: test_statistics.py    From Effective-Quadratures with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_sparse_grid_moments(self):
        mu = 1
        sigma = 2
        variance = sigma**2
        x1 = Parameter(distribution="Gaussian", shape_parameter_A=mu, shape_parameter_B=variance, order=6)
        x2 = Parameter(distribution="Gaussian", shape_parameter_A=mu, shape_parameter_B=variance, order=6)
        del variance
        parameters = [x1, x2]
        parameters = [x1, x2]
        basis = Basis('sparse-grid', level=5, growth_rule='linear')
        uqProblem = Poly(parameters, basis, method='numerical-integration')
        uqProblem.set_model(rosenbrock_fun)
        mean, variance = uqProblem.get_mean_and_variance()
        skewness, kurtosis = uqProblem.get_skewness_and_kurtosis()
        large_number = 2000000
        s = sigma * np.random.randn(large_number,2) + mu
        model_evals = evaluate_model(s, rosenbrock_fun)
        mean_mc = np.mean(model_evals)
        variance_mc = np.var(model_evals)
        skewness_mc = skew(model_evals)
        np.testing.assert_array_less(np.abs(mean-mean_mc)/mean * 100.0,  1.0)
        np.testing.assert_array_less(np.abs(variance-variance_mc)/variance * 100.0,  5.0)
        np.testing.assert_array_less(np.abs(skewness-skewness_mc)/skewness * 100.0,  5.0) 
Example #10
Source File: check_moments.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def nct_kurt_bug():
    '''test for incorrect kurtosis of nct

    D. Hogben, R. S. Pinkham, M. B. Wilk: The Moments of the Non-Central
    t-DistributionAuthor(s): Biometrika, Vol. 48, No. 3/4 (Dec., 1961),
    pp. 465-468
    '''
    from numpy.testing import assert_almost_equal
    mvsk_10_1 = (1.08372, 1.325546, 0.39993, 1.2499424941142943)
    assert_almost_equal(stats.nct.stats(10, 1, moments='mvsk'), mvsk_10_1, decimal=6)
    c1=np.array([1.08372])
    c2=np.array([.0755460, 1.25000])
    c3 = np.array([.0297802, .580566])
    c4 = np.array([0.0425458, 1.17491, 6.25])

    #calculation for df=10, for arbitrary nc
    nc = 1
    mc1 = c1.item()
    mc2 = (c2*nc**np.array([2,0])).sum()
    mc3 = (c3*nc**np.array([3,1])).sum()
    mc4 = c4=np.array([0.0425458, 1.17491, 6.25])
    mvsk_nc = mc2mvsk((mc1,mc2,mc3,mc4)) 
Example #11
Source File: test_analytics.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_kurt(self):
        from scipy.stats import kurtosis
        alt = lambda x: kurtosis(x, bias=False)
        self._check_stat_op('kurt', alt)

        index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
                           labels=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2],
                                   [0, 1, 0, 1, 0, 1]])
        s = Series(np.random.randn(6), index=index)
        tm.assert_almost_equal(s.kurt(), s.kurt(level=0)['bar'])

        # test corner cases, kurt() returns NaN unless there's at least 4
        # values
        min_N = 4
        for i in range(1, min_N + 1):
            s = Series(np.ones(i))
            df = DataFrame(np.ones((i, i)))
            if i < min_N:
                assert np.isnan(s.kurt())
                assert np.isnan(df.kurt()).all()
            else:
                assert 0 == s.kurt()
                assert (df.kurt() == 0).all() 
Example #12
Source File: test_analytics.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_kurt(self, float_frame_with_na, float_frame, float_string_frame):
        from scipy.stats import kurtosis

        def alt(x):
            if len(x) < 4:
                return np.nan
            return kurtosis(x, bias=False)

        assert_stat_op_calc('kurt', alt, float_frame_with_na)
        assert_stat_op_api('kurt', float_frame, float_string_frame)

        index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
                           codes=[[0, 0, 0, 0, 0, 0],
                                  [0, 1, 2, 0, 1, 2],
                                  [0, 1, 0, 1, 0, 1]])
        df = DataFrame(np.random.randn(6, 3), index=index)

        kurt = df.kurt()
        kurt2 = df.kurt(level=0).xs('bar')
        tm.assert_series_equal(kurt, kurt2, check_names=False)
        assert kurt.name is None
        assert kurt2.name == 'bar' 
Example #13
Source File: plotting.py    From QUANTAXIS with MIT License 6 votes vote down vote up
def plot_information_table(ic_data):
    """
    IC 统计量
    """
    ic_summary_table = pd.DataFrame()
    ic_summary_table["IC Mean"] = ic_data.mean()
    ic_summary_table["IC std."] = ic_data.std()
    ic_summary_table["Risk-Adjusted IC (IR)"] = ic_data.mean() / ic_data.std()
    t_stat, p_value = stats.ttest_1samp(ic_data, 0)
    ic_summary_table["t-stat (IC)"] = t_stat
    ic_summary_table["p-value (IC)"] = p_value
    ic_summary_table["IC Skew"] = stats.skew(ic_data)
    ic_summary_table["IC Kurtosis"] = stats.kurtosis(ic_data)

    print("Information Analysis")
    plotting_utils.print_table(ic_summary_table.apply(lambda x: x.round(3)).T) 
Example #14
Source File: test_stats.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_describe_axis_none(self):
        x = np.vstack((np.ones((3, 4)), 2 * np.ones((2, 4))))

        # expected values
        e_nobs, e_minmax = (20, (1.0, 2.0))
        e_mean = 1.3999999999999999
        e_var = 0.25263157894736848
        e_skew = 0.4082482904638634
        e_kurt = -1.8333333333333333

        # actual values
        a = stats.describe(x, axis=None)

        assert_equal(a.nobs, e_nobs)
        assert_almost_equal(a.minmax, e_minmax)
        assert_almost_equal(a.mean, e_mean)
        assert_almost_equal(a.variance, e_var)
        assert_array_almost_equal(a.skewness, e_skew, decimal=13)
        assert_array_almost_equal(a.kurtosis, e_kurt, decimal=13) 
Example #15
Source File: test_stats.py    From Computable with MIT License 6 votes vote down vote up
def test_kurtosis(self):
        #   sum((testcase-mean(testcase,axis=0))**4,axis=0)/((sqrt(var(testcase)*3/4))**4)/4
        #   sum((test2-mean(testmathworks,axis=0))**4,axis=0)/((sqrt(var(testmathworks)*4/5))**4)/5
        #   Set flags for axis = 0 and
        #   fisher=0 (Pearson's defn of kurtosis for compatiability with Matlab)
        y = stats.kurtosis(self.testmathworks,0,fisher=0,bias=1)
        assert_approx_equal(y, 2.1658856802973,10)

        # Note that MATLAB has confusing docs for the following case
        #  kurtosis(x,0) gives an unbiased estimate of Pearson's skewness
        #  kurtosis(x)  gives a biased estimate of Fisher's skewness (Pearson-3)
        #  The MATLAB docs imply that both should give Fisher's
        y = stats.kurtosis(self.testmathworks,fisher=0,bias=0)
        assert_approx_equal(y, 3.663542721189047,10)
        y = stats.kurtosis(self.testcase,0,0)
        assert_approx_equal(y,1.64) 
Example #16
Source File: test_distributions.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_tukeylambda_stats_ticket_1545():
    # Some test for the variance and kurtosis of the Tukey Lambda distr.
    # See test_tukeylamdba_stats.py for more tests.

    mv = stats.tukeylambda.stats(0, moments='mvsk')
    # Known exact values:
    expected = [0, np.pi**2/3, 0, 1.2]
    assert_almost_equal(mv, expected, decimal=10)

    mv = stats.tukeylambda.stats(3.13, moments='mvsk')
    # 'expected' computed with mpmath.
    expected = [0, 0.0269220858861465102, 0, -0.898062386219224104]
    assert_almost_equal(mv, expected, decimal=10)

    mv = stats.tukeylambda.stats(0.14, moments='mvsk')
    # 'expected' computed with mpmath.
    expected = [0, 2.11029702221450250, 0, -0.02708377353223019456]
    assert_almost_equal(mv, expected, decimal=10) 
Example #17
Source File: pipline.py    From MachineLearning with Apache License 2.0 6 votes vote down vote up
def get_stat_funs():
    """
    Previous version uses lambdas.
    """
    stat_funs = []
    
    stats = [len, np.min, np.max, np.median, np.std, skew, kurtosis] + 19 * [np.percentile]
    stats_kwargs = [{} for i in range(7)] + [{'q': i} for i in np.linspace(0.05, 0.95, 19)]

    for stat, stat_kwargs in zip(stats, stats_kwargs):
        stat_funs.append(_StatFunAdaptor(stat,**stat_kwargs))
        stat_funs.append(_StatFunAdaptor(stat, np.diff, **stat_kwargs))
        stat_funs.append(_StatFunAdaptor(stat, diff2, **stat_kwargs))
        stat_funs.append(_StatFunAdaptor(stat, np.unique, **stat_kwargs))
        stat_funs.append(_StatFunAdaptor(stat, np.unique, np.diff, **stat_kwargs))
        stat_funs.append(_StatFunAdaptor(stat, np.unique, diff2, **stat_kwargs))
    return stat_funs 
Example #18
Source File: test_analytics.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_kurt(self):
        from scipy.stats import kurtosis

        def alt(x):
            if len(x) < 4:
                return np.nan
            return kurtosis(x, bias=False)

        self._check_stat_op('kurt', alt)

        index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]],
                           labels=[[0, 0, 0, 0, 0, 0],
                                   [0, 1, 2, 0, 1, 2],
                                   [0, 1, 0, 1, 0, 1]])
        df = DataFrame(np.random.randn(6, 3), index=index)

        kurt = df.kurt()
        kurt2 = df.kurt(level=0).xs('bar')
        tm.assert_series_equal(kurt, kurt2, check_names=False)
        assert kurt.name is None
        assert kurt2.name == 'bar' 
Example #19
Source File: tmodel.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _set_start_params(self, start_params=None, use_kurtosis=False):
        if start_params is not None:
            self.start_params = start_params
        else:
            from statsmodels.regression.linear_model import OLS
            res_ols = OLS(self.endog, self.exog).fit()
            start_params = 0.1*np.ones(self.k_params)
            start_params[:self.k_vars] = res_ols.params

            if self.fix_df is False:

                if use_kurtosis:
                    kurt = stats.kurtosis(res_ols.resid)
                    df = 6./kurt + 4
                else:
                    df = 5

                start_params[-2] = df
                #TODO adjust scale for df
                start_params[-1] = np.sqrt(res_ols.scale)

            self.start_params = start_params 
Example #20
Source File: test_stats.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_kurtosis(self):
        # Scalar test case
        y = stats.kurtosis(self.scalar_testcase)
        assert_approx_equal(y, -3.0)
        #   sum((testcase-mean(testcase,axis=0))**4,axis=0)/((sqrt(var(testcase)*3/4))**4)/4
        #   sum((test2-mean(testmathworks,axis=0))**4,axis=0)/((sqrt(var(testmathworks)*4/5))**4)/5
        #   Set flags for axis = 0 and
        #   fisher=0 (Pearson's defn of kurtosis for compatibility with Matlab)
        y = stats.kurtosis(self.testmathworks, 0, fisher=0, bias=1)
        assert_approx_equal(y, 2.1658856802973, 10)

        # Note that MATLAB has confusing docs for the following case
        #  kurtosis(x,0) gives an unbiased estimate of Pearson's skewness
        #  kurtosis(x)  gives a biased estimate of Fisher's skewness (Pearson-3)
        #  The MATLAB docs imply that both should give Fisher's
        y = stats.kurtosis(self.testmathworks, fisher=0, bias=0)
        assert_approx_equal(y, 3.663542721189047, 10)
        y = stats.kurtosis(self.testcase, 0, 0)
        assert_approx_equal(y, 1.64)

        x = np.arange(10.)
        x[9] = np.nan
        assert_equal(stats.kurtosis(x), np.nan)
        assert_almost_equal(stats.kurtosis(x, nan_policy='omit'), -1.230000)
        assert_raises(ValueError, stats.kurtosis, x, nan_policy='raise')
        assert_raises(ValueError, stats.kurtosis, x, nan_policy='foobar') 
Example #21
Source File: test_distributions.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_moments(self):
        X = stats.skewnorm.rvs(a=4, size=int(1e6), loc=5, scale=2)
        expected = [np.mean(X), np.var(X), stats.skew(X), stats.kurtosis(X)]
        computed = stats.skewnorm.stats(a=4, loc=5, scale=2, moments='mvsk')
        assert_array_almost_equal(computed, expected, decimal=2)

        X = stats.skewnorm.rvs(a=-4, size=int(1e6), loc=5, scale=2)
        expected = [np.mean(X), np.var(X), stats.skew(X), stats.kurtosis(X)]
        computed = stats.skewnorm.stats(a=-4, loc=5, scale=2, moments='mvsk')
        assert_array_almost_equal(computed, expected, decimal=2) 
Example #22
Source File: pc_subjective_model_test.py    From sureal with Apache License 2.0 5 votes vote down vote up
def test_btnr_subjective_model(self):
        subjective_model = BradleyTerryNewtonRaphsonPairedCompSubjectiveModel(self.pc_dataset_reader)
        result = subjective_model.run_modeling(zscore_output=True)
        self.assertAlmostEqual(float(np.sum(result['quality_scores'])), 0, places=4)
        self.assertAlmostEqual(float(np.var(result['quality_scores'])), 1, places=4)
        self.assertAlmostEqual(st.kurtosis(result['quality_scores']), -0.05721221160408296, places=4)
        self.assertTrue(result['quality_scores_std'] is None) 
Example #23
Source File: test_stats.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_describe_result_attributes(self):
        actual = stats.describe(np.arange(5))
        attributes = ('nobs', 'minmax', 'mean', 'variance', 'skewness',
                      'kurtosis')
        check_named_results(actual, attributes) 
Example #24
Source File: test_mstats_basic.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_kurtosis(self):
        for n in self.get_n():
            x, y, xm, ym = self.generate_xy_sample(n)
            r = stats.kurtosis(x)
            rm = stats.mstats.kurtosis(xm)
            assert_almost_equal(r, rm, 10)

            r = stats.kurtosis(y)
            rm = stats.mstats.kurtosis(ym)
            assert_almost_equal(r, rm, 10) 
Example #25
Source File: bag_of_characters.py    From sato with Apache License 2.0 5 votes vote down vote up
def extract_bag_of_characters_features(data, n_val):
    
    characters_to_check = [ '['+  c + ']' for c in string.printable if c not in ( '\n', '\\', '\v', '\r', '\t', '^' )] + ['[\\\\]', '[\^]']
    
    f = OrderedDict()

    f['n_values'] = n_val
    data_no_null = data.dropna()
    all_value_features = OrderedDict()

    all_value_features['length'] = data_no_null.apply(len)

    for c in characters_to_check:
        all_value_features['n_{}'.format(c)] = data_no_null.str.count(c)
        
    for value_feature_name, value_features in all_value_features.items():
        f['{}-agg-any'.format(value_feature_name)] = any(value_features)
        f['{}-agg-all'.format(value_feature_name)] = all(value_features)
        f['{}-agg-mean'.format(value_feature_name)] = np.mean(value_features)
        f['{}-agg-var'.format(value_feature_name)] = np.var(value_features)
        f['{}-agg-min'.format(value_feature_name)] = np.min(value_features)
        f['{}-agg-max'.format(value_feature_name)] = np.max(value_features)
        f['{}-agg-median'.format(value_feature_name)] = np.median(value_features)
        f['{}-agg-sum'.format(value_feature_name)] = np.sum(value_features)
        f['{}-agg-kurtosis'.format(value_feature_name)] = kurtosis(value_features)
        f['{}-agg-skewness'.format(value_feature_name)] = skew(value_features)

    n_none = data.size - data_no_null.size - len([ e for e in data if e == ''])
    f['none-agg-has'] = n_none > 0
    f['none-agg-percent'] = n_none / len(data)
    f['none-agg-num'] = n_none
    f['none-agg-all'] = (n_none == len(data))
    #print(len(f))
    return f 
Example #26
Source File: pc_subjective_model_test.py    From sureal with Apache License 2.0 5 votes vote down vote up
def test_btmle_subjective_model(self):
        subjective_model = BradleyTerryMlePairedCompSubjectiveModel(self.pc_dataset_reader)
        result = subjective_model.run_modeling()
        self.assertAlmostEqual(float(np.sum(result['quality_scores'])), -187.18634399309573, places=4)
        self.assertAlmostEqual(float(np.var(result['quality_scores'])), 3.1442888768417054, places=4)
        self.assertAlmostEqual(st.kurtosis(result['quality_scores']), 0.5649254682803901, places=4)
        self.assertAlmostEqual(float(np.sum(result['quality_scores_std'])), 11.136592174843651, places=4)
        self.assertAlmostEqual(float(np.var(result['quality_scores_std'])), 0.003890667402965306, places=8)
        self.assertAlmostEqual(st.kurtosis(result['quality_scores_std']), 1.960577186185537, places=4)
        self.assertAlmostEqual(st.kurtosis(result['quality_scores_p']), 9.249782166616258, places=4)
        self.assertAlmostEqual(float(np.sum(result['quality_scores_p_std'])), 0.25667807232011897, places=4)
        self.assertAlmostEqual(float(np.sum(result['quality_scores_p_cov'])), 6.488285445421619e-16, places=4) 
Example #27
Source File: test_mstats_basic.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_kurtosis(self):
        # Set flags for axis = 0 and fisher=0 (Pearson's definition of kurtosis
        # for compatibility with Matlab)
        y = mstats.kurtosis(self.testmathworks,0,fisher=0,bias=1)
        assert_almost_equal(y, 2.1658856802973,10)
        # Note that MATLAB has confusing docs for the following case
        #  kurtosis(x,0) gives an unbiased estimate of Pearson's skewness
        #  kurtosis(x)  gives a biased estimate of Fisher's skewness (Pearson-3)
        #  The MATLAB docs imply that both should give Fisher's
        y = mstats.kurtosis(self.testmathworks,fisher=0, bias=0)
        assert_almost_equal(y, 3.663542721189047,10)
        y = mstats.kurtosis(self.testcase,0,0)
        assert_almost_equal(y,1.64)

        # test that kurtosis works on multidimensional masked arrays
        correct_2d = ma.array(np.array([-1.5, -3., -1.47247052385, 0.,
                                        -1.26979517952]),
                              mask=np.array([False, False, False, True,
                                             False], dtype=bool))
        assert_array_almost_equal(mstats.kurtosis(self.testcase_2d, 1),
                                  correct_2d)
        for i, row in enumerate(self.testcase_2d):
            assert_almost_equal(mstats.kurtosis(row), correct_2d[i])

        correct_2d_bias_corrected = ma.array(
            np.array([-1.5, -3., -1.88988209538, 0., -0.5234638463918877]),
            mask=np.array([False, False, False, True, False], dtype=bool))
        assert_array_almost_equal(mstats.kurtosis(self.testcase_2d, 1,
                                                  bias=False),
                                  correct_2d_bias_corrected)
        for i, row in enumerate(self.testcase_2d):
            assert_almost_equal(mstats.kurtosis(row, bias=False),
                                correct_2d_bias_corrected[i])

        # Check consistency between stats and mstats implementations
        assert_array_almost_equal_nulp(mstats.kurtosis(self.testcase_2d[2, :]),
                                       stats.kurtosis(self.testcase_2d[2, :]),
                                       nulp=4) 
Example #28
Source File: test_stats.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_kurtosis_array_scalar(self):
        assert_equal(type(stats.kurtosis([1,2,3])), float) 
Example #29
Source File: test_mstats_basic.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_describe_result_attributes(self):
        actual = mstats.describe(np.arange(5))
        attributes = ('nobs', 'minmax', 'mean', 'variance', 'skewness',
                      'kurtosis')
        check_named_results(actual, attributes, ma=True) 
Example #30
Source File: fusor_utils.py    From SVE with GNU General Public License v3.0 5 votes vote down vote up
def brkpt_stats(X):
    X1 = X[~np.isnan(X)]
    x_n,x_sm,x_mu,x_md,x_sd,x_sk,x_ks = 0,0,0.0,0.0,0.0,0.0,0.0
    x_n = len(X1)
    x_sm = sum(X1)
    if x_n>0:
        x_mu = np.mean(X1)
        x_md = np.median(X1)
        x_sd = np.std(X1)
        x_sk = stats.skew(X1)
        x_ks = stats.kurtosis(X1)
    return [x_n,x_sm,x_mu,x_md,x_sd,x_sk,x_ks]
    
#breakpoint differentials to the target