-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradientMatchingEngine_wParameterEstimation.cpp
More file actions
232 lines (172 loc) · 6.61 KB
/
GradientMatchingEngine_wParameterEstimation.cpp
File metadata and controls
232 lines (172 loc) · 6.61 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* GradientMatchingEngine_wParameterEstimation.cpp
*
* Created on: 23 Jun 2014
* Author: rusty
*/
#include "IInferenceEngine.h"
GradientMatchingEngine_wParameterEstimation::GradientMatchingEngine_wParameterEstimation(int unitCount, int timestepCount, int parametersPerUnit, vector<vector <double> >* interpolatedTimeseries, int simID){
this->unitCount=unitCount;
this->timestepCount = timestepCount;
this->parametersPerUnit = parametersPerUnit;
this->interpolatedTimeseries = interpolatedTimeseries;
this->simID=simID;
X = zeros<mat>(1,1);
G = zeros<mat>(1,1);
jHat.resize(unitCount, vector<double>(unitCount + parametersPerUnit*unitCount, 0));
//
// this->jHat = new double*[unitCount];
// for (int i=0; i < this->unitCount; i++){jHat[i] = new double[unitCount+(parametersPerUnit*unitCount)];}
//
// this->fullTimeseries = new double*[nTimesteps];
// int columnCount = unitCount + 1;
// for (int i = 0; i <= nTimesteps; i++) {
// fullTimeseries[i] = new double[columnCount];
// for (int j=0; j<columnCount; j++){
// fullTimeseries[i][j] = populationTimeseries.at(i).at(j);
// }
// }
}
bool GradientMatchingEngine_wParameterEstimation::runInference(int sampleStepLength){
// cout << interpolatedTimeseries->size() << endl;
if ((int)interpolatedTimeseries->size()<=unitCount+1){
cerr << "cannot run gradient matching. Too few observations -> system is not over-constrained. Please re-do interpolation." << endl;
return false;
}
if(!fillJhat(sampleStepLength)){
return false;
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!switch!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// saveResult();
return true;
}
// WRITE THIS:****************************************************
bool GradientMatchingEngine_wParameterEstimation::saveResult(){
// string fname = "_GM_result.log";
// stringstream str_ID;
// str_ID << ID;
// fname.insert(0, str_ID.str());
//
// ofstream ofile;
// ofile.open(fname.c_str());
// if(!ofile){
// cerr << "GM result log file not open" << endl;
// return false;
// }
// ofile << "recontructed adjacency (Jhat) = " << endl << endl;
// for (int i=0;i<unitCount;i++){
//
// for (int j=0;j<unitCount;j++){
// ofile << jHat[i][j] << " ";
// }
// ofile << endl;
// }
// ofile << endl;
//
// // this should be a separate function:
// ofile << "recontructed parameters = " << endl << endl;
// for (int i=0; i<unitCount; i++){ofile << jHat[i][unitCount] << endl;}
//
// ofile << "*********************************************************" << endl;
// ofile.close();
return true;
}
bool GradientMatchingEngine_wParameterEstimation::fillJhat(int sampleStepLength){
for (int i=0;i<unitCount;i++){
mat temp = zeros(1,unitCount+(parametersPerUnit));
if(!timmeJhatI(i, sampleStepLength,temp)){
return false;
}
for (int j=0;j<unitCount+(parametersPerUnit);j++){
jHat.at(i).at(j) = temp(0,j);
}
}
return true;
}
bool GradientMatchingEngine_wParameterEstimation::timmeJhatI(int unitID, int sampleStepLength, mat &JhatI){
int t = 0;
int t2;
// int M = (timestepCount/sampleStepLength) - 1;
int M = (int)interpolatedTimeseries->size();
mat X = zeros<mat>(1,M);
mat G = zeros<mat>(unitCount+(parametersPerUnit),M);
double xtau[unitCount];
double xdotTau;
for (int m=0;m<M;m++){
t2 = t + sampleStepLength;
xtau[unitID] = interpolatedTimeseries->at(m).at(unitID+1);
xdotTau = interpolatedTimeseries->at(m).at(unitID + 1 + unitCount);
// xtau[unitID] = (fullTimeseries[t2][1+unitID] + fullTimeseries[t][1+unitID])/2;
// xdotTau = (fullTimeseries[t2][1+unitID] - fullTimeseries[t][1+unitID])/((fullTimeseries[t2][0] - fullTimeseries[t][0]));
//xtau[unitID] = (histories[unitID]->getHistoryDiTi(0,t2)+histories[unitID]->getHistoryDiTi(0,t))/2;
//xdotTau = (histories[unitID]->getHistoryDiTi(0,t2)-histories[unitID]->getHistoryDiTi(0,t))/(thistories[t2]-thistories[t]);
X(0,m) = xdotTau;// - (xtau[unitID]*parameters[unitID]); // !! naughty workaround now
//X(0,m) = xdotTau - (xtau[unitID]*Units[unitID]->getParamI(0)); // !! need unit parameters...
for(int i=0;i<unitCount;i++){
xtau[i] = interpolatedTimeseries->at(m).at(i+1);
// (fullTimeseries[t2][1+i] + fullTimeseries[t][1+i])/2;
//G(i,m) = sin(xtau[i]);
G(i,m) = xtau[i] * xtau[unitID];
}
G(unitCount,m) = xtau[unitID];
t = t2;
}
//mat Jhat = X*trans(G)*inv(G*trans(G));
// INSERT CHECK FOR IF THIS OPERATION IS SUCCESSFUL..
mat Iv_temp;
if(!inv(Iv_temp, G*trans(G))){
cerr << "inference failure: sampled matrix singular" << endl;
return false;
}
JhatI = X*trans(G)*Iv_temp;
return true;
}
void GradientMatchingEngine_wParameterEstimation::printJhat(){
cout << "recontructed adjacency (Jhat) = " << endl << endl;
for (int i=0;i<unitCount;i++){
for (int j=0;j<unitCount;j++){
cout << jHat.at(i).at(j) << " ";
}
cout << endl;
}
cout << endl;
// this should be a separate function:
cout << "recontructed parameters = " << endl << endl;
for (int i=0; i<unitCount; i++){cout << jHat.at(i).at(unitCount) << endl;}
cout << "*********************************************************" << endl;
}
bool GradientMatchingEngine_wParameterEstimation::getEstimatedIM(vector<vector<double> >& estIM){
if ((int)estIM.size()!=unitCount || (int)estIM.at(0).size()!=unitCount){
cerr << "estimated interaction matrix could not be obtained: size mismatch." << endl;
return false;
}
for (int i=0; i<unitCount; i++){
for (int j=0; j<unitCount; j++){
estIM.at(i).at(j) = jHat.at(i).at(j);
}
}
return true;
}
bool GradientMatchingEngine_wParameterEstimation::getEstimatedParams(vector<double>& estParams){
if ((int)estParams.size()!=unitCount){
cerr << "estimated parameters could not be obtained: size mismatch." << endl;
return false;
}
for (int i=0; i<unitCount; i++){estParams.at(i) = jHat.at(i).at(unitCount);}
return true;
}
bool GradientMatchingEngine_wParameterEstimation::qualityOfReconstruction(double accuracy, double& quality, vector<double > IMelements){
return true;
}
bool GradientMatchingEngine_wParameterEstimation::percentageQualityOfReconstruction(double accuracy, double& quality, vector<double > IMelements){
return true;
}
bool GradientMatchingEngine_wParameterEstimation::originalQualityOfReconstruction(double accuracy, double& quality, vector<double > IMelements){
return true;
}
bool GradientMatchingEngine_wParameterEstimation::unweightedQualityOfReconstruction(double threshold, double& quality, vector< vector<double > > adjacency){
return true;
}
double GradientMatchingEngine_wParameterEstimation::getSampleRate(int sampleStepLength){
return 0;
}