(***********************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which typically
will have the same name as this file except ending in ".nb" instead of
".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent to
using the Evaluate Initialization Cells menu command in the front end.

DO NOT EDIT THIS FILE.  This entire file is regenerated automatically 
each time the parent Notebook file is saved in the Mathematica front end.
Any changes you make to this file will be overwritten.
***********************************************************************)





BeginPackage["DataAnalysis`Dissimilarity`","DataAnalysis`Covariances`",
    "DataAnalysis`Symbols`"];

Dissimilarity::usage = "Dissimilarity[X,disimil] calcula el arreglo \
triangular de disimilitudes, entre las filas de la matriz X, utilizando el \
indice disimil."

Jaccard::usage = "Indice de disimilitud para datos binarios."
Dice::usage = "Indice de disimilitud para datos binarios."
SokalMichener::usage = "Indice de disimilitud para datos binarios."
RusselRao::usage = "Indice de disimilitud para datos binarios."
Kulczinski1::usage = "Indice de disimilitud para datos binarios."
Ohiai::usage = "Indice de disimilitud para datos binarios."
SokalSneath1::usage = "Indice de disimilitud para datos binarios."
SokalSneath2::usage = "Indice de disimilitud para datos binarios."
Yule::usage = "Indice de disimilitud para datos binarios."
AngularDistance::usage = "Indice de disimilitud para datos binarios."
RogersTanimoto::usage = "Indice de disimilitud para datos binarios."

EuclideanDistance::usage = "Indice de disimilitud (distancia) para datos \
cuantitativos."
Manhattan::usage =  "Indice de disimilitud (distancia) para datos \
cuantitativos."
Chebychev::usage =  "Indice de disimilitud (distancia) para datos \
cuantitativos."
Minkowski::usage =  "Indice de disimilitud (distancia) para datos \
cuantitativos."
Camberra::usage =  "Indice de disimilitud (distancia) para datos \
cuantitativos."
ChiSquare::usage =  "Indice de disimilitud (distancia) sobre una tabla de \
contingencia."

Dissimilarity::NoKnow = "Dissimilarity no conoce el indice o distancia `1`"

Begin["`Private`"];

