<commit_before># -*- coding: utf-8 -*-

"""
Stratified permutation tests.

WIP: revisit FIXME w/ PS, reevaluate func names and args
"""

from __future__ import division, print_function, absolute_import

import math

import numpy as np
from numpy.random import RandomState

from scipy.stats import (binom,
                         ttest_ind)
from scipy.optimize import brentq


def permute_within_groups(group, condition, groups, prng=None):
    """
    Permutation of condition within each group.

    Parameters
    ----------
    group : array-like
      A 1-d array indicating group membership
    condition : array-like
      A 1-d array indcating treatment.
    groups : array-like
      The unique elements of group

    Returns
    -------
    permuted : array-like
      The within group permutation of condition.
    """
    permuted = condition.copy()
    if prng is None:
        prng = RandomState()

    # FIXME: do we need to pass `groups` in?
    # Yes, don't want to repeatedly identify unique elements (avoid additional flops)
    for g in groups:
        gg = group == g
        # FIXME: is this in-place?
        permuted[gg] = prng.permutation(condition[gg])
    return permuted


# maybe use kwargs from xtol and rtol?
def binom_conf_interval(n, x, cl=0.975, alternative="two-sided", p=None,
                        xtol=1e-12, rtol=4.4408920985006262e-16, maxiter=100):
    """
    Compute the confidence interval for a binomial.

    Parameters
    ----------
    n : int
      The number of Bernoulli trials.
    x : int
      The number of successes.
    cl : float in (0, 1)
      The desired confidence level.
    alternative : {"two-sided", "less", "greater"}
      Indicates the alternative hypothesis.
    p : float in (0, 1)<commit_msg>ENH: Shuffle in place<commit_after># -*- coding: utf-8 -*-

"""
Stratified permutation tests.

WIP: revisit FIXME w/ PS, reevaluate func names and args
"""

from __future__ import division, print_function, absolute_import

import math

import numpy as np
from numpy.random import RandomState

from scipy.stats import (binom,
                         ttest_ind)
from scipy.optimize import brentq


def permute_within_groups(group, condition, groups, prng=None):
    """
    Permutation of condition within each group.

    Parameters
    ----------
    group : array-like
      A 1-d array indicating group membership
    condition : array-like
      A 1-d array indcating treatment.
    groups : array-like
      The unique elements of group

    Returns
    -------
    permuted : array-like
      The within group permutation of condition.
    """
    permuted = condition.copy()
    if prng is None:
        prng = RandomState()

    # FIXME: do we need to pass `groups` in?
    # Yes, don't want to repeatedly identify unique elements (avoid additional flops)
    for g in groups:
        gg = group == g
        # FIXME: is this in-place?
        permuted[gg] = prng.shuffle(permuted[gg])
    return permuted


# maybe use kwargs from xtol and rtol?
def binom_conf_interval(n, x, cl=0.975, alternative="two-sided", p=None,
                        xtol=1e-12, rtol=4.4408920985006262e-16, maxiter=100):
    """
    Compute the confidence interval for a binomial.

    Parameters
    ----------
    n : int
      The number of Bernoulli trials.
    x : int
      The number of successes.
    cl : float in (0, 1)
      The desired confidence level.
    alternative : {"two-sided", "less", "greater"}
      Indicates the alternative hypothesis.
    p : float in (0, 1)
{'fraction_non_alphanumeric': 0.09754028837998303, 'fraction_numerical': 0.020356234096692113, 'mean_word_length': 2.3953934740882916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6398857', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1029, 'n_words': 372}