void test_deselect_matching( ) { state_map_type * map = state_map_alloc( ); bool_vector_type * mask1 = bool_vector_alloc(0 , false); bool_vector_type * mask2 = bool_vector_alloc(1000 , true); state_map_iset( map , 10 , STATE_INITIALIZED ); state_map_iset( map , 10 , STATE_HAS_DATA ); state_map_iset( map , 20 , STATE_INITIALIZED ); state_map_deselect_matching( map , mask1 , STATE_HAS_DATA | STATE_INITIALIZED ); state_map_deselect_matching( map , mask2 , STATE_HAS_DATA | STATE_INITIALIZED ); test_assert_int_equal( state_map_get_size( map ) , bool_vector_size( mask1 )); for (int i=0; i < bool_vector_size( mask1 ); i++) { if (i==10) test_assert_false( bool_vector_iget( mask1 , i )); else if (i== 20) test_assert_false( bool_vector_iget( mask2 , i )); else { test_assert_false( bool_vector_iget( mask1 , i )); test_assert_true( bool_vector_iget( mask2 , i )); } } bool_vector_free( mask1 ); bool_vector_free( mask2 ); state_map_free( map ); }
void enkf_plot_gen_kw_load( enkf_plot_gen_kw_type * plot_gen_kw, enkf_fs_type * fs, bool transform_data , int report_step, const bool_vector_type * input_mask ) { state_map_type * state_map = enkf_fs_get_state_map( fs ); int ens_size = state_map_get_size( state_map ); bool_vector_type * mask; if (input_mask) mask = bool_vector_alloc_copy( input_mask ); else mask = bool_vector_alloc( ens_size , true ); enkf_plot_gen_kw_resize( plot_gen_kw , ens_size ); { int iens; for (iens = 0; iens < ens_size; ++iens) { if (bool_vector_iget( mask , iens)) { enkf_plot_gen_kw_vector_type * vector = enkf_plot_gen_kw_iget( plot_gen_kw , iens ); enkf_plot_gen_kw_vector_load( vector , fs , transform_data , report_step ); } } } }
void misfit_ensemble_initialize( misfit_ensemble_type * misfit_ensemble , const ensemble_config_type * ensemble_config , const enkf_obs_type * enkf_obs , enkf_fs_type * fs , int ens_size , int history_length, bool force_init) { if (force_init || !misfit_ensemble->initialized) { misfit_ensemble_clear( misfit_ensemble ); msg_type * msg = msg_alloc("Evaluating misfit for observation: " , false); double ** chi2_work = __2d_malloc( history_length + 1 , ens_size ); bool_vector_type * iens_valid = bool_vector_alloc( ens_size , true ); hash_iter_type * obs_iter = enkf_obs_alloc_iter( enkf_obs ); const char * obs_key = hash_iter_get_next_key( obs_iter ); misfit_ensemble->history_length = history_length; misfit_ensemble_set_ens_size( misfit_ensemble , ens_size ); msg_show( msg ); while (obs_key != NULL) { obs_vector_type * obs_vector = enkf_obs_get_vector( enkf_obs , obs_key ); msg_update( msg , obs_key ); bool_vector_reset( iens_valid ); bool_vector_iset( iens_valid , ens_size - 1 , true ); obs_vector_ensemble_chi2( obs_vector , fs , iens_valid , 0 , misfit_ensemble->history_length, 0 , ens_size , chi2_work); /** Internalizing the results from the chi2_work table into the misfit structure. */ for (int iens = 0; iens < ens_size; iens++) { misfit_member_type * node = misfit_ensemble_iget_member( misfit_ensemble , iens ); if (bool_vector_iget( iens_valid , iens)) misfit_member_update( node , obs_key , misfit_ensemble->history_length , iens , (const double **) chi2_work); } obs_key = hash_iter_get_next_key( obs_iter ); } bool_vector_free( iens_valid ); msg_free(msg , true ); hash_iter_free( obs_iter ); __2d_free( chi2_work , misfit_ensemble->history_length + 1); misfit_ensemble->initialized = true; } }
int_vector_type * bool_vector_alloc_active_list( const bool_vector_type * mask ) { int_vector_type * active_list = int_vector_alloc(0,0); int i; for (i =0; i < bool_vector_size( mask ); i++) if (bool_vector_iget( mask , i )) int_vector_append( active_list , i ); return active_list; }
void test_index_list() { int_vector_type * index_list = int_vector_alloc( 0 , 0 ); int_vector_append( index_list , 10 ); int_vector_append( index_list , 20 ); int_vector_append( index_list , 30 ); { bool_vector_type * mask = int_vector_alloc_mask( index_list ); test_assert_false( bool_vector_get_default( mask )); test_assert_int_equal( 31 , bool_vector_size( mask )); test_assert_true( bool_vector_iget( mask , 10 )); test_assert_true( bool_vector_iget( mask , 20 )); test_assert_true( bool_vector_iget( mask , 30 )); test_assert_int_equal( 3 , bool_vector_count_equal( mask , true )); bool_vector_free( mask ); } int_vector_free( index_list ); }
stringlist_type * ert_run_context_alloc_runpath_list(const bool_vector_type * iactive , path_fmt_type * runpath_fmt , subst_list_type * subst_list , int iter) { stringlist_type * runpath_list = stringlist_alloc_new(); for (int iens = 0; iens < bool_vector_size( iactive ); iens++) { if (bool_vector_iget( iactive , iens )) stringlist_append_owned_ref( runpath_list , ert_run_context_alloc_runpath(iens , runpath_fmt , subst_list , iter)); else stringlist_append_ref( runpath_list , NULL ); } return runpath_list; }
int_vector_type * bool_vector_alloc_active_index_list(const bool_vector_type * mask , int default_value) { int_vector_type * index_list = int_vector_alloc(bool_vector_size( mask) , default_value); int active_index = 0; int i; for (i=0; i < bool_vector_size(mask); i++) { if (bool_vector_iget( mask , i)) { int_vector_iset(index_list , i , active_index); active_index++; } } return index_list; }
void rml_enkf_common_store_state( matrix_type * state , const matrix_type * A , const bool_vector_type * ens_mask ) { matrix_resize( state , matrix_get_rows( A ) , bool_vector_size( ens_mask ) , false); { const int ens_size = bool_vector_size( ens_mask ); int active_index = 0; for (int iens = 0; iens < ens_size; iens++) { if (bool_vector_iget( ens_mask , iens )) matrix_copy_column( state , A , iens , active_index ); else matrix_set_const_column( state , iens , 0); } } }
void rml_enkf_common_recover_state( const matrix_type * state , matrix_type * A , const bool_vector_type * ens_mask ) { const int ens_size = bool_vector_size( ens_mask ); const int active_size = bool_vector_count_equal( ens_mask , true ); const int rows = matrix_get_rows( state ); matrix_resize( A , rows , active_size , false ); { int active_index = 0; for (int iens = 0; iens < ens_size; iens++) { if (bool_vector_iget( ens_mask , iens )) matrix_copy_column( A , state , active_index , iens ); } } }
static bool obs_vector_has_vector_data( const obs_vector_type * obs_vector , const bool_vector_type * active_mask , enkf_fs_type * fs) { int vec_size = bool_vector_size( active_mask ); for (int iens = 0; iens < vec_size; iens++) { const enkf_config_node_type * data_config = obs_vector->config_node; if (bool_vector_iget( active_mask , iens )) { if (!enkf_config_node_has_vector(data_config , fs , iens)) { return false; } } } return true; }
static void ecl_sum_fprintf_header( const ecl_sum_type * ecl_sum , const stringlist_type * key_list , const bool_vector_type * has_var , FILE * stream, const ecl_sum_fmt_type * fmt) { fprintf(stream , "%s", fmt->date_header); { int i; for (i=0; i < stringlist_get_size( key_list ); i++) if (bool_vector_iget( has_var , i )) { fprintf(stream , "%s", fmt->sep); fprintf(stream , fmt->header_fmt , stringlist_iget( key_list , i )); } } fprintf( stream , "%s", fmt->newline); if (fmt->print_dash) { fprintf(stream , "%s", fmt->date_dash); { int i; for (i=0; i < stringlist_get_size( key_list ); i++) if (bool_vector_iget( has_var , i )) fprintf(stream , "%s", fmt->value_dash); } fprintf( stream , "%s", fmt->newline); } }
void data_ranking_display( const data_ranking_type * data_ranking , FILE * stream) { const int ens_size = data_ranking->ens_size; const int * permutations = data_ranking->sort_permutation; { int i; fprintf(stream,"\n\n"); fprintf(stream," # Realization %12s\n" , data_ranking->user_key); fprintf(stream,"----------------------------------\n"); for (i = 0; i < ens_size; i++) { if (bool_vector_iget( data_ranking->valid , permutations[i])) { int iens = permutations[i]; fprintf(stream,"%3d %3d %14.3f\n",i,iens,double_vector_iget(data_ranking->data_ensemble , iens)); } } fprintf(stream,"----------------------------------\n"); } }
void matrix_column_compressed_memcpy(matrix_type * target, const matrix_type * src, const bool_vector_type * mask) { if (bool_vector_count_equal( mask , true ) != matrix_get_columns( target )) util_abort("%s: size mismatch. \n",__func__); if (bool_vector_size( mask ) != matrix_get_columns( src)) util_abort("%s: size mismatch. \n",__func__); { int target_col = 0; int src_col; for (src_col = 0; src_col < bool_vector_size( mask ); src_col++) { if (bool_vector_iget( mask , src_col)) { matrix_copy_column( target , src , target_col , src_col); target_col++; } } } }
ert_run_context_type * ert_run_context_alloc_ENSEMBLE_EXPERIMENT(enkf_fs_type * fs , const bool_vector_type * iactive , path_fmt_type * runpath_fmt , subst_list_type * subst_list , int iter) { ert_run_context_type * context = ert_run_context_alloc( iactive , ENSEMBLE_EXPERIMENT , fs , fs , NULL , iter); { stringlist_type * runpath_list = ert_run_context_alloc_runpath_list( iactive , runpath_fmt , subst_list , iter ); for (int iens = 0; iens < bool_vector_size( iactive ); iens++) { if (bool_vector_iget( iactive , iens )) { run_arg_type * arg = run_arg_alloc_ENSEMBLE_EXPERIMENT( fs , iens , iter , stringlist_iget( runpath_list , iens)); vector_append_owned_ref( context->run_args , arg , run_arg_free__); } } stringlist_free( runpath_list ); } return context; }
static bool obs_vector_has_data_at_report_step( const obs_vector_type * obs_vector , const bool_vector_type * active_mask , enkf_fs_type * fs, int report_step) { void * obs_node = vector_iget( obs_vector->nodes , report_step ); if ( obs_node ) { node_id_type node_id = {.report_step = report_step }; for (int iens = 0; iens < bool_vector_size( active_mask ); iens++) { if (bool_vector_iget( active_mask , iens)) { node_id.iens = iens; if (! enkf_config_node_has_node(obs_vector->config_node , fs , node_id )) return false; } } } /* Will return true unconditionally if we do not have observation data at this report step; or alternatively if the active_mask is all false. */ return true; }
static bool obs_vector_has_vector_data( const obs_vector_type * obs_vector , const bool_vector_type * active_mask , enkf_fs_type * fs) { bool has_data = true; int iens = 0; while (true) { const enkf_config_node_type * data_config = obs_vector->config_node; if (bool_vector_iget( active_mask , iens )) { if (!enkf_config_node_has_vector(data_config , fs , iens , FORECAST)) { has_data = false; break; } } iens++; if (iens >= bool_vector_size( active_mask )) break; } return has_data; }
ert_run_context_type * ert_run_context_alloc_SMOOTHER_RUN(enkf_fs_type * simulate_fs , enkf_fs_type * target_update_fs , const bool_vector_type * iactive , path_fmt_type * runpath_fmt , subst_list_type * subst_list , int iter) { ert_run_context_type * context = ert_run_context_alloc( iactive , SMOOTHER_UPDATE , simulate_fs , simulate_fs , target_update_fs , iter); { stringlist_type * runpath_list = ert_run_context_alloc_runpath_list( iactive , runpath_fmt , subst_list , iter ); for (int iens = 0; iens < bool_vector_size( iactive ); iens++) { if (bool_vector_iget( iactive , iens )) { run_arg_type * arg = run_arg_alloc_SMOOTHER_RUN( simulate_fs , target_update_fs , iens , iter , stringlist_iget( runpath_list , iens)); vector_append_owned_ref( context->run_args , arg , run_arg_free__); } } stringlist_free( runpath_list ); } return context; }
static stringlist_type * ert_run_context_alloc_runpath_list(const bool_vector_type * iactive , path_fmt_type * runpath_fmt , subst_list_type * subst_list , int iter) { stringlist_type * runpath_list = stringlist_alloc_new(); for (int iens = 0; iens < bool_vector_size( iactive ); iens++) { if (bool_vector_iget( iactive , iens )) { char * tmp1 = path_fmt_alloc_path(runpath_fmt , false , iens, iter); /* 1: Replace first %d with iens, if a second %d replace with iter */ char * tmp2 = tmp1; if (subst_list) tmp2 = subst_list_alloc_filtered_string( subst_list , tmp1 ); /* 2: Filter out various magic strings like <CASE> and <CWD>. */ stringlist_append_copy( runpath_list , tmp2 ); if (subst_list) free( tmp2 ); free( tmp1 ); } else stringlist_append_ref( runpath_list , NULL ); } return runpath_list; }
void gen_data_config_update_active(gen_data_config_type * config, int report_step , const bool_vector_type * data_mask) { pthread_mutex_lock( &config->update_lock ); { if ( int_vector_iget( config->data_size_vector , report_step ) > 0) { if (config->active_report_step != report_step) { /* This is the first ensemeble member loading for this particular report_step. */ bool_vector_reset( config->active_mask ); bool_vector_iset( config->active_mask , int_vector_iget( config->data_size_vector , report_step ) - 1 , true ); config->mask_modified = true; } { int i; for (i=0; i < bool_vector_size( data_mask ); i++) { if (!bool_vector_iget( data_mask , i )) { bool_vector_iset( config->active_mask , i , false ); config->mask_modified = true; } } } if (config->mask_modified) { /** The global mask has been modified after the last load; i.e. we update the on-disk representation. */ char * filename = util_alloc_sprintf("%s_active" , config->key ); FILE * stream = enkf_fs_open_case_tstep_file( config->write_fs , filename , report_step , "w"); bool_vector_fwrite( config->active_mask , stream ); fclose( stream ); free( filename ); config->mask_modified = false; } } config->active_report_step = report_step; } pthread_mutex_unlock( &config->update_lock ); }
void test_state() { rng_type * rng = rng_alloc( MZRAN , INIT_DEFAULT ); int ens_size = 10; int active_size = 8; int rows = 100; matrix_type * state = matrix_alloc(1,1); bool_vector_type * ens_mask = bool_vector_alloc(ens_size , false); matrix_type * A = matrix_alloc( rows , active_size); matrix_type * A2 = matrix_alloc( rows, active_size ); matrix_type * A3 = matrix_alloc( 1,1 ); for (int i=0; i < active_size; i++) bool_vector_iset( ens_mask , i + 1 , true ); matrix_random_init(A , rng); rml_enkf_common_store_state( state , A , ens_mask ); test_assert_int_equal( matrix_get_rows( state ) , rows ); test_assert_int_equal( matrix_get_columns( state ) , ens_size ); { int g; int a = 0; for (g=0; g < ens_size; g++) { if (bool_vector_iget( ens_mask , g )) { test_assert_true( matrix_columns_equal( state , g , A , a )); a++; } } } rml_enkf_common_recover_state( state , A2 , ens_mask); rml_enkf_common_recover_state( state , A3 , ens_mask); test_assert_true( matrix_equal( A , A2 )); test_assert_true( matrix_equal( A , A3 )); bool_vector_free( ens_mask ); matrix_free( state ); matrix_free( A ); }
bool string_util_update_active_list( const char * range_string , int_vector_type * active_list ) { int_vector_sort( active_list ); { bool_vector_type * mask = alloc_mask( active_list ); bool valid = false; if (string_util_update_active_mask( range_string , mask )) { int_vector_reset( active_list ); { int i; for (i=0; i < bool_vector_size(mask); i++) { bool active = bool_vector_iget( mask , i ); if (active) int_vector_append( active_list , i ); } } valid = true; } bool_vector_free( mask ); return valid; } }
static void __ecl_sum_fprintf_line( const ecl_sum_type * ecl_sum , FILE * stream , int internal_index , const bool_vector_type * has_var , const int_vector_type * var_index , char * date_string , const ecl_sum_fmt_type * fmt) { fprintf(stream , fmt->days_fmt , ecl_sum_iget_sim_days(ecl_sum , internal_index)); fprintf(stream , "%s", fmt->sep ); { struct tm ts; time_t sim_time = ecl_sum_iget_sim_time(ecl_sum , internal_index ); util_localtime( &sim_time , &ts); strftime( date_string , DATE_STRING_LENGTH - 1 , fmt->date_fmt , &ts); fprintf(stream , "%s", date_string ); } { int ivar; for (ivar = 0; ivar < int_vector_size( var_index ); ivar++) { if (bool_vector_iget( has_var , ivar )) { fprintf(stream , "%s", fmt->sep); fprintf(stream , fmt->value_fmt , ecl_sum_iget(ecl_sum , internal_index, int_vector_iget( var_index , ivar ))); } } } fprintf(stream , "%s", fmt->newline); }
ert_run_context_type * ert_run_context_alloc_ENKF_ASSIMILATION(enkf_fs_type * fs , const bool_vector_type * iactive , path_fmt_type * runpath_fmt , subst_list_type * subst_list , init_mode_type init_mode , state_enum init_state_parameter , state_enum init_state_dynamic , int step1 , int step2 , int iter) { ert_run_context_type * context = ert_run_context_alloc( iactive , SMOOTHER_UPDATE , fs , fs , fs , init_mode , iter); { stringlist_type * runpath_list = ert_run_context_alloc_runpath_list( iactive , runpath_fmt , subst_list , iter ); for (int iens = 0; iens < bool_vector_size( iactive ); iens++) { if (bool_vector_iget( iactive , iens )) { run_arg_type * arg = run_arg_alloc_ENKF_ASSIMILATION( fs , iens , init_state_parameter , init_state_dynamic , step1 , step2 , stringlist_iget( runpath_list , iens)); vector_append_owned_ref( context->run_args , arg , run_arg_free__); } } stringlist_free( runpath_list ); } return context; }
void enkf_tui_run_manual_load__( void * arg ) { enkf_main_type * enkf_main = enkf_main_safe_cast( arg ); const int ens_size = enkf_main_get_ensemble_size( enkf_main ); bool_vector_type * iactive = bool_vector_alloc( 0 , false ); run_mode_type run_mode = ENSEMBLE_EXPERIMENT; int iter = 0; enkf_main_init_run(enkf_main , iactive , run_mode , INIT_NONE); /* This is ugly */ { char * prompt = util_alloc_sprintf("Which realizations to load (Ex: 1,3-5) <Enter for all> [M to return to menu] : [ensemble size:%d] : " , ens_size); char * select_string; util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> "); select_string = util_alloc_stdin_line(); enkf_tui_util_sscanf_active_list( iactive , select_string , ens_size ); util_safe_free( select_string ); free( prompt ); } { const model_config_type * model_config = enkf_main_get_model_config( enkf_main ); if (model_config_runpath_requires_iter( model_config )) { const char * prompt = "Which iteration to load from [0...?) : "; char * input; bool OK; util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> "); input = util_alloc_stdin_line(); if (input == NULL) return; OK = util_sscanf_int( input , &iter ); free( input ); if (!OK) return; } } if (bool_vector_count_equal( iactive , true )) { stringlist_type ** realizations_msg_list = util_calloc( ens_size , sizeof * realizations_msg_list ); int iens = 0; for (; iens < ens_size; ++iens) { realizations_msg_list[iens] = stringlist_alloc_new(); } enkf_main_load_from_forward_model(enkf_main, iter , iactive, realizations_msg_list); { qc_module_type * qc_module = enkf_main_get_qc_module( enkf_main ); runpath_list_type * runpath_list = qc_module_get_runpath_list( qc_module ); for (iens = 0; iens < ens_size; ++iens) { if (bool_vector_iget(iactive , iens)) { const enkf_state_type * state = enkf_main_iget_state( enkf_main , iens ); runpath_list_add( runpath_list , iens , enkf_state_get_run_path( state ) , enkf_state_get_eclbase( state )); } } qc_module_export_runpath_list( qc_module ); } for (iens = 0; iens < ens_size; ++iens) { stringlist_type * msg_list = realizations_msg_list[iens]; if (bool_vector_iget(iactive, iens)) { if (stringlist_get_size( msg_list )) { enkf_tui_display_load_msg( iens , msg_list ); } } stringlist_free(msg_list); } free(realizations_msg_list); } bool_vector_free( iactive ); }
void enkf_tui_run_manual_load__( void * arg ) { enkf_main_type * enkf_main = enkf_main_safe_cast( arg ); enkf_fs_type * fs = enkf_main_get_fs( enkf_main ); const int last_report = -1; const int ens_size = enkf_main_get_ensemble_size( enkf_main ); int step1,step2; bool_vector_type * iactive = bool_vector_alloc( 0 , false ); run_mode_type run_mode = ENSEMBLE_EXPERIMENT; enkf_main_init_run(enkf_main , run_mode); /* This is ugly */ step1 = 0; step2 = last_report; /** Observe that for the summary data it will load all the available data anyway. */ { char * prompt = util_alloc_sprintf("Which realizations to load (Ex: 1,3-5) <Enter for all> [M to return to menu] : [ensemble size:%d] : " , ens_size); char * select_string; util_printf_prompt(prompt , PROMPT_LEN , '=' , "=> "); select_string = util_alloc_stdin_line(); enkf_tui_util_sscanf_active_list( iactive , select_string , ens_size ); util_safe_free( select_string ); free( prompt ); } if (bool_vector_count_equal( iactive , true )) { int iens; arg_pack_type ** arg_list = util_calloc( ens_size , sizeof * arg_list ); thread_pool_type * tp = thread_pool_alloc( 4 , true ); /* num_cpu - HARD coded. */ for (iens = 0; iens < ens_size; iens++) { arg_pack_type * arg_pack = arg_pack_alloc(); arg_list[iens] = arg_pack; if (bool_vector_iget(iactive , iens)) { enkf_state_type * enkf_state = enkf_main_iget_state( enkf_main , iens ); arg_pack_append_ptr( arg_pack , enkf_state); /* 0: */ arg_pack_append_ptr( arg_pack , fs ); /* 1: */ arg_pack_append_int( arg_pack , step1 ); /* 2: This will be the load start parameter for the run_info struct. */ arg_pack_append_int( arg_pack , step1 ); /* 3: Step1 */ arg_pack_append_int( arg_pack , step2 ); /* 4: Step2 For summary data it will load the whole goddamn thing anyway.*/ arg_pack_append_bool( arg_pack , true ); /* 5: Interactive */ arg_pack_append_owned_ptr( arg_pack , stringlist_alloc_new() , stringlist_free__); /* 6: List of interactive mode messages. */ thread_pool_add_job( tp , enkf_state_load_from_forward_model_mt , arg_pack); } } thread_pool_join( tp ); thread_pool_free( tp ); printf("\n"); { qc_module_type * qc_module = enkf_main_get_qc_module( enkf_main ); runpath_list_type * runpath_list = qc_module_get_runpath_list( qc_module ); for (iens = 0; iens < ens_size; iens++) { if (bool_vector_iget(iactive , iens)) { const enkf_state_type * state = enkf_main_iget_state( enkf_main , iens ); runpath_list_add( runpath_list , iens , enkf_state_get_run_path( state ) , enkf_state_get_eclbase( state )); } } qc_module_export_runpath_list( qc_module ); } for (iens = 0; iens < ens_size; iens++) { if (bool_vector_iget(iactive , iens)) { stringlist_type * msg_list = arg_pack_iget_ptr( arg_list[iens] , 6 ); if (stringlist_get_size( msg_list )) enkf_tui_display_load_msg( iens , msg_list ); } } for (iens = 0; iens < ens_size; iens++) arg_pack_free( arg_list[iens]); free( arg_list ); } bool_vector_free( iactive ); }
bool model_config_internalize_state( const model_config_type * config , int report_step) { return bool_vector_iget(config->internalize_state , report_step); }
void stepwise_estimate( stepwise_type * stepwise , double deltaR2_limit , int CV_blocks) { int nvar = matrix_get_columns( stepwise->X0 ); int nsample = matrix_get_rows( stepwise->X0 ); double currentR2 = -1; bool_vector_type * active_rows = bool_vector_alloc( nsample , true ); /*Reset beta*/ for (int i = 0; i < nvar; i++) { matrix_iset(stepwise->beta, i , 0 , 0.0); } bool_vector_set_all( stepwise->active_set , false ); double MSE_min = 10000000; double Prev_MSE_min = MSE_min; double minR2 = -1; while (true) { int best_var = 0; Prev_MSE_min = MSE_min; /* Go through all the inactive variables, and calculate the resulting prediction error IF this particular variable is added; keep track of the variable which gives the lowest prediction error. */ for (int ivar = 0; ivar < nvar; ivar++) { if (!bool_vector_iget( stepwise->active_set , ivar)) { double newR2 = stepwise_test_var(stepwise , ivar , CV_blocks); if ((minR2 < 0) || (newR2 < minR2)) { minR2 = newR2; best_var = ivar; } } } /* If the best relative improvement in prediction error is better than @deltaR2_limit, the corresponding variable is added to the active set, and we return to repeat the loop one more time. Otherwise we just exit. */ { MSE_min = minR2; double deltaR2 = MSE_min / Prev_MSE_min; if (( currentR2 < 0) || deltaR2 < deltaR2_limit) { bool_vector_iset( stepwise->active_set , best_var , true ); currentR2 = minR2; bool_vector_set_all(active_rows, true); stepwise_estimate__( stepwise , active_rows ); } else { /* The gain in prediction error is so small that we just leave the building. */ /* NB! Need one final compuation of beta (since the test_var function does not reset the last tested beta value !) */ bool_vector_set_all(active_rows, true); stepwise_estimate__( stepwise , active_rows ); break; } if (bool_vector_count_equal( stepwise->active_set , true) == matrix_get_columns( stepwise->X0 )) { stepwise_estimate__( stepwise , active_rows ); break; /* All variables are active. */ } } } stepwise_set_R2(stepwise, currentR2); bool_vector_free( active_rows ); }
static double stepwise_estimate__( stepwise_type * stepwise , bool_vector_type * active_rows) { matrix_type * X; matrix_type * E; matrix_type * Y; double y_mean = 0; int nvar = matrix_get_columns( stepwise->X0 ); int nsample = matrix_get_rows( stepwise->X0 ); nsample = bool_vector_count_equal( active_rows , true ); nvar = bool_vector_count_equal( stepwise->active_set , true ); matrix_set( stepwise->beta , 0 ); // It is essential to make sure that old finite values in the beta0 vector do not hang around. /* Extracting the data used for regression, and storing them in the temporary local matrices X and Y. Selecting data is based both on which varibles are active (stepwise->active_set) and which rows should be used for regression, versus which should be used for validation (@active_rows). */ if ((nsample < matrix_get_rows( stepwise->X0 )) || (nvar < matrix_get_columns( stepwise->X0 ))) { X = matrix_alloc( nsample , nvar ); E = matrix_alloc( nsample , nvar ); Y = matrix_alloc( nsample , 1); { int icol,irow; // Running over all values. int arow,acol; // Running over active values. arow = 0; for (irow = 0; irow < matrix_get_rows( stepwise->X0 ); irow++) { if (bool_vector_iget( active_rows , irow )) { acol = 0; for (icol = 0; icol < matrix_get_columns( stepwise->X0 ); icol++) { if (bool_vector_iget( stepwise->active_set , icol )) { matrix_iset( X , arow , acol , matrix_iget( stepwise->X0 , irow , icol )); matrix_iset( E , arow , acol , matrix_iget( stepwise->E0 , irow , icol )); acol++; } } matrix_iset( Y , arow , 0 , matrix_iget( stepwise->Y0 , irow , 0 )); arow++; } } } } else { X = matrix_alloc_copy( stepwise->X0 ); E = matrix_alloc_copy( stepwise->E0 ); Y = matrix_alloc_copy( stepwise->Y0 ); } { if (stepwise->X_mean != NULL) matrix_free( stepwise->X_mean); stepwise->X_mean = matrix_alloc( 1 , nvar ); if (stepwise->X_norm != NULL) matrix_free( stepwise->X_norm); stepwise->X_norm = matrix_alloc( 1 , nvar ); matrix_type * beta = matrix_alloc( nvar , 1); /* This is the beta vector as estimated from the OLS estimator. */ regression_augmented_OLS( X , Y , E, beta ); /* In this code block the beta/tmp_beta vector which is dense with fewer elements than the full model is scattered into the beta0 vector which has full size and @nvar elements. */ { int ivar,avar; avar = 0; for (ivar = 0; ivar < matrix_get_columns( stepwise->X0 ); ivar++) { if (bool_vector_iget( stepwise->active_set , ivar )) { matrix_iset( stepwise->beta , ivar , 0 , matrix_iget( beta , avar , 0)); avar++; } } } matrix_free( beta ); } matrix_free( X ); matrix_free( E ); matrix_free( Y ); return y_mean; }
bool model_config_load_state( const model_config_type * config , int report_step) { return bool_vector_iget(config->__load_state , report_step); }
bool ecl_sum_vector_iget_is_rate(const ecl_sum_vector_type * ecl_sum_vector, int index){ return bool_vector_iget(ecl_sum_vector->is_rate_list, index); }