Package weka.core.matrix

Examples of weka.core.matrix.IntVector


   */
  private double [] pace(double[][] matrix_X, double [] vector_Y) {
   
    PaceMatrix X = new PaceMatrix( matrix_X );
    PaceMatrix Y = new PaceMatrix( vector_Y, vector_Y.length );
    IntVector pvt = IntVector.seq(0, X.getColumnDimension()-1);
    int n = X.getRowDimension();
    int kr = X.getColumnDimension();

    X.lsqrSelection( Y, pvt, 1 );
    X.positiveDiagonal( Y, pvt );
   
    PaceMatrix sol = (PaceMatrix) Y.clone();
    X.rsolve( sol, pvt, pvt.size() );
    DoubleVector r = Y.getColumn( pvt.size(), n-1, 0);
    double sde = Math.sqrt(r.sum2() / r.size());
   
    DoubleVector aHat = Y.getColumn( 0, pvt.size()-1, 0).times( 1./sde );

    DoubleVector aTilde = null;
    switch( paceEstimator) {
    case ebEstimator:
    case nestedEstimator:
    case subsetEstimator:
      NormalMixture d = new NormalMixture();
      d.fit( aHat, MixtureDistribution.NNMMethod );
      if( paceEstimator == ebEstimator )
  aTilde = d.empiricalBayesEstimate( aHat );
      else if( paceEstimator == ebEstimator )
  aTilde = d.subsetEstimate( aHat );
      else aTilde = d.nestedEstimate( aHat );
      break;
    case pace2Estimator:
    case pace4Estimator:
    case pace6Estimator:
      DoubleVector AHat = aHat.square();
      ChisqMixture dc = new ChisqMixture();
      dc.fit( AHat, MixtureDistribution.NNMMethod );
      DoubleVector ATilde;
      if( paceEstimator == pace6Estimator )
  ATilde = dc.pace6( AHat );
      else if( paceEstimator == pace2Estimator )
  ATilde = dc.pace2( AHat );
      else ATilde = dc.pace4( AHat );
      aTilde = ATilde.sqrt().times( aHat.sign() );
      break;
    case olsEstimator:
      aTilde = aHat.copy();
      break;
    case aicEstimator:
    case bicEstimator:
    case ricEstimator:
    case olscEstimator:
      if(paceEstimator == aicEstimator) olscThreshold = 2;
      else if(paceEstimator == bicEstimator) olscThreshold = Math.log( n );
      else if(paceEstimator == ricEstimator) olscThreshold = 2*Math.log( kr );
      aTilde = aHat.copy();
      for( int i = 0; i < aTilde.size(); i++ )
  if( Math.abs(aTilde.get(i)) < Math.sqrt(olscThreshold) )
    aTilde.set(i, 0);
    }
    PaceMatrix YTilde = new PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
    X.rsolve( YTilde, pvt, pvt.size() );
    DoubleVector betaTilde = YTilde.getColumn(0).unpivoting( pvt, kr );
   
    return betaTilde.getArrayCopy();
  }
