<commit_before>############################################################################
# Copyright 2016 <NAME>                                          #
#                                                                          #
# Licensed under the Apache License, Version 2.0 (the "License");          #
# you may not use this file except in compliance with the License.         #
# You may obtain a copy of the License at                                  #
#                                                                          #
#     http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                          #
# Unless required by applicable law or agreed to in writing, software      #
# distributed under the License is distributed on an "AS IS" BASIS,        #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and      #
# limitations under the License.                                           #
############################################################################

'''Project statistics module.'''

import math
import random
import functools
import statistics
import numpy as np
import scipy as sp
import scipy.stats

def order_sample(icdf, total, order):
    '''Sample the order statistic.

    Args:

    icdf: Distribution ICDF function.

    total: Total number of realizations.

    order: Order of the statistic.

    Returns: A sample of the order statistic.

    '''
    assert isinstance(total, int)
    assert isinstance(order, int)
    assert total >= order, 'order must be less or equal to total.'
    samples = [icdf(random.random()) for _ in range(total)]
    partition = np.partition(np.asarray(samples), order-1)
    # assert partition[order-1] <= partition[order]
    # assert partition[order] <= partition[order+1]
    return partition[order-1]

def order_mean_empiric(icdf, total, order, samples=1000):
    '''Compute the order static mean numerically.

    Args:

    icdf: Distribution ICDF function.

    total: Total number of realizations.

    order: Order of the statistic.

    samples: Samples to compute the mean from.

    Returns: The mean of the order statistic with variables drawn from
    the given distribution.

    '''
    samples = [order_sample(icdf, total, order) for _ in range(samples)]
    mean = statistics.mean(samples)
    variance = statistics.variance(samples, xbar=mean)
    return mean, variance

@functools.lru_cache(maxsize=1024)
def order_mean_shiftexp(total, order, parameter=1, exact=True):
    '''Compute the mean of the shifted exponential order statistic.

    Args:

    total: Total number of variables.

    order: Statistic order.

    parameter: Distribution parameter.

    exact: Compute the mean exactly. Set to Falset to speed up the
    computation.

    Returns: The mean of the order statistic with variables drawn from
    the shifted exponential distribution.

    '''
    if exact:
        mean = 1
        for i in range(total-order+1, total+1):
            mean += 1 / i

        mean *= parameter
        return mean
    else:
        raise NotImplementedError
        if total == order:
            raise ValueError('Non-exact computation required order < total.')
        return 1 + (1 / parameter) * math.log(total / (total - order))
<commit_msg>cleanup<commit_after>############################################################################
# Copyright 2016 <NAME>                                          #
#                                                                          #
# Licensed under the Apache License, Version 2.0 (the "License");          #
# you may not use this file except in compliance with the License.         #
# You may obtain a copy of the License at                                  #
#                                                                          #
#     http://www.apache.org/licenses/LICENSE-2.0                           #
#                                                                          #
# Unless required by applicable law or agreed to in writing, software      #
# distributed under the License is distributed on an "AS IS" BASIS,        #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and      #
# limitations under the License.                                           #
############################################################################

'''Project statistics module.'''

import math
import random
import functools
import statistics
import numpy as np
import scipy as sp
import scipy.stats

def order_sample(icdf, total, order):
    '''Sample the order statistic.

    Args:

    icdf: Distribution ICDF function.

    total: Total number of realizations.

    order: Order of the statistic.

    Returns: A sample of the order statistic.

    '''
    assert isinstance(total, int)
    assert isinstance(order, int)
    assert total >= order, 'order must be less or equal to total.'
    samples = [icdf(random.random()) for _ in range(total)]
    partition = np.partition(np.asarray(samples), order-1)
    # assert partition[order-1] <= partition[order]
    # assert partition[order] <= partition[order+1]
    return partition[order-1]

def order_mean_empiric(icdf, total, order, samples=1000):
    '''Compute the order static mean numerically.

    Args:

    icdf: Distribution ICDF function.

    total: Total number of realizations.

    order: Order of the statistic.

    samples: Samples to compute the mean from.

    Returns: The mean of the order statistic with variables drawn from
    the given distribution.

    '''
    samples = [order_sample(icdf, total, order) for _ in range(samples)]
    mean = statistics.mean(samples)
    variance = statistics.variance(samples, xbar=mean)
    return mean, variance

@functools.lru_cache(maxsize=1024)
def order_mean_shiftexp(total, order, parameter=1):
    '''Compute the mean of the shifted exponential order statistic.

    Args:

    total: Total number of variables.

    order: Statistic order.

    parameter: Distribution parameter.

    Returns: The mean of the order statistic with variables drawn from
    the shifted exponential distribution.

    '''
    mean = 1
    for i in range(total-order+1, total+1):
        mean += 1 / i

    mean *= parameter
    return mean

{'fraction_non_alphanumeric': 0.11885309079697857, 'fraction_numerical': 0.008016032064128256, 'mean_word_length': 2.164878048780488, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '993619', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1482, 'n_words': 601}