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

Ticket #967: roc.tol

File roc.tol, 4.0 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    Matrix py = Min(Max(TruncDecimal_M(py_, 6), 0), 1);
39    Set index = Range(1, 0, -1/Min(Max(numQ, 2), Rows(py)-1));
40    Set sliceSet =
41      SetOfReal(1)
42    <<EvalSet(index, Real(Real q){Real slice = MatQuantile(py, q)})<<
43      SetOfReal(0);
44
45    Set graf = EvalSet(sliceSet, Set(Real slice)
46    {
47      Matrix yEst = GE(py, Rand(Rows(py), 1 , slice, slice));
48      Real VP     = MatSum(And(yEst, y));
49      Real FN     = MatSum(And(Not(yEst), y));     
50      Real VN     = MatSum(And(Not(yEst), Not(y)));
51      Real FP     = MatSum(And(yEst, Not(y)));
52 
53      Real TVP    = VP/(VP+FN);
54      Real TFP    = 1-VN/(VN+FP);//=FP/(VN+FP) 
55      SetOfReal(TFP, TVP, slice)
56    })
57  };
58  Set Curve.Abs
59  (
60    Set ROCSet, // Table with reg = SetOfReal(TFP, TVP, slice, ...)
61    Real M, // Total
62    Real m  // Infected
63  )
64  {
65    Real p = m/(M-m);
66    EvalSet(ROCSet, Set(Set reg)
67    {
68      Real TFP        = reg[1];
69      Real TVP        = reg[2];
70      Real slice      = reg[3];
71      Real infected   = TVP*m;
72      Real population = (p*TVP+TFP)*M/(1+p);
73      SetOfReal(TFP, TVP, population, infected, slice)
74    })
75  };
76
77  Set Eval.TFPSet(Set tfpSet, Set ROCSet)
78  {
79    Set tROCSet = Traspose(ROCSet);
80    Set txFP    = tROCSet[1];
81    Set txVP    = tROCSet[2];
82    Set txSL    = tROCSet[3];
83   
84    Set setFunSet = SetOfSet
85    (
86      Traspose(SetOfSet(txFP, txVP)),
87      Traspose(SetOfSet(txFP, txSL))
88    );
89    _Interp(tfpSet, setFunSet, _.interp.Set)
90  };
91
92  Set EvalAbs.PopSet(Set popSet, Set ROCAbsSet)
93  {
94    Set tROCSet = Traspose(ROCAbsSet);
95    Set txPop    = tROCSet[3];
96    Set txInf    = tROCSet[4];
97    Set txSL     = tROCSet[5];
98   
99    Set setFunSet = SetOfSet
100    (
101      Traspose(SetOfSet(txPop, txInf)),
102      Traspose(SetOfSet(txPop, txSL))
103    );
104    _Interp(popSet, setFunSet, _.interp.Set)
105  };
106
107  Real Get.Area(Set ROCSet)
108  {
109    Set txFP     = Traspose(ROCSet)[1];
110    Set txVP     = Traspose(ROCSet)[2];
111    Set setFunSet = SetOfSet( Traspose(SetOfSet(txFP, txVP)) );
112    Real get.tvp(Real tfp)
113    {
114      Set tfpSet = SetOfReal(tfp);
115      Real tvp = _Interp(tfpSet, setFunSet, _.interp.Set)[1][2];
116      tvp
117    };
118    IntegrateQAG(get.tvp, 0, 1)
119  };
120
121  Real Get.AreaTrap(Set xySet)
122  {
123    Real auc = 0;
124    Real k = 2;
125    Real While(LE(k,Card(xySet)),
126    {
127      Real xk   = xySet[k][1];
128      Real xk_1 = xySet[k-1][1];
129
130      Real yk = xySet[k][2];
131      Real yk_1 = xySet[k-1][2];
132
133      Real auc:= (xk-xk_1)*(yk+yk_1)/2 + auc;
134      Real k := k+1
135    });
136    auc
137  };
138
139  Real Get.AreaFull(Set ROCSet)
140  {
141    Set txFP     = Traspose(ROCSet)[1];
142    Set txVP     = Traspose(ROCSet)[2];
143    Set setFunSet = SetOfSet( Traspose(SetOfSet(txFP, txVP)) );
144    Real get.tvp(Real tfp)
145    {
146      Set tfpSet = SetOfReal(tfp);
147      Real tvp = _Interp(tfpSet, setFunSet, _.interp.Set)[1][2];
148      tvp
149    };
150    IntegrateQAG(get.tvp, 0, 1)
151  }
152]];