Java tutorial
/** * The Clican-Pluto software suit is Copyright 2009, Clican Company and individual contributors, and is licensed under the GNU LGPL. * * @author clican * */ package com.clican.pluto.dataprocess.dpl.function.impl; import java.util.List; import java.util.Map; import org.apache.commons.math.stat.descriptive.moment.Variance; import com.clican.pluto.dataprocess.dpl.parser.bean.PrefixAndSuffix; import com.clican.pluto.dataprocess.exception.CalculationException; import com.clican.pluto.dataprocess.exception.DplParseException; import com.clican.pluto.dataprocess.exception.PrefixAndSuffixException; /** * SharpeRatio * * @author clican * */ public class SharpeRatio extends BaseMultiRowFunction { /** * ??,? */ private PrefixAndSuffix valuePas; public Object calculate(List<Map<String, Object>> rowSet) throws CalculationException, PrefixAndSuffixException { if (rowSet.size() == 0) { throw new CalculationException("SharpeRatio??"); } double[] values = new double[rowSet.size()]; double sum = 0; for (int i = 0; i < rowSet.size(); i++) { Map<String, Object> row = rowSet.get(i); Double value = valuePas.getValue(row); values[i] = value; sum += value; } double avg = sum / (rowSet.size()); Variance var = new Variance(false); Double result = Math.sqrt(var.evaluate(values, avg)); if (log.isDebugEnabled()) { log.debug("FRR[" + avg + "],STDEV[" + result + "]"); } return avg / result; } public void setParams(List<Object> params) throws DplParseException { super.setParams(params); valuePas = this.pasList.get(0); } } // $Id: SharpeRatio.java 16260 2010-07-16 09:20:15Z wei.zhang $