View Full Code Here


   */
  private double [] pace(double[][] matrix_X, double [] vector_Y) {
   
    PaceMatrix X = new PaceMatrix( matrix_X );
    PaceMatrix Y = new PaceMatrix( vector_Y, vector_Y.length );
    IntVector pvt = IntVector.seq(0, X.getColumnDimension()-1);
    int n = X.getRowDimension();
    int kr = X.getColumnDimension();

    X.lsqrSelection( Y, pvt, 1 );
    X.positiveDiagonal( Y, pvt );
   
    PaceMatrix sol = (PaceMatrix) Y.clone();
    X.rsolve( sol, pvt, pvt.size() );
    DoubleVector r = Y.getColumn( pvt.size(), n-1, 0);
    double sde = Math.sqrt(r.sum2() / r.size());
   
    DoubleVector aHat = Y.getColumn( 0, pvt.size()-1, 0).times( 1./sde );

    DoubleVector aTilde = null;
    switch( paceEstimator) {
    case ebEstimator:
    case nestedEstimator:
    case subsetEstimator:
      NormalMixture d = new NormalMixture();
      d.fit( aHat, MixtureDistribution.NNMMethod );
      if( paceEstimator == ebEstimator )
  aTilde = d.empiricalBayesEstimate( aHat );
      else if( paceEstimator == ebEstimator )
  aTilde = d.subsetEstimate( aHat );
      else aTilde = d.nestedEstimate( aHat );
      break;
    case pace2Estimator:
    case pace4Estimator:
    case pace6Estimator:
      DoubleVector AHat = aHat.square();
      ChisqMixture dc = new ChisqMixture();
      dc.fit( AHat, MixtureDistribution.NNMMethod );
      DoubleVector ATilde;
      if( paceEstimator == pace6Estimator )
  ATilde = dc.pace6( AHat );
      else if( paceEstimator == pace2Estimator )
  ATilde = dc.pace2( AHat );
      else ATilde = dc.pace4( AHat );
      aTilde = ATilde.sqrt().times( aHat.sign() );
      break;
    case olsEstimator:
      aTilde = aHat.copy();
      break;
    case aicEstimator:
    case bicEstimator:
    case ricEstimator:
    case olscEstimator:
      if(paceEstimator == aicEstimator) olscThreshold = 2;
      else if(paceEstimator == bicEstimator) olscThreshold = Math.log( n );
      else if(paceEstimator == ricEstimator) olscThreshold = 2*Math.log( kr );
      aTilde = aHat.copy();
      for( int i = 0; i < aTilde.size(); i++ )
  if( Math.abs(aTilde.get(i)) < Math.sqrt(olscThreshold) )
    aTilde.set(i, 0);
    }
    PaceMatrix YTilde = new PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
    X.rsolve( YTilde, pvt, pvt.size() );
    DoubleVector betaTilde = YTilde.getColumn(0).unpivoting( pvt, kr );
   
    return betaTilde.getArrayCopy();
  }
View Full Code Here

  /**
   * Sorts the point values of the discrete function.
   */
  public void  sort()
  {
    IntVector index = points.sortWithIndex();
    values = values.subvector( index );
  }
