<commit_before># Licence:
#
# TODO: Rename
# util_science
#
from __future__ import absolute_import, division, print_function
try:
    import numpy as np
    HAS_NUMPY = True
except ImportError:
    HAS_NUMPY = False
    # TODO remove numpy
    pass
from collections import defaultdict
#import six
from six.moves import zip, range
from utool import util_inject
print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[alg]')


def estimate_pdf(data, gridsize=1024):
    """
    estimate_pdf

    References;
        http://statsmodels.sourceforge.net/devel/generated/statsmodels.nonparametric.kde.KDEUnivariate.html
        https://jakevdp.github.io/blog/2013/12/01/kernel-density-estimation/

    Args:
        data (?):
        bw_factor (?):

    Returns:
        ?: data_pdf

    Example:
        >>> from utool.util_alg import *  # NOQA
        >>> import plottool as pt
        >>> data = '?'
        >>> bw_factor = '?'
        >>> data_pdf = estimate_pdf(data, bw_factor)
        >>> pt.plot(data_pdf.cdf)
        >>> print(data_pdf)
    """
    import utool as ut
    #import scipy.stats as spstats
    #import statsmodels
    import numpy as np
    import statsmodels.nonparametric.kde
    try:
        data_pdf = statsmodels.nonparametric.kde.KDEUnivariate(data)
        bw_choices = ['scott', 'silverman', 'normal_reference']
        bw = bw_choices[1]
        fitkw = dict(kernel='gau',
                     bw=bw,
                     fft=True,
                     weights=None,
                     adjust=1,
                     cut=3,
                     gridsize=gridsize,
                     clip=(-np.inf, np.inf),)
        data_pdf.fit(**fitkw)
        #density = data_pdf.density
    #try:
    #    data_pdf = spstats.gaussian_kde(data, bw_factor)
    #    data_pdf.covariance_factor = bw_factor
    except Exception as ex:
        ut.printex(ex, '! Exception while estimating kernel density',
                   keys=['data'])
        raise
    return data_pdf


def normalize(array, dim=0):
    return norm_zero_one(array, dim)


def norm_zero_one(array, dim=0):
    """
    normalizes a numpy array from 0 to 1
    """
<commit_msg>added bayes rule<commit_after># Licence:
#
# TODO: Rename
# util_science
#
from __future__ import absolute_import, division, print_function
try:
    import numpy as np
    HAS_NUMPY = True
except ImportError:
    HAS_NUMPY = False
    # TODO remove numpy
    pass
from collections import defaultdict
#import six
from six.moves import zip, range
from utool import util_inject
print, print_, printDBG, rrr, profile = util_inject.inject(__name__, '[alg]')


def bayes_rule(a_given_b, prob_a, prob_b):
    """
    bayes_rule

    Args:
        a_given_b (ndarray or float):
        prob_a (ndarray or float):
        prob_b (ndarray or float):

    Returns:
        ndarray or float: b_given_a

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_alg import *  # NOQA
        >>> a_given_b = .1
        >>> prob_a = .4
        >>> prob_b = .3
        >>> b_given_a = bayes_rule(a_given_b, prob_a, prob_b)
        >>> result = b_given_a
        >>> print(result)
        0.075

    """
    b_given_a = (a_given_b * prob_b) / prob_a
    return b_given_a


def estimate_pdf(data, gridsize=1024, adjust=1):
    """
    estimate_pdf

    References;
        http://statsmodels.sourceforge.net/devel/generated/statsmodels.nonparametric.kde.KDEUnivariate.html
        https://jakevdp.github.io/blog/2013/12/01/kernel-density-estimation/

    Args:
        data (?):
        bw_factor (?):

    Returns:
        ?: data_pdf

    Example:
        >>> from utool.util_alg import *  # NOQA
        >>> import plottool as pt
        >>> data = '?'
        >>> bw_factor = '?'
        >>> data_pdf = estimate_pdf(data, bw_factor)
        >>> pt.plot(data_pdf.cdf)
        >>> print(data_pdf)
    """
    import utool as ut
    #import scipy.stats as spstats
    #import statsmodels
    import numpy as np
    import statsmodels.nonparametric.kde
    try:
        data_pdf = statsmodels.nonparametric.kde.KDEUnivariate(data)
        bw_choices = ['scott', 'silverman', 'normal_reference']
        bw = bw_choices[1]
        fitkw = dict(kernel='gau',
                     bw=bw,
                     fft=True,
                     weights=None,
                     adjust=adjust,
                     cut=3,
                     gridsize=gridsize,
                     clip=(-np.inf, np.inf),)
        data_pdf.fit(**fitkw)
        #density = data_pdf.density
    #try:
    #    data_pdf = spstats.gaussian_kde(data, bw_factor)
    #    data_pdf.covariance_factor = bw_factor
    except Exception as ex:
        ut.printex(ex, '! Exception while estimating kernel density',
                   keys=['data'])
        raise
    return data_pdf


def normalize(array, dim=0):
    return norm_zero_one(array, dim)


def norm_zero_one(array, dim=0):
    """
    normalizes a numpy array from 0 to 1
    """

{'fraction_non_alphanumeric': 0.10733668341708542, 'fraction_numerical': 0.009045226130653266, 'mean_word_length': 2.131529263687854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7012732', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1581, 'n_words': 354}