\!\(\(DisimTBin[X_, disim_]\  := \ 
      Module[{ds, A, n, p, k}, \n\tp\  = \ Length[X[\([1]\)]]; \n\t
        n\  = \ Length[X]; \n\t
        ds[i_, j_]\  := \ 
          For[k = 1; A\  = \ {{0, 0}, {0, 0}}, 
            k <= p, \(k++\), \(A[\([X[\([i, k]\)] + 1, 
                  X[\([j, k]\)] + 1]\)]++\)]; \n\t
        Table[disim[ds[i, j]; A], {i, 1, n - 1}, {j, i + 1, n}]];\)\n
  \(Jaccard[{{d_, c_}, {b_, a_}}]\  := \ N[1\  - \ a/\((a + b + c)\)];\)\n
  \(Dice[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ 2\ \ a/\((2\ a + b + c)\)];\)\n
  \(SokalMichener[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ \((a + d)\)/\((a + b + c + d)\)];\)\n
  \(RusselRao[{{d_, 0}, {0, a_}}]\  := \ 0;\)\n
  \(RusselRao[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ a/\((a + b + c + d)\)];\)\n
  \(Kulczinski1[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ a \((1/\((a + b)\)\  + \ 1/\((a + c)\))\)/2];\)\n
  \(Ohiai[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ a/Sqrt[\((a + b)\) \((a + c)\)]];\)\n
  \(SokalSneath1[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ 2 \((a + d)\)/\((2 \((a + d)\) + b + c)\)];\)\n
  \(SokalSneath2[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ a/\((a + 2 \((b + c)\))\)];\)\n
  \(Yule[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ Abs[a\ d\  - \ b\ c]/\((a\ d + \ b\ c)\)];\)\n
  \(AngularDistance[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \ Abs[a\ d\  - \ b\ c]/
            Sqrt[\((a + \ b)\) \((c + d)\) \((a + c)\) \((b + d)\)]];\)\n
  \(RogersTanimoto[{{d_, c_}, {b_, a_}}]\  := \ 
      N[1\  - \((a + d)\)/\((a + d + \ 
                2 \((b + c)\))\)];\)\n\[IndentingNewLine]\n
  \(DisimTB[X_, disim_, {a_, b_}]\  := \ 
      Module[{ds, A, n, p, k, Rg1}, \n\tp\  = \ Length[X[\([1]\)]]; \n\t
        n\  = \ Length[X]; \n\tRg1 = Thread[{a, b} \[Rule] {1, 2}]; \n\t
        ds[xi_, xj_]\  := \ 
          With[{}, \n\t\t\t\tA\  = \ {{0, 0}, {0, 
                  0}}; \n\t\t\t\tDo[\(A[\([xi[\([k]\)] /. Rg1, 
                    xj[\([k]\)] /. Rg1]\)]++\), {k, p}]; \n\t\t\t\tdisim[
              A]]; \n\tTable[
          ds[X[\([i]\)], X[\([j]\)]], {i, 1, n - 1}, {j, i + 1, n}]];\)\n
  \(DisimTB[X_, disim_, ndls_Integer]\  := \ 
      Module[{ds, n, p, k, emp}, \n\tp\  = \ Length[X[\([1]\)]]; \n\t
        n\  = \ Length[X]; \n\t
        ds[xi_, xj_]\  := \ 
          With[{}, \n\t\t\tFor[k = 1; emp = 0, k <= p, \(k++\), 
              If[xi[\([k]\)] == 
                  xj[\([k]\)], \(emp++\)]]; \t\n\t\t\tdisim[{{ndls - p, 
                  p - emp/2}, {p - emp/2, emp}}]]; \n\t
        Table[ds[X[\([i]\)], X[\([j]\)]], {i, 1, n - 1}, {j, i + 1, 
            n}]];\)\[IndentingNewLine]\n
  \(Distancia[xi_, xj_]\  := \ With[{x\  = \ xi - xj}, Norm[x]];\)\n
  \(Distancia[xi_, xj_, M_]\  := \ With[{x\  = \ xi - xj}, Norm[x, M]];\)\n
  \(Euclidea1[u_] := \@\(u . u\);\)\n
  \(Euclidea2[u_, M_] := \@\(u . \((M\ u)\)\);\)\n
  \(Euclidea3[u_, M_] := \@\(u . M . u\);\)\[IndentingNewLine]
  \(DisimTCnt[X_, Metrica_]\  := \ 
      Module[{n, Dst, m}, \n\tn\  = \ Length[X]; \n\t
        m\  = \ Switch[Metrica, \n\t\t\ \ \ \ \ \ \ \ \ \ VarianceInverse, \ 
            1/Variance[
                X], \n\ \ \ \ \ \ \ \ \ \t\ \ \ \ \ \ \ \ \ \ Mahalanobis, 
            Covariance[X], \n\t\t\ \ \ \ \ \ \ \ \ \ Minkowski[_], \ 
            Metrica /. {Minkowski[a_] -> a}, \n\t\t\t\t\ \ \ \ \ \ \ \ _, 
            0]; \n\tDst\  = \ 
          Switch[Metrica, \[IndentingNewLine]\ \ \ \ \ \ \ \ \
EuclideanDistance, \ 
            Euclidea1[#1 - #2] &, \[IndentingNewLine]\ \ \ \ \ \ \ \ \
VarianceInverse, \ 
            Euclidea2[#1\  - \ #2, m] &, \n\t\ \ \ \ \ \ \ \ \ \ Mahalanobis, 
            Euclidea3[#1\  - \ #2, m] &, \n\t\ \ \ \ \ \ \ \ \ \ Manhattan, \ 
            Apply[Plus, Abs[#1 - #2]] &, \n\t\ \ \ \ \ \ \ \ \ \ Chebychev, 
            Apply[Max, 
                Abs[#1 - #2]] &, \n\t\ \ \ \ \ \ \ \ \ \ Minkowski[_], \
\((Apply[Plus, \((Abs[#1 - #2])\)^m])\)^\((1/
                    m)\) &, \n\t\ \ \ \ \ \ \ \ \ Camberra, 
            Apply[Plus, 
                Abs[#1 - #2]/\((#1 + #2)\)] &, \[IndentingNewLine]\ \ \ \ \ \ \
\ _, Message[Dissimilarity::NoKnow\ , Metrica]]; 
        Table[Dst[X[\([i]\)], X[\([j]\)]], {i, 1, n - 1}, {j, i + 1, 
            n}]];\)\[IndentingNewLine]\[IndentingNewLine]
  \(DisimTCtg[X_, Metrica_]\  := \ 
      Module[{n, Dst, m}, \n\tn\  = \ Length[X]; \n\t
        xio\  = Apply[Plus, X, {1}]; \n\txoj\  = \ 1/Apply[Plus, X]; \n\t
        Dst\  = \ 
          Switch[Metrica, \[IndentingNewLine]\ \ \ \ \ ChiSquare, \ 
            Euclidea2[X[\([#1]\)]/xio[\([#1]\)] - X[\([#2]\)]/xio[\([#2]\)], 
                xoj] &, \n\t\ \ \ \ \ _, 
            Message[Dissimilarity::NoKnow\ , Metrica]]; 
        Table[Dst[i, j], {i, 1, n - 1}, {j, i + 1, 
            n}]];\)\[IndentingNewLine]\[IndentingNewLine]
  Dissimilarity[X_, dsm_]\  := \ 
    Which[dsm\  === \ ChiSquare, DisimTCtg[X, dsm], \n\t
      MemberQ[{EuclideanDistance, VarianceInverse, Mahalanobis, Manhattan, 
          Chebychev, Camberra}, dsm], DisimTCnt[X, dsm], \n\t
      Head[dsm]\  === \ Minkowski, DisimTCnt[X, dsm], \n\t
      MemberQ[{Jaccard, Dice, SokalMichener, RusselRao, Kulczinski1, Ohiai, 
          SokalSneath1, SokalSneat2, Yule, AngularDistance, RogersTanimoto}, 
        dsm], DisimTBin[X, dsm]\ , \n\tTrue, 
      Message[Dissimilarity::NoKnow\ , dsm]]\)

			End[];

		EndPackage[];