| 1 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | Real AUCRoc(Set tasasROC, Set methodInterp) | 
|---|
| 3 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 4 | { | 
|---|
| 5 |   Set aux = Extract(tasasROC,1,2); | 
|---|
| 6 |   Set claTFP = Classify(aux, Real(Set reg1, Set reg2) | 
|---|
| 7 |   { | 
|---|
| 8 |     Real tfp1 = reg1[1]; | 
|---|
| 9 |     Real tfp2 = reg2[1]; | 
|---|
| 10 |     Compare(tfp1, tfp2) | 
|---|
| 11 |   }); | 
|---|
| 12 |   Set graph =   EvalSet(claTFP, Set(Set class) | 
|---|
| 13 |   { | 
|---|
| 14 |     Real maxTvp = SetMax(Traspose(class)[2]); | 
|---|
| 15 |     Select(class, Real(Set reg){EQ(reg[2], maxTvp)})[1] | 
|---|
| 16 |   }); | 
|---|
| 17 |   Set tasasROCUnique = Extract(graph,1,2); | 
|---|
| 18 |   If(Card(tasasROCUnique)>2, | 
|---|
| 19 |   { | 
|---|
| 20 |     Set txFP     = Traspose(tasasROCUnique)[1]; | 
|---|
| 21 |     Set txVP     = Traspose(tasasROCUnique)[2]; | 
|---|
| 22 |     Set setFunSet = SetOfSet(Traspose(SetOfSet(txFP, txVP)) ); | 
|---|
| 23 |     Real get.tvp(Real tfp) | 
|---|
| 24 |     { | 
|---|
| 25 |       Set tfpSet = SetOfReal(tfp); | 
|---|
| 26 |       Real tvp = Interp(tfpSet, setFunSet, methodInterp)[1][2]; | 
|---|
| 27 |       tvp | 
|---|
| 28 |     }; | 
|---|
| 29 |     IntegrateQAG(get.tvp, 0, 1) | 
|---|
| 30 |   }, | 
|---|
| 31 |   { | 
|---|
| 32 |     WriteLn("Intervalos insuficientes para calcular AUC","W"); | 
|---|
| 33 |     0 | 
|---|
| 34 |   }) | 
|---|
| 35 | }; | 
|---|
| 36 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 37 | PutDescription( | 
|---|
| 38 | "Calcula los valores area ROC (AUC) dado las tasas de aciertos de un modelo | 
|---|
| 39 | (TFP, TVP,TVN,TFN)", | 
|---|
| 40 |  AUCRoc); | 
|---|
| 41 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 45 | Real GiniRoc(Set tasasROC, Set methodInterp) | 
|---|
| 46 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 47 | {  2*AUCRoc(tasasROC, methodInterp) - 1  }; | 
|---|
| 48 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 49 | PutDescription( | 
|---|
| 50 | "Calcula el indice de GINI dado las tasas de aciertos ROC de un modelo | 
|---|
| 51 | (TFP, TVP,TVN,TFN)", | 
|---|
| 52 |  GiniRoc); | 
|---|
| 53 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 57 | Real KSRoc(Set tasasROC, Set methodInterp) | 
|---|
| 58 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 59 | { | 
|---|
| 60 |   Set aux = Extract(tasasROC,1,2); | 
|---|
| 61 |   Set claTFP = Classify(aux, Real(Set reg1, Set reg2) | 
|---|
| 62 |   { | 
|---|
| 63 |     Real tfp1 = reg1[1]; | 
|---|
| 64 |     Real tfp2 = reg2[1]; | 
|---|
| 65 |     Compare(tfp1, tfp2) | 
|---|
| 66 |   }); | 
|---|
| 67 |   Set graph =   EvalSet(claTFP, Set(Set class) | 
|---|
| 68 |   { | 
|---|
| 69 |     Real maxTvp = SetMax(Traspose(class)[2]); | 
|---|
| 70 |     Select(class, Real(Set reg){EQ(reg[2], maxTvp)})[1] | 
|---|
| 71 |   }); | 
|---|
| 72 |   Set tasasROCUnique = Extract(graph,1,2); | 
|---|
| 73 |  | 
|---|
| 74 |   Set txFP     = Traspose(tasasROCUnique)[1]; | 
|---|
| 75 |   Set txVP     = Traspose(tasasROCUnique)[2]; | 
|---|
| 76 |   Set setFunSet = SetOfSet(Traspose(SetOfSet(txFP, txVP)) ); | 
|---|
| 77 |   Real get.tvp(Real tfp) | 
|---|
| 78 |   { | 
|---|
| 79 |     Set tfpSet = SetOfReal(tfp); | 
|---|
| 80 |     Real tvp = Interp(tfpSet, setFunSet, methodInterp)[1][2]; | 
|---|
| 81 |     tvp | 
|---|
| 82 |   }; | 
|---|
| 83 |   Set curvasROC = EvalSet(Range(0,1,0.0001), Set (Real k){ | 
|---|
| 84 |     [[k,    get.tvp(k)]] | 
|---|
| 85 |   }); | 
|---|
| 86 |   Matrix yTvp = SetMat(Extract(curvasROC,2)); | 
|---|
| 87 |   Matrix yTfp = SetMat(Extract(curvasROC,1)); | 
|---|
| 88 |   Real IndKs = MatMax(yTvp-yTfp) | 
|---|
| 89 | }; | 
|---|
| 90 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 91 | PutDescription( | 
|---|
| 92 | "Calcula el indice Kolmogorov Smirnov ROC dado las tasas de aciertos de  | 
|---|
| 93 | un modelo (TFP, TVP,TVN,TFN)", | 
|---|
| 94 | KSRoc); | 
|---|
| 95 | ////////////////////////////////////////////////////////////////////////////// | 
|---|