At Bridgewater we have a philosophy of systemization - we build automated trading platforms where we can rapidly iterate and improve our strategies. As such, data quality becomes important to avoid costly errors. In order to ensure we're using accurate data for trading decisions, we've also automated much of the data validation logic. While some error checking (e.g. detecting 0s in data which should only contain positive numbers) is easy, other error checking is much harder (e.g. sporadic aberrations in pricing data). Most error checking done is through stream algorithms as the quantity and pace of data is far too much to analyze the data statically.
Write a moving average function that will take the average of the trailing N samples of market data samples. The first line of the input will be an integer, N, which will define the window of the moving average function. Output the moving average of the data starting with then Nth data point and continuing until the end of the file. You should use 3 decimal places of precision.
Note: Your algorithm should process the information continually as a stream i.e. begin outputting the moving average after the Nth datapoint and continuing until the end of the input file.
For instance, the following input:
2
1.000
2.000
3.000
4.000
Should yield the output:
1.500
2.500
3.500
package bridgewater; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.List; public class MovingAverageSolution { private List<String> inputs = new ArrayList<String>(); private int winSize = 0; public MovingAverageSolution(){ } public static void main(String[] args) throws Exception { BufferedReader br = null; MovingAverageSolution solution = new MovingAverageSolution(); try { InputStream inputStream = new FileInputStream("input1.txt"); Reader reader = new InputStreamReader(inputStream); br = new BufferedReader(reader); // br = new BufferedReader(new InputStreamReader(System.in)); solution.winSize = Integer.parseInt(br.readLine()); String input = br.readLine(); while(input != null){ solution.inputs.add(input); solution.process(solution.inputs); input = br.readLine(); } } finally { if (br != null) br.close(); } } public void process(List<String> inputs) { // System.out.println(inputs.size()); if(winSize<=0 || inputs.size() < winSize){ return; } double sum = 0; for(int i=inputs.size()-1; i>=inputs.size()-winSize; i--){ sum += Double.parseDouble(inputs.get(i)); } double average = sum*1.0 / winSize; System.out.println(String.format("%.3f", average)); } }
Write a moving median function that will take the median of the trailing N samples of market data samples and identify deviations from that median that vary by more than a percentage, P. The first line of the input will be an integer, N, which will define the window size of the moving median function; the second line will be a decimal, P, which defined the maximumpercentage change allowed in the data. Output any number which exceeds this percentage change (note: you should still include this numbers in your moving median). You should use 3 decimal places of precision.
Note: Your algorithm should process the information continually as a stream i.e. begin outputting the moving average after the Nth datapoint and continuing until the end of the input file.
For instance, the following input:
3
0.500
5.000
5.000
7.000
10.000
7.000
7.000
12.000
7.000
7.000
Should yield the output:
10.000
12.000
Since 10 and 12 each vary by more than 50% from the median of the previous 3 values.
package bridgewater; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MovingMedianSolution { private List<String> inputs = new ArrayList<String>(); private int winSize = 0; private double maxChange = 0; public MovingMedianSolution(){ } public static void main(String[] args) throws Exception { BufferedReader br = null; MovingMedianSolution solution = new MovingMedianSolution(); try { InputStream inputStream = new FileInputStream("input2.txt"); Reader reader = new InputStreamReader(inputStream); br = new BufferedReader(reader); // br = new BufferedReader(new InputStreamReader(System.in)); solution.winSize = Integer.parseInt(br.readLine()); solution.maxChange = Double.parseDouble(br.readLine()); String input = br.readLine(); while(input != null){ solution.inputs.add(input); solution.process(solution.inputs); input = br.readLine(); } } finally { if (br != null) br.close(); } } public void process(List<String> inputs) { if(winSize<=0 || maxChange<0 || inputs.size() < winSize+1){ return; } ArrayList<Double> al = new ArrayList<Double>(); for(int i=inputs.size()-2; i>=inputs.size()-1-winSize; i--){ al.add(Double.parseDouble(inputs.get(i))); } Collections.sort(al); double median = al.get(al.size()/2); // even size if((al.size() & 1) == 0){ // When it is odd size median = al.get(al.size()/2-1); } double cur = Double.parseDouble(inputs.get(inputs.size()-1)); if(Math.abs(cur-median)*1.0/median > maxChange){ System.out.println(String.format("%.3f", cur)); } } }