List of usage examples for java.util Formatter close
public void close()
From source file:net.sourceforge.fenixedu.domain.student.Registration.java
public void exportValues(StringBuilder result) { Formatter formatter = new Formatter(result); final Student student = getStudent(); formatter.format("%s: %s\n", BundleUtil.getString(Bundle.ACADEMIC, "label.ingression"), getIngression() == null ? " - " : getIngression().getFullDescription()); formatter.format("%s: %d\n", BundleUtil.getString(Bundle.ACADEMIC, "label.studentNumber"), student.getNumber());//w w w . java2s. c om formatter.format("%s: %s\n", BundleUtil.getString(Bundle.ACADEMIC, "label.Student.Person.name"), student.getPerson().getName()); formatter.format("%s: %s\n", BundleUtil.getString(Bundle.ACADEMIC, "label.degree"), getDegree().getPresentationName()); formatter.close(); }
From source file:org.wso2.extension.siddhi.execution.var.backtest.BacktestIncremental.java
public void runTest() throws FileNotFoundException { Formatter formatter = new Formatter(new File("MonteCarloBacktestResults.csv")); formatter.format("%s%n", "date,varclose,varavg,varmax,corrloss,varmedian,varmode,lossclose,lossavg,lossmax," + "corrvar,lossmedian,lossmode"); String[] dates = { "jan-23", "jan-24", "jan-25", "jan-26", "jan-27", "jan-30", "jan-31", "feb-1", "feb-2", "feb-3" }; String write = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s"; // VaRCalculator varCalculator = new HistoricalVaRCalculator(BATCH_SIZE, VAR_CI); VaRCalculator varCalculator = new ParametricVaRCalculator(BATCH_SIZE, VAR_CI); // VaRCalculator varCalculator = new MonteCarloVarCalculator(BATCH_SIZE, VAR_CI, 2500, 100, 0.01); Map<String, Integer> assets = initPortfolio(); Portfolio portfolio = varCalculator.createPortfolio("1", assets); varCalculator.addPortfolio("1", portfolio); for (int d = START_DATE; d <= END_DATE; d++) { System.out.println("\nDAY : " + dates[d - START_DATE] + "\n"); ArrayList<Event> list = readBacktestData(d); HashMap<Integer, Double> varMap = new HashMap(); HashMap<Integer, Double> lossMap = new HashMap(); double var = 0; double loss = 0; String corrVar;/* w ww . j a v a 2 s . c o m*/ String corrLoss; int counter = 0; for (int i = 0; i < list.size(); i++) { //System.out.print("Event " + (i + 1) + " : "); String jsonString = (String) varCalculator.calculateValueAtRisk(list.get(i)); currentPortfolioValue = portfolio.getTotalPortfolioValue(); if (jsonString != null) { JSONObject jsonObject = new JSONObject(jsonString); double tempVar = (Double) jsonObject.get(PORTFOLIO_KEY); // hardcoded for portfolio ID 1 if (tempVar < 0) { var = tempVar; varMap.put(counter, var); //System.out.printf("Var : %.4f", tempVar); } double tempLoss = currentPortfolioValue - previousPortfolioValue; if (tempLoss < 0) { loss = tempLoss; lossMap.put(counter, loss); //System.out.printf(" Loss : %.4f", tempLoss); } counter++; } previousPortfolioValue = currentPortfolioValue; //System.out.println(); } double[] vars = Stream.of(varMap.values().toArray(new Double[varMap.size()])) .mapToDouble(Double::doubleValue).toArray(); DescriptiveStatistics statVar = new DescriptiveStatistics(vars); double[] losses = Stream.of(lossMap.values().toArray(new Double[lossMap.size()])) .mapToDouble(Double::doubleValue).toArray(); DescriptiveStatistics statLoss = new DescriptiveStatistics(losses); System.out.println("Daily VaR CLOSE : " + var); System.out.println("Daily VaR AVG : " + statVar.getMean()); Double min = statVar.getMin(); System.out.println("Daily VaR MAX : " + min); Integer minIndex = null; for (Map.Entry<Integer, Double> e : varMap.entrySet()) { if (e.getValue().equals(min)) { minIndex = e.getKey(); } } if (lossMap.get(minIndex) == null) { corrLoss = "NO LOSS"; } else { corrLoss = lossMap.get(minIndex).toString(); } System.out.println("Crspng Loss : " + corrLoss); System.out.println("Daily VaR MEDIAN : " + statVar.getPercentile(50)); System.out.println("Daily VaR MODE : " + mode(statVar.getValues())); System.out.println(); System.out.println("Daily Loss CLOSE : " + loss); System.out.println("Daily Loss AVG : " + statLoss.getMean()); min = statLoss.getMin(); System.out.println("Daily Loss MAX : " + min); for (Map.Entry<Integer, Double> e : lossMap.entrySet()) { if (e.getValue().equals(min)) { minIndex = e.getKey(); } } if (varMap.get(minIndex) == null) { corrVar = "NO VAR"; } else { corrVar = varMap.get(minIndex).toString(); } System.out.println("Crspng VaR : " + corrVar); System.out.println("Daily Loss MEDIAN : " + statLoss.getPercentile(50)); System.out.println("Daily Loss MODE : " + mode(statLoss.getValues())); formatter.format("%s%n", String.format(write, dates[d - START_DATE], var, statVar.getMean(), statVar.getMin(), corrLoss, statVar.getPercentile(50), mode(statVar.getValues()), loss, statLoss.getMean(), statLoss.getMin(), corrVar, statLoss.getPercentile(50), mode(statLoss.getValues()))); } formatter.close(); }