示例#1
0
double L2R_Huber_SVC::get_func(const Eigen::VectorXd &w) {
  z = y * (X * w).array();
  int indexD2 = 0;
  double zi, tmp;
  double f = 0;
  indexs_D1.clear();
  indexs_D2.clear();
  XD2w_yD2 = Eigen::VectorXd::Zero(l);

  for (int i = 0; i < l; ++i) {
    zi = z.coeffRef(i);
    tmp = 1 - zi;
    if (zi <= 0) {
      indexs_D1.push_back(i);
      f += (tmp - 0.5);
    } else if (zi < 1) {
      indexs_D2.push_back(i);
      f += tmp * tmp * 0.5;
      XD2w_yD2.coeffRef(indexD2) = -y.coeffRef(i) * tmp;
      ++indexD2;
    }
  }
  f *= C;
  f += w.squaredNorm() / 2.0;
  return f;
}
示例#2
0
double l2r_l1hinge_spdc::get_primal_func(const Eigen::VectorXd &w) const {
  Eigen::ArrayXd wx = y_ * (x_ * w).array();
  double tmp = 0.0;
  for (int i = 0; i < num_ins_; ++i)
    tmp += std::max(0.0, 1.0 - wx[i]);
  return 0.5 * w.squaredNorm() + C * tmp;
}
double SmoothDualDecompositionFistaDescent::gradientStep(double rho,
            const Eigen::VectorXd& lambda,
            double& obj_lambda,
            Eigen::VectorXd& gradient,
            double& gradient_norm_squared,
            Eigen::VectorXd& y, double& omega)
{
    double obj_new;
    double obj_approx;
    
    // evaluate objective, gradient and gradient norm
    obj_lambda = computeObjectiveAndGradient(rho, gradient, lambda);
    gradient_norm_squared = gradient.squaredNorm();

    // backtracking
    do {
        y = lambda + gradient*(1.0/omega);
        obj_new = computeObjective(rho, y);
        obj_approx = obj_lambda+1/(2.0*omega)*gradient_norm_squared;
        if (obj_new < obj_approx) {
            omega *= _lipschitz_inc_u;
        }
    } while(obj_new < obj_approx);
    omega = std::max(_lipschitz_constant_optimistic, omega/_lipschitz_inc_d);

    return obj_new;
}
示例#4
0
bool AdaptiveTimeStepper::determineNewTimeStep(
    const Eigen::VectorXd & errorEstimate,
    const Eigen::VectorXd & solution,
    const double computedTimeStep,
    double & newTimeStep
    )
{
    assert( endTime > 0 );

    scalarList squaredNorm( Pstream::nProcs(), scalar( 0 ) );
    squaredNorm[Pstream::myProcNo()] = errorEstimate.squaredNorm();
    reduce( squaredNorm, sumOp<scalarList>() );
    double error = std::sqrt( sum( squaredNorm ) );

    squaredNorm = 0;
    squaredNorm[Pstream::myProcNo()] = solution.squaredNorm();
    reduce( squaredNorm, sumOp<scalarList>() );
    error /= std::sqrt( sum( squaredNorm ) );

    return determineNewTimeStep( error, computedTimeStep, newTimeStep );
}
示例#5
0
double cIKSolver::CalcObjVal(const Eigen::MatrixXd &joint_desc, const Eigen::VectorXd& pose, const Eigen::MatrixXd& cons_desc)
{
	// objective function is the 2-norm of the constraint violations
	double obj_val = 0;
	Eigen::VectorXd err;
	for (int c = 0; c < cons_desc.rows(); ++c)
	{
		const tConsDesc& curr_cons = cons_desc.row(c);
		double weight = curr_cons(eConsDesc::eConsDescWeight);
		err = BuildErr(joint_desc, pose, curr_cons);
		obj_val += weight * err.squaredNorm();
	}
	return obj_val;
}
示例#6
0
 SimuEuro(const option & o, long path, const std::vector<double> & RN){
     opt=o;
     N= path;
     asset_price.resize(N);
     asset_price.setZero();
     option_value= asset_price;
     
     for (long i=0; i< N; i++) {
         asset_price(i)=opt.S* exp((opt.r- opt.q)*opt.T-.5*opt.sigma*opt.sigma*opt.T+ opt.sigma* sqrt(opt.T)* RN[i]);
         
         if(opt.Call) option_value(i)= fmax(asset_price(i)- opt.K,0.0);
         else option_value(i)= fmax(-asset_price(i)+opt.K, 0.0);
     }
     
     mean= option_value.sum()/ option_value.size() * exp(-opt.T*opt.r);
     stdiv= option_value.squaredNorm()/ option_value.size()* exp(-opt.r*opt.T *2);
     stdiv= stdiv- pow(mean,2.0);
     stdiv= sqrt(stdiv/ N);
 };
示例#7
0
    SimuEuro(const option &o, long path, unsigned int seed=  int(time(0))) {
        opt=o;
        N= path;
        asset_price.resize(N);
        asset_price.setZero();
        option_value= asset_price;
        
        boost::mt19937 eng(seed);
        boost::normal_distribution<double> normal(0.0, 1.0);
        boost::variate_generator<boost::mt19937&, boost::normal_distribution<double>> rng(eng, normal);
        
        for (long i=0; i< N; i++) {
            asset_price(i)=opt.S* exp((opt.r- opt.q)*opt.T-.5*opt.sigma*opt.sigma*opt.T+ opt.sigma* sqrt(opt.T)* rng());
            
            if(opt.Call) option_value(i)= fmax(asset_price(i)- opt.K,0.0);
            else option_value(i)= fmax(-asset_price(i)+opt.K, 0.0);
        }
        
        mean= option_value.sum()/ option_value.size() * exp(-opt.T*opt.r);
        stdiv= option_value.squaredNorm()/ option_value.size()* exp(-opt.r*opt.T *2);
        stdiv= stdiv- pow(mean,2.0);
        stdiv= sqrt(stdiv/ N);

    };