Changeset 730

Show
Ignore:
Timestamp:
11/17/09 20:55:03 (14 years ago)
Author:
smidl
Message:

Reworked epdf_harness - testing is now more general.

Corrections of existing tests.

Files:
10 modified

Legend:

Unmodified
Added
Removed
  • applications/bdmtoolbox/mex/epdf_sample_mat.cpp

    r729 r730  
    2727 
    2828        int nos=10; 
    29         if (n_input>1) {nos = (int)mexGetPr(input[1]);} 
     29        if (n_input>1) {nos = (int)(*mxGetPr(input[1]));} 
    3030         
    3131        UImxArray Cfg(input[0]); 
  • library/bdm/stat/discrete.cpp

    r723 r730  
    122122} 
    123123 
     124void grid_fnc::set_values (const epdf &ep ) { 
     125        if ( sup.points() > 0 ) { 
     126                values ( 0 ) =  exp(ep.evallog( sup.first_vec() )); 
     127                for ( int j = 1; j < sup.points(); j++ ) { 
     128                        values ( j ) = exp(ep.evallog( sup.next_vec() )); 
     129                } 
     130        } 
    124131} 
     132 
     133} 
  • library/bdm/stat/discrete.h

    r706 r730  
    6262        //! Get next active vector, call ONLY after first_vector()! 
    6363        const vec& next_vec(); 
    64  
     64         
     65        //! return active vector, call ONLY after first_vector()! 
     66        const vec& act_vec(){return actvec;}; 
     67         
    6568        //! \todo to je asi navic .. v predkovi! 
    6669        ivec nearest_point ( const vec &val ); 
     
    140143        //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer 
    141144        void set_values ( double ( *evalptr ) ( const vec& ) ); 
    142  
     145         
     146        //! constructor function fills values by calling function \c f , double f(vec&), given by a pointer 
     147        void set_values ( const epdf &ep); 
     148         
    143149        //! get value nearest to the given point 
    144150        double nearest_val ( const vec &val ) { 
     
    149155                return vec_1 ( nearest_val ( val ) ); 
    150156        } 
     157        const vec & _values() const { return values; } 
    151158}; 
    152159UIREGISTER ( grid_fnc ); 
  • library/bdm/stat/exp_family.cpp

    r725 r730  
    3838         
    3939        return concat (cvectorize(M),cvectorize(R.to_mat())); 
     40} 
     41 
     42mat egiw::sample_mat(int n) const { 
     43        // TODO - correct approach - convert to product of norm * Wishart 
     44        mat M; 
     45        ldmat Vz; 
     46        ldmat Lam; 
     47        factorize(M,Vz,Lam); 
     48         
     49        chmat ChLam(Lam.to_mat()); 
     50        chmat iChLam; 
     51        ChLam.inv(iChLam); 
     52         
     53        eWishartCh Omega; //inverse Wishart, result is R,  
     54        Omega.set_parameters(iChLam,nu-2*nPsi-dimx); // 2*nPsi is there to match numercial simulations - check if analytically correct 
     55         
     56        mat OmChi; 
     57        mat Z(M.rows(),M.cols()); 
     58 
     59        mat Mi; 
     60        mat RChiT; 
     61        mat tmp(dimension(), n); 
     62        for (int i=0; i<n;i++){ 
     63                OmChi=Omega.sample_mat(); 
     64                RChiT=inv(OmChi); 
     65                Z=randn(M.rows(), M.cols()); 
     66                Mi = M + RChiT * Z * inv(Vz._L().T() *diag(sqrt(Vz._D()))); 
     67         
     68                tmp.set_col(i,concat (cvectorize(Mi),cvectorize(RChiT*RChiT.T()))); 
     69        } 
     70        return tmp; 
    4071} 
    4172 
  • library/bdm/stat/exp_family.h

    r728 r730  
    221221 
    222222                vec sample() const; 
     223                mat sample_mat(int n) const; 
    223224                vec mean() const; 
    224225                vec variance() const; 
  • library/tests/epdf_harness.cpp

    r725 r730  
    2828        UI::get ( variance, set, "variance", UI::compulsory ); 
    2929 
    30         UI::get ( support, set, "support", UI::optional ); 
    31         UI::get ( nbins, set, "nbins", UI::optional ); 
     30        support = UI::build<rectangular_support> ( set, "support", UI::optional ); 
    3231        UI::get ( nsamples, set, "nsamples", UI::optional ); 
    3332        UI::get ( R, set, "R", UI::optional ); 
     
    4443        CHECK_CLOSE_EX ( variance, hepdf->variance(), tolerance ); 
    4544 
    46         if ( support.rows() == 2 ) { 
    47                 int old_size = nbins.size(); 
    48                 if ( old_size < 2 ) { 
    49                         ivec new_nbins ( "100 100" ); 
    50                         for ( int i = 0; i < old_size; ++i ) { 
    51                                 new_nbins ( i ) = nbins ( i ); 
    52                         } 
    53  
    54                         nbins = new_nbins; 
    55                 } 
    56  
    57                 check_support_mean(); 
    58                 check_support_integral(); 
     45        if ( support ) { // support is given 
     46                grid_fnc ep_disc; 
     47                ep_disc.set_support(*support); 
     48                ep_disc.set_values(*hepdf); 
     49                // ep_disc is discretized at support points 
     50 
     51                double point_volume =prod(support->_steps()); 
     52                CHECK_CLOSE(1.0, sum(ep_disc._values())*point_volume, 0.01); 
     53 
     54                vec pdf=ep_disc._values(); 
     55                pdf /=sum(pdf); // normalize 
     56                 
     57                vec mea=pdf(0) * support->first_vec(); 
     58                mat Remp=pdf(0) * outer_product(support->act_vec(), support->act_vec()); 
     59 
     60                // run through all points 
     61                for (int i=1; i<support->points(); i++){ 
     62                        mea += pdf(i)*support->next_vec(); 
     63                        Remp += pdf(i) * outer_product(support->act_vec(), support->act_vec()); 
     64                } 
     65                CHECK_CLOSE(mean, mea, tolerance); 
     66                CHECK_CLOSE(R, Remp-outer_product(mea,mea), tolerance); 
    5967        } 
    6068 
     
    112120} 
    113121 
    114 void epdf_harness::check_support_mean() { 
    115         vec xb = support.get_row ( 0 ); 
    116         vec yb = support.get_row ( 1 ); 
    117  
    118         vec actual; 
    119         actual = num_mean2 ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ); 
    120  
    121         CHECK_CLOSE(mean, actual, tolerance); 
    122 } 
    123  
    124 void epdf_harness::check_support_integral() { 
    125         vec xb = support.get_row ( 0 ); 
    126         vec yb = support.get_row ( 1 ); 
    127  
    128         double nc = normcoef ( hepdf.get(), xb, yb, nbins ( 0 ), nbins ( 1 ) ); 
    129         CHECK_CLOSE(1.0,nc,0.01); 
    130 } 
    131122 
    132123void epdf_harness::check_sample_mean() { 
  • library/tests/epdf_harness.h

    r722 r730  
    2020#include "base/user_info.h" 
    2121#include "stat/emix.h" 
     22#include <stat/discrete.h> 
    2223 
    2324namespace bdm { 
     
    2829        vec mean; 
    2930        vec variance; 
    30         mat support; 
    31         ivec nbins; 
     31        shared_ptr<rectangular_support> support; 
    3232        int nsamples; 
    3333        mat R; 
  • library/tests/testsuite/egiw.cfg

    r725 r730  
    1414  mean = [ 1.1, 0.2 ]; 
    1515  variance = [ 0.02, 0.01333 ]; 
    16   support = ( "matrix", 2, 2, [ -2.0, 4.0, 0.01, 2.0 ] ); 
     16  R = ( "matrix", 2, 2, [ 0.02, 0.0, 0.0, 0.022 ] ); 
     17  support = { 
     18        class = "rectangular_support"; 
     19        ranges = ( [ -2.0, 4.0 ], [0.01, 2.0 ] ); 
     20        gridsizes = [ 100, 100 ]; 
     21        }; 
    1722  nbins = [ 100, 200 ]; 
    1823  tolerance = 0.01; 
     
    3338  mean = [2.0, 1.14286]; 
    3439  variance = [0.0285714, 0.0395795]; 
    35   support = ( "matrix", 2, 2, [ 0.0, 4.0, 0.01, 3.0 ] ); 
    36   nbins = [ 100, 200 ]; 
     40  R = ( "matrix", 2, 2, [ 0.028, 0.0, 0.0, 0.078 ] ); 
     41  support = { 
     42        class = "rectangular_support"; 
     43        ranges = ( [ 0.0, 4.0 ], [0.01, 3.0 ] ); 
     44        gridsizes = [ 100, 100 ]; 
     45        }; 
    3746  tolerance = 0.01; 
    3847} ); 
  • library/tests/testsuite/enorm.cfg

    r717 r730  
    1414  mean = [ 1.1, -1.0 ]; 
    1515  variance = [ 1, 2 ]; 
    16   support = ( "matrix", 2, 2, [ -5.0, 5.0, -5.0, 5.0 ] ); 
     16  support = { 
     17        class = "rectangular_support"; 
     18        ranges = ( [ -5.0, 5.0 ], [-5.0, 5.0 ] ); 
     19        gridsizes = [ 100, 100 ]; 
     20        }; 
    1721  R = ( "matrix", 2, 2, [ 1.0, -0.5, -0.5, 2.0 ] ); 
    1822  marginal_rv =     { 
     
    3640  mean = [ 0.0, 0.0 ]; 
    3741  variance = [ 5.0, 5.2 ]; 
    38   support = ( "matrix", 2, 2, [ -10.0, 10.0, -10.0, 10.0 ] ); 
     42  support = { 
     43        class = "rectangular_support"; 
     44        ranges = ( [ -10.0, 10.0 ], [-10.0, 10.0 ] ); 
     45        gridsizes = [ 100, 100 ]; 
     46        }; 
    3947  R = ( "matrix", 2, 2, [ 5.0, -0.05, -0.05, 5.2 ] ); 
    4048  nbins = [ 200, 200 ]; 
  • library/tests/testsuite/user_info_matrix.cfg

    r721 r730  
    2121  manufacturer = "noname"; 
    2222  electricLights = false; 
    23   matr = ( 0, 0, [ ] ); 
     23  matr = ( "matrix", 0, 0, [ ] ); 
    2424}; 
    2525pepikovo :