List of usage examples for org.apache.commons.lang ArrayUtils reverse
public static void reverse(boolean[] array)
Reverses the order of the given array.
From source file:net.firejack.platform.installation.processor.TemplateApplicationProcessor.java
@Override public void onApplicationEvent(TemplateEvent event) { if (!enabled) return;// www . j a v a 2 s . c o m if (event instanceof PrepareTemplateEvent) { File packageXml = FileUtils.getResource("dbupdate", "gateway", "package.xml"); File resourceZip = FileUtils.getResource("dbupdate", "gateway", "resource.zip"); if (packageXml.exists()) { StatusProviderTranslationResult translate = null; try { InputStream packageStream = FileUtils.openInputStream(packageXml); if (event.isModify()) { packageStream = modify(packageStream, event); } InputStream resourceStream = FileUtils.openInputStream(resourceZip); translate = packageInstallationService.activatePackage(packageStream, resourceStream); IOUtils.closeQuietly(packageStream); IOUtils.closeQuietly(resourceStream); } catch (IOException e) { logger.error(e, e); } if (translate != null) { PackageModel packageModel = translate.getPackage(); SystemModel systemModel = systemStore.findByLookup(OpenFlame.SYSTEM); String serverName = systemModel.getServerName(); String[] domainUrlParts = serverName.split("\\."); if (!serverName.equals("localhost") && domainUrlParts.length != 1 && !serverName.matches( "^(\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b)$")) { ArrayUtils.reverse(domainUrlParts); String rootDomainLookup = domainUrlParts[0] + "." + domainUrlParts[1]; RootDomainModel rootDomain = rootDomainStore.findByLookup(rootDomainLookup); packageModel.setParent(rootDomain); } DatabaseModel database = databaseStore.findByLookup(OpenFlame.DATABASE); database.setId(null); database.setName("gateway"); database.setUrlPath("gateway"); database.setHash(null); database.setUid(null); databaseStore.save(database); packageModel.setDatabase(database); packageStore.associate(packageModel, systemModel); RoleModel adminRole = roleStore.findByLookup(OpenFlame.ROLE_ADMIN); List<UserModel> users = userStore.findByRole(adminRole.getId()); String admin = DiffUtils.lookup(packageModel.getLookup(), "admin"); RoleModel role = roleStore.findByLookup(admin); if (role != null) { for (UserModel user : users) { UserRoleModel userRoleModel = new UserRoleModel(user, role); user.getUserRoles().add(userRoleModel); userStore.saveOrUpdate(user); } } this.packageModel.set(packageModel); } } } else if (event instanceof BuildTemplateEvent && packageModel.get() != null) { PackageModel packageModel = this.packageModel.get(); generateWarBroker.execute( new ServiceRequest<SimpleIdentifier<Long>>(new SimpleIdentifier<Long>(packageModel.getId()))); String name = packageModel.getUrlPath(); if (packageModel.getUrlPath() == null || packageModel.getUrlPath().isEmpty()) name = "ROOT"; String war = name + PackageFileType.APP_WAR.getDotExtension(); String file = packageModel.getName() + PackageFileType.APP_WAR.getDotExtension(); Database database = factory.convertTo(Database.class, packageModel.getDatabase()); databaseManager.createDatabase(database); NamedValues values = new NamedValues(); values.put("packageId", packageModel.getId()); values.put("name", war); values.put("file", file); deployBroker.execute(new ServiceRequest<NamedValues>(values)); this.packageModel.remove(); } }
From source file:com.ejwa.dinja.demo.model.RubiksCube.java
@SuppressWarnings("PMD.DataflowAnomalyAnalysis") private boolean setSlice(int[][] indices, Vector3f pRotation, Vector3f nRotation, BoxSection... boxSections) { final BoxSection[] b = new BoxSection[indices[0].length]; for (int i = 0; i < indices.length; i++) { sliceToRoll = indices[i];/*from w w w .ja v a 2 s. c om*/ for (int j = 0; j < indices[i].length; j++) { b[j] = this.boxSections.get(indices[i][j]); } if (ArrayHelper.indexOfSubArrayChunkWrapped(b, boxSections, 2) == -1) { ArrayUtils.reverse(b); if (ArrayHelper.indexOfSubArrayChunkWrapped(b, boxSections, 2) != -1) { rotationSlice.set(pRotation, Arrays.asList(b)); return true; } } else { rotationSlice.set(nRotation, Arrays.asList(b)); return true; } } return false; }
From source file:com.opengamma.financial.analytics.model.volatility.surface.HestonFourierIRFutureSurfaceFittingFunction.java
@Override public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final ValueRequirement desiredValue = desiredValues.iterator().next(); //currency//from w w w.j a v a2s . com final Currency currency = Currency.of(((UniqueId) target.getValue()).getValue()); // future curve final Object objectFuturePriceData = inputs.getValue(ValueRequirementNames.FUTURE_PRICE_CURVE_DATA); if (objectFuturePriceData == null) { throw new OpenGammaRuntimeException("Could not get futures price data"); } final NodalDoublesCurve futurePriceData = (NodalDoublesCurve) objectFuturePriceData; // surface final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE); final Object objectSurfaceData = inputs.getValue(ValueRequirementNames.STANDARD_VOLATILITY_SURFACE_DATA); if (objectSurfaceData == null) { throw new OpenGammaRuntimeException("Could not get volatility surface data"); } @SuppressWarnings("unchecked") final VolatilitySurfaceData<Double, Double> volatilitySurfaceData = (VolatilitySurfaceData<Double, Double>) objectSurfaceData; //assumes that the sorting is first x, then y if (volatilitySurfaceData.size() == 0) { throw new OpenGammaRuntimeException("Interest rate future option volatility surface definition name=" + futurePriceData.getName() + " contains no data"); } final SortedSet<Double> x = volatilitySurfaceData.getUniqueXValues(); final DoubleArrayList fittedOptionExpiryList = new DoubleArrayList(); final DoubleArrayList futureDelayList = new DoubleArrayList(); final DoubleArrayList kappaList = new DoubleArrayList(); final DoubleArrayList thetaList = new DoubleArrayList(); final DoubleArrayList vol0List = new DoubleArrayList(); final DoubleArrayList omegaList = new DoubleArrayList(); final DoubleArrayList rhoList = new DoubleArrayList(); final DoubleArrayList chiSqList = new DoubleArrayList(); final Map<DoublesPair, DoubleMatrix2D> inverseJacobians = new HashMap<DoublesPair, DoubleMatrix2D>(); for (final Double t : x) { final List<ObjectsPair<Double, Double>> strip = volatilitySurfaceData.getYValuesForX(t); final int n = strip.size(); final DoubleArrayList strikesList = new DoubleArrayList(n); final DoubleArrayList sigmaList = new DoubleArrayList(n); final DoubleArrayList errorsList = new DoubleArrayList(n); final Double futurePrice = futurePriceData.getYValue(t); if (strip.size() > 4 && futurePrice != null) { final double forward = 1 - futurePrice / 100; for (final ObjectsPair<Double, Double> value : strip) { if (value.first != null && value.second != null) { strikesList.add(1 - value.first / 100); sigmaList.add(value.second); errorsList.add(ERROR); } } if (!strikesList.isEmpty()) { final double[] strikes = strikesList.toDoubleArray(); final double[] sigma = sigmaList.toDoubleArray(); final double[] errors = errorsList.toDoubleArray(); ArrayUtils.reverse(strikes); ArrayUtils.reverse(sigma); ArrayUtils.reverse(errors); final LeastSquareResultsWithTransform fittedResult = new HestonModelFitter(forward, strikes, t, sigma, errors, HESTON_FUNCTION).solve(HESTON_INITIAL_VALUES); final DoubleMatrix1D parameters = fittedResult.getModelParameters(); fittedOptionExpiryList.add(t); futureDelayList.add(0); kappaList.add(parameters.getEntry(0)); thetaList.add(parameters.getEntry(1)); vol0List.add(parameters.getEntry(2)); omegaList.add(parameters.getEntry(3)); rhoList.add(parameters.getEntry(4)); inverseJacobians.put(DoublesPair.of(t.doubleValue(), 0.), fittedResult.getModelParameterSensitivityToData()); chiSqList.add(fittedResult.getChiSq()); } } } if (fittedOptionExpiryList.size() < 5) { //don't have sufficient fits to construct a surface throw new OpenGammaRuntimeException( "Could not construct Heston parameter surfaces; have under 5 surface points"); } final double[] fittedOptionExpiry = fittedOptionExpiryList.toDoubleArray(); final double[] futureDelay = futureDelayList.toDoubleArray(); final double[] kappa = kappaList.toDoubleArray(); final double[] theta = thetaList.toDoubleArray(); final double[] vol0 = vol0List.toDoubleArray(); final double[] omega = omegaList.toDoubleArray(); final double[] rho = rhoList.toDoubleArray(); final InterpolatedDoublesSurface kappaSurface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, kappa, INTERPOLATOR, "Heston kappa surface"); final InterpolatedDoublesSurface thetaSurface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, theta, INTERPOLATOR, "Heston theta surface"); final InterpolatedDoublesSurface vol0Surface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, vol0, INTERPOLATOR, "Heston vol0 surface"); final InterpolatedDoublesSurface omegaSurface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, omega, INTERPOLATOR, "Heston omega surface"); final InterpolatedDoublesSurface rhoSurface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, rho, INTERPOLATOR, "Heston rho surface"); final HestonFittedSurfaces fittedSurfaces = new HestonFittedSurfaces(kappaSurface, thetaSurface, vol0Surface, omegaSurface, rhoSurface, inverseJacobians, currency); final ValueProperties resultProperties = createValueProperties() .with(ValuePropertyNames.CURRENCY, currency.getCode()).with(ValuePropertyNames.SURFACE, surfaceName) .with(InstrumentTypeProperties.PROPERTY_SURFACE_INSTRUMENT_TYPE, InstrumentTypeProperties.IR_FUTURE_OPTION) .get(); final ValueSpecification resultSpecification = new ValueSpecification(ValueRequirementNames.HESTON_SURFACES, target.toSpecification(), resultProperties); return Sets.newHashSet(new ComputedValue(resultSpecification, fittedSurfaces)); }
From source file:net.firejack.platform.installation.processor.SetupSystemProcessor.java
public void setupSystemAlias() { String domainUrl = OpenFlameConfig.DOMAIN_URL.getValue(); Integer port = Integer.parseInt(OpenFlameConfig.PORT.getValue()); String[] domainUrlParts = domainUrl.split("\\."); ArrayUtils.reverse(domainUrlParts); if (domainUrlParts.length > 1 && !StringUtils.isNumeric(domainUrlParts[0])) { if (domainUrlParts.length == 2) { domainUrlParts = (String[]) ArrayUtils.add(domainUrlParts, "www"); }/* www .j a va 2s.c o m*/ String rootDomainLookup = domainUrlParts[0] + "." + domainUrlParts[1]; RootDomainModel rootDomain = rootDomainStore.findByLookup(rootDomainLookup); if (rootDomain == null) { rootDomain = new RootDomainModel(); rootDomain.setName(DiffUtils.findRootDomainName(rootDomainLookup)); rootDomain.setLookup(rootDomainLookup); rootDomainStore.save(rootDomain); } RegistryNodeModel parent = rootDomain; if (domainUrlParts.length > 3) { String domainLookup = rootDomainLookup; for (int i = 2; i < domainUrlParts.length - 1; i++) { String domainName = domainUrlParts[i]; domainLookup += "." + domainName; DomainModel domain = domainStore.findByLookup(domainLookup); if (domain == null) { domain = new DomainModel(); domain.setName(domainName); domain.setParent(parent); domainStore.save(domain); parent = domain; } } } String systemName = domainUrlParts[domainUrlParts.length - 1]; String systemLookup = StringUtils.join(domainUrlParts, "."); SystemModel system = systemStore.findByLookup(systemLookup); if (system == null) { SystemModel mainSystem = systemStore.findByLookup(OpenFlame.SYSTEM); system = new SystemModel(); system.setName(systemName); system.setServerName(domainUrl); system.setPort(port); system.setProtocol(EntityProtocol.HTTP); system.setStatus(RegistryNodeStatus.ACTIVE); system.setParent(parent); system.setMain(mainSystem); systemStore.save(system); } } }
From source file:fastcall.FArrayUtils.java
/** * Return an index of an array by descending order of value * @param array/*from w w w .j a va2 s . c om*/ * @return */ public static int[] getIndexByDescendingValue(int[] array) { int[] index = getIndexByAscendingValue(array); ArrayUtils.reverse(index); return index; }
From source file:fr.paris.lutece.plugins.asynchronousupload.service.AbstractAsynchronousUploadHandler.java
/** * {@inheritDoc}//from www .j a v a2 s. c o m */ @Override public String doRemoveUploadedFile(HttpServletRequest request, String strFieldName, List<Integer> listIndexesFilesToRemove) { if (StringUtils.isBlank(strFieldName)) { return JSONUtils.buildJsonErrorRemovingFile(request).toString(); } if ((listIndexesFilesToRemove != null) && (listIndexesFilesToRemove.size() != 0)) { // parse json JSON jsonFieldIndexes = JSONSerializer.toJSON(listIndexesFilesToRemove); if (!jsonFieldIndexes.isArray()) { return JSONUtils.buildJsonErrorRemovingFile(request).toString(); } JSONArray jsonArrayFieldIndexers = (JSONArray) jsonFieldIndexes; int[] tabFieldIndex = new int[jsonArrayFieldIndexers.size()]; for (int nIndex = 0; nIndex < jsonArrayFieldIndexers.size(); nIndex++) { try { tabFieldIndex[nIndex] = Integer.parseInt(jsonArrayFieldIndexers.getString(nIndex)); } catch (NumberFormatException nfe) { return JSONUtils.buildJsonErrorRemovingFile(request).toString(); } } // inverse order (removing using index - remove greater first to keep order) Arrays.sort(tabFieldIndex); ArrayUtils.reverse(tabFieldIndex); List<FileItem> fileItemsSession = getListUploadedFiles(strFieldName, request.getSession()); List<FileItem> listItemsToRemove = new ArrayList<FileItem>(listIndexesFilesToRemove.size()); for (int nFieldIndex : tabFieldIndex) { if (fileItemsSession.size() == 1 && nFieldIndex > 0) { nFieldIndex = nFieldIndex - 1; } listItemsToRemove.add(fileItemsSession.get(nFieldIndex)); removeFileItem(strFieldName, request.getSession(), nFieldIndex); } } JSONObject json = new JSONObject(); json.element(JSONUtils.JSON_KEY_SUCCESS, JSONUtils.JSON_KEY_SUCCESS); json.accumulateAll(JSONUtils.getUploadedFileJSON(getListUploadedFiles(strFieldName, request.getSession()))); json.element(JSONUtils.JSON_KEY_FIELD_NAME, strFieldName); return json.toString(); }
From source file:com.bigdata.dastor.db.Memtable.java
/** * obtain an iterator of columns in this memtable in the specified order starting from a given column. *///from w ww . j av a 2 s . co m public ColumnIterator getSliceIterator(ColumnFamily cf, SliceQueryFilter filter, AbstractType typeComparator) { final ColumnFamily columnFamily = cf == null ? ColumnFamily.create(getTableName(), filter.getColumnFamilyName()) : cf.cloneMeShallow(); final IColumn columns[] = (cf == null ? columnFamily : cf).getSortedColumns() .toArray(new IColumn[columnFamily.getSortedColumns().size()]); // TODO if we are dealing with supercolumns, we need to clone them while we have the read lock since they can be modified later if (filter.reversed) ArrayUtils.reverse(columns); IColumn startIColumn; final boolean isStandard = DatabaseDescriptor .getColumnFamilyType(getTableName(), filter.getColumnFamilyName()).equals("Standard"); if (isStandard) startIColumn = new Column(filter.start); else startIColumn = new SuperColumn(filter.start, null); // ok to not have subcolumnComparator since we won't be adding columns to this object // can't use a ColumnComparatorFactory comparator since those compare on both name and time (and thus will fail to match // our dummy column, since the time there is arbitrary). Comparator<IColumn> comparator = filter.getColumnComparator(typeComparator); int index; if (filter.start.length == 0 && filter.reversed) { /* scan from the largest column in descending order */ index = 0; } else { index = Arrays.binarySearch(columns, startIColumn, comparator); } final int startIndex = index < 0 ? -(index + 1) : index; return new AbstractColumnIterator() { private int curIndex_ = startIndex; public ColumnFamily getColumnFamily() { return columnFamily; } public boolean hasNext() { return curIndex_ < columns.length; } public IColumn next() { // clone supercolumns so caller can freely removeDeleted or otherwise mutate it return isStandard ? columns[curIndex_++] : ((SuperColumn) columns[curIndex_++]).cloneMe(); } }; }
From source file:com.opengamma.financial.analytics.model.volatility.surface.HestonFourierIRFutureSurfaceFittingFunctionDeprecated.java
@Override public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) { final Object objectSurfaceData = inputs.getValue(_surfaceRequirement); if (objectSurfaceData == null) { throw new OpenGammaRuntimeException("Could not get volatility surface data"); }/*from w w w.j a v a 2s.c o m*/ @SuppressWarnings("unchecked") final VolatilitySurfaceData<Double, Double> volatilitySurfaceData = (VolatilitySurfaceData<Double, Double>) objectSurfaceData; final Object objectFuturePriceData = inputs.getValue(_futurePriceRequirement); if (objectFuturePriceData == null) { throw new OpenGammaRuntimeException("Could not get futures price data"); } final NodalDoublesCurve futurePriceData = (NodalDoublesCurve) objectFuturePriceData; //assumes that the sorting is first x, then y if (volatilitySurfaceData.size() == 0) { throw new OpenGammaRuntimeException("Interest rate future option volatility surface definition name=" + _definitionName + " contains no data"); } final SortedSet<Double> x = volatilitySurfaceData.getUniqueXValues(); final DoubleArrayList fittedOptionExpiryList = new DoubleArrayList(); final DoubleArrayList futureDelayList = new DoubleArrayList(); final DoubleArrayList kappaList = new DoubleArrayList(); final DoubleArrayList thetaList = new DoubleArrayList(); final DoubleArrayList vol0List = new DoubleArrayList(); final DoubleArrayList omegaList = new DoubleArrayList(); final DoubleArrayList rhoList = new DoubleArrayList(); final DoubleArrayList chiSqList = new DoubleArrayList(); final Map<DoublesPair, DoubleMatrix2D> inverseJacobians = new HashMap<DoublesPair, DoubleMatrix2D>(); for (final Double t : x) { final List<ObjectsPair<Double, Double>> strip = volatilitySurfaceData.getYValuesForX(t); // FIXME This is bound to break. I changed x/t from an ordinal to an OG-Analytics Year, // via TimeCalculator.getTimeBetween(now, IRFutureOptionUtils.getTime(x,now)) where now is the valuationTime. See IRFutureOptionVolatilitySurfaceDataFunction final int n = strip.size(); final DoubleArrayList strikesList = new DoubleArrayList(n); final DoubleArrayList sigmaList = new DoubleArrayList(n); final DoubleArrayList errorsList = new DoubleArrayList(n); final Double futurePrice = futurePriceData.getYValue(t); if (strip.size() > 4 && futurePrice != null) { final double forward = 1 - futurePrice; for (final ObjectsPair<Double, Double> value : strip) { if (value.first != null && value.second != null) { strikesList.add(1 - value.first / 100); sigmaList.add(value.second); errorsList.add(ERROR); } } if (!strikesList.isEmpty()) { final double[] strikes = strikesList.toDoubleArray(); final double[] sigma = sigmaList.toDoubleArray(); final double[] errors = errorsList.toDoubleArray(); ArrayUtils.reverse(strikes); ArrayUtils.reverse(sigma); ArrayUtils.reverse(errors); final LeastSquareResultsWithTransform fittedResult = new HestonModelFitter(forward, strikes, t, sigma, errors, HESTON_FUNCTION).solve(HESTON_INITIAL_VALUES); final DoubleMatrix1D parameters = fittedResult.getModelParameters(); fittedOptionExpiryList.add(t); futureDelayList.add(0); kappaList.add(parameters.getEntry(0)); thetaList.add(parameters.getEntry(1)); vol0List.add(parameters.getEntry(2)); omegaList.add(parameters.getEntry(3)); rhoList.add(parameters.getEntry(4)); inverseJacobians.put(DoublesPair.of(t.doubleValue(), 0.), fittedResult.getModelParameterSensitivityToData()); chiSqList.add(fittedResult.getChiSq()); } } } if (fittedOptionExpiryList.size() < 5) { //don't have sufficient fits to construct a surface throw new OpenGammaRuntimeException( "Could not construct Heston parameter surfaces; have under 5 surface points"); } final double[] fittedOptionExpiry = fittedOptionExpiryList.toDoubleArray(); final double[] futureDelay = futureDelayList.toDoubleArray(); final double[] kappa = kappaList.toDoubleArray(); final double[] theta = thetaList.toDoubleArray(); final double[] vol0 = vol0List.toDoubleArray(); final double[] omega = omegaList.toDoubleArray(); final double[] rho = rhoList.toDoubleArray(); final InterpolatedDoublesSurface kappaSurface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, kappa, INTERPOLATOR, "Heston kappa surface"); final InterpolatedDoublesSurface thetaSurface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, theta, INTERPOLATOR, "Heston theta surface"); final InterpolatedDoublesSurface vol0Surface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, vol0, INTERPOLATOR, "Heston vol0 surface"); final InterpolatedDoublesSurface omegaSurface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, omega, INTERPOLATOR, "Heston omega surface"); final InterpolatedDoublesSurface rhoSurface = InterpolatedDoublesSurface.from(fittedOptionExpiry, futureDelay, rho, INTERPOLATOR, "Heston rho surface"); final HestonFittedSurfaces fittedSurfaces = new HestonFittedSurfaces(kappaSurface, thetaSurface, vol0Surface, omegaSurface, rhoSurface, inverseJacobians, _currency); return Sets.newHashSet(new ComputedValue(_resultSpecification, fittedSurfaces)); }
From source file:de.dfki.madm.anomalydetection.operator.statistical_based.RobustPCAOperator.java
@Override public void doWork() throws OperatorException { // check whether all attributes are numerical ExampleSet exampleSet = exampleSetInput.getData(ExampleSet.class); Tools.onlyNonMissingValues(exampleSet, "PCA"); Tools.onlyNumericalAttributes(exampleSet, "PCA"); // Get normal probability. double normProb = getParameterAsDouble(PARAMETER_OUTLIER_PROBABILITY); int olInst = exampleSet.size() - (int) Math.floor(exampleSet.size() * normProb); log("Ignoring " + olInst + " anomalyous instances for robustness."); // The robust estimate is based on removing top outliers first based on Mahalanobis distance (MD). // Since MD is the same as the outlier score when using all PCs, the PCA is done twice: // First with all examples, second with top-outliers removed (robust) // First PCA for outlier removal // create covariance matrix Matrix covarianceMatrix = CovarianceMatrix.getCovarianceMatrix(exampleSet); // EigenVector and EigenValues of the covariance matrix EigenvalueDecomposition eigenvalueDecomposition = covarianceMatrix.eig(); // create and deliver results double[] eigenvalues = eigenvalueDecomposition.getRealEigenvalues(); Matrix eigenvectorMatrix = eigenvalueDecomposition.getV(); double[][] eigenvectors = eigenvectorMatrix.getArray(); PCAModel model = new PCAModel(exampleSet, eigenvalues, eigenvectors); // Perform transformation ExampleSet res = model.apply((ExampleSet) exampleSet.clone()); // Compute simple list with MDs and sort according to MD. List<double[]> l = new LinkedList<double[]>(); double eIdx = 0; for (Example example : res) { double md = 0.0; int aNr = 0; for (Attribute attr : example.getAttributes()) { double pcscore = example.getValue(attr); md += (pcscore * pcscore) / model.getEigenvalue(aNr); aNr++;/*from w w w. ja v a 2 s . com*/ } double[] x = { md, eIdx }; l.add(x); eIdx++; } Collections.sort(l, new Comparator<double[]>() { public int compare(double[] first, double[] second) { return Double.compare(second[0], first[0]); } }); // Out of the list, create array with outlier-indexes and array (mapping) with good instances. Iterator<double[]> iter = l.iterator(); int[] olMapping = new int[olInst]; for (int i = 0; i < olInst; i++) { olMapping[i] = (int) ((double[]) iter.next())[1]; } Arrays.sort(olMapping); int[] mapping = new int[exampleSet.size() - olInst]; int olc = 0; int ctr = 0; for (int i = 0; i < exampleSet.size(); i++) { if (olc == olInst) { // Add last elements after last outlier mapping[ctr++] = i; continue; } if (olMapping[olc] != i) { mapping[ctr++] = i; } else { olc++; } } ExampleSet robustExampleSet = new MappedExampleSet(exampleSet, mapping); // creates a new example set without the top outliers. // --- // Second PCA (robust) covarianceMatrix = CovarianceMatrix.getCovarianceMatrix(robustExampleSet); eigenvalueDecomposition = covarianceMatrix.eig(); // create and deliver results eigenvalues = eigenvalueDecomposition.getRealEigenvalues(); eigenvectorMatrix = eigenvalueDecomposition.getV(); eigenvectors = eigenvectorMatrix.getArray(); // Apply on original set model = new PCAModel(exampleSet, eigenvalues, eigenvectors); // Perform transformation res = model.apply((ExampleSet) exampleSet.clone()); // Sort eigenvalues Arrays.sort(eigenvalues); ArrayUtils.reverse(eigenvalues); // if necessary reduce nbr of dimensions ... int reductionType = getParameterAsInt(PARAMETER_REDUCTION_TYPE); List<Integer> pcList = new ArrayList<Integer>(); if (reductionType == PCS_ALL) { for (int i = 0; i < exampleSet.getAttributes().size(); i++) { pcList.add(i); } } if (reductionType == PCS_TOP || reductionType == PCS_BOTH) { //top switch (getParameterAsInt(PARAMETER_TOP_METHODS)) { case PCS_TOP_FIX: for (int i = 0; i < getParameterAsInt(PARAMETER_NUMBER_OF_COMPONENTS_TOP); i++) { pcList.add(i); } break; case PCS_TOP_VAR: double var = getParameterAsDouble(PARAMETER_VARIANCE_THRESHOLD); boolean last = false; for (int i = 0; i < exampleSet.getAttributes().size(); i++) { if (model.getCumulativeVariance(i) < var) { pcList.add(i); } else if (!last) { // we need to add another PC to meet the minimum requirement. last = true; pcList.add(i); } } break; } } if (reductionType == PCS_LOWER || reductionType == PCS_BOTH) { //lower switch (getParameterAsInt(PARAMETER_LOW_METHODS)) { case PCS_LOW_FIX: for (int i = exampleSet.getAttributes().size() - getParameterAsInt(PARAMETER_NUMBER_OF_COMPONENTS_LOW); i < exampleSet.getAttributes() .size(); i++) { pcList.add(i); } break; case PCS_LOW_VAL: double val = getParameterAsDouble(PARAMETER_VALUE_THRESHOLD); for (int i = 0; i < eigenvalues.length; i++) { if (eigenvalues[i] <= val) { if (pcList.size() == 0) { pcList.add(i); } else if (pcList.get(pcList.size() - 1).intValue() < i) { pcList.add(i); } } } break; } } int[] opcs = ArrayUtils.toPrimitive(pcList.toArray(new Integer[pcList.size()])); if (opcs.length == 0) { throw new UserError(this, "Parameters thresholds are selected such that they did not match any principal component. Lower variance or increase eigenvalue threshold."); } if (opcs.length == exampleSet.getAttributes().size()) { log("Using all PCs for score."); } else { log("Using following PCs for score: " + Arrays.toString(opcs)); } // Normalize by Chi-Dist with d degrees of freedom double scoreNormalizer = 1.0; ChiSquaredDistributionImpl chi = new ChiSquaredDistributionImpl(opcs.length); try { scoreNormalizer = chi.inverseCumulativeProbability(normProb); } catch (MathException e) { System.err.println(e); } log("Normalizing score with chi cumulative propability: " + scoreNormalizer); // compute scores Attribute scoreAttr = AttributeFactory.createAttribute("outlier", Ontology.REAL); exampleSet.getExampleTable().addAttribute(scoreAttr); exampleSet.getAttributes().setOutlier(scoreAttr); for (int exNr = 0; exNr < exampleSet.size(); exNr++) { Example orig = exampleSet.getExample(exNr); Example pc = res.getExample(exNr); double oscore = 0.0; int aNr = 0; ctr = 0; for (Attribute attr : pc.getAttributes()) { if (ctr < opcs.length && opcs[ctr] != aNr) { // we skip this dimension aNr++; continue; } double pcscore = pc.getValue(attr); oscore += (pcscore * pcscore) / model.getEigenvalue(aNr); aNr++; ctr++; } orig.setValue(scoreAttr, oscore / scoreNormalizer); } exampleSetOutput.deliver(exampleSet); }
From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java
public static long decodeLittleEndian(byte[] value, int offset) { byte[] subArray = ArrayUtils.subarray(value, offset, offset + PtBytes.SIZEOF_LONG); ArrayUtils.reverse(subArray); return PtBytes.toLong(subArray); }