List of usage examples for org.apache.commons.math3.linear RealVector getEntry
public abstract double getEntry(int index) throws OutOfRangeException;
From source file:edu.oregonstate.eecs.mcplan.ml.KernelPrincipalComponentsAnalysis.java
public static void main(final String[] args) throws FileNotFoundException { final File root = new File("test/KernelPrincipalComponentsAnalysis"); root.mkdirs();// w w w. j a v a2 s . c o m final int seed = 42; final int N = 30; final RandomGenerator rng = new MersenneTwister(seed); final ArrayList<RealVector> data = new ArrayList<RealVector>(); final ArrayList<RealVector> shuffled = new ArrayList<RealVector>(); // final double[][] covariance = new double[][] { {1.0, 0.0}, // {0.0, 1.0} }; // final MultivariateNormalDistribution p = new MultivariateNormalDistribution( // rng, new double[] { 0.0, 0.0 }, covariance ); // final MultivariateNormalDistribution q = new MultivariateNormalDistribution( // rng, new double[] { 10.0, 0.0 }, covariance ); // // for( int i = 0; i < N; ++i ) { // data.add( new ArrayRealVector( p.sample() ) ); // data.add( new ArrayRealVector( q.sample() ) ); // } // Fn.shuffle( rng, data ); final double sigma = 0.01; final double[][] covariance = new double[][] { { sigma, 0.0 }, { 0.0, sigma } }; final MultivariateNormalDistribution p = new MultivariateNormalDistribution(rng, new double[] { 0.0, 0.0 }, covariance); for (final double r : new double[] { 1.0, 3.0, 5.0 }) { for (int i = 0; i < N; ++i) { final double theta = i * 2 * Math.PI / N; final double[] noise = p.sample(); final RealVector v = new ArrayRealVector( new double[] { r * Math.cos(theta) + noise[0], r * Math.sin(theta) + noise[1] }); data.add(v); shuffled.add(v); } } Fn.shuffle(rng, shuffled); final Csv.Writer data_writer = new Csv.Writer(new PrintStream(new File(root, "data.csv"))); for (final RealVector v : data) { for (int i = 0; i < v.getDimension(); ++i) { data_writer.cell(v.getEntry(i)); } data_writer.newline(); } data_writer.close(); System.out.println("[Training]"); final int Ncomponents = 2; final KernelPrincipalComponentsAnalysis<RealVector> kpca = new KernelPrincipalComponentsAnalysis<RealVector>( shuffled, new RadialBasisFunctionKernel(0.5), 1e-6); System.out.println("[Finished]"); for (int i = 0; i < Ncomponents; ++i) { System.out.println(kpca.eigenvectors.get(i)); } System.out.println("Transformed data:"); final KernelPrincipalComponentsAnalysis.Transformer<RealVector> transformer = kpca .makeTransformer(Ncomponents); final Csv.Writer transformed_writer = new Csv.Writer(new PrintStream(new File(root, "transformed.csv"))); for (final RealVector u : data) { final RealVector v = transformer.transform(u); System.out.println(v); for (int i = 0; i < v.getDimension(); ++i) { transformed_writer.cell(v.getEntry(i)); } transformed_writer.newline(); } transformed_writer.close(); }
From source file:net.liuxuan.temp.filterTest.java
public static void main(String[] args) { double constantVoltage = 10d; double measurementNoise = 0.1d; double processNoise = 1e-5d; // A = [ 1 ]/* ww w . j a v a 2s. co m*/ RealMatrix A = new Array2DRowRealMatrix(new double[] { 1d }); // B = null RealMatrix B = null; // H = [ 1 ] RealMatrix H = new Array2DRowRealMatrix(new double[] { 1d }); // x = [ 10 ] RealVector x = new ArrayRealVector(new double[] { constantVoltage }); // Q = [ 1e-5 ] RealMatrix Q = new Array2DRowRealMatrix(new double[] { processNoise }); // P = [ 1 ] RealMatrix P0 = new Array2DRowRealMatrix(new double[] { 1d }); // R = [ 0.1 ] RealMatrix R = new Array2DRowRealMatrix(new double[] { measurementNoise }); ProcessModel pm = new DefaultProcessModel(A, B, Q, x, P0); MeasurementModel mm = new DefaultMeasurementModel(H, R); KalmanFilter filter = new KalmanFilter(pm, mm); // process and measurement noise vectors RealVector pNoise = new ArrayRealVector(1); RealVector mNoise = new ArrayRealVector(1); RandomGenerator rand = new JDKRandomGenerator(); // iterate 60 steps for (int i = 0; i < 60; i++) { filter.predict(); // simulate the process // pNoise.setEntry(0, processNoise * rand.nextGaussian()); pNoise.setEntry(0, Math.sin(Math.PI * 2 * i / 6)); // System.out.println("============"); // System.out.println(Math.sin(Math.PI*2*i/6)); // x = A * x + p_noise x = A.operate(x).add(pNoise); // simulate the measurement // mNoise.setEntry(0, measurementNoise * rand.nextGaussian()); mNoise.setEntry(0, 0); // z = H * x + m_noise RealVector z = H.operate(x).add(mNoise); filter.correct(z); double voltage = filter.getStateEstimation()[0]; System.out.println(voltage); // state estimate shouldn't be larger than the measurement noise double diff = Math.abs(x.getEntry(0) - filter.getStateEstimation()[0]); System.out.println("diff = " + diff); } }
From source file:edu.byu.nlp.math.RealVectors.java
public static void incrementEntry(RealVector v, int index) { v.setEntry(index, v.getEntry(index) + 1); }
From source file:edu.byu.nlp.math.RealVectors.java
public static void decrementEntry(RealVector v, int index) { v.setEntry(index, v.getEntry(index) - 1); }
From source file:com.cloudera.oryx.kmeans.computation.VectorConvert.java
public static RealVector fromString(CharSequence input) { RealVector rv = VECTOR_FORMAT.parse(input.toString()); int density = 0; for (int i = 0; i < rv.getDimension(); i++) { if (rv.getEntry(i) != 0.0) { density++;//from w ww . j a va 2 s . c o m } } if (2 * density < rv.getDimension()) { return new OpenMapRealVector(rv); } else { return rv; } }
From source file:lambertmrev.conversions.java
public static int[][] vec2kMat(RealVector in) { int dim = in.getDimension(); int[][] mat = new int[dim][dim]; for (int ii = 0; ii < dim; ii++) { int tmp = (int) in.getEntry(ii); mat[ii][tmp] = 1;/* ww w .j ava 2s. c o m*/ } return mat; }
From source file:edu.byu.nlp.stats.GammaDistribution.java
/** * Generates gamma samples, one for each element in shapes. * //w ww. j av a 2 s. c o m * @param shapes */ public static double[] sample(RealVector shapes, RandomGenerator rnd) { double[] gamma = new double[shapes.getDimension()]; for (int i = 0; i < gamma.length; i++) { gamma[i] = sample(shapes.getEntry(i), rnd); } return gamma; }
From source file:edu.oregonstate.eecs.mcplan.ml.HilbertSpace.java
public static double inner_prod(final RealVector x, final RealMatrix M, final RealVector y) { // return x.dotProduct( M.operate( y ) ); double s = 0.0; for (int i = 0; i < M.getRowDimension(); ++i) { for (int j = 0; j < M.getColumnDimension(); ++j) { s += x.getEntry(i) * M.getEntry(i, j) * y.getEntry(j); }/*from w ww . jav a2s . c o m*/ } return s; }
From source file:edu.stanford.cfuller.imageanalysistools.filter.LocalBackgroundEstimationFilter.java
protected static void swap(int first, int second, RealVector toProcess) { double value = toProcess.getEntry(first); toProcess.setEntry(first, toProcess.getEntry(second)); toProcess.setEntry(second, value);/*from w w w .jav a 2s . c om*/ }
From source file:com.cloudera.oryx.kmeans.computation.pmml.ClusteringModelBuilder.java
private static String toString(RealVector vec) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < vec.getDimension(); i++) { if (i > 0) { sb.append(' '); }//from w w w .j a v a 2s. c o m sb.append(vec.getEntry(i)); } return sb.toString(); }