double maxExpectation(int M, vector <int> day, vector <int> win, vector <int> gain) { Max = M; IIVec Days; int i; for (i = 0; i < (int)day.size(); ++i) { if (win[i] > 0 && gain[i] > 0) { Days.push_back(II(day[i], i)); } } sort(Days.begin(), Days.end()); _day.clear(); _win.clear(); _gain.clear(); memset(memo, 0, sizeof(memo)); for (i = 0; i < (int)Days.size(); ++i) { _day.push_back(Days[i].first); _win.push_back(win[Days[i].second] * 0.01); _gain.push_back(gain[Days[i].second]); } if (Days.size() <= 0 || M <= _day[0]) { return M; } return rec(0, M - _day[0]); }
double ROC(const DoubleVec & _score_pos, const DoubleVec & _score_neg, DoubleVec &sens_vec, DoubleVec &spec_vec, DoubleVec &cutoffs) { if(_score_pos.size()==0 || _score_neg.size()==0) { cout << "Error: No positive or negative scores"; return(-1); } sens_vec.clear(); spec_vec.clear(); cutoffs.clear(); DoubleVec score_pos=_score_pos; DoubleVec score_neg=_score_neg; std::sort(score_pos.begin(),score_pos.end()); std::sort(score_neg.begin(),score_neg.end()); double integral=0; sens_vec.push_back(0); spec_vec.push_back(1); cutoffs.push_back(min(score_neg.front(),score_pos.front())); DoubleVec::const_iterator cut_neg=score_neg.begin(); DoubleVec::const_iterator cut_pos=score_pos.begin(); while(cut_pos<score_pos.end()) { while(cut_neg<score_neg.end()) // Move cut_neg to first value >= cut_pos { if(*cut_neg<*cut_pos) cut_neg++; else break; } unsigned TP=unsigned(cut_pos-score_pos.begin()); unsigned FP=unsigned(cut_neg-score_neg.begin()); unsigned TN=score_neg.size()-FP; unsigned FN=score_pos.size()-TP; double sens=double(TP)/score_pos.size(); double spec=double(TN)/score_neg.size(); integral+=(sens-sens_vec.back())*(spec+spec_vec.back())/2.0; sens_vec.push_back(sens); spec_vec.push_back(spec); cutoffs.push_back(*cut_pos); double last_cutoff=*cut_pos; while(cut_pos<score_pos.end()) // Advance cut_pos while = last cutoff !!Change that to move cut_pos to first value >= cut_neg, perfect symmetry { if(*cut_pos==last_cutoff) cut_pos++; else break; } while(cut_neg<score_neg.end()) // Advance cut_pos while = last cutoff { if(*cut_neg==last_cutoff) cut_neg++; else break; } TP=unsigned(cut_pos-score_pos.begin()); FP=unsigned(cut_neg-score_neg.begin()); TN=score_neg.size()-FP; FN=score_pos.size()-TP; sens=double(TP)/score_pos.size(); spec=double(TN)/score_neg.size(); integral+=(sens-sens_vec.back())*(spec+spec_vec.back())/2.0; sens_vec.push_back(sens); spec_vec.push_back(spec); double cp=score_pos.back(); double cn=score_neg.back(); if(cut_pos<score_pos.end()) cp=*cut_pos; if(cut_neg<score_neg.end()) cn=*cut_neg; cutoffs.push_back(min(cp,cn)); } integral+=(1-sens_vec.back())*(spec_vec.back()+0)/2.0; sens_vec.push_back(1); spec_vec.push_back(0); cutoffs.push_back(max(score_neg.back(),score_pos.back())); return(integral); }