1 10 package mondrian.udf; 11 12 import org.apache.commons.math.MathException; 13 import org.apache.commons.math.distribution.DistributionFactory; 14 import org.apache.commons.math.distribution.NormalDistribution; 15 import org.apache.log4j.Logger; 16 17 import mondrian.olap.Evaluator; 18 import mondrian.olap.Syntax; 19 import mondrian.olap.fun.MondrianEvaluationException; 20 import mondrian.olap.type.NumericType; 21 import mondrian.olap.type.Type; 22 import mondrian.spi.UserDefinedFunction; 23 24 25 44 public class InverseNormalUdf implements UserDefinedFunction { 45 private static final Logger LOGGER = Logger.getLogger(InverseNormalUdf.class); 46 47 private static DistributionFactory distributionFactory = DistributionFactory.newInstance(); 48 private static NormalDistribution nd = distributionFactory.createNormalDistribution(); 49 50 public String getName() { 51 return "InverseNormal"; 52 } 53 54 public String getDescription() { 55 return "Returns inverse normal distribution of its argument"; 56 } 57 58 public Syntax getSyntax() { 59 return Syntax.Function; 60 } 61 62 public Type getReturnType(Type[] types) { 63 return new NumericType(); 64 } 65 66 public Type[] getParameterTypes() { 67 return new Type[] {new NumericType()}; 68 } 69 70 public Object execute(Evaluator evaluator, Argument[] args) { 71 final Object argValue = args[0].evaluateScalar(evaluator); 72 LOGGER.debug("Inverse Normal argument was : " + argValue); 73 if (!(argValue instanceof Number )) { 74 return null; 78 } 79 80 final Double d = new Double (((Number ) argValue).doubleValue()); 81 LOGGER.debug("Inverse Normal argument as Double was : " + d); 82 83 if (d.isNaN()) { 84 return null; 85 } 86 92 double dbl = d.doubleValue(); 93 if (dbl < 0.0 || dbl > 1.0) { 94 LOGGER.debug("Invalid value for inverse normal distribution: " + dbl); 95 throw new MondrianEvaluationException("Invalid value for inverse normal distribution: " + dbl); 96 } 97 try { 98 Double result = new Double (nd.inverseCumulativeProbability(dbl)); 99 LOGGER.debug("Inverse Normal result : " + result.doubleValue()); 100 return result; 101 } catch (MathException e) { 102 LOGGER.debug("Exception calculating inverse normal distribution: " + dbl, e); 103 throw new MondrianEvaluationException("Exception calculating inverse normal distribution: " + dbl); 104 } 105 } 106 107 public String [] getReservedWords() { 108 return null; 109 } 110 111 } 112 113 | Popular Tags |