List of usage examples for org.apache.commons.math3.linear ArrayRealVector ArrayRealVector
public ArrayRealVector(ArrayRealVector v) throws NullArgumentException
From source file:com.itemanalysis.psychometrics.optimization.BOBYQAOptimizer.java
/** * The arrays BMAT and ZMAT are updated, as required by the new position * of the interpolation point that has the index KNEW. The vector VLAG has * N+NPT components, set on entry to the first NPT and last N components * of the product Hw in equation (4.11) of the Powell (2006) paper on * NEWUOA. Further, BETA is set on entry to the value of the parameter * with that name, and DENOM is set to the denominator of the updating * formula. Elements of ZMAT may be treated as zero if their moduli are * at most ZTEST. The first NDIM elements of W are used for working space. * @param beta//w ww .j a v a2 s .c om * @param denom * @param knew */ private void update(double beta, double denom, int knew) { printMethod(); // XXX final int n = currentBest.getDimension(); final int npt = numberOfInterpolationPoints; final int nptm = npt - n - 1; // XXX Should probably be split into two arrays. final ArrayRealVector work = new ArrayRealVector(npt + n); double ztest = ZERO; for (int k = 0; k < npt; k++) { for (int j = 0; j < nptm; j++) { // Computing MAX ztest = Math.max(ztest, Math.abs(zMatrix.getEntry(k, j))); } } ztest *= 1e-20; // Apply the rotations that put zeros in the KNEW-th row of ZMAT. for (int j = 1; j < nptm; j++) { final double d1 = zMatrix.getEntry(knew, j); if (Math.abs(d1) > ztest) { // Computing 2nd power final double d2 = zMatrix.getEntry(knew, 0); // Computing 2nd power final double d3 = zMatrix.getEntry(knew, j); final double d4 = Math.sqrt(d2 * d2 + d3 * d3); final double d5 = zMatrix.getEntry(knew, 0) / d4; final double d6 = zMatrix.getEntry(knew, j) / d4; for (int i = 0; i < npt; i++) { final double d7 = d5 * zMatrix.getEntry(i, 0) + d6 * zMatrix.getEntry(i, j); zMatrix.setEntry(i, j, d5 * zMatrix.getEntry(i, j) - d6 * zMatrix.getEntry(i, 0)); zMatrix.setEntry(i, 0, d7); } } zMatrix.setEntry(knew, j, ZERO); } // Put the first NPT components of the KNEW-th column of HLAG into W, // and calculate the parameters of the updating formula. for (int i = 0; i < npt; i++) { work.setEntry(i, zMatrix.getEntry(knew, 0) * zMatrix.getEntry(i, 0)); } final double alpha = work.getEntry(knew); final double tau = lagrangeValuesAtNewPoint.getEntry(knew); lagrangeValuesAtNewPoint.setEntry(knew, lagrangeValuesAtNewPoint.getEntry(knew) - ONE); // Complete the updating of ZMAT. final double sqrtDenom = Math.sqrt(denom); final double d1 = tau / sqrtDenom; final double d2 = zMatrix.getEntry(knew, 0) / sqrtDenom; for (int i = 0; i < npt; i++) { zMatrix.setEntry(i, 0, d1 * zMatrix.getEntry(i, 0) - d2 * lagrangeValuesAtNewPoint.getEntry(i)); } // Finally, update the matrix BMAT. for (int j = 0; j < n; j++) { final int jp = npt + j; work.setEntry(jp, bMatrix.getEntry(knew, j)); final double d3 = (alpha * lagrangeValuesAtNewPoint.getEntry(jp) - tau * work.getEntry(jp)) / denom; final double d4 = (-beta * work.getEntry(jp) - tau * lagrangeValuesAtNewPoint.getEntry(jp)) / denom; for (int i = 0; i <= jp; i++) { bMatrix.setEntry(i, j, bMatrix.getEntry(i, j) + d3 * lagrangeValuesAtNewPoint.getEntry(i) + d4 * work.getEntry(i)); if (i >= npt) { bMatrix.setEntry(jp, (i - npt), bMatrix.getEntry(i, j)); } } } }
From source file:com.itemanalysis.psychometrics.optimization.BOBYQAOptimizer.java
/** * Performs validity checks./* ww w .ja v a 2 s . c o m*/ * * @param lowerBound Lower bounds (constraints) of the objective variables. * @param upperBound Upperer bounds (constraints) of the objective variables. */ private void setup(double[] lowerBound, double[] upperBound) { printMethod(); // XXX double[] init = getStartPoint(); final int dimension = init.length; // Check problem dimension. if (dimension < MINIMUM_PROBLEM_DIMENSION) { throw new NumberIsTooSmallException(dimension, MINIMUM_PROBLEM_DIMENSION, true); } // Check number of interpolation points. final int[] nPointsInterval = { dimension + 2, (dimension + 2) * (dimension + 1) / 2 }; if (numberOfInterpolationPoints < nPointsInterval[0] || numberOfInterpolationPoints > nPointsInterval[1]) { throw new OutOfRangeException(LocalizedFormats.NUMBER_OF_INTERPOLATION_POINTS, numberOfInterpolationPoints, nPointsInterval[0], nPointsInterval[1]); } // Initialize bound differences. boundDifference = new double[dimension]; double requiredMinDiff = 2 * initialTrustRegionRadius; double minDiff = Double.POSITIVE_INFINITY; for (int i = 0; i < dimension; i++) { boundDifference[i] = upperBound[i] - lowerBound[i]; minDiff = Math.min(minDiff, boundDifference[i]); } if (minDiff < requiredMinDiff) { initialTrustRegionRadius = minDiff / 3.0; } // Initialize the data structures used by the "bobyqa" method. bMatrix = new Array2DRowRealMatrix(dimension + numberOfInterpolationPoints, dimension); zMatrix = new Array2DRowRealMatrix(numberOfInterpolationPoints, numberOfInterpolationPoints - dimension - 1); interpolationPoints = new Array2DRowRealMatrix(numberOfInterpolationPoints, dimension); originShift = new ArrayRealVector(dimension); fAtInterpolationPoints = new ArrayRealVector(numberOfInterpolationPoints); trustRegionCenterOffset = new ArrayRealVector(dimension); gradientAtTrustRegionCenter = new ArrayRealVector(dimension); lowerDifference = new ArrayRealVector(dimension); upperDifference = new ArrayRealVector(dimension); modelSecondDerivativesParameters = new ArrayRealVector(numberOfInterpolationPoints); newPoint = new ArrayRealVector(dimension); alternativeNewPoint = new ArrayRealVector(dimension); trialStepPoint = new ArrayRealVector(dimension); lagrangeValuesAtNewPoint = new ArrayRealVector(dimension + numberOfInterpolationPoints); modelSecondDerivativesValues = new ArrayRealVector(dimension * (dimension + 1) / 2); }
From source file:objenome.util.Utils.java
public static ArrayRealVector gradient(DiffableFunction f, Scalar[] parameters, ArrayRealVector result) { if (result == null) result = new ArrayRealVector(parameters.length); double[] d = result.getDataRef(); for (int i = 0; i < parameters.length; ++i) { d[i] = f.partialDerive(parameters[i]); }// www . j a v a 2 s . c o m return result; }
From source file:org.apache.eagle.security.userprofile.model.eigen.UserProfileEigenModeler.java
@Override public List<UserProfileEigenModel> generate(String site, String user, RealMatrix matrix) { LOG.info(String.format("Receive aggregated user activity matrix: %s size: %s x %s", user, matrix.getRowDimension(), matrix.getColumnDimension())); computeStats(matrix);//from w w w . j a v a 2s. c o m RealMatrix normalizedInputMatrix = normalizeData(matrix); int lowVariantColumnCount = 0; for (int j = 0; j < normalizedInputMatrix.getColumnDimension(); j++) { if (statistics[j].isLowVariant()) { lowVariantColumnCount++; } } if (normalizedInputMatrix.getColumnDimension() == lowVariantColumnCount) { LOG.info("found user: " + user + " with all features being low variant. Nothing to do..."); UserProfileEigenModel noopModel = new UserProfileEigenModel(System.currentTimeMillis(), site, user, null, null, 0, null, null, null, null, null, statistics); return Arrays.asList(noopModel); } else { computeCovarianceAndSVD(normalizedInputMatrix, lowVariantColumnCount); computeDimensionWithMaxVariance(); computePrincipalComponents(); maximumL2Norm = new ArrayRealVector(principalComponents.length); minimumL2Norm = new ArrayRealVector(principalComponents.length); for (int i = 0; i < principalComponents.length; i++) { RealMatrix trainingDataTranspose = computeMaxDistanceOnPCs(i); } UserProfileEigenModel userprofileEigenModel = new UserProfileEigenModel(System.currentTimeMillis(), site, user, uMatrix, diagonalMatrix, dimension, minVector, maxVector, principalComponents, maximumL2Norm, minimumL2Norm, statistics); return Arrays.asList(userprofileEigenModel); } }
From source file:org.apache.eagle.security.userprofile.TestUserProfileAnomalyEigenEvaluator.java
@Test public void testDetect() { UserProfileAnomalyEigenEvaluator eigenEvaluator = new UserProfileAnomalyEigenEvaluator(); String[] testCmdType = { "getfileinfo", "open", "listStatus", "setTimes", "setPermission", "rename", "mkdirs", "create", "setReplication", "contentSummary", "delete", "setOwner", "fsck" }; List<String> tmpCmdTypesAsList = new ArrayList<String>(); tmpCmdTypesAsList = Arrays.asList(testCmdType); Seq<String> testCmd = JavaConversions.asScalaBuffer(tmpCmdTypesAsList); String testSite = "sandbox"; long testTimestamp = 14054440; String testUser = "test_user"; RealMatrix testMatrix = new Array2DRowRealMatrix(1, testCmdType.length); for (int i = 0; i < testMatrix.getColumnDimension(); i++) testMatrix.addToEntry(0, i, 3.0); UserActivityAggModel testAggModel = new UserActivityAggModel(testUser, testMatrix, testCmd, testSite, testTimestamp);//from w w w .j a v a 2s .co m Long testVersion = new Long(1); RealMatrix testUMat = new Array2DRowRealMatrix(testCmdType.length, testCmdType.length); RealMatrix testDiagonalMat = new Array2DRowRealMatrix(testCmdType.length, testCmdType.length); for (int i = 0; i < testCmdType.length; i++) { for (int j = 0; j < testCmdType.length; j++) { testUMat.addToEntry(i, j, 1.0); testDiagonalMat.addToEntry(i, j, 1.0); } } int dimension = testCmdType.length - 1; double[] minVector = new double[testCmdType.length]; double[] maxVector = new double[testCmdType.length]; for (int i = 0; i < minVector.length; i++) { minVector[i] = 1; maxVector[i] = 1; } RealVector testMinVec = new ArrayRealVector(minVector); RealVector testMaxVec = new ArrayRealVector(maxVector); RealVector[] testPCs = new ArrayRealVector[testCmdType.length]; for (int i = 0; i < testCmdType.length; i++) { testPCs[i] = new ArrayRealVector(testCmdType.length); for (int j = 0; j < testPCs[i].getDimension(); j++) { testPCs[i].addToEntry(j, 1.0); } } RealVector testMaxL2Norm = new ArrayRealVector(maxVector); RealVector testMinL2Norm = new ArrayRealVector(minVector); UserCommandStatistics userCommandStatistics[] = new UserCommandStatistics[testCmdType.length]; for (int i = 0; i < testCmdType.length; i++) { userCommandStatistics[i] = new UserCommandStatistics(); userCommandStatistics[i].setCommandName(testCmdType[i]); userCommandStatistics[i].setLowVariant(false); userCommandStatistics[i].setMean(1.0); userCommandStatistics[i].setStddev(1.0); } UserProfileEigenModel testEigenModel = new UserProfileEigenModel(testVersion, testSite, testUser, testUMat, testDiagonalMat, dimension, testMinVec, testMaxVec, testPCs, testMaxL2Norm, testMinL2Norm, userCommandStatistics); List<MLCallbackResult> testResults = eigenEvaluator.detect("test_user", "eigen", testAggModel, testEigenModel); Assert.assertEquals(testResults.size(), testMatrix.getRowDimension()); for (MLCallbackResult result : testResults) { Assert.assertEquals(result.isAnomaly(), true); } }
From source file:org.apache.predictionio.examples.java.recommendations.tutorial1.Algorithm.java
@Override public Model train(TrainingData data) { // pre-process Map<Integer, Map<Integer, Float>> itemMap = new HashMap<Integer, Map<Integer, Float>>(); Map<Integer, Integer> userIndexMap = new HashMap<Integer, Integer>(); Map<Integer, Integer> itemIndexMap = new HashMap<Integer, Integer>(); int itemIndex = 0; int userIndex = 0; for (TrainingData.Rating r : data.ratings) { Map<Integer, Float> userRating = itemMap.get(r.iid); if (userRating == null) { // new item userRating = new HashMap<Integer, Float>(); itemMap.put(r.iid, userRating); itemIndexMap.put(r.iid, itemIndex); itemIndex += 1; // increment item index for next item }/* w w w. j a va 2s.c om*/ userRating.put(r.uid, r.rating); // update user index Integer u = userIndexMap.get(r.uid); if (u == null) { // new user userIndexMap.put(r.uid, userIndex); userIndex += 1; } } int numOfItems = itemIndexMap.size(); int numOfUsers = userIndexMap.size(); Map<Integer, RealVector> itemVectors = new HashMap<Integer, RealVector>(); Map<Integer, RealVector> userHistory = new HashMap<Integer, RealVector>(); for (Map.Entry<Integer, Map<Integer, Float>> entry : itemMap.entrySet()) { Integer itemID = entry.getKey(); Integer iindex = itemIndexMap.get(itemID); Map<Integer, Float> userRatingMap = entry.getValue(); RealVector item = new ArrayRealVector(numOfUsers); // dimension is numOfUsers for (Map.Entry<Integer, Float> r : userRatingMap.entrySet()) { Integer userID = r.getKey(); Float rating = r.getValue(); Integer uindex = userIndexMap.get(userID); item.setEntry(uindex, rating); // update user History RealVector user = userHistory.get(userID); if (user == null) { user = new OpenMapRealVector(numOfItems); userHistory.put(userID, user); } user.setEntry(iindex, rating); } itemVectors.put(itemID, item); } // calculate sim Map<Integer, RealVector> itemSimilarity = new HashMap<Integer, RealVector>(); List<Integer> item1List = new ArrayList<Integer>(itemIndexMap.keySet()); List<Integer> item2List = new ArrayList<Integer>(item1List); int numSimilarItems = 100; Comparator<IndexAndScore> comparator = new IndexAndScoreComparator(); Map<Integer, Queue<IndexAndScore>> topItemSimilarity = new HashMap<Integer, Queue<IndexAndScore>>(); for (Integer itemID1 : item1List) { item2List.remove(0); Integer index1 = itemIndexMap.get(itemID1); for (Integer itemID2 : item2List) { RealVector vector1 = itemVectors.get(itemID1); RealVector vector2 = itemVectors.get(itemID2); double score = vector1.cosine(vector2); if (score > params.threshold) { Integer index2 = itemIndexMap.get(itemID2); setTopItemSimilarity(topItemSimilarity, itemID1, index2, score, numSimilarItems, comparator); setTopItemSimilarity(topItemSimilarity, itemID2, index1, score, numSimilarItems, comparator); } } } for (Map.Entry<Integer, Queue<IndexAndScore>> entry : topItemSimilarity.entrySet()) { Iterator<IndexAndScore> it = entry.getValue().iterator(); RealVector vector = new OpenMapRealVector(numOfItems); while (it.hasNext()) { IndexAndScore d = it.next(); vector.setEntry(d.index, d.score); } itemSimilarity.put(entry.getKey(), vector); } return new Model(itemSimilarity, userHistory); }
From source file:org.apache.predictionio.examples.java.recommendations.tutorial4.CollaborativeFilteringAlgorithm.java
@Override public CollaborativeFilteringModel train(PreparedData data) { // pre-process Map<Integer, Map<Integer, Float>> itemMap = new HashMap<Integer, Map<Integer, Float>>(); Map<Integer, Integer> userIndexMap = new HashMap<Integer, Integer>(); Map<Integer, Integer> itemIndexMap = new HashMap<Integer, Integer>(); int itemIndex = 0; int userIndex = 0; for (TrainingData.Rating r : data.ratings) { Map<Integer, Float> userRating = itemMap.get(r.iid); if (userRating == null) { // new item userRating = new HashMap<Integer, Float>(); itemMap.put(r.iid, userRating); itemIndexMap.put(r.iid, itemIndex); itemIndex += 1; // increment item index for next item }//from ww w. j ava2 s .c o m userRating.put(r.uid, r.rating); // update user index Integer u = userIndexMap.get(r.uid); if (u == null) { // new user userIndexMap.put(r.uid, userIndex); userIndex += 1; } } int numOfItems = itemIndexMap.size(); int numOfUsers = userIndexMap.size(); Map<Integer, RealVector> itemVectors = new HashMap<Integer, RealVector>(); Map<Integer, RealVector> userHistory = new HashMap<Integer, RealVector>(); for (Map.Entry<Integer, Map<Integer, Float>> entry : itemMap.entrySet()) { Integer itemID = entry.getKey(); Integer iindex = itemIndexMap.get(itemID); Map<Integer, Float> userRatingMap = entry.getValue(); RealVector item = new ArrayRealVector(numOfUsers); // dimension is numOfUsers for (Map.Entry<Integer, Float> r : userRatingMap.entrySet()) { Integer userID = r.getKey(); Float rating = r.getValue(); Integer uindex = userIndexMap.get(userID); item.setEntry(uindex, rating); // update user History RealVector user = userHistory.get(userID); if (user == null) { user = new OpenMapRealVector(numOfItems); userHistory.put(userID, user); } user.setEntry(iindex, rating); } itemVectors.put(itemID, item); } // calculate sim Map<Integer, RealVector> itemSimilarity = new HashMap<Integer, RealVector>(); List<Integer> item1List = new ArrayList<Integer>(itemIndexMap.keySet()); List<Integer> item2List = new ArrayList<Integer>(item1List); int numSimilarItems = 100; Comparator<IndexAndScore> comparator = new IndexAndScoreComparator(); Map<Integer, Queue<IndexAndScore>> topItemSimilarity = new HashMap<Integer, Queue<IndexAndScore>>(); for (Integer itemID1 : item1List) { item2List.remove(0); Integer index1 = itemIndexMap.get(itemID1); for (Integer itemID2 : item2List) { RealVector vector1 = itemVectors.get(itemID1); RealVector vector2 = itemVectors.get(itemID2); double score = vector1.cosine(vector2); if (score > params.threshold) { Integer index2 = itemIndexMap.get(itemID2); setTopItemSimilarity(topItemSimilarity, itemID1, index2, score, numSimilarItems, comparator); setTopItemSimilarity(topItemSimilarity, itemID2, index1, score, numSimilarItems, comparator); } } } for (Map.Entry<Integer, Queue<IndexAndScore>> entry : topItemSimilarity.entrySet()) { Iterator<IndexAndScore> it = entry.getValue().iterator(); RealVector vector = new OpenMapRealVector(numOfItems); while (it.hasNext()) { IndexAndScore d = it.next(); vector.setEntry(d.index, d.score); } itemSimilarity.put(entry.getKey(), vector); } return new CollaborativeFilteringModel(itemSimilarity, userHistory); }
From source file:org.apache.predictionio.examples.java.recommendations.tutorial4.FeatureBasedAlgorithm.java
public FeatureBasedModel train(PreparedData data) { Map<Integer, RealVector> userFeatures = new HashMap<Integer, RealVector>(); Map<Integer, Integer> userActions = new HashMap<Integer, Integer>(); for (Integer uid : data.userInfo.keySet()) { userFeatures.put(uid, new ArrayRealVector(data.featureCount)); userActions.put(uid, 0);//from w w w . j ava2 s .co m } for (TrainingData.Rating rating : data.ratings) { final int uid = rating.uid; final int iid = rating.iid; final double rate = rating.rating; // Skip features outside the range. if (!(params.min <= rate && rate <= params.max)) continue; final double actualRate = (rate - params.drift) * params.scale; final RealVector userFeature = userFeatures.get(uid); final RealVector itemFeature = data.itemFeatures.get(iid); userFeature.combineToSelf(1, actualRate, itemFeature); userActions.put(uid, userActions.get(uid) + 1); } // Normalize userFeatures by l-inf-norm for (Integer uid : userFeatures.keySet()) { final RealVector feature = userFeatures.get(uid); feature.mapDivideToSelf(feature.getLInfNorm()); } // Normalize itemFeatures by weight Map<Integer, RealVector> itemFeatures = new HashMap<Integer, RealVector>(); for (Integer iid : data.itemFeatures.keySet()) { final RealVector feature = data.itemFeatures.get(iid); final RealVector normalizedFeature = feature.mapDivide(feature.getL1Norm()); itemFeatures.put(iid, normalizedFeature); } return new FeatureBasedModel(userFeatures, userActions, itemFeatures); }
From source file:org.apache.predictionio.examples.java.recommendations.tutorial4.Preparator.java
public PreparedData prepare(TrainingData trainingData) { Map<Integer, RealVector> itemFeatures = new HashMap<Integer, RealVector>(); int featureSize = trainingData.genres.size(); for (Integer iid : trainingData.itemInfo.keySet()) { String[] info = trainingData.itemInfo.get(iid); RealVector features = new ArrayRealVector(featureSize); for (int i = 0; i < featureSize; i++) { features.setEntry(i, Double.parseDouble(info[i + indexOffset])); }/* w w w . j ava2s .c o m*/ itemFeatures.put(iid, features); } return new PreparedData(trainingData, itemFeatures, featureSize); }
From source file:org.apache.solr.client.solrj.io.eval.DotProductEvaluator.java
@Override public Object doWork(Object first, Object second) throws IOException { if (null == first) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the first value", toExpression(constructingFactory))); }/*from w ww.j av a 2 s . com*/ if (null == second) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the second value", toExpression(constructingFactory))); } if (!(first instanceof List<?>)) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a list of numbers", toExpression(constructingFactory), first.getClass().getSimpleName())); } if (!(second instanceof List<?>)) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a list of numbers", toExpression(constructingFactory), first.getClass().getSimpleName())); } RealVector v = new ArrayRealVector( ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()); RealVector v2 = new ArrayRealVector( ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()); return v.dotProduct(v2); }