Package weka.core.matrix

Examples of weka.core.matrix.DoubleVector


    double val;
   
    if( j == ks -1 ) val = b.A[j][0];
    else if( j > ks - 1 ) {
      int jm = Math.min(n-1, j);
      DoubleVector u = getColumn(ks,jm,p[j]);
      DoubleVector v = b.getColumn(ks,jm,0);
      val = v.innerProduct(u) / u.norm2();
    }
    else {                 // ks > j
      for( k = j+1; k < ks; k++ ) // make a copy of A[j][]
  xxx[k] = A[j][p[k]];
      val = b.A[j][0];
View Full Code Here


   @return solution */
  public DoubleVector nnls( PaceMatrix b, IntVector pvt ) {
    int j, t, counter = 0, jm = -1, n = pvt.size();
    double ma, max, alpha, wj;
    int [] p = pvt.getArray();
    DoubleVector x = new DoubleVector( n );
    double [] xA = x.getArray();
    PaceMatrix z = new PaceMatrix(n, 1);
    PaceMatrix bt;
 
    // step 1
    int kp = 0; // #variables in the positive set P
    while ( true ) {         // step 2
      if( ++counter > 3*n // should never happen
  throw new RuntimeException("Does not converge");
      t = -1;
      max = 0.0;
      bt = new PaceMatrix( b.transpose() );
      for( j = kp; j <= n-1; j++ ) {   // W = A' (b - A x)
  wj = bt.times( 0, kp, m-1, this, p[j] );
  if( wj > max ) {        // step 4
    max = wj;
    t = j;
  }
      }
     
      // step 3
      if ( t == -1) break; // optimum achieved
     
      // step 5
      pvt.swap( kp, t );       // move variable from set Z to set P
      kp++;
      xA[kp-1] = 0;
      steplsqr( b, pvt, kp-1, kp-1, true );
      // step 6
      ma = 0;
      while ( ma < 1.5 ) {
  for( j = 0; j <= kp-1; j++ ) z.A[j][0] = b.A[j][0];
  rsolve(z, pvt, kp);
  ma = 2; jm = -1;
  for( j = 0; j <= kp-1; j++ ) {  // step 7, 8 and 9
    if( z.A[j][0] <= 0.0 ) { // alpha always between 0 and 1
      alpha = xA[j] / ( xA[j] - z.A[j][0] );
      if( alpha < ma ) {
        ma = alpha; jm = j;
      }
    }
  }
  if( ma > 1.5 )
    for( j = 0; j <= kp-1; j++ ) xA[j] = z.A[j][0]// step 7
  else {
    for( j = kp-1; j >= 0; j-- ) { // step 10
      // Modified to avoid round-off error (which seemingly
      // can cause infinite loop).
      if( j == jm ) { // step 11
        xA[j] = 0.0;
        steplsqr( b, pvt, kp, j, false );
        kp--;  // move variable from set P to set Z
      }
      else xA[j] += ma * ( z.A[j][0] - xA[j] );
    }
  }
      }
    }
    x.setSize(kp);
    pvt.setSize(kp);
    return x;
  }
View Full Code Here

    double beta1 = 0// coefficient value of the first cluster
    int k2 = 20;      // number of coefficients of the second cluster
    double beta2 = 5; // coefficient value of the second cluster
    int k = 1 + k1 + k2;

    DoubleVector beta = new DoubleVector( 1 + k1 + k2 );
    beta.set( 0, beta0 );
    beta.set( 1, k1, beta1 );
    beta.set( k1+1, k1+k2, beta2 );

    System.out.println("The data set contains " + n +
           " observations plus " + (k1 + k2) +
           " variables.\n\nThe coefficients of the true model"
           + " are:\n\n" + beta );
 
    System.out.println("\nThe standard deviation of the error term is " +
           sd );
 
    System.out.println("==============================================="
           + "============");
   
    PaceMatrix X = new PaceMatrix( n, k1+k2+1 );
    X.setMatrix( 0, n-1, 0, 0, 1 );
    X.setMatrix( 0, n-1, 1, k1+k2, random(n, k1+k2) );
 
    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
View Full Code Here

  /** Fits the mixture (or mixing) distribution to the data.
   *  @param data the data supposedly generated from the mixture
   *  @param method the method to be used. Refer to the static final
   *  variables of this class. */
  public void fit( DoubleVector data, int method ) {
    DoubleVector data2 = (DoubleVector) data.clone();
    if( data2.unsorted() ) data2.sort();

    int n = data2.size();
    int start = 0;
    DoubleVector subset;
    DiscreteFunction d = new DiscreteFunction();
    for( int i = 0; i < n-1; i++ ) {
      if( separable( data2, start, i, data2.get(i+1) ) &&
    separable( data2, i+1, n-1, data2.get(i) ) ) {
  subset = (DoubleVector) data2.subvector( start, i );
View Full Code Here

   */
  public DiscreteFunction fitForSingleCluster( DoubleVector data,
                 int method ) {
   
    if( data.size() < 2 ) return new DiscreteFunction( data );
    DoubleVector sp = supportPoints( data, 0 );
    PaceMatrix fi = fittingIntervals( data );
    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

    formalize();
  }
   
  private DiscreteFunction  formalize()
  {
    if( points == null ) points = new DoubleVector();
    if( values == null ) values = new DoubleVector();
 
    if( points.isEmpty() ) {
      if( ! values.isEmpty() )
  throw new IllegalArgumentException("sizes not match");
    }
    else {
      int n = points.size();
      if( values.isEmpty() ) {
  values = new DoubleVector( n, 1./n );
      }
      else {
  if( values.size() != n )
    throw new IllegalArgumentException("sizes not match");
      }
View Full Code Here

    //make a copy of the array
    double []disCopy = new double[distance.length];
    for (int i=0;i<distance.length; i++)
      disCopy[i]= distance[i];

    DoubleVector sortVector = new DoubleVector(disCopy);
    sortVector.sort();
    sorted = sortVector.getArrayCopy();
    return sorted;
  }
View Full Code Here

TOP

Related Classes of weka.core.matrix.DoubleVector

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.