示例#1
0
def get_least_polynomial_coefficients( v, num_dims, num_pts, k, 
                                       basis_indices = None ):
    if basis_indices is None:

        H = numpy.zeros( (num_pts, polynomial_space_dimension(num_dims, 
                                                              k[num_pts-1,0] ) ), 
                         numpy.double );
        #print k
        v_counter = 0;
        for q in range( num_pts ):
            current_size = polynomial_subspace_dimension( num_dims, k[q,0] );
            v_inds = range( v_counter, v_counter + current_size );

            previous_dimension = polynomial_space_dimension(num_dims, k[q,0]-1 );
            H_cols = range(previous_dimension,previous_dimension+current_size);
            #print H_cols, v_inds
            H[q,H_cols] = v[v_inds];
            v_counter += current_size;
            
    else:
        num_basis_indices = len( basis_indices )
        H = numpy.zeros( ( num_pts, num_basis_indices ), 
                         numpy.double );
        v_counter = 0;
        previous_dimension = 0
        degree = k[0,0]
        #print k, basis_indices
        for q in range( num_pts ):
            """
            if ( k[q] != degree or v_counter == 0 ):
                degree = k[q,0]
                current_size = 0
                while ( q+current_size < num_pts and k[q+current_size]==degree ):
                    current_size += 1
                if ( q +current_size == num_pts and k[q] != k[q-1]):
                    current_size = num_basis_indices - q
                    """
            if ( k[q] != k[q-1] or q == 0):
                current_size = 0
                for index in basis_indices:
                    if ( index.level_sum() == k[q] ):
                        current_size += 1
            v_inds = range( v_counter, v_counter + current_size );
            H_cols = range(previous_dimension,previous_dimension+current_size);
            #print H_cols, v_inds
            H[q,H_cols] = v[v_inds];
            v_counter += current_size;
            if ( q+1 < num_pts and k[q] != k[q+1]):
                previous_dimension += current_size
        
    return H
示例#2
0
    def cv_vs_error_study( build_pts, build_vals, domain, 
               test_pts, test_vals, 
               results_file = None,
               cv_file = None, solver_type = 2 ):
        num_dims = build_pts.shape[0]
        if ( num_dims == 10 ):
            max_order = 5
        elif ( num_dims == 15 ):
            max_order = 4
        else: 
            max_order = 3

        poly_1d = [ LegendrePolynomial1D() ]
        basis = TensorProductBasis( num_dims, poly_1d )
        pce = PCE( num_dims, order = 0, basis = basis, func_domain = domain )

        orders = numpy.arange( 1, max_order + 1 )
        solvers = numpy.array( [solver_type], numpy.int32 )
        cv_params_grid_array = cartesian_product( [solvers,orders] )
        cv_params_grid = []
        for i in xrange( cv_params_grid_array.shape[0] ):
            cv_params = {}
            cv_params['solver'] = numpy.int32( cv_params_grid_array[i,0] )
            cv_params['order'] = numpy.int32( cv_params_grid_array[i,1] )
            num_pce_terms = polynomial_space_dimension( num_dims,
                                                        cv_params['order'] )
            if ( cv_params['solver'] <= 1 and 
                 num_pce_terms >= build_pts.shape[1] ):
                cv_params['lambda'] = 1.e-12
            cv_params_grid.append( cv_params )

        # print cv_params_grid

        # cv_iterator = LeaveOneOutCrossValidationIterator()    
        cv_iterator = KFoldCrossValidationIterator( num_folds = 20 )
        CV = GridSearchCrossValidation( cv_iterator, pce,
                                        use_predictor_cross_validation = True,
                                        use_fast_predictor_cross_validation = True )
        t0 = time.time()
        CV.run( build_pts, build_vals, cv_params_grid )
        time_taken = time.time() - t0
        print 'cross validation took ', time_taken, ' seconds'

        print "################"
        print "Best cv params: ", CV.best_cv_params
        print "Best cv score: ", CV.best_score
        print "################"

        for order in orders:
            residual_norms = numpy.empty( len( CV.cv_params_set ), numpy.double )
            scores = numpy.empty( len( CV.cv_params_set ), numpy.double )
            k = 0
            for i in xrange( len( CV.cv_params_set ) ):
                if ( CV.cv_params_set[i]['order'] == order ):
                    residual_norms[k] = CV.cv_params_set[i]['norm_residual']
                    scores[k] = CV.scores[i]
                    k += 1

            residual_norms.resize( k )
            scores.resize( k )

            pce = PCE( num_dims, 
                       order = order, 
                       basis = basis, 
                       func_domain = domain )
            V = pce.vandermonde( build_pts ).T
            pce.set_solver( CV.best_cv_params['solver'] )
            # pce.linear_solver.max_iterations = 3
            sols, sol_metrics = pce.linear_solver.solve( V, build_vals )
            from sklearn.linear_model import orthogonal_mp
            l2_error = numpy.empty( ( sols.shape[1] ), numpy.double )
            residuals = numpy.empty( ( sols.shape[1] ), numpy.double )
            test_pts = numpy.random.uniform( 0., 1., ( num_dims, 1000 ) )
            f = GenzModel( domain, 'oscillatory' )
            # f.set_coefficients( 4.5, 'no-decay' )
            f.set_coefficients( 4.5, 'quadratic-decay' )
            test_vals = f( test_pts ).reshape( ( test_pts.shape[1], 1 ) )
            for i in xrange( sols.shape[1] ):
                coeff = sols[:,i]
                pce.set_coefficients( coeff )
                residuals[i] = numpy.linalg.norm( build_vals - 
                                                  pce.evaluate_set( build_pts ) )
                num_test_pts = test_pts.shape[1]
                pce_vals_pred = pce.evaluate_set( test_pts ).T
                error = test_vals.squeeze() - pce_vals_pred
                l2_error[i] = numpy.linalg.norm( error ) / numpy.sqrt( num_test_pts )

            import pylab
            print residuals, l2_error
            print residual_norms, scores
            pylab.loglog( residuals, l2_error, label  = str( order ) + 'true' )
            pylab.loglog( residual_norms, scores, label = str( order )+'-cv' )
        pylab.xlim([1e-3,10])
        pylab.legend()
        pylab.show()