math
Class Stats

java.lang.Object
  extended by math.Stats

public class Stats
extends java.lang.Object

User: lyon Date: Aug 27, 2007 Time: 7:15:31 AM Copyright DocJava, Inc. 2005.


Constructor Summary
Stats()
           
 
Method Summary
static double avg(double[] source, int period, int startPoint)
           
static double bestFit(double[] source, int period)
          Calculate the line of best fit of the data given by source.
static double[] bestFitFunction(double[] source, int period)
          Return the equation of the line of best fit of the data given by source.
static double bollingerLower(double[] source, int period, double standardDeviation)
          Calculate the lower band of the bollinger graph.
static double bollingerUpper(double[] source, int period, double standardDeviation)
          Calculate the upper band of the bollinger graph.
 double corr(double[] x, double[] y, int period)
          Calculate the Pearson product-moment correlation between the two variables.
static double[] cumProbability(double[] data, double[] x)
          This method calculates the cumulative probability curve of array data at locations x.
static double cumulativeNormalDistributionFunction(double x)
           
static double ema(double[] source, int period, double smoothingConstant)
          Calculate the Exponential Moving Average (EMA) value.
static double ema(double[] source, int period, int startPoint, double smoothingConstant)
           
static double getAverage(byte[] in)
           
static double getAverage(double[] in)
           
static double getAverage(short[] in)
           
static double getAverageUnsigned(byte[] in)
           
static double[] getAvg(double[] source, int period)
          Find the average of the given data.
static double getCoefficientOfVariation(double[] doubles)
          Compute the coefficient of variation by dividing the standard deviation by the sample mean
static double[] getEma(double[] source, int period, double smoothingConstant)
          Calculate the Exponential Moving Average (EMA) value.
static double getMean(double[] source, int period)
          Find the average of the given quotes.
static double getStandardDeviation(double[] d)
           
static double getStandardDeviation(double[] source, int period)
          Find the standard deviation of the given values.
static double getVariance(double[] data)
          This method calculates variance of a data set.
static double[] getWindowedSums(double[] source, int period)
          Find the sum of the given data.
static double getWindowSum(double[] source, int period, int startPoint)
          Add up the data from the start point for period data points
static double interp1(double[] x, double[] y, double x0)
          This method linearly interpolates the (x,y) data at points x0.
static double macd(double[] sourceSlow, double[] sourceFast)
          Calculate the Moving Average Convergence Divergence (MACD) value.
static double median(double[] data)
          This method calculates the median of a data set.
static double momentum(double[] source, int period)
          Calculate the Momentum value.
static int obv(double[] sourceOpen, double[] sourceClose, double[] sourceVolume, int range, int initialValue)
          Calculate the On Balance Volume (OBV) value.
static double powerMovingAverage(double[] sampleData, int startPoint, int period)
          startpoint < sampleData.length - period
static double roundDouble(double d, int places)
           
static double rsi(double[] source, int period)
          Calculate the Relative Strength Indicator (RSI) value.
static double sma(double[] sampleData, int startPoint, int period)
           
static double stdev(double[] source, int period, int startpoint)
          Find the standard deviation of the given values.
static void testSma()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Stats

public Stats()
Method Detail

stdev

public static double stdev(double[] source,
                           int period,
                           int startpoint)
Find the standard deviation of the given values. This algorithm will calculate the standard deviation of the first period days. If a quote is missing on any of the days, then that day will be skipped and the function will find the average of the shorter period.

Parameters:
source - the source quotes
period - the number of days to average
Returns:
the standard deviation

avg

public static double avg(double[] source,
                         int period,
                         int startPoint)

getWindowSum

public static double getWindowSum(double[] source,
                                  int period,
                                  int startPoint)
Add up the data from the start point for period data points

Parameters:
source -
period -
startPoint -
Returns:

getAvg

public static double[] getAvg(double[] source,
                              int period)
Find the average of the given data. This function will calculate the average of the first period days. If a quote is missing on any of the days, then that day will be skipped and the function will find the average of the shorter period.

Parameters:
source - source of quotes to average
period - the number of days to average
Returns:
the average

getAverage

public static double getAverage(double[] in)

getAverage

public static double getAverage(short[] in)

getAverage

public static double getAverage(byte[] in)

