Java Time Specific Average

I have a text file:

DATE 20090105
1 2.25 1.5
3 3.6 0.099
4 3.6 0.150
6 3.6 0.099
8 3.65 0.0499
DATE 20090105
DATE 20090106
1 2.4 1.40
2 3.0 0.5
5 3.3 0.19
7 2.75 0.5
10 2.75 0.25
DATE 20090106
DATE 20090107
2 3.0 0.5
2 3.3 0.19
9 2.75 0.5
DATE 20100107

On each day I have:

Time Rating Variance

I want to work out the average variance at a specific time on the biggest time scale.

The file is massive and this is just a small edited sample. This means I don't know the latest time and the earliest time (it's around 2600) and the latest time may be around 50000.

So for example on all the days I only have 1 value at time t=1, hence that is the average variance at that time.

At time t=2, on the first day, the variance at time t=2 takes value 1.5 as it last until t=3, on the second day it takes value=0.5 and on the third day it takes value ((0.5+0.18)/2). So the avg variance over all the days at time t=2 is the sum of all the variances at that time, divided by the number of different variances at that time.

For the last time in the day, the time scale it takes is t=1.

I'm just wondering as to how I would even go about this.

As a complete beginner I'm finding this quite complicated. I am a Uni Student, but university is finished and I am trying to learn Java to help out with my Dads business over the summer. So any help with regards to solutions is greatly appreciated.

This question and answers originated from www.stackoverflow.com
Question by (7/13/2010 1:54:47 PM)

Answer

Ok, I've got a code which works. But it takes a very long time(around 7 months worth of day, with 30,000 variances a day) because it has to loop round so many times. Are there any other better suggestions?

I mean this code, for something seemingly simple, would take around 24-28 hours...

package VarPackage;

import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList;

public class ReadText {

public static void main(String[] args) throws Exception {
    String inputFileName="C:\\MFile";


    ArrayList<String> fileLines = new ArrayList<String>();
    FileReader fr;
    BufferedReader br;

    // Time
    int t = 1;


    fr = new FileReader(inputFileName);
    br = new BufferedReader(fr);
    String line;


    while ((line=br.readLine())!=null) {
     fileLines.add(line);
    }

    AvgVar myVar = new AvgVar(fileLines);

    for(t=1; t<10; t++){ 
    System.out.print("Average Var at Time t=" + t + " = " + myVar.avgVar(t)+"\n");

}

} }

===================================

NewClass

package VarPackage;

import java.util.ArrayList;

public class AvgVar { // Class Variables private ArrayList inputData = new ArrayList();

// Constructor AvgVar(ArrayList fileData){ inputData = fileData; }

public double avgVar(int time){

 double avgVar = 0;

 ArrayList<double[]> avgData = avgDuplicateVars(inputData);

 for(double[] arrVar : avgData){
 avgVar += arrVar[time-1];
 //System.out.print(arrVar[time-1] + "," + arrVar[time] + "," + arrVar[time+1] + "\n");
 //System.out.print(avgVar + "\n");
 }

 avgVar /= numDays(inputData);

 return avgVar;
}

private int numDays(ArrayList<String> varData){

 int n = 0;
 int flag = 0;

for(String line : varData){

String[] myData = line.split(" ");

if(myData[0].equals("DATE") && flag == 0){

    flag = 1;

   }
   else if(myData[0].equals("DATE") && flag == 1){

    n = n + 1;
    flag = 0;

   }

}

return n;

}

private ArrayList<double[]> avgDuplicateVars(ArrayList<String> varData){

 ArrayList<double[]> avgData = new ArrayList<double[]>();

 double[] varValue = new double[86400];
 double[] varCount = new double[86400];

 int n = 0;
 int flag = 0;

for(String iLine : varData){

String[] nLine = iLine.split(" ");
   if(nLine[0].equals("DATE") && flag == 0){

    for (int i=0; i<86400; i++){
    varCount[i] = 0;
    varValue[i] = 0;
    }

    flag = 1;

   }
   else if(nLine[0].equals("DATE") && flag == 1){

    for (int i=0; i<86400; i++){
    if (varCount[i] != 0){
    varValue[i] /= varCount[i];
    }
    }

    varValue = fillBlankSpreads(varValue, 86400);

    avgData.add(varValue.clone());

    flag = 0;

   }
   else{

    n = Integer.parseInt(nLine[0])-1;

    varValue[n] += Double.parseDouble(nLine[2]);
    varCount[n] += 1;

   }

}

return avgData;

}

private double[] fillBlankSpreads(double[] varValue, int numSpread){
//Filling the Data with zeros to make the code faster
 for (int i=1; i<numSpread; i++){
 if(varValue[i] == 0){
 varValue[i] = varValue[i-1];
 }
 }

 return varValue;
}

}

Answer by

Find More Answers
Related Topics  java  time  average
Related Questions
  • Mysql Average on time column?

    SELECT avg( duration ) as average FROM `login`; The datatype for duration is "time", thus my value is like: 00:00:14, 00:20:23 etc I execute the query it gives me: 2725.78947368421 What is…
  • Average Time to Reply to Message

    Is it possible to calculate the average time to reply to a message just with the following columns: id | ref | client | admin | date | message id is the unique message number ref is the m…
  • Average week calculation for different time dimensions

    I have the following time dimensions: 1) Year-Week-Date 2) Year-Month-Date 3) Year-Tertial-Month-Date 4) Year-Quarter-Month-Date What I want is a calculation that shows the Average Week Sa…
  • Calculate average in java

    EDIT: Ive written code for the average, but i dont know how to make it so that it also uses ints from my args.length rather than the array I need to write a java program that can calculate: 1. th…
  • SQL Work out the average time difference between total rows

    I've searched around SO and can't seem to find a question with an answer that works fine for me. I have a table with almost 2 million rows in, and each row has a MySQL Date formatted field. I'd l…
  • Java calculating average of numbers from another file

    public void computeAverage(String [] names, int [] scores, char [] grades){ int av = 0; for (int i = 0; i < names.length; i++) av = (scores[i]++ / 26); System.out.print(av); } Hey …
  • phpmyadmin average time

    I am working with a joomla helpdesk component (Huruhelpdesk) and i need to work a query to see the average time of the tickets since the user open the ticket (start_date) till the reps close the tic…
  • Taking the time average of the product of two waves

    How might one go about calculating the time average of the product of two identical waves with different phases? For example, what would be the time average of: $$ \cos(k x-w t) \cdot \cos(kx-wt+…
  • run a Java program in specific time

    i need help to run my Java program on the server at a specific time like 2 pm (to index the new files). Someone told me that Java has some thing called jobs but I don't know how to work with that…
  • Java/Sql: return an average from a specific column problem

    I'm using embedded SQL with Java. I have a simple table where I want to return an average from the column that contains temperatures. I tried to use ResultSet but it wasn't successful, I think this …