tests.h 5.44 KB
Newer Older
Damien Leroux's avatar
Damien Leroux committed
1
2
3
4
5
6
#ifndef _SPEL_MODEL_TESTS_H_
#define _SPEL_MODEL_TESTS_H_

static inline
VectorXd f_test(model& model_current, model& model_new)
{
7
8
9
10
11
12
    /*auto res_cur = model_current.residuals().array();*/
    /*auto res_new = model_new.residuals().array();*/
    /*VectorXd rss_cur = (res_cur * res_cur).colwise().sum();*/
    /*VectorXd rss_new = (res_new * res_new).colwise().sum();*/
	VectorXd rss_cur = model_current.rss();
	VectorXd rss_new = model_new.rss();
Damien Leroux's avatar
Damien Leroux committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    int dof_cur = model_current.rank();
    int dof_new = model_new.rank();
    int nind = model_current.Y().innerSize();
#if 0
    std::cout << "==================================================================" << std::endl;
    std::cout << "nind " << nind << std::endl;
    std::cout << "cur" << std::endl << model_current.X().transpose() << std::endl;
    std::cout << "dof_cur " << dof_cur << std::endl;
    std::cout << "rss_cur " << rss_cur.transpose() << std::endl;
    /*std::cout << "res_cur" << std::endl << res_cur.transpose() << std::endl;*/
    std::cout << "new" << std::endl << model_new.X().transpose() << std::endl;
    std::cout << "dof_new " << dof_new << std::endl;
    std::cout << "res_new" << std::endl << res_new.transpose() << std::endl;
    /*std::cout << "rss_new " << rss_new.transpose() << std::endl;*/
    std::cout << "==================================================================" << std::endl;
#endif
    if (dof_new <= dof_cur) {
        /*std::cout << "dof_new[" << dof_new << "] <= dof_cur[" << dof_cur << ']' << std::endl;*/
        return VectorXd::Zero(rss_new.innerSize());
    }
    if (nind <= dof_new) {
        /*std::cout << "nind[" << nind << "] <= dof_new[" << dof_new << ']' << std::endl;*/
        /* FIXME: HALT! */
        throw 0;
        return VectorXd::Zero(rss_new.innerSize());
    }
    VectorXd F(rss_new.innerSize());
    int dof_num = dof_new - dof_cur;
    int dof_denom = nind - dof_new;
    double dof_num_inv = 1. / dof_num;
    double dof_denom_inv = 1. / dof_denom;
    /*std::cout << "dof_num " << dof_num << std::endl;*/
    /*std::cout << "dof_denom " << dof_denom << std::endl;*/
    for (int i = 0; i < rss_new.innerSize(); ++i) {
        if (dof_num > 0) {
            double rnew = rss_new(i);
            set_if_much_smaller_than(rnew, rss_cur(i));
            F(i) = ((rss_cur(i) - rnew) * dof_num_inv) / (rnew * dof_denom_inv);
            if (F(i) <= 0) {
                F(i) = 0;
            } else {
                /* NdDL : c'est immonde. */
                F(i) = -log10(boost::math::ibeta(dof_denom * .5, dof_num * .5, dof_denom / (dof_denom + dof_num * F(i))));
            }
        } else {
            F(i) = 0;
        }
    }
    return F;
}


static inline
double chi2(model& model_current, model& model_new)
{
68
69
70
71
72
73
    /*auto res_cur = model_current.residuals().array();*/
    /*auto res_new = model_new.residuals().array();*/
    /*VectorXd rss_cur = (res_cur * res_cur).colwise().sum();*/
    /*VectorXd rss_new = (res_new * res_new).colwise().sum();*/
	VectorXd rss_cur = model_current.rss();
	VectorXd rss_new = model_new.rss();
Damien Leroux's avatar
Damien Leroux committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    int dof_cur = model_current.rank();
    int dof_new = model_new.rank();
    double Chi2 = 0;
    for (int i = 0; i < rss_cur.size(); ++i) {
        if (around_zero(rss_cur(i))) {
            break;
        }
        double rnew = rss_new(i);
        set_if_much_smaller_than(rnew, rss_cur(i));
        Chi2 += log(rss_cur(i) / rss_new(i));
    }
    int df = model_current.Y().outerSize() * (dof_new - dof_cur);
    if (Chi2 <= 0 || df <= 0) {
        return 0;
    } else {
        return -log10(boost::math::gamma_q(df * .5, Chi2 * .5));
    }
}

static inline
VectorXd chi2(model& model_current, model& model_new, size_t bloc_size)
{
96
97
98
99
100
101
    /*auto res_cur = model_current.residuals().array();*/
    /*auto res_new = model_new.residuals().array();*/
    /*VectorXd rss_cur = (res_cur * res_cur).colwise().sum();*/
    /*VectorXd rss_new = (res_new * res_new).colwise().sum();*/
	VectorXd rss_cur = model_current.rss();
	VectorXd rss_new = model_new.rss();
Damien Leroux's avatar
Damien Leroux committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    int dof_cur = model_current.rank();
    int dof_new = model_new.rank();
    double Chi2 = 0;
    assert(!(model_current.Y().outerSize() % bloc_size) && "STOOPID. BLOC SIZE MUST BE A DIVISOR OF THE NUMBER OF COLUMNS IN Y.");
    VectorXd ret(model_current.Y().outerSize() / bloc_size);
    for (int c = 0, ofs = 0; ofs < model_current.Y().outerSize(); ofs += bloc_size, ++c) {
        for (size_t i = ofs; i < (ofs + bloc_size); ++i) {
            if (around_zero(rss_cur(i))) {
                break;
            }
            double rnew = rss_new(i);
            set_if_much_smaller_than(rnew, rss_cur(i));
            Chi2 += log(rss_cur(i) / rss_new(i));
        }
        int df = bloc_size * (dof_new - dof_cur);
        if (Chi2 <= 0 || df <= 0) {
            ret(c) = 0;
        } else {
            ret(c) = -log10(boost::math::gamma_q(df * .5, Chi2 * .5));
        }
    }
    return ret;
}

static inline
VectorXd r2(model& mini_model, model& final_model)
{
129
130
131
132
133
134
    /*auto res_mini = mini_model.residuals().array();*/
    /*auto res_final = final_model.residuals().array();*/
    /*VectorXd tss = (res_mini * res_mini).colwise().sum();*/
    /*VectorXd rss = (res_final * res_final).colwise().sum();*/
	VectorXd tss = mini_model.rss();
	VectorXd rss = final_model.rss();
Damien Leroux's avatar
Damien Leroux committed
135
136
137
138
139
140
141
142
143
144
145
146
    VectorXd ret(tss.innerSize());
    for (int i = 0; i < ret.innerSize(); ++i) {
        if (around_zero(tss(i))) {
            ret(i) = 0;
        } else {
            ret(i) = (tss(i) - rss(i)) / tss(i);
        }
    }
    return ret;
}

#endif