
    M/PhE                     6    d Z ddlZd	dZd Zd Zd
dZddZdS )zLow discrepancy sequence tools.    Nc           	         t          j        |           } | j        \  }}|7|                    d          }|                    d          }| |z
  ||z
  z  } t          | dz
            }t          j        t          j        dd|z  z   d|dz  z  z
  d                    }d}t          |          D ]i}	| dd|	f         }
|ddt          |
dddf         dz
            z  z   dt          |
dz
            z  z   dt          |
dddf         |
z
            z  z
  z  }j|                                }d|z  d|z  |z  z
  d	|dz  z  |z  z   }|S )
a  Discrepancy.

    Compute the centered discrepancy on a given sample.
    It is a measure of the uniformity of the points in the parameter space.
    The lower the value is, the better the coverage of the parameter space is.

    Parameters
    ----------
    sample : array_like (n_samples, k_vars)
        The sample to compute the discrepancy from.
    bounds : tuple or array_like ([min, k_vars], [max, k_vars])
        Desired range of transformed data. The transformation apply the bounds
        on the sample and not the theoretical space, unit cube. Thus min and
        max values of the sample will coincide with the bounds.

    Returns
    -------
    discrepancy : float
        Centered discrepancy.

    References
    ----------
    [1] Fang et al. "Design and modeling for computer experiments",
      Computer Science and Data Analysis Series Science and Data Analysis
      Series, 2006.
    Nr   axis      ?      gUUUUUU?g       @      ?)	npasarrayshapeminmaxabssumprodrange)sampleboundsn_sampledimmin_max_abs_disc1prod_arris0disc2c2s                [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tools/sequences.pydiscrepancyr!      s   6 ZFLMHc zzqz!!zzqz!!4-D4K0v|DF271sTz>C$!)O;!DDDEEEH3ZZ 2 2AAAqD\Q3r!!!T'{S0111247#b3h--4GH3r!!!T'{R/0001 	2 LLNNE3
x%!7
7
Q
%
'(B I    c                    t          j        | dz  | dz  dk    z   t                    }t          dt	          | dz            dz  dz             D ]C}||         r9d|z  dz   dz  }d|||z  dz  dd|z  <   d|||d|dz  z  z
  d	z   z  dz  dd|z  <   Dt           j        dddt          j        |          d
         dd         z  dz   dz  f         S )a(  Prime numbers from 2 to *n*.

    Parameters
    ----------
    n : int
        Sup bound with ``n >= 6``.

    Returns
    -------
    primes : list(int)
        Primes in ``2 <= p < n``.

    References
    ----------
    [1] `StackOverflow <https://stackoverflow.com/questions/2068372>`_.
          r   )dtyper   r   FN   r   )r
   onesboolr   intr_nonzero)nsiever   ks       r    primes_from_2_tor0   :   s   " GAFa!eqj)666E1c!s(mmq(1,-- A A8 	AA	AA',E!a%1*#a!e#$;@E!q1A;*+q07!a%785ARZ..q1!""559Q>?@@r"   c                     g dd|          }t          |          | k     r4d}	 t          |          d|          }t          |          | k    rn|dz  }1|S )zList of the n-first prime numbers.

    Parameters
    ----------
    n : int
        Number of prime numbers wanted.

    Returns
    -------
    primes : list(int)
        List of primes.
    )r   r$                              %   )   +   /   5   ;   =   C   G   I   O   S   Y   a   e   g   k   m   q                                                                           i  i  i  i  i  i  i  i%  i3  i7  i9  i=  iK  iQ  i[  i]  ia  ig  io  iu  i{  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i#  i-  i3  i9  i;  iA  iK  iQ  iW  iY  i_  ie  ii  ik  iw  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i)  i+  i5  i7  i;  i=  iG  iU  iY  i[  i_  im  iq  is  iw  i  i  i  i  i  i  i  i  i  i  i  i  i  i  N
   zNot enought primesi  )lenr0   )r-   primes
big_numbers      r    n_primesrj   T   s|    1 1 1 24!5F 6{{Q
	%j11"1"5F6{{a$J		 Mr"   r   c                     g }t          ||| z             D ]J}d\  }}|}|dk    r&t          ||          \  }}||z  }|||z  z  }|dk    &|                    |           K|S )a  Van der Corput sequence.

    Pseudo-random number generator based on a b-adic expansion.

    Parameters
    ----------
    n_sample : int
        Number of element of the sequence.
    base : int
        Base of the sequence.
    start_index : int
        Index to start the sequence from.

    Returns
    -------
    sequence : list (n_samples,)
        Sequence of Van der Corput.
    )g        r	   r   )r   divmodappend)	r   basestart_indexsequencer   n_th_numberdenomquotient	remainders	            r    van_der_corputru   z   s    & H;h 677 % %#Ull"(4"8"8HiTME9u,,K ll 	$$$$Or"   c                     t          |           }fd|D             }t          j        |          j        dd         }|7|                    d          }|                    d          }|||z
  z  |z   }|S )a  Halton sequence.

    Pseudo-random number generator that generalize the Van der Corput sequence
    for multiple dimensions. Halton sequence use base-two Van der Corput
    sequence for the first dimension, base-three for its second and base-n for
    its n-dimension.

    Parameters
    ----------
    dim : int
        Dimension of the parameter space.
    n_sample : int
        Number of samples to generate in the parametr space.
    bounds : tuple or array_like ([min, k_vars], [max, k_vars])
        Desired range of transformed data. The transformation apply the bounds
        on the sample and not the theoretical space, unit cube. Thus min and
        max values of the sample will coincide with the bounds.
    start_index : int
        Index to start the sequence from.

    Returns
    -------
    sequence : array_like (n_samples, k_vars)
        Sequence of Halton.

    References
    ----------
    [1] Halton, "On the efficiency of certain quasi-random sequences of points
      in evaluating multi-dimensional integrals", Numerische Mathematik, 1960.

    Examples
    --------
    Generate samples from a low discrepancy sequence of Halton.

    >>> from statsmodels.tools import sequences
    >>> sample = sequences.halton(dim=2, n_sample=5)

    Compute the quality of the sample using the discrepancy criterion.

    >>> uniformity = sequences.discrepancy(sample)

    If some wants to continue an existing design, extra points can be obtained.

    >>> sample_continued = sequences.halton(dim=2, n_sample=5, start_index=5)
    c                 8    g | ]}t          d z   |          S )r   )ru   ).0bdimr   ro   s     r    
<listcomp>zhalton.<locals>.<listcomp>   s)    OOO$nX\4==OOOr"   r   Nr   r   )rj   r
   arrayTr   r   )r   r   r   ro   rn   r   r   r   s    ` `    r    haltonr}      s    \ C==D POOOO$OOOFXf#F zzqz!!zzqz!!4$;'$.Mr"   )N)r   r   )Nr   )__doc__numpyr
   r!   r0   rj   ru   r}    r"   r    <module>r      s    % %    2 2 2 2jA A A4# # #L   @: : : : : :r"   