getAverageUnsigned

public static double getAverageUnsigned(byte[] in)

getWindowedSums

public static double[] getWindowedSums(double[] source,
                                       int period)
Find the sum of the given data. This function will calculate the sum of the first period days. If a quote is missing on any of the days, then that day will be skipped and the function will find the sum of the shorter period.

Parameters:
source - source of data to sum
period - the number of days to sum
Returns:
the sum

getStandardDeviation

public static double getStandardDeviation(double[] source,
                                          int period)
Find the standard deviation of the given values. This algorthim will calculate the standard deviation of the first period days. If a quote is missing on any of the days, then that day will be skipped and the function will find the average of the shorter period.

Parameters:
source - the source quottes
period - the number of days to average
Returns:
the standard deviation

getStandardDeviation

public static double getStandardDeviation(double[] d)

getMean

public static double getMean(double[] source,
                             int period)
Find the average of the given quotes. This function will calculate the average of the first period days.

Parameters:
source - source of quotes to average
period - the number of days to average
Returns:
the average

getEma

public static double[] getEma(double[] source,
                              int period,
                              double smoothingConstant)
Calculate the Exponential Moving Average (EMA) value. The Exponential Moving Average is a weighted moving average where the most recent values are weighted higher than the previous values.

The formula for the EMA is as follows:

EMA(current) = EMA(previous) + k * (day close - EMA(previous))

Where EMA(current) is the current EMA value you are calculating, EMA(previous) is the previous value and k is a smoothing constant.

Parameters:
source - the source of quotes to average
period - the number of days to analyse
smoothingConstant -
Returns:
the exponential moving average

ema

public static double ema(double[] source,
                         int period,
                         int startPoint,
                         double smoothingConstant)

corr

public double corr(double[] x,
                   double[] y,
                   int period)
