Skip to content

Commit

Permalink
Per #2887, add Npairs member to the ContingencyTable class, eliminate…
Browse files Browse the repository at this point in the history
… the n() accessor function, and carefully replace references to n() with n_pairs() for the integer number of matched pairs or total() with the double-precision sum of the weights.
  • Loading branch information
JohnHalleyGotway committed Oct 4, 2024
1 parent 97cfbfa commit 17dc1b7
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 260 deletions.
42 changes: 21 additions & 21 deletions src/libcode/vx_stat_out/stat_columns.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2088,7 +2088,7 @@ void write_fho_cols(const CTSInfo &cts_info,
// O_RATE
//
at.set_entry(r, c+0, // Total Count
cts_info.cts.n());
cts_info.cts.n_pairs());

at.set_entry(r, c+1, // Forecast Rate = FY/N
cts_info.cts.f_rate());
Expand All @@ -2114,7 +2114,7 @@ void write_ctc_cols(const CTSInfo &cts_info,
// FN_OY, FN_ON, EC_VALUE
//
at.set_entry(r, c+0, // Total Count
cts_info.cts.n());
cts_info.cts.n_pairs());

at.set_entry(r, c+1, // FY_OY
cts_info.cts.fy_oy());
Expand Down Expand Up @@ -2167,7 +2167,7 @@ void write_cts_cols(const CTSInfo &cts_info, int i,
// EC_VALUE
//
at.set_entry(r, c+0, // Total count
cts_info.cts.n());
cts_info.cts.n_pairs());

at.set_entry(r, c+1, // Base Rate (oy_tp)
cts_info.baser.v);
Expand Down Expand Up @@ -2812,8 +2812,8 @@ void write_mctc_cols(const MCTSInfo &mcts_info,
// Dump out the MCTC line:
// TOTAL, N_CAT, Fi_Oj, EC_VALUE
//
at.set_entry(r, c+0, // Total Count
mcts_info.cts.total());
at.set_entry(r, c+0, // Total number of pairs
mcts_info.cts.n_pairs());

at.set_entry(r, c+1, // Number of categories
mcts_info.cts.nrows());
Expand Down Expand Up @@ -2853,8 +2853,8 @@ void write_mcts_cols(const MCTSInfo &mcts_info, int i,
// HSS_EC, HSS_EC_BCL, HSS_EC_BCU,
// EC_VALUE
//
at.set_entry(r, c+0, // Total count
mcts_info.cts.total());
at.set_entry(r, c+0, // Total number of pairs
mcts_info.cts.n_pairs());

at.set_entry(r, c+1, // Number of categories
mcts_info.cts.nrows());
Expand Down Expand Up @@ -3268,7 +3268,7 @@ void write_pct_cols(const PCTInfo &pct_info,
// THRESH (last threshold)
//
at.set_entry(r, c+0, // Total Count
pct_info.pct.n());
pct_info.pct.n_pairs());

at.set_entry(r, c+1, // N_THRESH
pct_info.pct.nrows() + 1);
Expand All @@ -3283,11 +3283,11 @@ void write_pct_cols(const PCTInfo &pct_info,
col++;

at.set_entry(r, col, // Event Count (OY)
pct_info.pct.event_count_by_row(i));
pct_info.pct.event_total_by_row(i));
col++;

at.set_entry(r, col, // Non-Event Count (ON)
pct_info.pct.nonevent_count_by_row(i));
pct_info.pct.nonevent_total_by_row(i));
col++;
}

