两道面试题解答之二 Moving Average, Moving Median

原创 2013年12月05日 05:57:58

Question 1 / 2 (Moving Average)

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));
	}

}




Question 2 / 2 (Moving Median and Error Detection)

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));
		}
	}

}












PCL: Surface模块之MovingLeastSquares(滑动最小二乘法)

背景知识:
  • u012337034
  • u012337034
  • 2014年07月07日 21:21
  • 5505

滑动均值Moving Average与中值滤波Moving Median

1. 滑动均值滤波 方法: 把连续取N个采样值看成一个队列,队列的长度固定为N,每次采样到一个新数据放入队尾,并扔掉原来队首的一次数据.(先进先出原则)。把队列中的N个数据进行算术平均运算,就可获得新...
  • yaked
  • yaked
  • 2016年08月14日 23:31
  • 2432

EWMA 指数加权移动平均 模型

Exponentially Weighted Moving Average(EWMA)指数加权移动平均是一种常用的序列数据处理方式,如下: 在时间 t, 根据实际的观测值(或量测值)我们可以求取...
  • x_i_y_u_e
  • x_i_y_u_e
  • 2015年03月11日 09:50
  • 6436

滑动均值Moving Average与中值滤波Moving Median

1. 滑动均值滤波 方法: 把连续取N个采样值看成一个队列,队列的长度固定为N,每次采样到一个新数据放入队尾,并扔掉原来队首的一次数据.(先进先出原则)。把队列中的N个数据进行算术平均运算,就可获得新...
  • yaked
  • yaked
  • 2016年08月14日 23:31
  • 2432

移动平均法又称滑动平均法、滑动平均模型法(Moving average,MA)

什么是移动平均法?  移动平均法是用一组最近的实际数据值来预测未来一期或几期内公司产品的需求量、公司产能等的一种常用方法。移动平均法适用于即期预测。当产品需求既不快速增长也不快速下降,且不存在季节性因...
  • BVL10101111
  • BVL10101111
  • 2016年11月30日 10:53
  • 5010

滑动平均法、滑动平均模型法(Moving average,MA)

什么是移动平均法?   移动平均法是用一组最近的实际数据值来预测未来一期或几期内公司产品的需求量、公司产能等的一种常用方法。移动平均法适用于即期预测。当产品需求既不快速增长也不快速下降,且不存在...
  • qq_39521554
  • qq_39521554
  • 2018年01月10日 20:35
  • 66

9、Tensorflow: 移动平均法又称滑动平均法、滑动平均模型法(Moving average,MA)

什么是移动平均法?     移动平均法是用一组最近的实际数据值来预测未来一期或几期内公司产品的需求量、公司产能等的一种常用方法。移动平均法适用于即期预测。当产品需求既不快速增长也不快速下降...
  • qq_38906523
  • qq_38906523
  • 2017年12月16日 13:32
  • 127

一、MA(Moving Average)移动平均

一、MA(Moving Average)移动平均一MAMoving Average移动平均 介绍 特点 常用移动平均线 WMA Weigted Moving Average 加权移动平均线 EMA E...
  • zonnin
  • zonnin
  • 2017年11月15日 02:20
  • 92

移动平均法(Moving average,MA) 指数平滑法(Exponential Smoothing,ES)

____tz_zs 注:本博客概念解释部分均来自 MBA智库百科 一、移动平均法(Moving average,MA) 移动平均法 - MBA智库百科 移动平均法又称滑动平均法、滑...
  • tz_zs
  • tz_zs
  • 2017年10月25日 14:40
  • 365

MT4平台自带的EA :Moving Average 详解

MT4平台自带的EA :Moving Average 详解 使用过MT4平台的人都知道,MT4平台自带了一个自动交易的EA指标,那就是Moving Average ,下面我们详细解释一下该指标的语句...
  • ljw
  • ljw
  • 2011年10月25日 22:55
  • 2628
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:两道面试题解答之二 Moving Average, Moving Median
举报原因:
原因补充:

(最多只允许输入30个字)