List of usage examples for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector
public ArrayRealVector(ArrayRealVector v) throws NullArgumentException
From source file:Theta.java
/** * @param args the command line arguments */// ww w .j a v a 2s .c om public static void main(String[] args) { // TODO code application logic here double[] u = { 1, 1 }; double[] v = { 7, 9 }; RealVector vectorU = new ArrayRealVector(u); RealVector vectorV = new ArrayRealVector(v); double distance = vectorU.getDistance(vectorV); System.out.println(distance); //RealVector projected=vectorU.projection(vectorV); //System.out.println(projected.toString()); }
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 ]/*w w w . ja v a 2 s. 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:jat.examples.TwoBodyExample.TwoBodyExample.java
public static void main(String[] args) { TwoBodyExample x = new TwoBodyExample(); // create a TwoBody orbit using orbit elements TwoBodyAPL sat = new TwoBodyAPL(7000.0, 0.3, 0.0, 0.0, 0.0, 0.0); double[] y = sat.randv(); ArrayRealVector v = new ArrayRealVector(y); DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00"); RealVectorFormat format = new RealVectorFormat(df2); System.out.println(format.format(v)); // find out the period of the orbit double period = sat.period(); // set the final time = one orbit period double tf = period; // set the initial time to zero double t0 = 0.0; // propagate the orbit FirstOrderIntegrator dp853 = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10); dp853.addStepHandler(sat.stepHandler); // double[] y = new double[] { 7000.0, 0, 0, .0, 8, 0 }; // initial // state/*from w ww. j av a2 s.com*/ dp853.integrate(sat, 0.0, y, 8000, y); // now y contains final state at // tf Double[] objArray = sat.time.toArray(new Double[sat.time.size()]); double[] timeArray = ArrayUtils.toPrimitive(objArray); double[] xsolArray = ArrayUtils.toPrimitive(sat.xsol.toArray(new Double[sat.time.size()])); double[] ysolArray = ArrayUtils.toPrimitive(sat.ysol.toArray(new Double[sat.time.size()])); double[][] XY = new double[timeArray.length][2]; // int a=0; // System.arraycopy(timeArray,0,XY[a],0,timeArray.length); // System.arraycopy(ysolArray,0,XY[1],0,ysolArray.length); for (int i = 0; i < timeArray.length; i++) { XY[i][0] = xsolArray[i]; XY[i][1] = ysolArray[i]; } Plot2DPanel p = new Plot2DPanel(); // Plot2DPanel p = new Plot2DPanel(min, max, axesScales, axesLabels); ScatterPlot s = new ScatterPlot("orbit", Color.RED, XY); // LinePlot l = new LinePlot("sin", Color.RED, XY); // l.closed_curve = false; // l.draw_dot = true; p.addPlot(s); p.setLegendOrientation(PlotPanel.SOUTH); double plotSize = 10000.; double[] min = { -plotSize, -plotSize }; double[] max = { plotSize, plotSize }; p.setFixedBounds(min, max); new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); System.out.println("end"); }
From source file:edu.oregonstate.eecs.mcplan.ml.KMeans.java
/** * @param args/*from w w w . j a v a 2s . c o m*/ */ public static void main(final String[] args) { final int nclusters = 2; final ArrayList<RealVector> data = new ArrayList<RealVector>(); for (int x = -1; x <= 1; ++x) { for (int y = -1; y <= 1; ++y) { data.add(new ArrayRealVector(new double[] { x, y })); data.add(new ArrayRealVector(new double[] { x + 10, y + 10 })); } } final KMeans kmeans = new KMeans(nclusters, data.toArray(new RealVector[data.size()])); /* { @Override public double distance( final RealVector a, final RealVector b ) { return a.getL1Distance( b ); } }; */ kmeans.run(); for (int i = 0; i < kmeans.centers().length; ++i) { System.out.println("Center " + i + ": " + kmeans.centers()[i]); for (int j = 0; j < kmeans.clusters().length; ++j) { if (kmeans.clusters()[j] == i) { System.out.println("\tPoint " + data.get(j)); } } } }
From source file:gamlss.algorithm.Gamlss.java
/** * Main method./*from ww w. j ava 2 s . co m*/ * @param args - command-line arguments */ public static void main(final String[] args) { //String fileName = "Data/dataReduced.csv"; String fileName = "Data/oil.csv"; // String fileName = "Data/sp.csv"; //String fileName = "Data/dataReduced.csv"; CSVFileReader readData = new CSVFileReader(fileName); readData.readFile(); ArrayList<String> data = readData.storeValues; ArrayRealVector y = new ArrayRealVector(data.size()); BlockRealMatrix muX = new BlockRealMatrix(data.size(), 1); BlockRealMatrix sigmaX = new BlockRealMatrix(data.size(), 1); BlockRealMatrix nuX = new BlockRealMatrix(data.size(), 1); BlockRealMatrix tauX = new BlockRealMatrix(data.size(), 1); ArrayRealVector w = new ArrayRealVector(data.size()); BlockRealMatrix muS = new BlockRealMatrix(data.size(), 1); BlockRealMatrix sigmaS = new BlockRealMatrix(data.size(), 1); BlockRealMatrix nuS = new BlockRealMatrix(data.size(), 1); BlockRealMatrix tauS = new BlockRealMatrix(data.size(), 1); for (int i = 0; i < data.size(); i++) { String[] line = data.get(i).split(","); y.setEntry(i, Double.parseDouble(line[0])); muX.setEntry(i, 0, Double.parseDouble(line[1])); muS.setEntry(i, 0, Double.parseDouble(line[1])); sigmaX.setEntry(i, 0, Double.parseDouble(line[1])); sigmaS.setEntry(i, 0, Double.parseDouble(line[1])); nuX.setEntry(i, 0, Double.parseDouble(line[1])); nuS.setEntry(i, 0, Double.parseDouble(line[1])); tauX.setEntry(i, 0, Double.parseDouble(line[1])); tauS.setEntry(i, 0, Double.parseDouble(line[1])); } Hashtable<Integer, BlockRealMatrix> designMatrices = new Hashtable<Integer, BlockRealMatrix>(); designMatrices.put(DistributionSettings.MU, muX); designMatrices.put(DistributionSettings.SIGMA, sigmaX); designMatrices.put(DistributionSettings.NU, nuX); designMatrices.put(DistributionSettings.TAU, tauX); HashMap<Integer, BlockRealMatrix> smoothMatrices = new HashMap<Integer, BlockRealMatrix>(); smoothMatrices.put(DistributionSettings.MU, muS); smoothMatrices.put(DistributionSettings.SIGMA, sigmaS); smoothMatrices.put(DistributionSettings.NU, nuS); smoothMatrices.put(DistributionSettings.TAU, tauS); //smoothMatrices.put(DistributionSettings.MU, null); //smoothMatrices.put(DistributionSettings.SIGMA, null); //smoothMatrices.put(DistributionSettings.NU, null); //smoothMatrices.put(DistributionSettings.TAU, null); DistributionSettings.DISTR = DistributionSettings.SST; Controls.GLOB_DEVIANCE_TOL = 5500; Controls.INTER = 50;//only for the PB smoother Controls.SMOOTHER = Controls.PB; //or PB Controls.IS_SVD = true; Controls.BIG_DATA = true; Controls.JAVA_OPTIMIZATION = false; Controls.GAMLSS_NUM_CYCLES = 50; //Gamlss gamlss = new Gamlss(y, designMatrices, null); Gamlss gamlss = new Gamlss(y, designMatrices, null); //Gamlss gamlss = new Gamlss(y, null, smoothMatrices); gamlss.saveFittedDistributionParameters("Data/oilresults.csv"); }
From source file:edu.oregonstate.eecs.mcplan.ml.ConstrainedKMeans.java
/** * @param args/*from w w w . jav a 2 s .co m*/ */ public static void main(final String[] args) { final RandomGenerator rng = new MersenneTwister(42); final int K = 2; final int d = 2; final ArrayList<RealVector> X = new ArrayList<RealVector>(); final double u = 2.0; final double ell = 8.0; final double gamma = 10.0; for (final int s : new int[] { 0, 5, 10 }) { for (int x = -1; x <= 1; ++x) { for (int y = -1; y <= 1; ++y) { X.add(new ArrayRealVector(new double[] { x + s, y })); } } } final TIntObjectMap<Pair<int[], double[]>> M = new TIntObjectHashMap<Pair<int[], double[]>>(); M.put(16, Pair.makePair(new int[] { 20 }, new double[] { 1.0 })); M.put(0, Pair.makePair(new int[] { 8 }, new double[] { 1.0 })); final TIntObjectMap<Pair<int[], double[]>> C = new TIntObjectHashMap<Pair<int[], double[]>>(); C.put(13, Pair.makePair(new int[] { 20 }, new double[] { 1.0 })); C.put(10, Pair.makePair(new int[] { 16 }, new double[] { 1.0 })); final ArrayList<double[]> S = new ArrayList<double[]>(); M.forEachKey(new TIntProcedure() { @Override public boolean execute(final int i) { final Pair<int[], double[]> p = M.get(i); if (p != null) { for (final int j : p.first) { S.add(new double[] { i, j }); } } return true; } }); final ArrayList<double[]> D = new ArrayList<double[]>(); C.forEachKey(new TIntProcedure() { @Override public boolean execute(final int i) { final Pair<int[], double[]> p = C.get(i); if (p != null) { for (final int j : p.first) { D.add(new double[] { i, j }); } } return true; } }); final ConstrainedKMeans kmeans = new ConstrainedKMeans(K, d, X, M, C, rng) { RealMatrix A_ = MatrixUtils.createRealIdentityMatrix(d); double Dmax_ = 1.0; @Override public double distance(final RealVector x1, final RealVector x2) { final RealVector diff = x1.subtract(x2); return Math.sqrt(HilbertSpace.inner_prod(diff, A_, diff)); } @Override public double distanceMax() { return Dmax_; } @Override protected void initializeDistanceFunction() { double max_distance = 0.0; for (int i = 0; i < X.size(); ++i) { for (int j = i + 1; j < X.size(); ++j) { final double d = distance(X.get(i), X.get(j)); if (d > max_distance) { max_distance = d; } } } Dmax_ = max_distance; } @Override protected boolean updateDistanceFunction() { final InformationTheoreticMetricLearner itml = new InformationTheoreticMetricLearner(S, D, u, ell, A_, gamma, rng_); itml.run(); final double delta = A_.subtract(itml.A()).getFrobeniusNorm(); A_ = itml.A(); initializeDistanceFunction(); return delta > convergence_tolerance_; } }; kmeans.run(); for (int i = 0; i < kmeans.mu().length; ++i) { System.out.println("Mu " + i + ": " + kmeans.mu()[i]); for (int j = 0; j < kmeans.assignments().length; ++j) { if (kmeans.assignments()[j] == i) { System.out.println("\tPoint " + X.get(j)); } } } }
From source file:edu.oregonstate.eecs.mcplan.ml.InformationTheoreticMetricLearner.java
/** * @param args//from w ww.ja v a2s . c o m */ public static void main(final String[] args) { final RandomGenerator rng = new MersenneTwister(42); final int d = 2; final double u = 5.0; final double ell = 7.0; final double gamma = 1.0; final ArrayList<RealVector> X = new ArrayList<RealVector>(); final RealMatrix A0 = MatrixUtils.createRealIdentityMatrix(d); for (final int w : new int[] { 0, 5 }) { for (final int h : new int[] { 0, 50 }) { for (int x = -1; x <= 1; ++x) { for (int y = -1; y <= 1; ++y) { X.add(new ArrayRealVector(new double[] { x + w, y + h })); } } } } final ArrayList<int[]> S = new ArrayList<int[]>(); S.add(new int[] { 4, 12 }); // Must link diagonally S.add(new int[] { 21, 31 }); final ArrayList<double[]> Sd = new ArrayList<double[]>(); for (final int[] s : S) { final double[] a = X.get(s[0]).subtract(X.get(s[1])).toArray(); Sd.add(a); } final ArrayList<int[]> D = new ArrayList<int[]>(); D.add(new int[] { 5, 23 }); D.add(new int[] { 13, 32 }); // Cannot link vertically final ArrayList<double[]> Dd = new ArrayList<double[]>(); for (final int[] dd : D) { final double[] a = X.get(dd[0]).subtract(X.get(dd[1])).toArray(); Dd.add(a); } final InformationTheoreticMetricLearner itml = new InformationTheoreticMetricLearner(Sd, Dd, u, ell, A0, gamma, rng); itml.run(); final RealMatrix A = itml.A(); System.out.println(A0.toString()); for (final int[] c : S) { final RealVector diff = X.get(c[0]).subtract(X.get(c[1])); System.out.println(diff.dotProduct(A0.operate(diff))); } for (final int[] c : D) { final RealVector diff = X.get(c[0]).subtract(X.get(c[1])); System.out.println(diff.dotProduct(A0.operate(diff))); } System.out.println(A.toString()); for (final int[] c : S) { final RealVector diff = X.get(c[0]).subtract(X.get(c[1])); System.out.println(diff.dotProduct(A.operate(diff))); } for (final int[] c : D) { final RealVector diff = X.get(c[0]).subtract(X.get(c[1])); System.out.println(diff.dotProduct(A.operate(diff))); } // int i = 0; // for( final int w : new int[] { 0, 5 } ) { // for( final int h : new int[] { 0, 5 } ) { // for( int x = -1; x <= 1; ++x ) { // for( int y = -1; y <= 1; ++y ) { // System.out.println( itml.A().operate( X.get( i++ ) ) ); // } // } // } // } }
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();/*from ww w . j a v a 2s. c om*/ 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:edu.oregonstate.eecs.mcplan.ml.KulisLowRankKernelLearner.java
/** * @param args/* w w w . j a v a 2s. c om*/ */ public static void main(final String[] args) { final RandomGenerator rng = new MersenneTwister(42); final int d = 2; final double u = 5.0; final double ell = 7.0; final double gamma = 1.0; final ArrayList<RealVector> X = new ArrayList<RealVector>(); final RealMatrix A0 = MatrixUtils.createRealIdentityMatrix(d); for (final int w : new int[] { 0, 5 }) { for (final int h : new int[] { 0, 5 }) { for (int x = -1; x <= 1; ++x) { for (int y = -1; y <= 1; ++y) { X.add(new ArrayRealVector(new double[] { x + w, y + h })); } } } } final ArrayList<int[]> S = new ArrayList<int[]>(); S.add(new int[] { 4, 31 }); // Must link diagonally final ArrayList<int[]> D = new ArrayList<int[]>(); D.add(new int[] { 4, 13 }); D.add(new int[] { 22, 31 }); D.add(new int[] { 13, 22 }); // Cannot link vertically final KulisLowRankKernelLearner itml = new KulisLowRankKernelLearner(X, S, D, u, ell, A0, gamma, rng); itml.run(); final RealMatrix A = itml.A(); System.out.println(A0.toString()); for (final int[] c : S) { final RealVector diff = X.get(c[0]).subtract(X.get(c[1])); System.out.println(diff.dotProduct(A0.operate(diff))); } for (final int[] c : D) { final RealVector diff = X.get(c[0]).subtract(X.get(c[1])); System.out.println(diff.dotProduct(A0.operate(diff))); } System.out.println(A.toString()); for (final int[] c : S) { final RealVector diff = X.get(c[0]).subtract(X.get(c[1])); System.out.println(diff.dotProduct(A.operate(diff))); } for (final int[] c : D) { final RealVector diff = X.get(c[0]).subtract(X.get(c[1])); System.out.println(diff.dotProduct(A.operate(diff))); } // int i = 0; // for( final int w : new int[] { 0, 5 } ) { // for( final int h : new int[] { 0, 5 } ) { // for( int x = -1; x <= 1; ++x ) { // for( int y = -1; y <= 1; ++y ) { // System.out.println( itml.A().operate( X.get( i++ ) ) ); // } // } // } // } }
From source file:edu.oregonstate.eecs.mcplan.ml.LinearDiscriminantAnalysis.java
public static void main(final String[] args) throws FileNotFoundException { final File root = new File("test/LinearDiscriminantAnalysis"); root.mkdirs();/*from w w w . j a v a 2 s .c om*/ final int seed = 42; final int N = 30; final double shrinkage = 1e-6; final RandomGenerator rng = new MersenneTwister(seed); final Pair<ArrayList<double[]>, int[]> dataset = Datasets.twoVerticalGaussian2D(rng, N); final ArrayList<double[]> data = dataset.first; final int[] label = dataset.second; final int Nlabels = 2; final int[] shuffle_idx = Fn.linspace(0, Nlabels * N); Fn.shuffle(rng, shuffle_idx); final ArrayList<double[]> shuffled = new ArrayList<double[]>(); final int[] shuffled_label = new int[label.length]; for (int i = 0; i < data.size(); ++i) { shuffled.add(Fn.copy(data.get(shuffle_idx[i]))); shuffled_label[i] = label[shuffle_idx[i]]; } final Csv.Writer data_writer = new Csv.Writer(new PrintStream(new File(root, "data.csv"))); for (final double[] v : data) { for (int i = 0; i < v.length; ++i) { data_writer.cell(v[i]); } data_writer.newline(); } data_writer.close(); System.out.println("[Training]"); // final KernelPrincipalComponentsAnalysis<RealVector> kpca // = new KernelPrincipalComponentsAnalysis<RealVector>( shuffled, new RadialBasisFunctionKernel( 0.5 ), 1e-6 ); final LinearDiscriminantAnalysis lda = new LinearDiscriminantAnalysis(shuffled, shuffled_label, Nlabels, shrinkage); System.out.println("[Finished]"); for (final RealVector ev : lda.eigenvectors) { System.out.println(ev); } System.out.println("Transformed data:"); final LinearDiscriminantAnalysis.Transformer transformer = lda.makeTransformer(); final Csv.Writer transformed_writer = new Csv.Writer(new PrintStream(new File(root, "transformed.csv"))); for (final double[] u : data) { final RealVector uvec = new ArrayRealVector(u); System.out.println(uvec); final RealVector v = transformer.transform(uvec); System.out.println("-> " + v); for (int i = 0; i < v.getDimension(); ++i) { transformed_writer.cell(v.getEntry(i)); } transformed_writer.newline(); } transformed_writer.close(); }