Ejemplo n.º 1
0
void KernelEstimator::addValue(double data, const double weight) {

    if (weight == 0) {
        return;
    }
    data = round(data);
    int insertIndex = findNearestValue(data);
    if ((mNumValues <= insertIndex) || (mValues[insertIndex] != data)) {
        if (mNumValues < mValues.size()) {
            int left = mNumValues - insertIndex;
            std::cout << "Please cross-check here for array copy";
            std::copy(mValues.begin() + insertIndex, mValues.begin() + left, mValues.begin() + insertIndex + 1);
            std::copy(mWeights.begin() + insertIndex, mWeights.begin() + left, mWeights.begin() + insertIndex + 1);

            mValues[insertIndex] = data;
            mWeights[insertIndex] = weight;
            mNumValues++;
        }
        else {
            double_array newValues(mValues.size() * 2);
            double_array newWeights(mValues.size() * 2);
            int left = mNumValues - insertIndex;
            std::copy(mValues.begin(), mValues.begin() + insertIndex, newValues.begin());
            std::copy(mWeights.begin(), mWeights.begin() + insertIndex, newWeights.begin());

            newValues[insertIndex] = data;
            newWeights[insertIndex] = weight;
            std::copy(mValues.begin() + insertIndex, mValues.begin() + left, newValues.begin() + insertIndex + 1);
            std::copy(mWeights.begin() + insertIndex, mWeights.begin() + left, newWeights.begin() + insertIndex + 1);

            mNumValues++;
            mValues = newValues;
            mWeights = newWeights;
        }
        if (weight != 1) {
            mAllWeightsOne = false;
        }
    }
    else {
        mWeights[insertIndex] += weight;
        mAllWeightsOne = false;
    }
    mSumOfWeights += weight;
    double range = mValues[mNumValues - 1] - mValues[0];
    if (range > 0) {
        mStandardDev = std::fmax(range / sqrt(mSumOfWeights), mPrecision / (2 * 3));
        // allow at most 3 sds within one interval
    }
}
Ejemplo n.º 2
0
 void PhraseFeature::updateWeights(const FVector& weights) {
   for (set<PhraseFeature*>::iterator i = s_phraseFeatures.begin();
       i != s_phraseFeatures.end(); ++i) {
     PhraseFeature* pf = *i;
     vector<float> newWeights(pf->m_featureNames.size());
     for (size_t j = 0; j < pf->m_featureNames.size(); ++j) {
       FValue weight = weights[pf->m_featureNames[j]];
       newWeights[j] = weight;
     }
     ScoreComponentCollection mosesWeights = StaticData::Instance().GetAllWeights();
     mosesWeights.Assign(pf->m_phraseDictionary,newWeights);
     (const_cast<StaticData&>(StaticData::Instance()))
       .SetAllWeights(mosesWeights);
     //pf->m_phraseDictionary->GetFeature()->SetWeightTransModel(newWeights);
   }
 }
	SEXP importanceResamplingAuxiliary(SEXP lowerBound_sexp, SEXP trueProbabilities_sexp, SEXP n_sexp, SEXP seed_sexp)
	{
	BEGIN_RCPP
		std::vector<double> trueProbabilities;
		try
		{
			trueProbabilities = Rcpp::as<std::vector<double> >(trueProbabilities_sexp);
		}
		catch(...)
		{
			throw std::runtime_error("Input trueProbabilities must be a numeric vector");
		}
		for(std::vector<double>::iterator trueProbability = trueProbabilities.begin(); trueProbability != trueProbabilities.end(); trueProbability++)
		{
			if(*trueProbability <= 0 || *trueProbability >= 1)
			{
				throw std::runtime_error("Input trueProbability must be in (0, 1)");
			}
		}
		int nBernoullis = trueProbabilities.size();

		int lowerBound;
		try
		{
			lowerBound = Rcpp::as<int>(lowerBound_sexp);
		}
		catch(...)
		{
			throw std::runtime_error("Input lowerBound must be an integer");
		}
		if(lowerBound <= nBernoullis/2)
		{
			throw std::runtime_error("Input lowerBound must be bigger than nBernoullis/2");
		}
		if(lowerBound >= nBernoullis)
		{
			throw std::runtime_error("Input lowerBound must be smaller than nBernoullis");
		}

		int n;
		try
		{
			n = Rcpp::as<int>(n_sexp);
		}
		catch(...)
		{
			throw std::runtime_error("Input n must be an integer");
		}
		if(n < 1)
		{
			throw std::runtime_error("Input n must be positive");
		}

		int seed;
		try
		{
			seed = Rcpp::as<int>(seed_sexp);
		}
		catch(...)
		{
			throw std::runtime_error("Input seed must be an integer");
		}

		boost::mt19937 randomSource;
		randomSource.seed(seed);
		double newProbability = (double)lowerBound / (double)nBernoullis;
		std::vector<double> ratio1, ratio2;
		for(std::vector<double>::iterator trueProbability = trueProbabilities.begin(); trueProbability != trueProbabilities.end(); trueProbability++)
		{
			ratio1.push_back(*trueProbability / newProbability);
			ratio2.push_back((1 - *trueProbability) / (1 - newProbability));
		}
		boost::random::bernoulli_distribution<> bernoulli(newProbability);
		std::vector<int> valuesOfOne(n, 0);
		std::vector<int> newValuesOfOne(n, 0);
		std::vector<int> toSample;
		std::vector<mpfr_class> weights(n, 1), newWeights(n, 1);

		mpfr_class product = 1;
		for(int bernoulliCounter = 0; bernoulliCounter < nBernoullis; bernoulliCounter++)
		{
			toSample.clear();
			//Sample and update the weights. Everything in weightRatio1 has a weight of averageWeight * ratio1. Everything in weightRatio2 has a weight of averageWeight * ratio2. Anything in neither has a weight of 0. 
			for(int i = 0; i < n; i++)
			{
				int value = bernoulli(randomSource);
				if(value)
				{
					valuesOfOne[i]++;
					weights[i] *= ratio1[bernoulliCounter];
					toSample.push_back(i);
				}
				else
				{
					if(valuesOfOne[i] + nBernoullis - 1 - bernoulliCounter > lowerBound)
					{
						toSample.push_back(i);
						weights[i] *= ratio2[bernoulliCounter];
					}
				}
			}
			product *= (double) toSample.size() / (double)n;
			//Resampling step
			if(toSample.size() == 0)
			{
				return Rcpp::List::create(Rcpp::Named("estimate") = 0.0);
			}
			else
			{
				//With this probability we select something with weight ratio1
				boost::random::uniform_int_distribution<> simpleRandom(0, toSample.size()-1);
				for(int i = 0; i < n; i++)
				{
					int sampled = toSample[simpleRandom(randomSource)];
					newValuesOfOne[i] = valuesOfOne[sampled];
					newWeights[i] = weights[sampled];
				}
			}
			valuesOfOne.swap(newValuesOfOne);
			weights.swap(newWeights);
		}
		mpfr_class sum = 0;
		for(int i = 0; i < n; i++)
		{
			sum += weights[i];
		}
		sum /= n;
		sum *= product;
		return Rcpp::List::create(Rcpp::Named("estimate") = sum.convert_to<double>());
	END_RCPP
	}