View Full Code Here

 
    PaceMatrix Y = new
      PaceMatrix( X.times( new PaceMatrix(beta) ).
      plusEquals( randomNormal(n,1).times(sd) ) );

    IntVector pvt = (IntVector) IntVector.seq(0, k1+k2);

    /*System.out.println( "The OLS estimate (by jama.Matrix.solve()) is:\n\n" +
      (new PaceMatrix(X.solve(Y))).getColumn(0) );*/
 
    X.lsqrSelection( Y, pvt, 1 );
    X.positiveDiagonal( Y, pvt );

    PaceMatrix sol = (PaceMatrix) Y.clone();
    X.rsolve( sol, pvt, pvt.size() );
    DoubleVector betaHat = sol.getColumn(0).unpivoting( pvt, k );
    System.out.println( "\nThe OLS estimate (through lsqr()) is: \n\n" +
      betaHat );

    System.out.println( "\nQuadratic loss of the OLS estimate (||X b - X bHat||^2) = " +
      ( new PaceMatrix( X.times( new
        PaceMatrix(beta.minus(betaHat)) )))
      .getColumn(0).sum2() );

    System.out.println("=============================================" +
           "==============");
    System.out.println("             *** Pace estimation *** \n");
    DoubleVector r = Y.getColumn( pvt.size(), n-1, 0);
    double sde = Math.sqrt(r.sum2() / r.size());
 
    System.out.println( "Estimated standard deviation = " + sde );

    DoubleVector aHat = Y.getColumn( 0, pvt.size()-1, 0).times( 1./sde );
    System.out.println("\naHat = \n" + aHat );
 
    System.out.println("\n========= Based on chi-square mixture ============");

    ChisqMixture d2 = new ChisqMixture();
    int method = MixtureDistribution.NNMMethod;
    DoubleVector AHat = aHat.square();
    d2.fit( AHat, method );
    System.out.println( "\nEstimated mixing distribution is:\n" + d2 );
 
    DoubleVector ATilde = d2.pace2( AHat );
    DoubleVector aTilde = ATilde.sqrt().times(aHat.sign());
    PaceMatrix YTilde = new
      PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
    X.rsolve( YTilde, pvt, pvt.size() );
    DoubleVector betaTilde =
    YTilde.getColumn(0).unpivoting( pvt, k );
    System.out.println( "\nThe pace2 estimate of coefficients = \n" +
      betaTilde );
    System.out.println( "Quadratic loss = " +
      ( new PaceMatrix( X.times( new
        PaceMatrix(beta.minus(betaTilde)) )))
      .getColumn(0).sum2() );
 
    ATilde = d2.pace4( AHat );
    aTilde = ATilde.sqrt().times(aHat.sign());
    YTilde = new PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
    X.rsolve( YTilde, pvt, pvt.size() );
    betaTilde = YTilde.getColumn(0).unpivoting( pvt, k );
    System.out.println( "\nThe pace4 estimate of coefficients = \n" +
      betaTilde );
    System.out.println( "Quadratic loss = " +
      ( new PaceMatrix( X.times( new
        PaceMatrix(beta.minus(betaTilde)) )))
      .getColumn(0).sum2() );
 
    ATilde = d2.pace6( AHat );
    aTilde = ATilde.sqrt().times(aHat.sign());
    YTilde = new PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
    X.rsolve( YTilde, pvt, pvt.size() );
    betaTilde = YTilde.getColumn(0).unpivoting( pvt, k );
    System.out.println( "\nThe pace6 estimate of coefficients = \n" +
      betaTilde );
    System.out.println( "Quadratic loss = " +
      ( new PaceMatrix( X.times( new
        PaceMatrix(beta.minus(betaTilde)) )))
      .getColumn(0).sum2() );
 
    System.out.println("\n========= Based on normal mixture ============");
 
    NormalMixture d = new NormalMixture();
    d.fit( aHat, method );
    System.out.println( "\nEstimated mixing distribution is:\n" + d );
 
    aTilde = d.nestedEstimate( aHat );
    YTilde = new PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
    X.rsolve( YTilde, pvt, pvt.size() );
    betaTilde = YTilde.getColumn(0).unpivoting( pvt, k );
    System.out.println( "The nested estimate of coefficients = \n" +
      betaTilde );
    System.out.println( "Quadratic loss = " +
      ( new PaceMatrix( X.times( new
        PaceMatrix(beta.minus(betaTilde)) )))
      .getColumn(0).sum2() );
 
 
    aTilde = d.subsetEstimate( aHat );
    YTilde = new PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
    X.rsolve( YTilde, pvt, pvt.size() );
    betaTilde =
    YTilde.getColumn(0).unpivoting( pvt, k );
    System.out.println( "\nThe subset estimate of coefficients = \n" +
      betaTilde );
    System.out.println( "Quadratic loss = " +
      ( new PaceMatrix( X.times( new
        PaceMatrix(beta.minus(betaTilde)) )))
      .getColumn(0).sum2() );
 
    aTilde = d.empiricalBayesEstimate( aHat );
    YTilde = new PaceMatrix((new PaceMatrix(aTilde)).times( sde ));
    X.rsolve( YTilde, pvt, pvt.size() );
    betaTilde = YTilde.getColumn(0).unpivoting( pvt, k );
    System.out.println( "\nThe empirical Bayes estimate of coefficients = \n"+
      betaTilde );
 
    System.out.println( "Quadratic loss = " +
View Full Code Here

    PaceMatrix pm = probabilityMatrix( sp, fi );
    PaceMatrix epm = new
      PaceMatrix( empiricalProbability( data, fi ).
      timesEquals( 1. / data.size() ) );
   
    IntVector pvt = (IntVector) IntVector.seq(0, sp.size()-1);
    DoubleVector weights;
   
    switch( method ) {
    case NNMMethod:
      weights = pm.nnls( epm, pvt );
      break;
    case PMMethod:
      weights = pm.nnlse1( epm, pvt );
      break;
    default:
      throw new IllegalArgumentException("unknown method");
    }
   
    DoubleVector sp2 = new DoubleVector( pvt.size() );
    for( int i = 0; i < sp2.size(); i++ ){
      sp2.set( i, sp.get(pvt.get(i)) );
    }
   
    DiscreteFunction d = new DiscreteFunction( sp2, weights );
    d.sort();
    d.normalize();
View Full Code Here

TOP

Related Classes of weka.core.matrix.IntVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.