示例#1
0
//creates an 2 element array of x,y values where nu^2<x<1 and -beta<y<beta, this function calls the MC rejection algorithm (test probability) and then returns the xy pair if they are valid according to the test algorithm
double* XYgenerator::generateXYpair(){
	static double xy[2];
	
	xy[0] = RNG->Uniform(nu*nu,1);
	xy[1] = RNG->Uniform(-getBeta(xy[0]),getBeta(xy[0]));
	
	//if(testProbability(getProbability(xy[0],xy[1]),xy[0],xy[1]))
	//	return xy;
	//else generateXYpair();
	while(!testProbability(getProbability(xy[0],xy[1]),xy[0],xy[1])){
		xy[0] = RNG->Uniform(nu*nu,1);
		xy[1] = RNG->Uniform(-getBeta(xy[0]),getBeta(xy[0]));
	}
	
	return xy;
}
示例#2
0
 std::string SBMoffat::SBMoffatImpl::serialize() const
 {
     std::ostringstream oss(" ");
     oss.precision(std::numeric_limits<double>::digits10 + 4);
     oss << "galsim._galsim.SBMoffat("<<getBeta()<<", "<<getScaleRadius();
     oss << ", None, None, "<<getTrunc()<<", "<<getFlux();
     oss << ", galsim.GSParams("<<*gsparams<<"))";
     return oss.str();
 }
