close Warning: Can't synchronize with repository "(default)" (/var/svn/tolp does not appear to be a Subversion repository.). Look in the Trac log for more information.

Ticket #1350: roc_v1.tol

File roc_v1.tol, 3.8 KB (added by Pedro Gea, 13 years ago)
Line 
1NameBlock ROC =
2[[
3  Set _.interp.Set = SetOfText("linear", "cspline", "akima");
4  Set _.slice.Set  = Range(0, 1, 0.02);
5
6  Set _Interp
7  (
8    Set xSet,      // Set xSet with we want to know y for all funSet
9    Set setFunSet, // Set of tables with same structure SetOfReal(x, y)
10    Set interpSet  // Set with gsl valid interpolate method (see gsl_interp)
11  )
12  {
13    Set setxyFunSet = EvalSet(setFunSet, Set(Set funSet)
14    {
15      SetOfMatrix(SetCol(Traspose(funSet)[1]), SetCol(Traspose(funSet)[2]))
16    });   
17 
18    EvalSet(xSet, Set(Real x)
19    {
20      Set ySet = EvalSet(setxyFunSet, Real(Set xyFunSet)
21      {
22        Matrix xFunSet = xyFunSet[1];
23        Matrix yFunSet = xyFunSet[2];
24
25        Set ySetAux = EvalSet(interpSet, Real(Text code)
26        {
27          Code gslInterp = gsl_interp(code, xFunSet, yFunSet);
28          gslInterp(0, x)
29        });
30        SetAvr(ySetAux)
31      });
32      SetOfReal(x)<<ySet
33    })
34  };
35
36  Set Curve(Matrix y, Matrix py, Real numQ)
37  {
38    Set index = Range(1, 0, -1/Min(Max(numQ, 2), Rows(py)-1));
39    Set graf = EvalSet(index, Set(Real q)
40    {
41      Real slice = MatQuantile(py, q);
42      Matrix yEst = GE(py, Rand(Rows(py), 1 , slice, slice));
43      Real VP     = MatSum(And(yEst, y));
44      Real FN     = MatSum(And(Not(yEst), y));     
45      Real VN     = MatSum(And(Not(yEst), Not(y)));
46      Real FP     = MatSum(And(yEst, Not(y)));
47 
48      Real TVP    = VP/(VP+FN);
49      Real TFP    = 1-VN/(VN+FP);//=FP/(VN+FP) 
50      SetOfReal(TFP, TVP, slice)
51    })
52  };
53
54  Set Curve.Abs
55  (
56    Set ROCSet, // Table with reg = SetOfReal(TFP, TVP, slice, ...)
57    Real M, // Total
58    Real m  // Infected
59  )
60  {
61    Real p = m/(M-m);
62    EvalSet(ROCSet, Set(Set reg)
63    {
64      Real TFP        = reg[1];
65      Real TVP        = reg[2];
66      Real slice      = reg[3];
67      Real infected   = TVP*m;
68      Real population = (p*TVP+TFP)*M/(1+p);
69      SetOfReal(TFP, TVP, population, infected, slice)
70    })
71  };
72
73  Set Eval.TFPSet(Set tfpSet, Set ROCSet)
74  {
75    Set tROCSet = Traspose(ROCSet);
76    Set txFP    = tROCSet[1];
77    Set txVP    = tROCSet[2];
78    Set txSL    = tROCSet[3];
79   
80    Set setFunSet = SetOfSet
81    (
82      Traspose(SetOfSet(txFP, txVP)),
83      Traspose(SetOfSet(txFP, txSL))
84    );
85    _Interp(tfpSet, setFunSet, _.interp.Set)
86  };
87
88  Set EvalAbs.PopSet(Set popSet, Set ROCAbsSet)
89  {
90    Set tROCSet = Traspose(ROCAbsSet);
91    Set txPop    = tROCSet[3];
92    Set txInf    = tROCSet[4];
93    Set txSL     = tROCSet[5];
94   
95    Set setFunSet = SetOfSet
96    (
97      Traspose(SetOfSet(txPop, txInf)),
98      Traspose(SetOfSet(txPop, txSL))
99    );
100    _Interp(popSet, setFunSet, _.interp.Set)
101  };
102
103  Real Get.Area(Set ROCSet)
104  {
105    Set txFP     = Traspose(ROCSet)[1];
106    Set txVP     = Traspose(ROCSet)[2];
107    Set setFunSet = SetOfSet( Traspose(SetOfSet(txFP, txVP)) );
108    Real get.tvp(Real tfp)
109    {
110      Set tfpSet = SetOfReal(tfp);
111      Real tvp = _Interp(tfpSet, setFunSet, _.interp.Set)[1][2];
112      tvp
113    };
114    IntegrateQAG(get.tvp, 0, 1)
115  };
116
117  Real Get.AreaTrap(Set xySet)
118  {
119    Real auc = 0;
120    Real k = 2;
121    Real While(LE(k,Card(xySet)),
122    {
123      Real xk   = xySet[k][1];
124      Real xk_1 = xySet[k-1][1];
125
126      Real yk = xySet[k][2];
127      Real yk_1 = xySet[k-1][2];
128
129      Real auc:= (xk-xk_1)*(yk+yk_1)/2 + auc;
130      Real k := k+1
131    });
132    auc
133  };
134
135  Set Get.AreaFull(Set ROCSet)
136  {
137    Set txFP     = Traspose(ROCSet)[1];
138    Set txVP     = Traspose(ROCSet)[2];
139    Set setFunSet = SetOfSet( Traspose(SetOfSet(txFP, txVP)) );
140    Real get.tvp(Real tfp)
141    {
142      Set tfpSet = SetOfReal(tfp);
143      Real tvp = _Interp(tfpSet, setFunSet, _.interp.Set)[1][2];
144      tvp
145    };
146    IntegrateQAG(get.tvp, 0, 1)
147  }
148]];