Exemple #1
0
DoubleVec *VectorOutputVal::value_list() const {
  DoubleVec *res = new DoubleVec(0);
  res->reserve(size_);
  for(unsigned int i=0;i<size_;i++)
    res->push_back(data[i]);
  return res;
}
Exemple #2
0
	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]);
	}
Exemple #3
0
void test_intersect()
{
	DoubleVec dv;
	IntVec iv;

	const struct {
		unsigned int pos;
		double val;
	} dTbl[] = {
		{ 5, 3.14 }, { 10, 2 }, { 12, 1.4 }, { 100, 2.3 }, { 1000, 4 },
	};

	const struct {
		unsigned int pos;
		int val;
	} iTbl[] = {
		{ 2, 4 }, { 5, 2 }, { 100, 4 }, { 101, 3}, { 999, 4 }, { 1000, 1 }, { 2000, 3 },
	};

	const struct {
		unsigned int pos;
		double d;
		int i;
	} interTbl[] = {
		 { 5, 3.14, 2 }, { 100, 2.3, 4 }, { 1000, 4, 1 }
	};

	for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(dTbl); i++) {
		dv.push_back(dTbl[i].pos, dTbl[i].val);
	}
	for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(iTbl); i++) {
		iv.push_back(iTbl[i].pos, iTbl[i].val);
	}

	int j = 0;
	for (typename DoubleVec::const_iterator i = dv.begin(), ie = dv.end(); i != ie; ++i) {
		CYBOZU_TEST_EQUAL(i->pos(), dTbl[j].pos);
		CYBOZU_TEST_EQUAL(i->val(), dTbl[j].val);
		j++;
	}

	j = 0;
	for (typename IntVec::const_iterator i = iv.begin(), ie = iv.end(); i != ie; ++i) {
		CYBOZU_TEST_EQUAL(i->pos(), iTbl[j].pos);
		CYBOZU_TEST_EQUAL(i->val(), iTbl[j].val);
		j++;
	}

	int sum = 0;
	for (typename IntVec::const_iterator i = iv.begin(), ie = iv.end(); i != ie; ++i) {
		sum += i->val() * i->val();
	}
	CYBOZU_TEST_EQUAL(sum, 71);

	typedef cybozu::nlp::Intersection<DoubleVec, IntVec> InterSection;
	InterSection inter(dv, iv);

	j = 0;
	for (typename InterSection::const_iterator i = inter.begin(), ie = inter.end(); i != ie; ++i) {
		CYBOZU_TEST_EQUAL(i->pos(), interTbl[j].pos);
		CYBOZU_TEST_EQUAL(i->val1(), interTbl[j].d);
		CYBOZU_TEST_EQUAL(i->val2(), interTbl[j].i);
		j++;
	}
}
Exemple #4
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);
}