void CyclicCoordinateDescent::computeAsymptoticPrecisionMatrix(void) {

	typedef std::vector<int> int_vec;
	int_vec indices;
	hessianIndexMap.clear();

	int index = 0;
	for (int j = 0; j < J; ++j) {
		if (!fixBeta[j] &&
				(priorType != priors::LAPLACE || getBeta(j) != 0.0)) {
			indices.push_back(j);
			hessianIndexMap[j] = index;
			index++;
		}
	}

	hessianMatrix.resize(indices.size(), indices.size());
	modelSpecifics.makeDirty(); // clear hessian terms

	for (size_t ii = 0; ii < indices.size(); ++ii) {
		for (size_t jj = ii; jj < indices.size(); ++jj) {
			const int i = indices[ii];
			const int j = indices[jj];
//			std::cerr << "(" << i << "," << j << ")" << std::endl;
			double fisherInformation = 0.0;
			modelSpecifics.computeFisherInformation(i, j, &fisherInformation, useCrossValidation);
//			if (fisherInformation != 0.0) {
				// Add tuple to sparse matrix
//				tripletList.push_back(Triplet<double>(ii,jj,fisherInformation));
//			}
			hessianMatrix(jj,ii) = hessianMatrix(ii,jj) = fisherInformation;

//			std::cerr << "Info = " << fisherInformation << std::endl;
//			std::cerr << "Hess = " << getHessianDiagonal(0) << std::endl;
//			exit(-1);
		}
	}

//	sm.setFromTriplets(tripletList.begin(), tripletList.end());
//	cout << sm;
//	auto inv = sm.triangularView<Upper>().solve(dv1);
//	cout << sm.inverse();
//	cout << hessianMatrix << endl;

	// Take inverse
//	hessianMatrix = hessianMatrix.inverse();

//	cout << hessianMatrix << endl;
}
示例#4
0
/* Sample probabilities according to successes and failures */
void sampleProbabilities(const gsl_rng *r,
			 const int *CDFdata, int nCDFdata,
			 parameters *p, int nPar, const intparameters *ip, int nIPar) {
  int k;
  for(k=0; k<nPar; k++) {
    int k0=(k<=0)?0:getIntProposal(ip,k-1);
    int k1=(k<nIPar)?getIntProposal(ip,k):nCDFdata-1;
    int success=succInterval(CDFdata, k0, k1)/* (k0<=0)?CDFdata[k1-1]:CDFdata[k1-1]-CDFdata[k0-1] */;
    int failures=k1-k0-success;
    double estP;
    double alpha=getAlpha(), beta=getBeta();

    estP=gsl_ran_beta(r, success+alpha+1, failures+beta+1);
    /* fprintf(OUT, "p=%g\n", estP); */
    setProposal(p, k, estP);
  }
}
void AdaptiveSO2CPGSynPlas::updateWeights() {
    const double& phi     = getPhi();
    const double& beta    = getBeta();
    const double& gamma   = getGamma();
    const double& epsilon = getEpsilon();
    const double& mu      = getMu();
    const double& F       = getOutput(2);
    const double& w01     = getWeight(0,1);
    const double& x       = getOutput(0);
    const double& y       = getOutput(1);
    const double& P       = getPerturbation();

    // general approach
    setPhi    ( phi     + mu * gamma * F * w01 * y );
    setBeta   ( beta    + betaHebbRate    * x * F - betaDecayRate    * (beta    - betaZero) );
    setGamma  ( gamma   + gammaHebbRate   * x * F - gammaDecayRate   * (gamma   - gammaZero) );
    setEpsilon( epsilon + epsilonHebbRate * F * P - epsilonDecayRate * (epsilon - epsilonZero) );

}
示例#6
0
/* Selects changepoint position and adds new changepoint before or after */
void sampleDeath(const gsl_rng *r,
		 parameters* p, int actP, int nPar,
		 intparameters *ip, int actIP, int nIPar) {
  /* select change point that will be deleted */
  int kIndex=gsl_rng_uniform_int(r, actIP);
  int kDown, kUp;
  int succ, fail;
  /* Open probabilities that replaces probabilities 'left' and 'right'
     of deleted changepoint.*/
  double pNew;
  int i;
  static int hello=1;
  const int*CDF=getData(), nCDF=getNdata();
  double alpha=getAlpha(), beta=getBeta();

  if(hello) fprintf(OUT, "sampleDeath()\n"), hello=0;

  /*Copy kIndex parameters unchanged */
  copyIntOrg2Prop(ip, kIndex);

  /* Omit kIndex */
  for(i=kIndex; i<nIPar-1; i++) {
    setIntProposal(ip, i, getIntParameter(ip,i+1));
  }

  /*Copy kIndex double parameters unchanged */
  copyOrg2Prop(p, kIndex);

  kDown=(kIndex<=0)?0:getIntParameter(ip, kIndex-1);
  kUp=(kIndex==actIP)?nCDF-1:getIntParameter(ip,kIndex)-1;
  succ=succInterval(CDF, kDown, kUp), fail=failInterval(CDF, kDown, kUp);
  pNew=gsl_ran_beta(r, succ+alpha+1,fail+beta+1);
  setProposal(p, kIndex, pNew);

  /* Balance with corresponding birth move */
  proposalScale=(double)kUp-kDown;

  for(i=kIndex+1; i<nPar-1; i++) {
    setProposal(p, i, getParameter(p,i+1));
  }
}
void CyclicCoordinateDescent::kktSwindle(const ModeFindingArguments& arguments) {

	const auto maxIterations = arguments.maxIterations;
	const auto convergenceType = arguments.convergenceType;
	const auto epsilon = arguments.tolerance;

	// Make sure internal state is up-to-date
	checkAllLazyFlags();


	std::list<ScoreTuple> activeSet;
	std::list<ScoreTuple> inactiveSet;
	std::list<int> excludeSet;

	// Initialize sets
	int intercept = -1;
	if (hXI.getHasInterceptCovariate()) {
		intercept = hXI.getHasOffsetCovariate() ? 1 : 0;
	}

	for (int index = 0; index < J; ++index) {
		if (fixBeta[index]) {
			excludeSet.push_back(index);
		} else {
			if (index == intercept || // Always place intercept into active set
                !jointPrior->getSupportsKktSwindle(index)) {
// 				activeSet.push_back(index);
				activeSet.push_back(std::make_tuple(index, 0.0, true));
			} else {
				inactiveSet.push_back(std::make_tuple(index, 0.0, false));
			}
		}
	}

	bool done = false;
	int swindleIterationCount = 1;

//	int initialActiveSize = activeSet.size();
	int perPassSize = arguments.swindleMultipler;

	while (!done) {

		if (noiseLevel >= QUIET) {
			std::ostringstream stream;
			stream << "\nKKT Swindle count " << swindleIterationCount << ", activeSet size =  " << activeSet.size();
			logger->writeLine(stream);
		}

		// Enforce all beta[inactiveSet] = 0
		for (auto& inactive : inactiveSet) {
			if (getBeta(std::get<0>(inactive)) != 0.0) { // Touch only if necessary
				setBeta(std::get<0>(inactive), 0.0);
			}
		}

		double updateTime = 0.0;
		lastReturnFlag = SUCCESS;
		if (activeSet.size() > 0) { // find initial mode
			auto start = bsccs::chrono::steady_clock::now();

			findMode(begin(activeSet), end(activeSet), maxIterations, convergenceType, epsilon);

			auto end = bsccs::chrono::steady_clock::now();
			bsccs::chrono::duration<double> elapsed_seconds = end-start;
			updateTime = elapsed_seconds.count();
		}

		if (noiseLevel >= QUIET) {
			std::ostringstream stream;
			stream << "update time: " << updateTime << " in " << lastIterationCount << " iterations.";
			logger->writeLine(stream);
		}

		if (inactiveSet.size() == 0 || lastReturnFlag != SUCCESS) { // Computed global mode, nothing more to do, or failed

			done = true;

		} else { // still inactive covariates


			if (swindleIterationCount == maxIterations) {
				lastReturnFlag = MAX_ITERATIONS;
				done = true;
				if (noiseLevel > SILENT) {
					std::ostringstream stream;
					stream << "Reached maximum swindle iterations";
					logger->writeLine(stream);
				}
			} else {

				auto checkConditions = [this] (const ScoreTuple& score) {
					return (std::get<1>(score) <= jointPrior->getKktBoundary(std::get<0>(score)));
				};

//				auto checkAlmostConditions = [this] (const ScoreTuple& score) {
//					return (std::get<1>(score) < 0.9 * jointPrior->getKktBoundary(std::get<0>(score)));
//				};

				// Check KKT conditions

				computeKktConditions(inactiveSet);

				bool satisfied = std::all_of(begin(inactiveSet), end(inactiveSet), checkConditions);

				if (satisfied) {
					done = true;
				} else {
//					auto newActiveSize = initialActiveSize + perPassSize;

					auto count1 = std::distance(begin(inactiveSet), end(inactiveSet));
					auto count2 = std::count_if(begin(inactiveSet), end(inactiveSet), checkConditions);


					// Remove elements from activeSet if less than 90% of boundary
// 					computeKktConditions(activeSet); // TODO Already computed in findMode
//
// // 					for (auto& active : activeSet) {
// // 						std::ostringstream stream;
// // 						stream << "Active: " << std::get<0>(active) << " : " << std::get<1>(active) << " : " << std::get<2>(active);
// // 						logger->writeLine(stream);
// // 					}
//
// 					int countActiveViolations = 0;
// 					while(activeSet.size() > 0 &&
// 						checkAlmostConditions(activeSet.back())
// 					) {
// 						auto& back = activeSet.back();
//
// // 						std::ostringstream stream;
// // 						stream << "Remove: " << std::get<0>(back) << ":" << std::get<1>(back)
// // 						       << " cut @ " << jointPrior->getKktBoundary(std::get<0>(back))
// // 						       << " diff = " << (std::get<1>(back) - jointPrior->getKktBoundary(std::get<0>(back)));
// // 						logger->writeLine(stream);
//
// 						inactiveSet.push_back(back);
// 						activeSet.pop_back();
// 						++countActiveViolations;
// 					}
					// end

					// Move inactive elements into active if KKT conditions are not met
					while (inactiveSet.size() > 0
							&& !checkConditions(inactiveSet.front())
//  							&& activeSet.size() < newActiveSize
					) {
						auto& front = inactiveSet.front();

// 						std::ostringstream stream;
// 						stream << std::get<0>(front) << ":" << std::get<1>(front);
// 						logger->writeLine(stream);

						activeSet.push_back(front);
						inactiveSet.pop_front();
					}

					if (noiseLevel >= QUIET) {
						std::ostringstream stream;
// 						stream << "  Active set violations: " << countActiveViolations << std::endl;
						stream << "Inactive set violations: " << (count1 - count2);
						logger->writeLine(stream);
					}
				}
			}
		}
		++swindleIterationCount;
		perPassSize *= 2;

		logger->yield();			// This is not re-entrant safe
	}

	// restore fixBeta
	std::fill(fixBeta.begin(), fixBeta.end(), false);
	for (auto index : excludeSet) {
		fixBeta[index] = true;
	}
}
示例#8
0
void Adaboost(TrainData *data,int T){
    InitWi(data);
    double temptheta=0.0,theta1=0.0;
    double error,beta;
    int p=0;                            //p=0 <=> '<' p=0 <=> '>'
    double min;
//////////////left is positive & right is nagitive//////////////
    for(int i=0;i<T;i++){
        //get theta first
        p=0;
        min=DBL_MAX;//////Be careful
        nomalization(data);
        for(int j=0;j<Data_Size_G;j++){
            InitStatus(data);
            temptheta=data[j].property;
            for(int k=0;k<Data_Size_G;k++){
                if((data[k].property<=temptheta)&&(data[k].label==0))
                    data[k].status=MISS;
                if((data[k].property>temptheta)&&(data[k].label))
                    data[k].status=MISS;
            }
            error=getError(data);
            if(error<=min&&error<0.5){
                theta1=temptheta;
                min=error;
            
            }
        }
        
//////////////right is positive & left is nagitive//////////////
        temptheta=0.0;
        double theta2=0.0;
        for(int j=0;j<Data_Size_G;j++){
            InitStatus(data);
            temptheta=data[j].property;
            for(int k=0;k<Data_Size_G;k++){
                if((data[k].property>=temptheta)&&(data[k].label==0))
                    data[k].status=MISS;
                if((data[k].property<temptheta)&&(data[k].label))
                    data[k].status=MISS;
            }
            error=getError(data);
            if(error<=min){
                theta2=temptheta;
                min=error;
                p=1;
                
            }
        }
//////////////////////////////////////////////////////////////////////////
        InitStatus(data);
        double theta=p?theta2:theta1;
        if(p)
            for(int k=0;k<Data_Size_G;k++){
                if((data[k].property>=theta)&&(data[k].label==0))
                    data[k].status=MISS;
                if((data[k].property<theta)&&(data[k].label))
                    data[k].status=MISS;
            }
        else
            for(int k=0;k<Data_Size_G;k++){
                if((data[k].property<=theta)&&(data[k].label==0))
                    data[k].status=MISS;
                if((data[k].property>theta)&&(data[k].label))
                    data[k].status=MISS;
            }
        
        
        
        error=getError(data);
        beta=getBeta(error);
        updataWi(data, beta);
        
        if(p)
            printf("|>=|   |Threshold:%9lf|error:%9lf |Alpha:%9lf|\n",theta,error,getAlpha(beta));
        else
            printf("|<=|   |Threshold:%9lf|error:%9lf |Alpha:%9lf|\n",theta,error,getAlpha(beta));
        
      
    }

}
示例#9
0
void KaiserWindowDesigner::getWindow(RealArray &w)
{
	w.resize(size);
	Real beta = getBeta();
	calculateWindow(w, beta);
}
示例#10
0
/* Selects changepoint position and adds new changepoint before or after */
void sampleBirth(const gsl_rng *r,
		 parameters* p, int actP, int nPar,
		 intparameters *ip, int actIP, int nIPar) {
  /* select parameter */
  int kIndex=gsl_rng_uniform_int(r, actIP+1);
  int kDown=(kIndex<=0)?0:getIntParameter(ip,kIndex-1);
  int kUp=(kIndex==actIP)?getNdata()-1:getIntParameter(ip,kIndex)-1;

  /*sample new change point*/
  int kNew;
  int i;
  static int hello=1;
  const int *CDF=getData(), nCDF=getNdata();
  int succ0, succ1, fail0, fail1;
  double p0, p1;
  double alpha=getAlpha(), beta=getBeta();

  /* Adding the first changepoint */
  if(actIP==0) {
    /* k must be between 1 and nCDF-1*/
    int kNew=gsl_rng_uniform_int(r,nCDF-1)+1;

    /* Successes and failures before and after new changepoint kNew
       are used for sampling p0 and p1... */
    succ0=succInterval(CDF, 0, kNew), fail0=failInterval(CDF, 0, kNew);
    succ1=succInterval(CDF, kNew, nCDF), fail1=failInterval(CDF, kNew, nCDF);

    /* ... from beta distributions. */
    p0=gsl_ran_beta(r, succ0+1+alpha, fail0+beta+1);
    p1=gsl_ran_beta(r, succ1+1+alpha, fail1+beta+1);

    /* Set new proposal */
    setIntProposal(ip, 0, kNew);
    setProposal(p,0,p0);
    setProposal(p,1,p1);

    /* Balance with corresponding death move */
    proposalScale=1/((double)(nCDF-1));
    return;
  }

  /* Find kUp and kDown that are more than one data point apart. */
  while(kUp-kDown<=1) {
    kIndex=gsl_rng_uniform_int(r, actIP+1);
    kDown=(kIndex<=0)?0:getIntParameter(ip,kIndex-1);
    kUp=(kIndex==actIP)?getNdata()-1:getIntParameter(ip,kIndex)-1;
  }

  /* Balance with corresponding death move */
  proposalScale=1/((double)(kUp-kDown));

  do {
    kNew=kDown+gsl_rng_uniform_int(r, kUp-kDown);
  }while (kNew==0);

  if(hello) fprintf(OUT, "sampleBirth()\n"), hello=0;

  /*Copy kIndex parameters unchanged */
  copyIntOrg2Prop(ip, kIndex);

  /* Insert new changepoint */
  setIntProposal(ip, kIndex, kNew);

  /* Shift changepoints after inserted changepoint. */
  for(i=kIndex+1; i<nIPar; i++) {
    setIntProposal(ip, i, getIntParameter(ip,i-1));
  }

  /* Now copy kIndex open probabilities. */
  if(kIndex-1>0) copyOrg2Prop(p, kIndex);

  /* Sample open probabilities for the segments 'left' and 'right' of
     the new changepoint. */
  succ0=succInterval(CDF, kDown, kNew), fail0=failInterval(CDF, kDown, kNew);
  succ1=succInterval(CDF, kNew, kUp), fail1=failInterval(CDF, kNew, kUp);
  p0=gsl_ran_beta(r, succ0+alpha+1, fail0+beta+1);
  p1=gsl_ran_beta(r, succ1+alpha+1, fail1+beta+1);

  setProposal(p, kIndex, p0);
  setProposal(p, kIndex+1, p1);

  /* Shift the remaining open probabilities. */
  for(i=kIndex+2; i<nPar; i++) {
    double pI=getParameter(p,i-1);
    setProposal(p, i, pI);
  }
}