forked from rikigigi/analisi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcoliblocchi.h
204 lines (176 loc) · 5.58 KB
/
calcoliblocchi.h
1
2
3
4
5
6
7
8
9
10
11
12
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
*
* (c) Riccardo Bertossa, 2019
*
* Use at your own risk.
*
* If you modified the code, I could be happy if you contribute on github!
*
**/
#ifndef CALCOLIBLOCCHI_H
#define CALCOLIBLOCCHI_H
#include <vector>
#include <iostream>
#include <assert.h>
template <class T> class MediaVar{
public:
MediaVar(T*Tmedio, T*Tvar,T*delta,T*tmp) : Tmedio(Tmedio), Tvar(Tvar),delta(delta),tmp(tmp) {}
void calcola_begin(unsigned int s,T * calc){
Tmedio->reset(s);
Tmedio->azzera();
Tvar->reset(s);
Tvar->azzera();
delta->reset(s);
tmp->reset(s);
iblock=0;
}
void calcola(T* calcolo) {
#ifdef DEBUG2
std::cerr << "*delta = *calcolo - *Tmedio;\n";
#endif
//algoritmo media e varianza online
*delta = *calcolo;
*delta -= *Tmedio;
//*delta = *calcolo - *Tmedio
#ifdef DEBUG2
std::cerr << "*delta = *calcolo - *Tmedio;\n";
#endif
*tmp = *delta;
*tmp /= (iblock+1);
*Tmedio += *tmp;
//*Tmedio += (*delta)/(iblock+1); // calcolo / double , calcolo+=calcolo
#ifdef DEBUG2
std::cerr << "*Tvar += (*delta)*(*calcolo-*Tmedio);\n";
#endif
*tmp = *calcolo;
*tmp -= *Tmedio;
*tmp *= *delta;
*Tvar += *tmp;
iblock++;
}
void calcola_end(unsigned int n_b){
*Tvar/=((n_b-1)*n_b);
}
private:
T *Tmedio,*Tvar,*calcolo,*delta,*tmp;
unsigned int iblock;
};
/**
* Calcola, oltre la media e la varianza, anche la covarianza
**/
template <class T> class MediaVarCovar{
public:
MediaVarCovar(unsigned int n_columns,
std::vector< std::pair<unsigned int,unsigned int> > Cvar_list
) : n_columns(n_columns),Cvar_list(Cvar_list),dx(0),dx2(0) {
for (unsigned int i=0;i<Cvar_list.size();i++){
if(Cvar_list.at(i).first>=n_columns || Cvar_list.at(i).second>=n_columns ){
std::cerr << "Errore: indice della covarianza fuori dal range!\n";
abort();
}
#ifdef DEBUG
else {
std::cerr << "Cov("<<Cvar_list.at(i).first<<","<<Cvar_list.at(i).second<<") ";
}
std::cerr <<"\n";
#endif
}
}
~MediaVarCovar(){
dealloc();
}
void calcola_begin(unsigned int si_,T*c){
dealloc();
unsigned int size_blockT=c->lunghezza();
assert(size_blockT%n_columns==0);
size_block=size_blockT/n_columns;
iblock=0;
for (unsigned int icol=0;icol<n_columns;icol++){
medie.push_back(new double[size_block]);
varianze.push_back(new double[size_block]);
for (unsigned int i=0;i<size_block;i++){
varianze.back()[i]=0;
medie.back()[i]=0;
}
}
for (unsigned int icvar=0;icvar<Cvar_list.size();icvar++){
covarianze.push_back(new double[size_block]);
for (unsigned int i=0;i<size_block;i++){
covarianze.back()[i]=0;
}
}
dx=new double[n_columns];
dx2=new double[n_columns];
}
void dealloc(){
for(unsigned int i=0;i<medie.size();i++){
delete [] medie.at(i);
delete [] varianze.at(i);
}
for(unsigned int i=0;i<covarianze.size();i++){
delete [] covarianze.at(i);
}
medie.clear();
varianze.clear();
covarianze.clear();
delete [] dx;
delete [] dx2;
}
void calcola(T* calcolo) {
assert(dx2!=0 && dx!=0);
assert(calcolo->lunghezza()%n_columns==0);
assert(size_block==calcolo->lunghezza()/n_columns);
iblock++;
for (unsigned int ib=0;ib<size_block;ib++){
for (unsigned int icol=0;icol<n_columns;icol++){
//calcola dx=x-media
dx[icol]=calcolo->elemento(ib*n_columns+icol)-medie[icol][ib];
//aggiorna media+=dx/iblock
medie[icol][ib]+=dx[icol]/iblock;
//calcola dx2=x-media
dx2[icol]=calcolo->elemento(ib*n_columns+icol)-medie[icol][ib];
//calcola varianze
varianze[icol][ib]+=dx[icol]*dx2[icol];
}
//calcola covarianze
for (unsigned int icvar=0;icvar<Cvar_list.size();icvar++){
covarianze[icvar][ib]+=dx[Cvar_list[icvar].first]*dx2[Cvar_list[icvar].second];
}
}
}
void calcola_end(unsigned int n_b){
assert(iblock>1);
assert(n_b==iblock);
for (unsigned int ib=0;ib<size_block;ib++){
for (unsigned int icol=0;icol<n_columns;icol++){
varianze[icol][ib]/=(iblock*(iblock-1));
}
for (unsigned int icvar=0;icvar<Cvar_list.size();icvar++){
covarianze[icvar][ib]/=(iblock*(iblock-1));
}
}
delete [] dx;
delete [] dx2;
dx=0;dx2=0;
}
double * media(unsigned int col_idx){
return medie.at(col_idx);
}
double * varianza(unsigned int col_idx){
return varianze.at(col_idx);
}
double * covarianza(unsigned int cvar_idx){
return covarianze.at(cvar_idx);
}
unsigned int size(){return size_block;}
unsigned int n_column(){return medie.size();}
unsigned int n_cvar(){return covarianze.size();}
private:
std::vector<double*>medie,varianze,covarianze;
double *dx,*dx2;
std::vector<unsigned int> idx;
std::vector< std::pair<unsigned int,unsigned int> > Cvar_list;
unsigned int n_columns,size_block;
unsigned int iblock;
};
#endif // CALCOLIBLOCCHI_H