
    phY                        / S Qr SSKrSSKJr  SSKJrJrJrJ	r	J
r
   " S S5      rS rS rS	 rS
 rS rS rS rS r " S S5      rS r " S S\5      rS r " S S\5      rS r " S S\5      rS r " S S\5      rS rSS jrg) )ContrastMatrix	TreatmentPolySumHelmertDiff    N)
PatsyError)repr_pretty_delegaterepr_pretty_implsafe_issubdtypeno_picklingassert_no_picklingc                   ,    \ rS rSrSrS r\rS r\	r
Srg)r      aY  A simple container for a matrix used for coding categorical factors.

Attributes:

.. attribute:: matrix

   A 2d ndarray, where each column corresponds to one column of the
   resulting design matrix, and each row contains the entries for a single
   categorical variable level. Usually n-by-n for a full rank coding or
   n-by-(n-1) for a reduced rank coding, though other options are
   possible.

.. attribute:: column_suffixes

   A list of strings to be appended to the factor name, to produce the
   final column names. E.g. for treatment coding the entries will look
   like ``"[T.level1]"``.
c                     [         R                  " U5      U l        X l        U R                  R                  S   [        U5      :w  a  [        S5      eg )N   z(matrix and column_suffixes don't conform)npasarraymatrixcolumn_suffixesshapelenr	   )selfr   r   s      gC:\Users\julio\OneDrive\Documentos\Trabajo\IdeasFrscas\Cabanna\env\Lib\site-packages\patsy/contrasts.py__init__ContrastMatrix.__init__*   sE    jj(.;;Q3#77GHH 8    c                 H    [        XU R                  U R                  /5        g N)r   r   r   )r   pcycles      r   _repr_pretty_ContrastMatrix._repr_pretty_2   s    4;;0D0D"EFr   )r   r   N)__name__
__module____qualname____firstlineno____doc__r   r
   __repr__r"   r   __getstate____static_attributes__ r   r   r   r      s!    &I $HG Lr   r   c                  D   [        SS/SS//SS/5      n [        R                  " U R                  [        R                  " S5      5      (       d   eU R
                  SS/:X  d   e[        U 5        SS KnUR                  [        [         S/S//SS/5        [        U 5        g )Nr   r   ab   )r   r   array_equalr   eyer   reprpytestraisesr	   r   )cmr4   s     r   test_ContrastMatrixr7   8   s    	!Q!Q(3*	5B>>"))RVVAY////#s+++H
MM*nsQCj3*Err   c                     [        U [        5      (       a  U $ [        U [        5      (       a   U R                  S5      $ [        U 5      $ ! [         a    [        U 5      s $ f = f)Nutf-8)
isinstancestrbytesdecodeUnicodeDecodeErrorr3   )objs    r   _obj_to_readable_strr@   K   s\    #s
	C			::g&& Cy " 	9	s   A
 
A"!A"c                      S n U " SS5        U " SS5        U " SS5        U " SS5        U " SR                  S5      S5        U " SR                  S	5      S
5        g )Nc                 P    [        U 5      n[        U5      [        L d   eX!:X  d   eg r   )r@   typer;   )r?   expectedgots      r   t$test__obj_to_readable_str.<locals>.tX   s(    "3'CyCr   r   1g      ?z1.0asdfu   €r9   ziso-8859-15zb'\xa4')encode)rF   s    r   test__obj_to_readable_strrK   W   sY    
 aIc5Mffff hoog) hoom$j1r   c                 T    U Vs/ s H  nSU < [        U5      < S3PM     sn$ s  snf )N[])r@   )prefixlevelslevels      r   _name_levelsrR   j   s%    JPQ& 4U ;<&QQQs   %c                  0    [        SSS/5      SS/:X  d   eg )Nr.   r/   cz[ab]z[ac])rR   r,   r   r   test__name_levelsrU   n   s     c3Z(VV,<<<<r   c                 h    [        [        R                  " [        U 5      5      [	        SU 5      5      $ )N )r   r   r2   r   rR   )rP   s    r   _dummy_coderX   r   s#    "&&V-|B/GHHr   c                     X;   a  U R                  U5      $ [        U[        5      (       a>  US:  a  U[        U 5      -  nSUs=::  a  [        U 5      :  d  O  [	        SU< S35      eU$ [	        SU< S35      e)Nr   zspecified level z is out of rangez
 not found)indexr:   intr   r	   )rP   	level_refs     r   
_get_levelr]   v   sl    ||I&&)S!!q=V$IY,V,YPQQ
yB
CCr   c                  `   [        / SQS5      S:X  d   e[        / SQS5      S:X  d   e[        / SQS5      S:X  d   e[        / SQS5      S:X  d   eSS Kn U R                  [        [         SS/S5        U R                  [        [         SS/S	5        U R                  [        [         SS/S
5        g )Nr.   r/   rT   r   r0   r/   r   r0   r   r   r.   rT   )r]   r4   r5   r	   )r4   s    r   test__get_levelrc      s    oq)Q...or*a///os+q000i#q(((
MM*j3*a8
MM*j3*b9
MM*j3*c:r   c                   2    \ rS rSrSrSS jrS rS r\r	Sr
g)	r      a  Treatment coding (also known as dummy coding).

This is the default coding.

For reduced-rank coding, one level is chosen as the "reference", and its
mean behaviour is represented by the intercept. Each column of the
resulting matrix represents the difference between the mean of one level
and this reference level.

For full-rank coding, classic "dummy" coding is used, and each column of
the resulting matrix represents the mean of the corresponding level.

The reference level defaults to the first level, or can be specified
explicitly.

.. ipython:: python

   # reduced rank
   dmatrix("C(a, Treatment)", balanced(a=3))
   # full rank
   dmatrix("0 + C(a, Treatment)", balanced(a=3))
   # Setting a reference level
   dmatrix("C(a, Treatment(1))", balanced(a=3))
   dmatrix("C(a, Treatment('a2'))", balanced(a=3))

Equivalent to R ``contr.treatment``. The R documentation suggests that
using ``Treatment(reference=-1)`` will produce contrasts that are
"equivalent to those produced by many (but not all) SAS procedures".
Nc                     Xl         g r   	reference)r   rh   s     r   r   Treatment.__init__   s    "r   c                     [        U5      $ r   )rX   r   rP   s     r   code_with_interceptTreatment.code_with_intercept   s    6""r   c           	      l   U R                   c  SnO[        XR                   5      n[        R                  " [	        U5      S-
  5      n[        R
                  " US U2S S 24   [        R                  " S[	        U5      S-
  45      X2S 2S S 24   45      n[        SUS U XS-   S  -   5      n[        XE5      $ )Nr   r   zT.)	rh   r]   r   r2   r   vstackzerosrR   r   )r   rP   rh   r2   	contrastsnamess         r   code_without_intercept Treatment.code_without_intercept   s    >>!I"6>>:IffS[1_%II)Q1c&kAo*>!?ZQR]AST
	 T6*9#51}8O#OPi//r   rg   r   )r$   r%   r&   r'   r(   r   rl   rs   r   r*   r+   r,   r   r   r   r      s    <##
0 Lr   r   c                  r   [        5       n U R                  / SQ5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ SQ/ SQ/5      (       d   eU R                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  SS/S	S/SS	//5      (       d   e[        S	S
9R                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  S	S/SS/SS	//5      (       d   e[        SS
9R                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  S	S/SS/SS	//5      (       d   e[        SS
9R                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  S	S/SS/SS	//5      (       d   e[        5       R                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  SS/S	S/SS	//5      (       d   eg )Nr_   )z[a]z[b]z[c])r   r   r   )r   r   r   )r   r   r   z[T.b]z[T.c]r   r   rg   z[T.a]r/   ra   z[T.1]z[T.0])r   rl   r   r   allcloser   rs   t1r   s     r   test_Treatmentrz      s   	B##O4F!!%::::;;v}}y)Y&GHHHH&&7F!!gw%7777;;v}}1v1v1v&>????#::?KF!!gw%7777;;v}}1v1v1v&>????$;;OLF!!gw%7777;;v}}1v1v1v&>????%<<_MF!!gw%7777;;v}}1v1v1v&>???? [//	:F!!gw%7777;;v}}1v1v1v&>????r   c                   8    \ rS rSrSrS	S jrS rS rS r\	r
Srg)
r      ae  Orthogonal polynomial contrast coding.

This coding scheme treats the levels as ordered samples from an underlying
continuous scale, whose effect takes an unknown functional form which is
`Taylor-decomposed`__ into the sum of a linear, quadratic, etc. components.

.. __: https://en.wikipedia.org/wiki/Taylor_series

For reduced-rank coding, you get a linear column, a quadratic column,
etc., up to the number of levels provided.

For full-rank coding, the same scheme is used, except that the zero-order
constant polynomial is also included. I.e., you get an intercept column
included as part of your categorical term.

By default the levels are treated as equally spaced, but you can override
this by providing a value for the `scores` argument.

Examples:

.. ipython:: python

   # Reduced rank
   dmatrix("C(a, Poly)", balanced(a=4))
   # Full rank
   dmatrix("0 + C(a, Poly)", balanced(a=3))
   # Explicit scores
   dmatrix("C(a, Poly([1, 2, 10]))", balanced(a=3))

This is equivalent to R's ``contr.poly``. (But note that in R, reduced
rank encodings are always dummy-coded, regardless of what contrast you
have set.)
Nc                     Xl         g r   scores)r   r   s     r   r   Poly.__init__   s    r   c                 B   [        U5      nU R                  nUc  [        R                  " U5      n[        R                  " U[
        S9n[        U5      U:w  a  [        SU< S[        U5      < S35      eXDR                  5       -  nUR                  S5      [        R                  " U5      R                  S5      -  n[        R                  R                  U5      u  pgU[        R                  " [        R                  " U5      5      -  nU[        R                  " [        R                  " US-  SS	95      -  nSUS S 2S
4'   / SQnU[        SU5       V	s/ s H	  n	SU	< 3PM     sn	-  nUS U nU(       a  [!        Xh5      $ [!        US S 2SS 24   USS  5      $ s  sn	f )N)dtypeznumber of levels (z#) does not match number of scores ())r`   r   )r   r`   r0   r   )axisr   )	.Constant.Linear
.Quadratic.Cubic   ^)r   r   r   aranger   floatr	   meanreshapelinalgqrsigndiagsqrtsumranger   )
r   	interceptrP   nr   raw_polyqrrr   is
             r   _code_eitherPoly._code_either  sT   K>YYq\FF%0v;!,-s6{<  	++->>'*biil.B.B7.KKyy||H%	RWWRWWQZ  	RWWRVVAqDq)**!Q$@a411,44bq	!!++ "!AqrE(E!"I66 5s   Fc                 &    U R                  SU5      $ )NTr   rk   s     r   rl   Poly.code_with_intercept   s      v..r   c                 &    U R                  SU5      $ )NFr   rk   s     r   rs   Poly.code_without_intercept#  s      //r   r~   r   )r$   r%   r&   r'   r(   r   r   rl   rs   r   r*   r+   r,   r   r   r   r      s#     D7>/0 Lr   r   c                  v   [        5       n U R                  / SQ5      nUR                  / SQ:X  d   e/ SQ/ SQ/ SQ/n[        UR                  5        [
        R                  " UR                  U5      (       d   eU R                  / SQ5      nUR                  SS/:X  d   e[        UR                  5        [
        R                  " UR                  SS	/S
S/SS	//5      (       d   e[        / SQS9R                  / SQ5      nUR                  / SQ:X  d   e[        UR                  5        [
        R                  " UR                  / SQ/ SQ/ SQ/5      (       d   e[        / SQS9R                  / SQ5      nUR                  / SQ:X  d   e[        UR                  5        [
        R                  " UR                  / SQ/ SQ/ SQ/5      (       d   eS
S KnUR                  [        [        S
S/S9R                  / SQ5        U R                  [        [        S5      5      5      nUR                  / SQ:X  d   eg )Nr_   )r   r   r   )r   ;f>,p ?)r   r   >,p )r   ;f?r   r   r   r   r   r   r   r   )r   
      r~   )r   g
gQ}.?)r   g?Q?g3)r   gGUg?guCz?)r   r      )r   g9UgHA?)r   g)U?gM
lOb)r   gWl?gQ?r      )r   r   r   r   z^4z^5)r   rl   r   printr   r   rw   rs   r4   r5   r	   listr   )ry   r   rD   r4   s       r   	test_Polyr   )  s	   	B##O4F!!%KKKK 	6"4H
 
&--;;v}}h////&&7F!!i%>>>>	&--;;"$56"#!#45	
    %99/JF!!%KKKK	&--;;776	
    %99/JF!!%KKKK	&--;;665	
    
MM*d1a&1EEW##DqN3F!! &   r   c                   >    \ rS rSrSrS
S jrS rS rS rS r	\
rS	rg)r   im  aG  Deviation coding (also known as sum-to-zero coding).

Compares the mean of each level to the mean-of-means. (In a balanced
design, compares the mean of each level to the overall mean.)

For full-rank coding, a standard intercept term is added.

One level must be omitted to avoid redundancy; by default this is the last
level, but this can be adjusted via the `omit` argument.

.. warning:: There are multiple definitions of 'deviation coding' in
   use. Make sure this is the one you expect before trying to interpret
   your results!

Examples:

.. ipython:: python

   # Reduced rank
   dmatrix("C(a, Sum)", balanced(a=4))
   # Full rank
   dmatrix("0 + C(a, Sum)", balanced(a=4))
   # Omit a different level
   dmatrix("C(a, Sum(1))", balanced(a=3))
   dmatrix("C(a, Sum('a1'))", balanced(a=3))

This is equivalent to R's `contr.sum`.
Nc                     Xl         g r   omit)r   r   s     r   r   Sum.__init__  s    	r   c                 b    U R                   c  [        U5      S-
  $ [        XR                   5      $ )Nr   )r   r   r]   rk   s     r   _omit_iSum._omit_i  s)    99v;?"fii00r   c                    [        U5      nU R                  U5      n[        R                  " US-
  5      n[        R                  " X"S-
  45      nUS U2S S 24   US U2S S 24'   SXSS S 24'   XCS 2S S 24   XSS-   S 2S S 24'   U$ Nr   r`   )r   r   r   r2   empty)r   rP   r   omit_ir2   outs         r   _sum_contrastSum._sum_contrast  s    Kf%ffQUmhhq5z"gvgqj/GVGQJAI"7A:QJL!O
r   c                     U R                  U5      n[        R                  " [        R                  " [	        U5      5      UR
                  45      nS/UR                  -   n[        X45      $ )N[mean])rs   r   column_stackonesr   r   r   r   )r   rP   contrastr   r   s        r   rl   Sum.code_with_intercept  sS    ..v6"''#f+"6!HI#*x'?'??f66r   c                     U R                  U5      nU R                  U5      nUS U XS-   S  -   n[        U[        SU5      5      $ )Nr   zS.)r   r   r   rR   )r   rP   r   r   included_levelss        r   rs   Sum.code_without_intercept  sM    ##F+f% &/FA:<,@@fl4&IJJr   r   r   )r$   r%   r&   r'   r(   r   r   r   rl   rs   r   r*   r+   r,   r   r   r   r   m  s(    :17K Lr   r   c                  .   [        5       n U R                  / SQ5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ SQ/ SQ/5      (       d   eU R                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  SS	/S	S/S
S
//5      (       d   eU R                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  SS	/S	S/S
S
//5      (       d   e[        SS9nUR                  / SQ5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ SQ/ SQ/5      (       d   eUR                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  SS	/S
S
/S	S//5      (       d   eUR                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  S
S
/SS	/S	S//5      (       d   e[        SS9nUR                  / SQ5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ SQ/ SQ/5      (       d   eUR                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  S
S
/SS	/S	S//5      (       d   e[        SS9nUR                  / SQ5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ SQ/ SQ/5      (       d   eUR                  / SQ5      nUR                  SS/:X  d   e[        R                  " UR
                  S
S
/SS	/S	S//5      (       d   eg )Nr_   )r   [S.a][S.b])r   r   r   )r   r   r   r   r`   r`   r   r   r   r   r`   )r`   rv   rb   z[S.-1]z[S.-2]r   )r   r   [S.c]r   )r   r   r0   z[S.0]z[S.2]rb   )r   r   r   r.   )r   rl   r   r   rw   r   rs   )ry   r   t2t3t4s        r   test_Sumr     sJ   	B##O4F!!%AAAA;;v}}y)[&IJJJJ&&7F!!gw%7777;;v}}1v1vBx&@AAAA&&|4F!!h%9999;;v}}1v1vBx&@AAAA	!B##O4F!!%AAAA;;v}}y+y&IJJJJ&&7F!!gw%7777;;v}}1vBx!Q&@AAAA&&y1F!!gw%7777;;v}}Bx!Q!Q&@AAAA	"B##O4F!!%AAAA;;v}}{Iy&IJJJJ&&7F!!gw%7777;;v}}Bx!Q!Q&@AAAA	#B##O4F!!%AAAA;;v}}{Iy&IJJJJ&&7F!!gw%7777;;v}}Bx!Q!Q&@AAAAr   c                   .    \ rS rSrSrS rS rS r\r	Sr
g)r   i  a  Helmert contrasts.

Compares the second level with the first, the third with the average of
the first two, and so on.

For full-rank coding, a standard intercept term is added.

.. warning:: There are multiple definitions of 'Helmert coding' in
   use. Make sure this is the one you expect before trying to interpret
   your results!

Examples:

.. ipython:: python

   # Reduced rank
   dmatrix("C(a, Helmert)", balanced(a=4))
   # Full rank
   dmatrix("0 + C(a, Helmert)", balanced(a=4))

This is equivalent to R's `contr.helmert`.
c                     [        U5      n[        R                  " X"S-
  45      n[        R                  " SU5      USS  [        R                  " US-
  5      '   SU[        R
                  " US-
  5      '   U$ r   )r   r   rp   r   diag_indicestriu_indices)r   rP   r   contrs       r   _helmert_contrastHelmert._helmert_contrast  sd    K  !U$,.IIaOab	"//!a%()(*booa!e$%r   c           	          [         R                  " [         R                  " [        U5      5      U R	                  U5      45      n[        SS/[        USS  5      -   5      n[        X#5      $ )NH.r   r   )r   r   r   r   r   rR   r   r   )r   rP   r   r   s       r   rl   Helmert.code_with_intercept  s[    ??WWS[!4#9#9&#AB
 'tk]T&*=M-MNh88r   c           	      V    U R                  U5      n[        U[        SUSS  5      5      $ )Nr   r   )r   r   rR   r   rP   r   s      r   rs   Helmert.code_without_intercept	  s,    ))&1hT6!":(FGGr   r,   N)r$   r%   r&   r'   r(   r   rl   rs   r   r*   r+   r,   r   r   r   r     s    .,9H Lr   r   c            	         [        5       n / SQS4 H  nU R                  U5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ SQ/ SQ/ SQ/5      (       d   eU R                  U5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ S	Q/ S
Q/ SQ/5      (       a  M   e   g )Nr.   r/   rT   d)z[H.intercept][H.b][H.c][H.d])r   r`   r`   r`   )r   r   r`   r`   )r   r   r0   r`   )r   r   r      )r   r   r   )r`   r`   r`   r   )r   r0   r`   )r   r   r   )r   rl   r   r   rw   r   rs   )ry   rP   r   s      r   test_Helmertr     s    	B')=>''/%%)UUUU{{MMnm\J
 
 	
 
 **62%%)DDDD{{MML+z9M
 
 	
 
 ?r   c                   .    \ rS rSrSrS rS rS r\r	Sr
g)r   i   a  Backward difference coding.

This coding scheme is useful for ordered factors, and compares the mean of
each level with the preceding level. So you get the second level minus the
first, the third level minus the second, etc.

For full-rank coding, a standard intercept term is added (which gives the
mean value for the first level).

Examples:

.. ipython:: python

   # Reduced rank
   dmatrix("C(a, Diff)", balanced(a=3))
   # Full rank
   dmatrix("0 + C(a, Diff)", balanced(a=3))
c                    [        U5      n[        R                  " X"S-
  45      n[        R                  " SU5      n[        R                  " XD5      n[        R
                  " US-
  5      u  pg[        R                  " U5      nXR-
  [        U5      -  X6U   Xx   4'   [        R                  " XDS S S2   5      n	[        R                  " US-
  5      u  pg[        R                  " U5      nU	[        U5      -  X6U   S-   Xx   4'   U$ r   )	r   r   rp   r   repeatr   argsortr   tril_indices)
r   rP   nlevelsr   	int_range	upper_introw_icol_i	col_order	lower_ints
             r   _diff_contrastDiff._diff_contrast4  s    f+'Q;/0IIa)	IIi3	w{3 JJu%	5>5HEM
 5
I 001 IIi4R49	w{3JJu%	8AE'N8RI"E$445r   c                     [         R                  " [         R                  " [        U5      5      U R	                  U5      45      n[        U[        SU5      5      $ )ND.)r   r   r   r   r   r   rR   r   s      r   rl   Diff.code_with_interceptG  sA    ??BGGCK$8$:M:Mf:U#VWhT6(BCCr   c           	      V    U R                  U5      n[        U[        SUS S 5      5      $ )Nr   r`   )r   r   rR   r   s      r   rs   Diff.code_without_interceptK  s,    &&v.hT6#2;(GHHr   r,   N)r$   r%   r&   r'   r(   r   rl   rs   r   r*   r+   r,   r   r   r   r      s    &&DI Lr   r   c                     [        5       n U R                  / SQ5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ SQ/ SQ/ SQ/5      (       d   eU R                  / SQ5      nUR                  / SQ:X  d   e[        R                  " UR
                  / SQ/ S	Q/ S
Q/ SQ/5      (       d   eg )Nr   )[D.a][D.b][D.c]z[D.d])r                     п)r         ?r   r   )r   r         ?r   )r   r   r         ?)r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r  )r   rl   r   r   rw   r   rs   rx   s     r   	test_diffr  R  s    	B##$89F!!%IIII;;-,+*		
    &&';<F!!%@@@@;;*)('		
   r   c           
         Uc  Un[        U5      (       a  U" 5       n[        U[        5      (       a  U$ [        R                  " U5      n[        UR                  [        R                  5      (       a,  [        U[        S[        UR                  S   5      5      5      $ U (       a  UR                  U5      $ UR                  U5      $ )Ncustomr   )callabler:   r   r   r   r   r   numberrR   r   r   rl   rs   )r   rP   r   defaultas_arrays        r   code_contrast_matrixr	  s  s    :(N++zz(#Hx~~ryy11l8U8>>!3D-EF
 	
 ++F33..v66r   r   ) __all__numpyr   patsyr	   
patsy.utilr
   r   r   r   r   r   r7   r@   rK   rR   rU   rX   r]   rc   r   rz   objectr   r   r   r   r   r   r   r  r	  r,   r   r   <module>r     s    L    D&	2&R=I	D
;1 1h@0K6 K\AH>& >B#BL9f 9x
 /6 /dB7r   