/*-
 * Copyright 2016 Diamond Light Source Ltd.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.eclipse.dawnsci.analysis.dataset.impl;

public class Outliers {

	private final static double MADSCALEFACTOR = 1.4826;
	private final static double SNSCALEFACTOR = 1.1926;
	
	public static double[] medianAbsoluteDeviation(Dataset data) {
		
		double median = (Double)Stats.median(data);
		data = Maths.subtract(data, median);
		data = Maths.abs(data);
		double median2 = (Double)Stats.median(data);
		double mad = MADSCALEFACTOR * median2;
		
		return new double[]{mad, median};
	}
	
	public static double snNaive(Dataset data) {
		
		Dataset medAbs = DatasetFactory.zeros(data);
		Dataset dif = DatasetFactory.zeros(data);
		
		IndexIterator it = data.getIterator();
		int count = 0;
		while (it.hasNext()) {
			double val = data.getElementDoubleAbs(it.index);
			Maths.subtract(data, val, dif);
			Maths.abs(dif,dif);
			//Lower median - Math.floor((n/2)+1) of sorted
			dif.sort(null);
			medAbs.setObjectAbs(count++, dif.getElementDoubleAbs((int)Math.floor((dif.getSize()/2)+1)));
		}
		
		
		//Higher median - Math.floor((n+1)/2) of sorted
		medAbs.sort(null);
		double median = medAbs.getElementDoubleAbs((int)Math.floor((medAbs.getSize()+1)/2));
		
		return median * SNSCALEFACTOR;
	}
	
	public static double lowMed(Dataset data) {
		return data.getElementDoubleAbs((int)Math.floor((data.getSize()/2)+1));
	}
	
	public static double highMed(Dataset data) {
		return data.getElementDoubleAbs((int)Math.floor((data.getSize()+1)/2));
	}
	
	
}

@@ -1232,68 +1232,18 @@
 ++, 
-dif.getElementDoubleAbs((int)Math.floor((dif.getSize()/2)+1)
+lowMed(dif
 ));%0A
@@ -1345,74 +1345,22 @@
 n = 
-medAbs.getElementDoubleAbs((int)Math.floor((medAbs.getSize()+1)/2)
+highMed(medAbs
 );%0A%09
@@ -1512,18 +1512,16 @@
 ize()/2)
-+1
 ));%0A%09%7D%0A%09
@@ -1637,16 +1637,18 @@
 e()+1)/2
+-1
 ));%0A%09%7D%0A%09

Allow for Java's 0-based arrays, rather than the paper's 1-based arrays.