int main(){ Gaussian_cdf normal_cdf(0,1); typedef Non_param_cdf<> non_parametric_cdf; std::vector<double> uniform_values; non_parametric_cdf from; for(int i=1; i<=1000; i++) uniform_values.push_back( uniform_random() ); std::vector<double> tmp(uniform_values); // add two extreme values tmp.push_back(-4.0); tmp.push_back(4.0); build_cdf(tmp.begin(), tmp.end(), from, 100); // don't transform the extremes of the data set tmp cdf_transform(tmp.begin()+1, tmp.end()-1, from, normal_cdf); // second method std::vector<double> tmp2(uniform_values); uniform_cdf reference_cdf; cdf_transform(tmp2.begin(), tmp2.end(), reference_cdf, normal_cdf); std::cout << "transform\n3\nunif\nnormal1\nnormal2" << std::endl; for(int i=1; i<=1000; i++) std::cout << uniform_values[i-1] << " " << tmp[i-1] << " " << tmp2[i-1] << std::endl; }
int LU_sim::execute( GsTL_project* ) { // Initialize the global random number generator Global_random_number_generator::instance()->seed( seed_ ); // Set up a progress notifier int total_steps = simul_grid_->size() * (nb_of_realizations_); int frequency = std::max( total_steps / 20, 1 ); SmartPtr<Progress_notifier> progress_notifier = utils::create_notifier( "Running LU_sim", total_steps, frequency ); // In LU simulation, the marginal is a Gaussian cdf, // with mean 0 and variance 1. Gaussian_cdf marginal( 0.0, 1.0 ); //typedef Geostat_grid::random_path_iterator iterator; typedef Gval_iterator< TabularMapIndex > iterator; std::vector< int > id_unknown; std::vector< int > id_data; //harddata_grid_->select_property(harddata_property_->name()); if( harddata_grid_ ) { for( int i=0; i< harddata_property_->size(); i++ ) { if(!harddata_property_->is_informed(i) ) continue; id_data.push_back( i ); } } for( int i=0; i< simul_grid_->size(); i++ ) { if( simul_grid_ == harddata_grid_ && harddata_property_->is_informed(i) ) continue; id_unknown.push_back( i ); } iterator begin_d( harddata_grid_, harddata_property_ ,0, id_data.size(), TabularMapIndex(&id_data) ); iterator end_d( harddata_grid_, harddata_property_ ,id_data.size(), id_data.size(), TabularMapIndex(&id_data) ); Grid_continuous_property* prop = multireal_property_->new_realization(); iterator begin_u( simul_grid_, prop ,0, id_unknown.size(), TabularMapIndex(&id_unknown) ); iterator end_u( simul_grid_, prop ,id_unknown.size(), id_unknown.size(), TabularMapIndex(&id_unknown) ); Random_number_generator gen; LU_simulator< Covariance<Location>, Random_number_generator, Geostat_grid> lu_sim( covar_,gen, simul_grid_ ); lu_sim.initialize_matrix(begin_u, end_u, begin_d, end_d); // loop on all realizations for( int nreal = 0; nreal < nb_of_realizations_ ; nreal ++ ) { // update the progress notifier progress_notifier->message() << "working on realization " << nreal+1 << gstlIO::end; if( !progress_notifier->notify() ) return 1; // Create a new property to hold the realization and tell the simulation // grid to use it as the current property appli_message( "Creating new realization" ); //Grid_continuous_property* prop = multireal_property_->new_realization(); if(nreal>0) prop = multireal_property_->new_realization(); prop->set_parameters(parameters_); simul_grid_->select_property( prop->name() ); // initialize the new realization with the hard data, if that was requested if( property_copier_ ) { property_copier_->copy( harddata_grid_, harddata_property_, simul_grid_, prop ); } iterator begin_unk( simul_grid_, prop ,0, id_unknown.size(), TabularMapIndex(&id_unknown) ); iterator end_unk( simul_grid_, prop ,id_unknown.size(), id_unknown.size(), TabularMapIndex(&id_unknown) ); int status = lu_sim(begin_unk,end_unk,marginal); // initialize the new realization with the hard data, if that was requested // if( property_copier_ ) { // property_copier_->copy( harddata_grid_, harddata_property_, // simul_grid_, prop ); //initializer_->assign( prop, harddata_grid_, harddata_property_->name() ); // } if( status == -1 ) { clean( prop ); return 1; } // back-transform if needed if( use_target_hist_ ) { cdf_transform( prop->begin(), prop->end(), marginal, *target_cdf_.raw_ptr() ); } } clean(); return 0; }
int main() { std::cout << "____________________________________" << std::endl << " Testing transformations from cdf to pdf and inversely" <<std::endl; int zval[5] = {1,2,3,4,5}; double prob[5] = {0.2, 0.3, 0.6, 0.8, 1}; typedef Non_param_pdf<int> Pdf; typedef Categ_non_param_cdf<int> Cdf; Cdf categ_cdf( 5, prob); Pdf pdf(zval, zval+5); cdf_to_pdf(pdf, categ_cdf); for(int i=0; i<=4; i++) std::cout << pdf.prob(i) << " " ; std::cout << std::endl; Cdf back(5); pdf_to_cdf(back,pdf); for(int i=0; i<=4; i++) std::cout << back.prob(i) << " " ; std::cout << std::endl; cdf_to_pdf(pdf, back); for(int i=0; i<=4; i++) std::cout << pdf.prob(i) << " " ; std::cout << std::endl; Pdf new_pdf(categ_cdf); for(int i=0; i<=4; i++) std::cout << new_pdf.prob(i) << " " ; std::cout << std::endl; std::cout << std::endl << "____________________________________" << std::endl << " Testing transformations from cdf to pdf and inversely "<<std::endl; Gaussian_cdf normal(0,1); std::vector<double> range(1000); for(std::vector<double>::iterator it=range.begin(); it!=range.end(); it++) *it = drand48()*100; std::ofstream out1( "uniform.dbg" ); std::copy( range.begin(), range.end(), std::ostream_iterator<double>( out1, "\n" ) ); cdf_transform(range.begin(), range.end(), normal); std::ofstream out2( "nscore.dbg" ); std::copy( range.begin(), range.end(), std::ostream_iterator<double>( out2, "\n" ) ); std::cout << std::endl << "____________________________________" << std::endl << " Testing cdf correction "<<std::endl; double order_relation[5] = {0.2, 0.3, -0.2, 0.1, 1.2}; Cdf bad_cdf(5, order_relation); std::cout << "Is bad cdf ok? " << is_valid_cdf(bad_cdf) << std::endl; make_cdf_valid(bad_cdf); std::cout << "Corrected cdf: " << std::endl; for(int i=0; i<bad_cdf.size(); i++) std::cout << bad_cdf.prob(i) << " " ; std::cout << std::endl; std::cout << std::endl << "____________________________________" << std::endl << " Testing cdf construction "<<std::endl; double val_range[9] = {1,1,1,4,7,12,31,55,60}; double z_values[7] = {1,4,7,12,31,55,60}; Non_param_cdf<> new_cdf( z_values, z_values+7); build_cdf( val_range, val_range+9, new_cdf.z_begin(), new_cdf.z_end(), new_cdf.p_begin() ); Non_param_cdf<>::z_iterator z_it = new_cdf.z_begin(); for( ; z_it != new_cdf.z_end(); z_it++ ) { std::cout << "P( " << *z_it << " ) = " << new_cdf.prob( *z_it ) << std::endl; } std::cout << std::endl; build_cdf( val_range, val_range+9, new_cdf, true ); z_it = new_cdf.z_begin(); for( ; z_it != new_cdf.z_end(); z_it++ ) { std::cout << "P( " << *z_it << " ) = " << new_cdf.prob( *z_it ) << std::endl; } }