Calculate the Pearson product-moment correlation between the two variables. This will return a correlation co-efficient which is in the range of -1 (negative correlation) through to (no correlation) through to 1 (perfect correlation.

The correlation co-efficient is calculated as follows:

 r = sum(Zx * Zy)
     ------------
         N - 1
 

Where Zx = X - E(X) -------- Sx

Where E(X) is the mean of X and Sx is the standard deviation of X.

Simillarly for Zy.

Parameters:
x - values to test against
y - values to detect correlation against x
period - number of days to analyse
Returns:
the correlation co-efficient

rsi

public static double rsi(double[] source,
                         int period)
Calculate the Relative Strength Indicator (RSI) value. Technical Analysis by Martin J. Pring describes the RSI as:

"It is a momentum indicator, or oscillator, that measures the relative internal strength of a security against itself....".

The formula for the RSI is as follows:

 

100 RSI = 100 - ------ 1 + RS

average of x days' up closes RS = ------------------------------ average of x days' down closes

To calculate an X day RSI you need X + 1 quote values. So make the period argument one more day that the period of the RSI.

Returns:
RSI

ema

public static double ema(double[] source,
                         int period,
                         double smoothingConstant)
Calculate the Exponential Moving Average (EMA) value. The Exponential Moving Average is a weighted moving average where the most recent values are weighted higher than the previous values.

The formula for the EMA is as follows:

EMA(current) = EMA(previous) + k * (day close - EMA(previous))

Where EMA(current) is the current EMA value you are calculating, EMA(previous) is the previous value and k is a smoothing constant.

Parameters:
source - the source of quotes to average
period - the number of days to analyse
smoothingConstant - a smoothing constant
Returns:
the exponential moving average

macd

public static double macd(double[] sourceSlow,
                          double[] sourceFast)
Calculate the Moving Average Convergence Divergence (MACD) value. The Moving Average Convergence Divergence is the remainder of the 26 days EMA and the 12 days EMA. The smoothing constant for the EMA functions is set to 0.1.

The formula for the MACD is as follows:

MACD = EMA(26) - EMA(12)

Where EMA(26) is the 26 days EMA and EMA(12) is the 12 days EMA.

Parameters:
sourceSlow - the source of quotes used by EMA to average (slow average)
sourceFast - the source of quotes used by EMA to average (fast average)
Returns:
the moving average convergence divergence

momentum

public static double momentum(double[] source,
                              int period)
Calculate the Momentum value. The Moving Average Convergence Divergence is the remainder of the today value and the period delayed value.

The formula for the Momentum is as follows:

Momentum = Quote(Today) - Quote(Today+1-period)

Where Quote is got from the input parameter: source.

Parameters:
source - the source of quotes
Returns:
the momentum

obv

public static int obv(double[] sourceOpen,
                      double[] sourceClose,
                      double[] sourceVolume,
                      int range,
                      int initialValue)
Calculate the On Balance Volume (OBV) value. The On Balance Volume is counted adding or subtracting the day volume from range until today, starting from an initial value.

The formula for the OBV is as follows:

if close(current)>open(current): OBV(current) = OBV(previous) + Volume(current) if close(current)

Parameters:
sourceOpen - the source of open quotes
sourceClose - the source of close quotes
sourceVolume - the source of volumes
range - the range which we calculate over
initialValue - the starting value of OBV
Returns:
the on balance volume value

bollingerUpper

public static double bollingerUpper(double[] source,
                                    int period,
                                    double standardDeviation)
Calculate the upper band of the bollinger graph. The upper band can be calculated by:

BollingerUpper = Average + 2 * SD

Where SD is the standard deviation.

Parameters:
source - the source of quotes
period - the number of days to analyse
Returns:
the upper bollinger band

bollingerLower

public static double bollingerLower(double[] source,
                                    int period,
                                    double standardDeviation)
Calculate the lower band of the bollinger graph. The lower band can be calculated by:

BollingerLower = Average - 2 * SD

Where SD is the standard deviation.

Parameters:
source - the source of quotes
period - the number of days to analyse
Returns:
the lower bollinger band

roundDouble

public static double roundDouble(double d,
                                 int places)

bestFit

public static double bestFit(double[] source,
                             int period)
Calculate the line of best fit of the data given by source.

using the formula: slope = period * Sum(xy) - Sum(x)Sum(y) / period * Sum(x^2) - (Sum(x))^2 intercept = ( Sum(y) - slope * Sum(x) ) / period

Parameters:
source - the source of quotes
period - the number of days to analyse
Returns:
the value of the trend at the end of period

bestFitFunction

public static double[] bestFitFunction(double[] source,
                                       int period)
Return the equation of the line of best fit of the data given by source. Uses the same formula as bestFit, but returns slope and intercept so that it can be used on charts.

Parameters:
source - the source of quotes
period - the number of days to analyse
Returns:
the value of the trend at the end of period

testSma

public static void testSma()

sma

public static double sma(double[] sampleData,
                         int startPoint,
                         int period)
Parameters:
sampleData -
startPoint - - offset into the sample data
period - - interval of the average
Returns:

powerMovingAverage

public static double powerMovingAverage(double[] sampleData,
                                        int startPoint,
                                        int period)
startpoint < sampleData.length - period

Parameters:
sampleData -
startPoint - - offset into the sample data
period - - interval of the average
Returns:

getCoefficientOfVariation

public static double getCoefficientOfVariation(double[] doubles)
Compute the coefficient of variation by dividing the standard deviation by the sample mean

Parameters:
doubles -
Returns:
a number that is multiplied by 100 to obtain percentage points

cumulativeNormalDistributionFunction

public static double cumulativeNormalDistributionFunction(double x)

median

public static double median(double[] data)
This method calculates the median of a data set.

Parameters:
data - The input data set
Returns:
the median of data.

getVariance

public static double getVariance(double[] data)
This method calculates variance of a data set. This method normalizes by n-1 where n is the sequence length, so it is the best unbiased estimate of variance if the data set is sampled from a normal distribution.

Parameters:
data - The input data set
Returns:
the variance of data.

cumProbability

public static double[] cumProbability(double[] data,
                                      double[] x)
This method calculates the cumulative probability curve of array data at locations x.

Parameters:
data - The input data set
x - The locations to calculate cumulative probability
Returns:
the cumulative probabilities

interp1

public static double interp1(double[] x,
                             double[] y,
                             double x0)
This method linearly interpolates the (x,y) data at points x0. In this method, if x0 < x[0], y0 = y[0], and if x0 > x[x.length-1], y0 = y[x.length-1].

Parameters:
x - Input x values
y - Input y values
x0 - Locations to interpolate
Returns:
the interpolated values