Expand Down Expand Up @@ -3384,7 +3384,7 @@ void write_pstd_cols(const PCTInfo &pct_info, int alpha_i,

void write_pjc_cols(const PCTInfo &pct_info,
AsciiTable &at, int r, int c) {
int i, col, n;
int i, col;

//
// Nx2 Contingency Table Joint/Continuous Probability
Expand All @@ -3396,29 +3396,29 @@ void write_pjc_cols(const PCTInfo &pct_info,
// THRESH (last threshold)
//
at.set_entry(r, c+0, // Total Count
pct_info.pct.n());
pct_info.pct.n_pairs());

at.set_entry(r, c+1, // N_THRESH
pct_info.pct.nrows() + 1);

//
// Write THRESH, OY, ON for each row of the Nx2 table
//
n = pct_info.pct.n();
double total = pct_info.pct.total();
for(i=0, col=c+2; i<pct_info.pct.nrows(); i++) {

at.set_entry(r, col, // THRESH
pct_info.pct.threshold(i));
col++;

at.set_entry(r, col, // OY_TP
(n == 0 ? bad_data_double :
pct_info.pct.event_count_by_row(i)/(double) n));
(is_eq(total, 0.0) ? bad_data_double :
pct_info.pct.event_total_by_row(i)/total));
col++;

at.set_entry(r, col, // ON_TP
(n == 0 ? bad_data_double :
pct_info.pct.nonevent_count_by_row(i)/(double) n));
(is_eq(total, 0.0) ? bad_data_double :
pct_info.pct.nonevent_total_by_row(i)/total));
col++;

at.set_entry(r, col, // CALIBRATION
Expand Down Expand Up @@ -3462,7 +3462,7 @@ void write_prc_cols(const PCTInfo &pct_info,
// THRESH (last threshold)
//
at.set_entry(r, c+0, // Total Count
pct_info.pct.n());
pct_info.pct.n_pairs());

at.set_entry(r, c+1, // N_THRESH
pct_info.pct.nrows() + 1);
Expand Down Expand Up @@ -3513,7 +3513,7 @@ void write_eclv_cols(const TTContingencyTable &ct,
// N_PNT, [CL_], [VALUE_] (for each point)
//
at.set_entry(r, c+0, // Total Number of pairs
ct.n());
ct.n_pairs());

at.set_entry(r, c+1, // Base Rate
ct.baser());
Expand Down Expand Up @@ -3553,7 +3553,7 @@ void write_nbrctc_cols(const NBRCTSInfo &nbrcts_info,
// FN_OY, FN_ON
//
at.set_entry(r, c+0, // Total Count
nbrcts_info.cts_info.cts.n());
nbrcts_info.cts_info.cts.n_pairs());

at.set_entry(r, c+1, // FY_OY
nbrcts_info.cts_info.cts.fy_oy());
Expand Down Expand Up @@ -3601,7 +3601,7 @@ void write_nbrcts_cols(const NBRCTSInfo &nbrcts_info, int i,
// BAGSS, BAGSS_BCL, BAGSS_BCU
//
at.set_entry(r, c+0, // Total count
nbrcts_info.cts_info.cts.n());
nbrcts_info.cts_info.cts.n_pairs());

at.set_entry(r, c+1, // Base Rate (oy_tp)
nbrcts_info.cts_info.baser.v);
Expand Down
48 changes: 31 additions & 17 deletions src/libcode/vx_statistics/contable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ ContingencyTable & ContingencyTable::operator+=(const ContingencyTable & t) {
exit(1);
}

// Increment the number of pairs
Npairs += t.Npairs;

// Increment table entries
for(int i=0; i<E.size(); ++i) E[i] += t.E[i];

Expand All @@ -92,6 +95,7 @@ void ContingencyTable::clear() {

E.clear();
Nrows = Ncols = 0;
Npairs = 0;
ECvalue = bad_data_double;
Name.clear();

Expand All @@ -107,6 +111,7 @@ void ContingencyTable::assign(const ContingencyTable & t) {
E = t.E;
Nrows = t.Nrows;
Ncols = t.Ncols;
Npairs = t.Npairs;
ECvalue = t.ECvalue;
Name = t.Name;

Expand Down Expand Up @@ -135,6 +140,7 @@ void ContingencyTable::dump(ostream & out, int depth) const {

out << prefix << "Nrows = " << Nrows << "\n";
out << prefix << "Ncols = " << Ncols << "\n";
out << prefix << "Npairs = " << Npairs << "\n";
out << prefix << "ECvalue = " << ECvalue << "\n";
out << prefix << "\n";

Expand Down Expand Up @@ -201,6 +207,15 @@ void ContingencyTable::set_size(int NR, int NC) {

////////////////////////////////////////////////////////////////////////

void ContingencyTable::set_n_pairs(int n) {

Npairs = n;

return;
}

////////////////////////////////////////////////////////////////////////

void ContingencyTable::set_ec_value(double v) {

// Do not override the default value with bad data
Expand Down Expand Up @@ -239,6 +254,8 @@ void ContingencyTable::set_entry(int row, int col, double value) {

E[(rc_to_n(row, col))] = value;

// Number of pairs defined by set_n_pairs(int)

return;
}

Expand All @@ -248,6 +265,9 @@ void ContingencyTable::inc_entry(int row, int col, double weight) {

E[(rc_to_n(row, col))] += weight;

// Increment pair counter
Npairs++;

return;
}

Expand Down Expand Up @@ -483,74 +503,68 @@ double TTContingencyTable::on() const {

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::n() const {
return total();
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::f_rate() const {
return compute_proportion(fy(), n());
return compute_proportion(fy(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::h_rate() const {
return compute_proportion(fy_oy(), n());
return compute_proportion(fy_oy(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::o_rate() const {
return compute_proportion(oy(), n());
return compute_proportion(oy(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::fy_oy_tp() const {
return compute_proportion(fy_oy(), n());
return compute_proportion(fy_oy(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::fy_on_tp() const {
return compute_proportion(fy_on(), n());
return compute_proportion(fy_on(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::fn_oy_tp() const {
return compute_proportion(fn_oy(), n());
return compute_proportion(fn_oy(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::fn_on_tp() const {
return compute_proportion(fn_on(), n());
return compute_proportion(fn_on(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::fy_tp() const {
return compute_proportion(fy(), n());
return compute_proportion(fy(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::fn_tp() const {
return compute_proportion(fn(), n());
return compute_proportion(fn(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::oy_tp() const {
return compute_proportion(oy(), n());
return compute_proportion(oy(), total());
}

////////////////////////////////////////////////////////////////////////

double TTContingencyTable::on_tp() const {
return compute_proportion(on(), n());
return compute_proportion(on(), total());
}

////////////////////////////////////////////////////////////////////////
Expand Down
20 changes: 10 additions & 10 deletions src/libcode/vx_statistics/contable.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ContingencyTable {
int Nrows;
int Ncols;

int Npairs;
double ECvalue;

ConcatString Name;
Expand All @@ -67,13 +68,15 @@ class ContingencyTable {
virtual void set_size(int);
virtual void set_size(int NR, int NC);

void set_n_pairs(int);
void set_ec_value(double);
void set_name(const char *);

// Get attributes
int nrows() const;
int ncols() const;

int n_pairs() const;
double ec_value() const;
ConcatString name() const;

Expand Down Expand Up @@ -110,6 +113,7 @@ class ContingencyTable {
inline int ContingencyTable::nrows() const { return Nrows; }
inline int ContingencyTable::ncols() const { return Ncols; }

inline int ContingencyTable::n_pairs() const { return Npairs; }
inline double ContingencyTable::ec_value() const { return ECvalue; }
inline ConcatString ContingencyTable::name() const { return Name; }

Expand Down Expand Up @@ -159,18 +163,16 @@ class Nx2ContingencyTable : public ContingencyTable {
void inc_nonevent (double value, double weight=1.0);

// Get table entries
double event_count_by_thresh(double) const;
double nonevent_count_by_thresh(double) const;
double event_total_by_thresh(double) const;
double nonevent_total_by_thresh(double) const;

double event_count_by_row(int row) const;
double nonevent_count_by_row(int row) const;
double event_total_by_row(int row) const;
double nonevent_total_by_row(int row) const;

// Set counts
void set_event(int row, double);
void set_nonevent(int row, double);

double n() const;

// Column totals
double event_col_total() const;
double nonevent_col_total() const;
Expand Down Expand Up @@ -202,8 +204,8 @@ class Nx2ContingencyTable : public ContingencyTable {

////////////////////////////////////////////////////////////////////////

inline double Nx2ContingencyTable::event_count_by_row (int row) const { return entry(row, nx2_event_column); }
inline double Nx2ContingencyTable::nonevent_count_by_row (int row) const { return entry(row, nx2_nonevent_column); }
inline double Nx2ContingencyTable::event_total_by_row (int row) const { return entry(row, nx2_event_column); }
inline double Nx2ContingencyTable::nonevent_total_by_row (int row) const { return entry(row, nx2_nonevent_column); }

inline double Nx2ContingencyTable::event_col_total () const { return col_total(nx2_event_column); }
inline double Nx2ContingencyTable::nonevent_col_total () const { return col_total(nx2_nonevent_column); }
Expand Down Expand Up @@ -253,8 +255,6 @@ class TTContingencyTable : public ContingencyTable {
double fn() const;
double fy() const;

double n() const;

// FHO rates where:
// f_rate = FY/N
// h_rate = fy_oy/N
Expand Down
Loading

0 comments on commit 17dc1b7

Please sign in to comment.