List of usage examples for org.apache.commons.math.stat.descriptive DescriptiveStatistics getMean
public double getMean()
From source file:playground.johannes.sna.util.TXTWriter.java
public static void writeStatistics(TDoubleObjectHashMap<DescriptiveStatistics> statsMap, String xLab, String file) throws IOException { double[] keys = statsMap.keys(); Arrays.sort(keys);//from www.j a v a2 s . com BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(xLab); writer.write(TAB); writer.write("mean"); writer.write(TAB); writer.write("median"); writer.write(TAB); writer.write("min"); writer.write(TAB); writer.write("max"); writer.write(TAB); writer.write("n"); writer.newLine(); for (double key : keys) { DescriptiveStatistics stats = statsMap.get(key); writer.write(String.valueOf(key)); writer.write(TAB); writer.write(String.valueOf(stats.getMean())); writer.write(TAB); writer.write(String.valueOf(stats.getPercentile(50))); writer.write(TAB); writer.write(String.valueOf(stats.getMin())); writer.write(TAB); writer.write(String.valueOf(stats.getMax())); writer.write(TAB); writer.write(String.valueOf(stats.getN())); writer.newLine(); } writer.close(); }
From source file:playground.johannes.snowball2.GraphStatistic.java
protected TObjectDoubleHashMap<String> getStatisticsMap(DescriptiveStatistics stats) { TObjectDoubleHashMap<String> statsMap = new TObjectDoubleHashMap<String>(); statsMap.put(MIN_KEY, stats.getMin()); statsMap.put(MAX_KEY, stats.getMax()); statsMap.put(MEAN_KEY, stats.getMean()); statsMap.put(VARIANCE_KEY, stats.getVariance()); statsMap.put(SKEWNESS_KEY, stats.getSkewness()); statsMap.put(KURTOSIS_KEY, stats.getKurtosis()); return statsMap; }
From source file:playground.johannes.snowball2.SnowballExe.java
private TObjectDoubleHashMap<String> computeStatistics(Graph g, Map<String, GraphStatistic> statistics, Map<String, DescriptiveStatistics> references, int iteration) { TObjectDoubleHashMap<String> meanValues = new TObjectDoubleHashMap<String>(); for (String key : statisticsKeys) { logger.info(String.format("Calculating statistics... %1$s.", key)); GraphStatistic s = statistics.get(key); if (s != null) { DescriptiveStatistics ref = references.get(key); DescriptiveStatistics stats = s.calculate(g, iteration, ref); if (ref == null && iteration == -1) references.put(key, stats); meanValues.put(key, stats.getMean()); } else {/*w ww. j a v a 2s . c o m*/ meanValues.put(key, Double.NaN); } } return meanValues; }
From source file:playground.johannes.snowball2.TypeClusters.java
public DescriptiveStatistics calculate(Graph g, int iteration, DescriptiveStatistics reference) { Set<Vertex> vertices = g.getVertices(); Map<String, Collection<Vertex>> clusters = new HashMap<String, Collection<Vertex>>(); for (Vertex v : vertices) { String type = (String) v.getUserDatum(UserDataKeys.TYPE_KEY); if (type != null) { Collection<Vertex> cluster = clusters.get(type); if (cluster == null) { cluster = new LinkedList<Vertex>(); clusters.put(type, cluster); }//ww w.ja v a 2s .c om cluster.add(v); } } Map<String, Graph> subGraphs = new HashMap<String, Graph>(); for (String type : clusters.keySet()) { subGraphs.put(type, extractCluster(clusters.get(type))); } DescriptiveStatistics degree; DescriptiveStatistics clustering; if (g instanceof SampledGraph) { degree = SampledGraphStatistics.getDegreeStatistics((SampledGraph) g); clustering = SampledGraphStatistics.getClusteringStatistics((SampledGraph) g); } else { degree = GraphStatistics.getDegreeStatistics(g); clustering = GraphStatistics.getClusteringStatistics(g); } // CountComponents cc = new CountComponents(null, null); for (String type : clusters.keySet()) { Graph subGraph = subGraphs.get(type); float z = (float) degree.getMean(); float c = (float) clustering.getMean(); int components = GraphStatistics.getDisconnectedComponents(subGraph).size(); System.out.println(String.format( "Subgraph of type %1$s has %2$s vertices, %3$s components, mean degree %4$s and mean clustering %5$s.", type, subGraph.numVertices(), components, z, c)); } return new DescriptiveStatistics(); }
From source file:playground.johannes.socialnets.NetworkGenerator2.java
private static void dumpStats(SocialNetwork socialNet, String histfile, int it) throws FileNotFoundException, IOException { int numEdges = socialNet.getEdges().size(); int numVertices = socialNet.getVertices().size(); logger.info(String.format("%1$s vertices, %2$s edges.", numVertices, numEdges)); int isolates = 0; for (Vertex v : socialNet.getVertices()) if (v.getEdges().size() == 0) isolates++;/*from w w w . j a v a 2 s .com*/ logger.info(String.format("%1$s isolates.", isolates)); DescriptiveStatistics stats = GraphStatistics.getDegreeStatistics(socialNet); double meanDegree = stats.getMean(); logger.info(String.format("Mean degree is %1$s.", meanDegree)); WeightedStatistics wstats = new WeightedStatistics(); wstats.addAll(stats.getValues()); WeightedStatistics.writeHistogram(wstats.absoluteDistribution(), "/Users/fearonni/vsp-work/socialnets/data-analysis/socialnetgenerator/" + it + ".degreehist.txt"); double clustering = GraphStatistics.getClusteringStatistics(socialNet).getMean(); logger.info(String.format("Mean clustering coefficient is %1$s.", clustering)); double mutuality = GraphStatistics.getMutuality(socialNet); logger.info(String.format("Mutuality is %1$s.", mutuality)); double dcorrelation = GraphStatistics.getDegreeCorrelation(socialNet); logger.info(String.format("Degree correlation is %1$s.", dcorrelation)); // logger.info(String.format("Closeness is %1$s.", GraphStatistics.getCentrality(socialNet).getGraphCloseness())); logger.info("Calculating distance distribution..."); WeightedStatistics stats3 = new WeightedStatistics(); // HashMap<Ego, TDoubleDoubleHashMap> egoHist = new HashMap<Ego, TDoubleDoubleHashMap>(); double binsize = 1000; for (Ego ego : socialNet.getVertices()) { TDoubleDoubleHashMap hist = new TDoubleDoubleHashMap(); Coord c1 = ego.getPerson().getSelectedPlan().getFirstActivity().getCoord(); for (Ego p2 : socialNet.getVertices()) { Coord c2 = p2.getPerson().getSelectedPlan().getFirstActivity().getCoord(); double d = c1.calcDistance(c2); double bin = Math.floor(d / binsize); double val = hist.get(bin); val++; hist.put(bin, val); } // egoHist.put(ego, hist); for (Vertex n : ego.getNeighbours()) { Coord c2 = ((Ego) n).getPerson().getSelectedPlan().getFirstActivity().getCoord(); double dist = c1.calcDistance(c2); stats3.add(dist, 1 / hist.get(Math.floor(dist / binsize))); } } // WeightedStatistics stats3 = new WeightedStatistics(); // for(Object o : socialNet.getEdges()) { // Edge e = (Edge)o; // Ego e1 = (Ego) e.getVertices().getFirst(); // Ego e2 = (Ego) e.getVertices().getSecond(); // Coord c1 = e1.getPerson().getSelectedPlan().getFirstActivity().getCoord(); // Coord c2 = e2.getPerson().getSelectedPlan().getFirstActivity().getCoord(); // double dist = c1.calcDistance(c2); // stats3.add(dist); // } WeightedStatistics.writeHistogram(stats3.absoluteDistribution(1000), "/Users/fearonni/vsp-work/socialnets/data-analysis/socialnetgenerator/" + it + ".edgelength.txt"); PajekVisWriter pWriter = new PajekVisWriter(); pWriter.write(socialNet, "/Users/fearonni/vsp-work/socialnets/data-analysis/socialnetgenerator/" + it + ".socialnet.net"); WeightedStatistics stats4 = new WeightedStatistics(); for (Object o : socialNet.getEdges()) { Edge e = (Edge) o; Ego e1 = (Ego) e.getVertices().getFirst(); Ego e2 = (Ego) e.getVertices().getSecond(); int age1 = e1.getPerson().getAge(); int age2 = e2.getPerson().getAge(); double dAge = 0; if (age1 > age2) dAge = age1 / (double) age2; else dAge = age2 / (double) age1; stats4.add(dAge); } WeightedStatistics.writeHistogram(stats4.absoluteDistribution(0.05), "/Users/fearonni/vsp-work/socialnets/data-analysis/socialnetgenerator/" + it + ".agedist.txt"); logger.info("Random link selection = " + rndLinkCount); logger.info("Number of components = " + GraphStatistics.getComponents(socialNet).size()); }
From source file:playground.johannes.socialnets.SpatialNetworkGenerator.java
private static void dumpStats(SocialNetwork socialNet, String histfile, int it) throws FileNotFoundException, IOException { int numEdges = socialNet.getEdges().size(); int numVertices = socialNet.getVertices().size(); logger.info(String.format("%1$s vertices, %2$s edges.", numVertices, numEdges)); int isolates = 0; for (Vertex v : socialNet.getVertices()) if (v.getEdges().size() == 0) isolates++;/*from ww w.j ava 2 s. c o m*/ logger.info(String.format("%1$s isolates.", isolates)); DescriptiveStatistics stats = GraphStatistics.getDegreeStatistics(socialNet); double meanDegree = stats.getMean(); logger.info(String.format("Mean degree is %1$s.", meanDegree)); WeightedStatistics wstats = new WeightedStatistics(); wstats.addAll(stats.getValues()); WeightedStatistics.writeHistogram(wstats.absoluteDistribution(), "/Users/fearonni/vsp-work/socialnets/data-analysis/socialnetgenerator/" + it + ".degreehist.txt"); double clustering = GraphStatistics.getClusteringStatistics(socialNet).getMean(); logger.info(String.format("Mean clustering coefficient is %1$s.", clustering)); double mutuality = GraphStatistics.getMutuality(socialNet); logger.info(String.format("Mutuality is %1$s.", mutuality)); double dcorrelation = GraphStatistics.getDegreeCorrelation(socialNet); logger.info(String.format("Degree correlation is %1$s.", dcorrelation)); // logger.info(String.format("Closeness is %1$s.", GraphStatistics.getCentrality(socialNet).getGraphCloseness())); WeightedStatistics stats3 = new WeightedStatistics(); double binsize = 1000; for (Ego ego : socialNet.getVertices()) { TDoubleDoubleHashMap hist = new TDoubleDoubleHashMap(); Coord c1 = ego.getPerson().getSelectedPlan().getFirstActivity().getCoord(); for (Ego p2 : socialNet.getVertices()) { Coord c2 = p2.getPerson().getSelectedPlan().getFirstActivity().getCoord(); double d = c1.calcDistance(c2); double bin = Math.floor(d / binsize); double val = hist.get(bin); val++; hist.put(bin, val); } // egoHist.put(ego, hist); for (Vertex n : ego.getNeighbours()) { Coord c2 = ((Ego) n).getPerson().getSelectedPlan().getFirstActivity().getCoord(); double dist = c1.calcDistance(c2); stats3.add(dist, 1 / hist.get(Math.floor(dist / binsize))); } } // for(Object o : socialNet.getEdges()) { // Edge e = (Edge)o; // Ego e1 = (Ego) e.getVertices().getFirst(); // Ego e2 = (Ego) e.getVertices().getSecond(); // Coord c1 = e1.getPerson().getSelectedPlan().getFirstActivity().getCoord(); // Coord c2 = e2.getPerson().getSelectedPlan().getFirstActivity().getCoord(); // double dist = c1.calcDistance(c2); // stats3.add(dist); // } WeightedStatistics.writeHistogram(stats3.absoluteDistribution(1000), "/Users/fearonni/vsp-work/socialnets/data-analysis/socialnetgenerator/" + it + ".edgelength.txt"); PajekVisWriter pWriter = new PajekVisWriter(); pWriter.write(socialNet, "/Users/fearonni/vsp-work/socialnets/data-analysis/socialnetgenerator/" + it + ".socialnet.net"); WeightedStatistics stats4 = new WeightedStatistics(); for (Object o : socialNet.getEdges()) { Edge e = (Edge) o; Ego e1 = (Ego) e.getVertices().getFirst(); Ego e2 = (Ego) e.getVertices().getSecond(); int age1 = e1.getPerson().getAge(); int age2 = e2.getPerson().getAge(); double dAge = 0; if (age1 > age2) dAge = age1 / (double) age2; else dAge = age2 / (double) age1; stats4.add(dAge); } WeightedStatistics.writeHistogram(stats4.absoluteDistribution(0.05), "/Users/fearonni/vsp-work/socialnets/data-analysis/socialnetgenerator/" + it + ".agedist.txt"); }
From source file:playground.johannes.socialnetworks.survey.ivt2009.graph.io.GraphBuilder.java
private void loadSociogramData(Collection<VertexRecord> records, SQLDumpReader sqlData) { logger.info("Loading sociogram data..."); Map<String, VertexRecord> map = sqlData.getFullAlterKeyMappping(records); TObjectIntHashMap<Vertex> rawDegrees = new TObjectIntHashMap<Vertex>(); for (Vertex v : proj.getVertices()) { rawDegrees.put(v, v.getNeighbours().size()); }// w w w .j a va2s .c om int edgecnt = 0; int doublecnt = 0; int egoEdge = 0; Set<Vertex> notOkVertices = new HashSet<Vertex>(); Set<Vertex> okVertices = new HashSet<Vertex>(); DescriptiveStatistics notOkStats = new DescriptiveStatistics(); DescriptiveStatistics okStats = new DescriptiveStatistics(); DescriptiveStatistics numDistr = new DescriptiveStatistics(); DescriptiveStatistics numDistrNoZero = new DescriptiveStatistics(); DescriptiveStatistics sizeDistr = new DescriptiveStatistics(); TDoubleArrayList sizeValues = new TDoubleArrayList(); TDoubleArrayList kSizeValues = new TDoubleArrayList(); TDoubleArrayList numValues = new TDoubleArrayList(); TDoubleArrayList numValues2 = new TDoubleArrayList(); TDoubleArrayList kNumValues = new TDoubleArrayList(); for (VertexRecord record : records) { if (record.isEgo) { List<Set<String>> cliques = sqlData.getCliques(record); numDistr.addValue(cliques.size()); Vertex v = idMap.get(record.id); numValues.add(cliques.size()); kNumValues.add(v.getNeighbours().size()); if (!cliques.isEmpty()) numDistrNoZero.addValue(cliques.size()); for (Set<String> clique : cliques) { sizeDistr.addValue(clique.size()); sizeValues.add(clique.size()); kSizeValues.add(rawDegrees.get(projMap.get(v))); numValues2.add(cliques.size()); List<SocialSparseVertex> vertices = new ArrayList<SocialSparseVertex>(clique.size()); for (String alter : clique) { VertexRecord r = map.get(record.egoSQLId + alter); if (r != null) { SocialSparseVertex vertex = idMap.get(r.id); if (vertex != null) { vertices.add(vertex); } else { logger.warn("Vertex not found."); } } else { logger.warn("Record not found."); } } for (int i = 0; i < vertices.size(); i++) { for (int j = i + 1; j < vertices.size(); j++) { SampledVertexDecorator<SocialSparseVertex> vProj1 = projMap.get(vertices.get(i)); SampledVertexDecorator<SocialSparseVertex> vProj2 = projMap.get(vertices.get(j)); if (!vProj1.isSampled() && !vProj2.isSampled()) { if (Math.random() < 0.62) { SocialSparseEdge socialEdge = builder.addEdge(graph, vertices.get(i), vertices.get(j)); if (socialEdge != null) { projBuilder.addEdge(proj, vProj1, vProj2, socialEdge); edgecnt++; if (vProj1.isSampled() || vProj2.isSampled()) { egoEdge++; if (vProj1.isSampled()) notOkVertices.add(vProj1); else notOkVertices.add(vProj2); } } else { doublecnt++; if (vProj1.isSampled()) okVertices.add(vProj1); else if (vProj2.isSampled()) okVertices.add(vProj2); } } } } } } } } for (Vertex v : okVertices) okStats.addValue(rawDegrees.get(v)); for (Vertex v : notOkVertices) notOkStats.addValue(rawDegrees.get(v)); try { TDoubleDoubleHashMap hist = Histogram.createHistogram(okStats, new LinearDiscretizer(1), false); TXTWriter.writeMap(hist, "k", "n", "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/k_ok.txt"); TDoubleDoubleHashMap hist2 = Histogram.createHistogram(notOkStats, new LinearDiscretizer(1), false); TXTWriter.writeMap(hist2, "k", "n", "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/k_notok.txt"); TDoubleDoubleHashMap ratio = new TDoubleDoubleHashMap(); double[] keys = hist.keys(); for (double k : keys) { double val1 = hist2.get(k); double val2 = hist.get(k); ratio.put(k, val1 / (val2 + val1)); } TXTWriter.writeMap(ratio, "k", "p", "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/k_ratio.txt"); logger.info("Mean num of cliques: " + numDistrNoZero.getMean()); logger.info("Mean size: " + sizeDistr.getMean()); logger.info("Median num of cliques: " + StatUtils.percentile(numDistrNoZero.getValues(), 50)); logger.info("Median size: " + StatUtils.percentile(sizeDistr.getValues(), 50)); TDoubleDoubleHashMap histNum = Histogram.createHistogram(numDistrNoZero, FixedSampleSizeDiscretizer.create(numDistrNoZero.getValues(), 2, 20), true); Histogram.normalize(histNum); TXTWriter.writeMap(histNum, "num", "freq", "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/numCliques.txt"); TDoubleDoubleHashMap histSize = Histogram.createHistogram(sizeDistr, FixedSampleSizeDiscretizer.create(sizeDistr.getValues(), 2, 20), true); Histogram.normalize(histSize); TXTWriter.writeMap(histSize, "size", "freq", "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/numPersons.txt"); Discretizer discretizer = FixedSampleSizeDiscretizer.create(kSizeValues.toNativeArray(), 20, 20); TDoubleArrayList valuesX = new TDoubleArrayList(); for (int i = 0; i < kSizeValues.size(); i++) { valuesX.add(discretizer.discretize(kSizeValues.get(i))); } Correlations.writeToFile(Correlations.mean(valuesX.toNativeArray(), sizeValues.toNativeArray()), "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/size_k.txt", "k", "size"); discretizer = FixedSampleSizeDiscretizer.create(kNumValues.toNativeArray(), 20, 20); valuesX = new TDoubleArrayList(); for (int i = 0; i < kNumValues.size(); i++) { valuesX.add(discretizer.discretize(kNumValues.get(i))); } Correlations.writeToFile(Correlations.mean(valuesX.toNativeArray(), numValues.toNativeArray()), "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/num_k.txt", "k", "n"); Correlations.writeToFile(Correlations.mean(numValues2.toNativeArray(), sizeValues.toNativeArray()), "/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/augmented/size_num.txt", "num", "size"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } logger.info(String.format("Inserted %1$s edges, %2$s edges already present.", edgecnt, doublecnt)); logger.info(String.format("Inserted %1$s edges between at least one ego.", egoEdge)); }
From source file:playground.johannes.studies.coopsim.SweepMerge1D.java
/** * @param args/*from w ww . ja v a 2s . c om*/ * @throws IOException */ public static void main(String[] args) throws IOException { File root = new File("/Volumes/cluster.math.tu-berlin.de/net/ils2/jillenberger/leisure/runs/run272/"); // String property = "score_join_culture"; String property = "d_trip_culture"; int valIdx = 1; // String parameterKey = "beta_join"; // String parameterKey = "performing"; String parameterKey = "alterProba_culture"; int dumps = 1; File analysis = new File(String.format("%1$s/analysis/", root.getAbsolutePath())); analysis.mkdirs(); TDoubleDoubleHashMap values = new TDoubleDoubleHashMap(); File tasks = new File(String.format("%1$s/tasks/", root.getAbsolutePath())); for (File file : tasks.listFiles()) { if (file.isDirectory()) { if (!file.getName().equals("analysis")) { File output = new File(String.format("%1$s/output/", file.getAbsolutePath())); String[] dirs = output.list(); if (dirs.length > 0) { Arrays.sort(dirs, new Comparator<String>() { @Override public int compare(String o1, String o2) { return Double.compare(Double.parseDouble(o1), Double.parseDouble(o2)); } }); /* * get parameter value */ Config config = new Config(); ConfigReader creader = new ConfigReader(config); creader.readFile(String.format("%1$s/config.xml", file.getAbsolutePath())); double paramValue = Double.parseDouble(config.findParam("socialnets", parameterKey)); // double paramValue = Double.parseDouble(config.findParam("planCalcScore", parameterKey)); int start = dirs.length - dumps; start = Math.max(0, start); if (dirs.length < dumps) { logger.warn(String.format("Less than %1$s samples.", dumps)); } DescriptiveStatistics stat = new DescriptiveStatistics(); for (int i = start; i < dirs.length; i++) { // for(int i = 12; i < 13; i++) { File statsFile = new File( String.format("%1$s/%2$s/statistics.txt", output.getAbsolutePath(), dirs[i])); if (statsFile.exists()) { /* * get property value */ BufferedReader reader = new BufferedReader(new FileReader(statsFile)); String line = reader.readLine(); while ((line = reader.readLine()) != null) { String[] tokens = line.split("\t"); String key = tokens[0]; double val = Double.parseDouble(tokens[valIdx]); if (key.equals(property)) { stat.addValue(val); break; } } } } values.put(paramValue, stat.getMean()); } else { logger.warn("No samples."); } } } } TXTWriter.writeMap(values, parameterKey, property, String.format("%1$s/%2$s.txt", analysis.getAbsolutePath(), property)); }
From source file:playground.johannes.studies.coopsim.SweepMerge2D.java
/** * @param args//from ww w . j av a 2 s . c om * @throws IOException */ public static void main(String[] args) throws IOException { File root = new File("/Volumes/cluster.math.tu-berlin.de/net/ils2/jillenberger/leisure/runs/run205/"); String property = "d_trip_home"; int valIdx = 1; String parameterKey1 = "traveling"; String parameterKey2 = "performing"; File analysis = new File(String.format("%1$s/analysis/", root.getAbsolutePath())); analysis.mkdirs(); KeyMatrix<Double> matrix = new KeyMatrix<Double>(); File tasks = new File(String.format("%1$s/tasks/", root.getAbsolutePath())); for (File file : tasks.listFiles()) { if (file.isDirectory()) { if (!file.getName().equals("analysis")) { File output = new File(String.format("%1$s/output/", file.getAbsolutePath())); String[] dirs = output.list(); if (dirs.length > 0) { Arrays.sort(dirs, new Comparator<String>() { @Override public int compare(String o1, String o2) { return Double.compare(Double.parseDouble(o1), Double.parseDouble(o2)); } }); /* * get parameter value */ Config config = new Config(); ConfigReader creader = new ConfigReader(config); creader.readFile(String.format("%1$s/config.xml", file.getAbsolutePath())); double paramValue1 = Double.parseDouble(config.findParam("planCalcScore", parameterKey1)); double paramValue2 = Double.parseDouble(config.findParam("planCalcScore", parameterKey2)); int start = dirs.length - 10; start = Math.max(0, start); if (dirs.length < 10) { logger.warn("Less than 10 samples."); } DescriptiveStatistics stat = new DescriptiveStatistics(); for (int i = start; i < dirs.length; i++) { File statsFile = new File( String.format("%1$s/%2$s/statistics.txt", output.getAbsolutePath(), dirs[i])); if (statsFile.exists()) { /* * get property value */ BufferedReader reader = new BufferedReader(new FileReader(statsFile)); String line = reader.readLine(); while ((line = reader.readLine()) != null) { String[] tokens = line.split("\t"); String key = tokens[0]; double val = Double.parseDouble(tokens[valIdx]); if (key.equals(property)) { stat.addValue(val); break; } } } } matrix.putValue(stat.getMean(), Math.abs(paramValue1), Math.abs(paramValue2)); } else { logger.warn("No samples."); } } } } matrix.write(String.format("%1$s/%2$s.matrix.txt", analysis.getAbsolutePath(), property)); }
From source file:playground.johannes.studies.ivt.WebdiaryDistCalc.java
/** * @param args//from w w w . j ava 2 s . co m * @throws IOException * @throws NumberFormatException */ public static void main(String[] args) throws NumberFormatException, IOException { GeometryFactory factory = new GeometryFactory(); DistanceCalculator distCalc = new WGS84DistanceCalculator(); TDoubleDoubleHashMap correl = new TDoubleDoubleHashMap(); TDoubleArrayList xvals = new TDoubleArrayList(); TDoubleArrayList yvals = new TDoubleArrayList(); SocialSampledGraphProjection<SocialSparseGraph, SocialSparseVertex, SocialSparseEdge> graph = GraphReaderFacade .read("/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/graph/graph.graphml"); Map<String, SocialSparseVertex> map = new HashMap<String, SocialSparseVertex>(); for (SocialSampledVertexDecorator<SocialSparseVertex> v : graph.getVertices()) { map.put(v.getPerson().getPerson().getId().toString(), v.getDelegate()); } List<String> files = new ArrayList<String>(); files.add("/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/raw/alters1.txt"); files.add("/Users/jillenberger/Work/socialnets/data/ivt2009/11-2011/raw/alters2.txt"); TIntIntHashMap idMapping = idMapping(files); BufferedReader reader = new BufferedReader( new FileReader("/Users/jillenberger/Work/socialnets/data/ivt2009/webdiary/webdiary.xy.csv")); String line = reader.readLine(); String[] tokens = line.split(";"); int userIdx = getIndex("\"User_ID\"", tokens); int typeIdx = getIndex("\"Zweck_ID\"", tokens); int latIdx = getIndex("\"lat\"", tokens); int longIdx = getIndex("\"long\"", tokens); int membersIdx = getIndex("\"Aktivitaet_Andere_Mitglieder\"", tokens); int notfound = 0; int valid = 0; int nomembers = 0; while ((line = reader.readLine()) != null) { tokens = line.split(";"); int type = Integer.parseInt(tokens[typeIdx]); if (type == 8) { // double lat = Double.parseDouble(tokens[latIdx]); double lat = Double.parseDouble(tokens[tokens.length - 2]); double lon = Double.parseDouble(tokens[tokens.length - 1]); int id = Integer.parseInt(tokens[userIdx]); if (idMapping.contains(id)) { int id2 = idMapping.get(id); SocialVertex v = map.get(String.valueOf(id2)); Point p1 = v.getPoint(); if (p1 != null) { Point p2 = factory.createPoint(new Coordinate(lon, lat)); double d = distCalc.distance(p1, p2); int members = -1; if (!tokens[membersIdx].equals("\"NULL\"")) { try { members = Integer.parseInt(tokens[membersIdx]); } catch (Exception e) { e.printStackTrace(); } } else { members = 0; nomembers++; } if (members >= 0 && d < 300000) { // xvals.add(v.getNeighbours().size()); // yvals.add(members); xvals.add(members); yvals.add(d); valid++; } } } else { // System.err.println("Ego not found."); notfound++; } } } System.out.println("Valid samples = " + valid); System.err.println("No members = " + nomembers); System.err.println("Not found = " + notfound); Discretizer disc = new FixedBordersDiscretizer(new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 60 }); // correl = Correlations.mean(xvals.toNativeArray(), yvals.toNativeArray(), FixedSampleSizeDiscretizer.create(xvals.toNativeArray(), 20, 20)); correl = Correlations.mean(xvals.toNativeArray(), yvals.toNativeArray(), disc); // TXTWriter.writeMap(correl, "members", "disr", "/Users/jillenberger/Work/socialnets/data/ivt2009/webdiary/members_k.txt"); TXTWriter.writeMap(correl, "members", "disr", "/Users/jillenberger/Work/socialnets/data/ivt2009/webdiary/d_members.txt"); TDoubleObjectHashMap<DescriptiveStatistics> correl2 = Correlations.statistics(xvals.toNativeArray(), yvals.toNativeArray(), disc); // TXTWriter.writeBoxplotStats(correl2,"/Users/jillenberger/Work/socialnets/data/ivt2009/webdiary/d_members.stats.txt"); TXTWriter.writeStatistics(correl2, "members", "/Users/jillenberger/Work/socialnets/data/ivt2009/webdiary/d_members.stats.txt"); DescriptiveStatistics stats = new DescriptiveStatistics(xvals.toNativeArray()); TDoubleDoubleHashMap hist = Histogram.createHistogram(stats, new DummyDiscretizer(), false); TXTWriter.writeMap(hist, "members", "n", "/Users/jillenberger/Work/socialnets/data/ivt2009/webdiary/members.txt"); DescriptiveStatistics dists = new DescriptiveStatistics(yvals.toNativeArray()); System.out.println("Mean dist = " + dists.getMean()); }