Skip to content

Commit 86fdcd9

Browse files
authored
Merge pull request #430 from yisangriB/master
sy - fixing nan handling error for GSA
2 parents 22f1906 + c26b7ab commit 86fdcd9

1 file changed

Lines changed: 41 additions & 13 deletions

File tree

modules/performUQ/SimCenterUQ/nataf_gsa/runGSA.cpp

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,41 @@ void runGSA::preprocess_gmat(vector<vector<double>> gmat, vector<vector<double>>
200200
std::vector<double> avg(nqoi, 0.0);
201201
std::vector<double> var(nqoi, 0.0);
202202
std::vector<double> normVar(nqoi, 0.0);
203-
for (std::vector<double>& row : gmat_eff)
204-
{
205-
std::transform(avg.begin(), avg.end(), row.begin(), avg.begin(), std::plus<double>()); // sum
206-
std::transform(var.begin(), var.end(), row.begin(), var.begin(), [](double a, double b) {return a + b*b; }); // square sum
207-
}
208-
const double scale(1/(double)nmc);
209-
std::transform(avg.begin(), avg.end(), avg.begin() ,[scale](double element) { return element *= scale; }); // avg of value
210-
std::transform(var.begin(), var.end(), var.begin(), [scale](double element) { return element *= scale; }); // avg of squared value
211-
212-
// Final Variance
213-
std::transform(var.begin(), var.end(), avg.begin(), var.begin(), [](double a, double b) {return abs(a - b * b); });
214-
varQoI = var;
203+
std::vector<int> validCount(nqoi, 0); // NEW: per-column non-NaN count
204+
205+
// for (std::vector<double>& row : gmat_eff)
206+
// {
207+
// std::transform(avg.begin(), avg.end(), row.begin(), avg.begin(), std::plus<double>()); // sum
208+
// std::transform(var.begin(), var.end(), row.begin(), var.begin(), [](double a, double b) {return a + b*b; }); // square sum
209+
// }
210+
// const double scale(1/(double)nmc);
211+
// std::transform(avg.begin(), avg.end(), avg.begin() ,[scale](double element) { return element *= scale; }); // avg of value
212+
// std::transform(var.begin(), var.end(), var.begin(), [scale](double element) { return element *= scale; }); // avg of squared value
213+
//
214+
// // Final Variance
215+
// std::transform(var.begin(), var.end(), avg.begin(), var.begin(), [](double a, double b) {return abs(a - b * b); });
216+
// varQoI = var;
217+
218+
for (std::vector<double>& row : gmat_eff)
219+
{
220+
// NEW: element-wise accumulation that skips NaNs
221+
for (size_t j = 0; j < row.size(); ++j) {
222+
if (!std::isnan(row[j])) {
223+
avg[j] += row[j];
224+
var[j] += row[j] * row[j];
225+
validCount[j]++;
226+
}
227+
}
228+
}
229+
// CHANGED: scale per-column by its own valid count instead of a single nmc
230+
for (size_t j = 0; j < avg.size(); ++j) {
231+
double s = (validCount[j] > 0) ? 1.0 / (double)validCount[j] : 0.0;
232+
avg[j] *= s;
233+
var[j] *= s;
234+
}
235+
// Final Variance
236+
std::transform(var.begin(), var.end(), avg.begin(), var.begin(), [](double a, double b) {return abs(a - b * b); });
237+
varQoI = var;
215238

216239
// Normalized effective matrix
217240
for (auto& row : gmat_eff)
@@ -536,7 +559,8 @@ void runGSA::runMultipleGSA(vector<vector<double>> gmat_red, int Kos)
536559

537560
void runGSA::runSingleCombGSA(vector<vector<double>> gmat, int Ko, vector<int> comb, vector<double>& Si, char Opt)
538561
{
539-
//
562+
563+
//
540564
// we will ignore NaN in gvec
541565
//
542566

@@ -576,6 +600,7 @@ void runGSA::runSingleCombGSA(vector<vector<double>> gmat, int Ko, vector<int> c
576600
gvec.reserve(nmc);
577601
for (int i = 0; i < nmc; i++) {
578602
gvec.push_back(gmat[i][nq]);
603+
// std::cout<<gmat[i][nq]<<std::endl;
579604
}
580605

581606

@@ -654,6 +679,7 @@ void runGSA::runSingleCombGSA(vector<vector<double>> gmat, int Ko, vector<int> c
654679
count_valid = 0;
655680
for (int ns = 0; ns < nmc; ns++)
656681
{
682+
//std::cout << gvec[ns] << std::endl;
657683
// Only if g is not NaN
658684
if (!std::isnan(gvec[ns])) {
659685
data(ne, count_valid) = xval[ns][idx];
@@ -677,8 +703,10 @@ void runGSA::runSingleCombGSA(vector<vector<double>> gmat, int Ko, vector<int> c
677703
Kthres = nmc_new / 10; // main
678704
}
679705

706+
680707
while (1) {
681708

709+
682710
try
683711
{
684712
status = model.learn(data, Kos, maha_dist, static_subset, 1000, 1000, V * 1.e-12, false);// max kmeans iter = 100, max EM iter = 200, convergence variance = V*1.e-15

0 commit comments

Comments
 (0)