Example #1
0
     def test_least_interpolant( self ):
          num_dims = 2
          func_domain = TensorProductDomain( num_dims, [[-1,1]] )

          x = -numpy.cos( numpy.linspace( 0., 1., 10 ) * numpy.pi );
          [X,Y] = numpy.meshgrid( x, x )
          build_points = numpy.vstack( ( X.reshape(( 1,X.shape[0]*X.shape[1])), 
                                         Y.reshape(( 1, Y.shape[0]*Y.shape[1]))))
          build_values = matlab_peaks( build_points )

          poly_1d = [ LegendrePolynomial1D() ]
          basis = TensorProductBasis( num_dims, poly_1d )
          linear_solver = LeastInterpolation()
          predictor = PCE( build_points.shape[0], basis = basis, 
                           linear_solver = linear_solver ) 
          predictor.function_domain( func_domain )
          predictor.build( build_points, build_values )
          result = predictor.evaluate_set( build_points )
          if ( result.ndim > 1 ): result = result.reshape( result.shape[0] )

          error = result - build_values
          l2_error = numpy.sqrt( numpy.dot( error.T, error ) / \
                                      build_points.shape[1] )
          print 'error at nodes: %1.15e' %l2_error
          assert l2_error < self.eps

          poly_1d = [ JacobiPolynomial1D( 0.5, 0.5 ) ]
          basis = TensorProductBasis( num_dims, poly_1d )
          linear_solver = LeastInterpolation()
          predictor = PCE( build_points.shape[0], basis = basis, 
                           linear_solver = linear_solver ) 
          predictor.function_domain( func_domain )
          predictor.build( build_points, build_values )
          result = predictor.evaluate_set( build_points )
          if ( result.ndim > 1 ): result = result.reshape( result.shape[0] )
    num_dims = 2
    poly_1d = [ LegendrePolynomial1D() ]
    basis = TensorProductBasis( num_dims, poly_1d )
    domain = TensorProductDomain( num_dims, ranges = [[-1.,1.]] )
    #linear_solver = LARSSolver( solver = CSSolvers.LASSO_REGRESSION )
    linear_solver = LARSSolver( solver = CSSolvers.LEAST_ANGLE_REGRESSION )
    #linear_solver = OMPSolver()
    #linear_solver = LeastSquaresSolver()
    pce = PolynomialChaosExpansion( num_dims, basis, order, domain,
                                    linear_solver = linear_solver )
    print pce.num_terms

    f = lambda x: numpy.sum( x**5, axis = 0 )
    # f = lambda x: numpy.sum( 1/2.*(3*x**2-1.), axis = 0 )
    from examples.test_functions import matlab_peaks
    f = lambda x: matlab_peaks( x )
    
    #rng = numpy.random
    seed = 0
    rng = numpy.random.RandomState( seed )    

    build_pts = rng.uniform( -1, 1, ( 2, 1000 ) )
    build_vals = f( build_pts )
    from utilities.math_utils import nchoosek
    pce.build( build_pts, build_vals )

    test_pts =  rng.uniform( -1, 1, ( 2, 10000 ) )
    test_vals = f( test_pts )
    print numpy.linalg.norm( test_vals - pce.evaluate_set( test_pts ) ) / numpy.sqrt( test_pts.shape[1] )

    me, te, ie = pce.get_sensitivities()