int Indicator_kriging::median_ik( Progress_notifier* progress_notifier ) {
  bool ok = true;
  
  // create all the properties we will populate
  std::vector< Grid_continuous_property* > simul_properties;
  for( int thres = 0; thres < thres_count_; thres++ ) {
    Grid_continuous_property* prop = multireal_property_->new_realization();
    prop->set_parameters(parameters_);
    simul_properties.push_back( prop );
  }

  std::vector<double> krig_weights;
  SK_constraints Kconstraints;
  typedef std::vector<double>::const_iterator weight_iterator;
  typedef SK_combiner< weight_iterator, Neighborhood > SKCombiner;

  // the following line could probably be omitted
  simul_grid_->select_property( simul_properties[0]->name() );

  Geostat_grid::iterator begin = simul_grid_->begin();
  Geostat_grid::iterator end = simul_grid_->end();
  
  
  for( ; begin != end; ++begin ) {
    if( !progress_notifier->notify() ) return 1;

    if( begin->is_informed() ) continue;
      
    neighborhood_->find_neighbors( *begin );
//    if( neighborhood_->is_empty() ){
    if( neighborhood_->size() < min_neigh_ ){
      //if we don't have any conditioning data, skip the node
      continue;
    }
    else {
      int status = kriging_weights( krig_weights,
				    *begin,
				    *(neighborhood_.raw_ptr()),
				    covar_, Kconstraints );
      
      if(status == 0) {
	      // the kriging system could be solved
	      // Since we're using the same covariance and the 
	      // same neighborhood for all thresholds, we can re-use the same
	      // weights for all thresholds 

       	GsTLInt node_id = begin->node_id();
      	Non_parametric_cdf<float>::p_iterator p_it = ccdf_->p_begin();

      	for( int thres = 0; thres < thres_count_; thres++, ++p_it ) {

      	  // tell the neighbors to work on the correct property
	        for( Neighborhood::iterator it = neighborhood_->begin();
	             it != neighborhood_->end(); it++ ) {
      	    it->set_property_array( hdata_properties_[ thres ] );
	        }

    	    SKCombiner combiner( marginal_probs_[thres] );
	        double estimate = combiner( krig_weights.begin(), 
				                              krig_weights.end(),
				                              *(neighborhood_.raw_ptr()) );
	        *p_it = estimate;
	      }

      	// make sure the ccdf is a valid cdf
      	ccdf_->make_valid();

      	// output the ccdf probabilities to the grid properties
      	p_it = ccdf_->p_begin();
      	for( int thres2 = 0; thres2 < thres_count_; thres2++, ++p_it ) {
      	  simul_properties[ thres2 ]->set_value( *p_it, node_id );
      	}
      }
      else {
      	// the kriging system could not be solved, issue a warning and skip the
      	// node
        ok = false;
      }
    }
  }

  if( !ok )
    GsTLcerr << "The kriging system could not be solved for every node\n" << gstlIO::end; 


  return 0;
}
int Indicator_kriging::full_ik( Progress_notifier* progress_notifier ) {
  bool ok = true;
  bool order_relation_problems = false;

  // create all the properties we will populate
  std::vector< Grid_continuous_property* > simul_properties;
  for( int thres = 0; thres < thres_count_; thres++ ) {
    Grid_continuous_property* prop = multireal_property_->new_realization();
    prop->set_parameters(parameters_);
    simul_properties.push_back( prop );
  }

  std::vector<double> krig_weights;
  SK_constraints Kconstraints;
  typedef std::vector<double>::const_iterator weight_iterator;
  typedef SK_combiner< weight_iterator, Neighborhood > SKCombiner;

  // the following line could probably be omitted
  simul_grid_->select_property( simul_properties[0]->name() );

  Geostat_grid::iterator begin = simul_grid_->begin();
  Geostat_grid::iterator end = simul_grid_->end();

  // loop over the grid

  for( ; begin != end; ++begin ) {
    if( !progress_notifier->notify() ) return 1;

    if( begin->is_informed() ) continue;
    

    // for each threshold / class:
    Non_parametric_cdf<float>::p_iterator p_it = ccdf_->p_begin();
    for( int thres = 0; thres < thres_count_; thres++, ++p_it ) {
      neighborhoods_vector_[thres]->find_neighbors( *begin );
 
      if( neighborhoods_vector_[thres]->is_empty() ){
        //if we don't have any conditioning data, use the marginal
        *p_it = marginal_probs_[thres];
        continue;
      }
      
      int status = kriging_weights( krig_weights,
	                          			  *begin,
				                            *(neighborhoods_vector_[thres].raw_ptr()),
				                            covar_vector_[thres], Kconstraints );
      
      if( status != 0 ) {
        // the kriging system could not be solved, issue a warning and skip the
        // node
        ok = false;
        *p_it = marginal_probs_[thres];
        continue;
      }
      
      SKCombiner combiner( marginal_probs_[thres] );
      double estimate = combiner( krig_weights.begin(), 
		                          		krig_weights.end(),
			  	                        *(neighborhoods_vector_[thres].raw_ptr()) );
      *p_it = estimate;
    }

    // make sure the ccdf is a valid cdf
    if( !ccdf_->make_valid() ) {
      // there was a problem making the cdf a valid cdf:
      // leave the node un-estimated and set the flag so that an error will
      // be reported
      order_relation_problems = true; 
      continue;
    }

    GsTLInt node_id = begin->node_id();

    // output the ccdf probabilities to the grid properties
    p_it = ccdf_->p_begin();
    for( int thres2 = 0; thres2 < thres_count_; thres2++, ++p_it ) {
  	  simul_properties[ thres2 ]->set_value( *p_it, node_id );
    }
  }


  if( !ok )
    GsTLcerr << "The kriging system could not be solved for every node\n" 
             << gstlIO::end; 
  if( order_relation_problems ) {
    GsTLcerr << "A cdf could not be estimated for all nodes because of major "
             << "order-relation problems (all probabilities < 0 )" 
             << gstlIO::end;
  }

  return 0;
}
Ejemplo n.º 3
0
int main() {

  Point center(41,29);

  // The numbering of the nodes corresponds to gslib's output order
  Node P5(38,28,0.5740);
  Node P8(45,29,1.2110);
  Node P4(41,26,2.1270);
  Node P3(39,31,8.3400);
  Node P6(38,31,18.6420);
  Node P2(39,30,7.9380);
  Node P7(39,32,2.2840);
  Node P1(40,31,2.5090);
  Node P9(37,20,0.5740);
  Node P10(25,28,1.2110);
  Node P11(39,26,2.1270);
  Node P12(32,31,8.3400);
  Node P13(30,34,18.6420);
  Node P14(33,35,7.9380);
  Node P15(42,32,2.2840);
  Node P16(31,23,2.5090);

  neighborhood voisin;
  voisin.add_node(P1);
  voisin.add_node(P2);
  voisin.add_node(P3);
  voisin.add_node(P4);
  voisin.add_node(P5);
  voisin.add_node(P6);
  voisin.add_node(P7);
  voisin.add_node(P8);
  voisin.add_node(P9);
  voisin.add_node(P10);
  voisin.add_node(P11);
  voisin.add_node(P12);
  voisin.add_node(P13);
  voisin.add_node(P14);
  voisin.add_node(P15);
  voisin.add_node(P16);
  


  

  typedef matrix_lib_traits<TNT_lib<double> >::Vector TNTvector;

  covariance covar;
  
  //______________________________
  // Simple Kriging
  //______________________________

  std::cout << std::endl <<std::endl;
  std::cout << "____________________________________________" 
	    << std::endl
	    << "Simple kriging"
	    << std::endl << std::endl;

  TNTvector SK_weights;
  SK_constraints SK;
  OK_constraints OK;
  double sk_variance;
  

  TNT::stopwatch chrono;
  chrono.start();
  for(int count = 0 ; count < 50000 ; count ++) {
    int change=1;
    if(drand48() <0.5) change = -1;
    (voisin[0].location())[0] += change;
    kriging_weights<GSTL_TNT_lib>(SK_weights,
				  center, voisin,
				  covar, OK );
  }

  chrono.stop();
  std::cout << "time elapsed using Gauss: " << chrono.read() << std::endl;
  
  chrono.reset();
  chrono.start();
  for(int count = 0 ; count < 50000 ; count ++) {
    int change=1;
    if(drand48() <0.5) change = -1;
    voisin[0].property_value() = 0.5*count;
    kriging_weights(SK_weights, 
		    center, voisin,
		    covar, OK );
  }

  chrono.stop();
  std::cout << "time elapsed using LU: " << chrono.read() << std::endl;
  
  
  /*
  //______________________________
  // Ordinary Kriging
  //______________________________

  std::cout << std::endl <<std::endl;
  std::cout << "____________________________________________" 
	    << std::endl
	    << "Ordinary kriging"
	    << std::endl << std::endl;

  TNTvector OK_weights;
  OK_constraints OK;
  double ok_variance;
  status = kriging_weights(OK_weights, ok_variance,
			   center, voisin,
			   covar, OK);
						   
  std::cout << "Here are the weights:" << std::endl
	    << OK_weights << std::endl;

  
  

  //______________________________
  // Kriging with Trend
  //______________________________

  std::cout << std::endl <<std::endl;
  std::cout << "____________________________________________" 
	    << std::endl
	    << "Kriging with Trend"
	    << std::endl << std::endl;
  
  TNTvector KT_weights;
  KT_constraints<functIter> KT(functArray.begin(),functArray.end());
  double kt_variance;
  status = kriging_weights(KT_weights, kt_variance,
			   center, voisin,
			   covar, KT);

						  
  std::cout << "Here are the weights:" << std::endl
	    << KT_weights << std::endl;

  */

  return 0;


}