diff --git a/drawing/papaya/examples/BoxPlotExample/BoxPlotExample.pde b/drawing/papaya/examples/BoxPlotExample/BoxPlotExample.pde
new file mode 100644
index 0000000..01ffa54
--- /dev/null
+++ b/drawing/papaya/examples/BoxPlotExample/BoxPlotExample.pde
@@ -0,0 +1,109 @@
+/*
+ Sketch demonstrating how to plot box plots using the BoxPlot class.
+ Author: Adila Faruk
+ */
+
+// import the library
+import papaya.*;
+//import papaya.BoxPlot;
+// --------------------- Sketch-wide variables ----------------------
+BoxPlot[] boxPlot = new BoxPlot[2];
+PFont titleFont, mediumFont, smallFont;
+
+float[] breaks;
+int[] tension;
+int minBreaks, maxBreaks;
+float[][] fiveNumSummary;
+
+// boxplot labels
+String[] xLabels = new String[] {
+ "L","M","H"
+};
+
+
+// -----------------------------------
+void setup() {
+ size(630, 500);
+ smooth();
+ noLoop();
+
+ titleFont = createFont("Helvetica", 22, true);
+ smallFont = createFont("Helvetica", 11, true);
+ mediumFont = createFont("Helvetica", 14, true);
+ // Read in the data
+ // This data set gives the number of warp breaks per loom,
+ // where a loom corresponds to a fixed length of yarn.
+ // breaks: number of breaks in wool
+ // tension: tension (L,M,H).
+ String lines[] = loadStrings("warpbreaks.txt");
+
+ int ln = lines.length-1;
+ breaks = new float[ln];
+ tension = new int[ln];
+
+ for (int i=0; i
+
+
+
+
+
+Each package has a page that contains a list of its classes and interfaces, with a summary for each. This page can contain four categories:
+Each class, interface, nested class and nested interface has its own separate page. Each of these pages has three sections consisting of a class/interface description, summary tables, and detailed member descriptions:
+
+
+Each annotation type has its own separate page with the following sections:
+Each enum has its own separate page with the following sections:
+
+
+
+
+This help file applies to API documentation generated using the standard doclet.
+
+
+BoxPlot class
+
+
+
+
+
+
+
+
+
+
+
+
+Static Class for casting Object arrays to their corresponding primitive type.
+ Similar to the
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Methods related to comparing two populations. Each method returns an array with the
+ first element containing the t-statistic, and the second corresponding to the
+ p-value, or significance, of rejecting the null hypothesis.
+
+ Tests can be:y
data and the best-fit linear
+ * line for that data.
+ * @author Adila Faruk
+ */
+public class LinearModelPlot extends Visuals {
+ private PApplet myParent;
+ boolean drawRectangle = true, drawAxes = true;
+
+ public float leftBound = 40, rightBound=20;
+ public float topBound = 20, bottomBound=20;
+
+ float minX, minY, maxX, maxY; // plot min/max
+ float minXDat, minYDat, maxXDat, maxYDat; // data min/max
+ boolean dataExtremesHaveBeenSet = false;
+
+ // best fit line
+ int sColor = color(200, 100, 100); // stroke color for the best fit line
+ float sWeight = 1; // stroke weight for the best fit line
+ boolean writeLineEquation = true;
+
+ /**Constructor */
+ public LinearModelPlot(PApplet _theParent, float _plotLeft, float _plotTop, float _plotWidth, float _plotHeight) {
+ super(_theParent, _plotLeft, _plotTop, _plotWidth, _plotHeight);
+ myParent = _theParent;
+ bgColor = super.bgColor;
+ }
+
+ /* Set the minimum/maximum values on the x and y axis to nice numbers.
+ E.g. 60 instead of 63.183628, 90 instead of 87.1, etc. */
+ public void setDataExtremes(float _minXDat, float _maxXDat, float _minYDat, float _maxYDat) {
+ minXDat = _minXDat;
+ maxXDat = _maxXDat;
+ minYDat = _minYDat;
+ maxYDat = _maxYDat;
+ dataExtremesHaveBeenSet = true;
+ }
+
+ /* Plots the scatter plot of the data.
+ x,y the x-y dataset of interest.
+ _fColor the color of the ellipses corresponding to each x-y pair
+ */
+ public void drawXY( float[] x, float[] y, int _fColor) {
+ if (drawRectangle) {
+ drawRect(plotLeft-leftBound, plotTop-topBound, plotWidth+leftBound+rightBound, plotHeight+topBound+bottomBound, bgColor);
+ }
+ if (drawAxes) {
+ drawAxes(50, .5f);
+ }
+ // Check if the data extemes have been set.
+ if (!dataExtremesHaveBeenSet) {
+ throw new IllegalStateException("Set the data extremes first.");
+ }
+
+ // map the data.
+ float[] xDatMapped = Mat.map(x, minXDat, maxXDat, plotLeft, plotLeft+plotWidth);
+ float[] yDatMapped = Mat.map(y, minYDat, maxYDat, plotTop+plotHeight, plotTop);
+ minX = Descriptive.min(xDatMapped);
+ maxX = Descriptive.max(xDatMapped);
+ minY = Descriptive.min(yDatMapped);
+ maxY = Descriptive.max(yDatMapped);
+ myParent.fill(_fColor);
+ myParent.noStroke();
+ myParent.smooth();
+ for (int i=0; ix2,y2
.
+ *
+ * The multiplier c
( 0 <= c<= 0.5 ) is chosen such that
+ * the points (x1,y1) and (x2,y2) are in the rectangle with corners
+ *
+ * (meanX-c*rangeX/2, meanY+c*rangeY/2) (meanX+c*rangeX/2, meanY+c*rangeY/2)
+ *
+ * (meanX-c*rangeX/2, meanY-c*rangeY/2) (meanX+c*rangeX/2, meanY-c*rangeY/2)
+ *
+ * where
+ * rangeX = _maxX - _minX
+ * and
+ * rangeY = _maxY - _minY.
+ * This is done in the most inefficient way possible in the universe... :)
+ * @param coeff, the slope and intercept where coeff[0] = slope, coeff[1] = intercept
+ * @param meanVals = float[]{mean(x),mean(y)}}
+ * @param multiplier corresponds to c
above and is the multiplier to use when plotting
+ * the line. A smaller c value results in a longer line. The default it set to .15, and will be used if a
+ * value of c<= 0 or c >=.50 is entered.
+ */
+ public void drawBestFitLine(float[] coeff, float[] meanVals, float multiplier) {
+ // Check if the data extemes have been set.
+ if (!dataExtremesHaveBeenSet) {
+ throw new IllegalStateException("Set the data extremes first.");
+ }
+
+ float meanX = meanVals[0];
+ float meanY = meanVals[1];
+ float x1, y1, x2, y2;
+ float slope = coeff[0];
+ float intercept = coeff[1];
+ float mult;
+ if (multiplier<0 || multiplier>=.5) {
+ mult = .15f;
+ }
+ else mult = 1-multiplier;
+
+ x1 = (meanX+mult*(minXDat-meanX));
+ y1= slope*(x1)+intercept;
+ x2 = (meanX+mult*(maxXDat-meanX));
+ y2= slope*(x2)+intercept;
+ if (slope!=0) {
+ // if the intercept b is less than the minimum y value
+ // update the left-most point
+ if (y1
+
+
+
+
+
+
diff --git a/drawing/papaya/reference/allclasses-noframe.html b/drawing/papaya/reference/allclasses-noframe.html
new file mode 100644
index 0000000..b5c60eb
--- /dev/null
+++ b/drawing/papaya/reference/allclasses-noframe.html
@@ -0,0 +1,109 @@
+
+
+
+
+
+
+
+BoxPlot
+
+
+Cast
+
+Comparison
+
+Comparison.TTest
+
+Correlation
+
+Correlation.Significance
+
+Correlation.Weighted
+
+CorrelationPlot
+
+Descriptive
+
+Descriptive.Mean
+
+Descriptive.Pooled
+
+Descriptive.Sum
+
+Descriptive.Weighted
+
+Distance
+
+Eigenvalue
+
+Find
+
+Frequency
+
+Gamma
+
+Linear
+
+Linear.BoxCox
+
+Linear.Significance
+
+Linear.StdErr
+
+LU
+
+Mat
+
+MDS
+
+NaNs
+
+Normality
+
+Normality.Dago
+
+OneWayAnova
+
+PapayaConstants
+
+Polynomial
+
+Probability
+
+QR
+
+Rank
+
+ScatterPlot
+
+Sorting
+
+SubPlot
+
+SVD
+
+Unique
+
+Visuals
+
+
+
+
+
+
+
+
diff --git a/drawing/papaya/reference/constant-values.html b/drawing/papaya/reference/constant-values.html
new file mode 100644
index 0000000..8585797
--- /dev/null
+++ b/drawing/papaya/reference/constant-values.html
@@ -0,0 +1,277 @@
+
+
+
+
+
+
+
+BoxPlot
+
+
+Cast
+
+Comparison
+
+Comparison.TTest
+
+Correlation
+
+Correlation.Significance
+
+Correlation.Weighted
+
+CorrelationPlot
+
+Descriptive
+
+Descriptive.Mean
+
+Descriptive.Pooled
+
+Descriptive.Sum
+
+Descriptive.Weighted
+
+Distance
+
+Eigenvalue
+
+Find
+
+Frequency
+
+Gamma
+
+Linear
+
+Linear.BoxCox
+
+Linear.Significance
+
+Linear.StdErr
+
+LU
+
+Mat
+
+MDS
+
+NaNs
+
+Normality
+
+Normality.Dago
+
+OneWayAnova
+
+PapayaConstants
+
+Polynomial
+
+Probability
+
+QR
+
+Rank
+
+ScatterPlot
+
+Sorting
+
+SubPlot
+
+SVD
+
+Unique
+
+Visuals
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+Constant Field Values
+
+Contents
+
+
+
+
+
+
+
+
+
+papaya.*
+
+
+
+
+
+papaya.PapayaConstants
+
+
+
+
+public static final int
+BASELINE
+0
+
+
+
+public static final double
+big
+4.503599627370496E15
+
+
+
+public static final double
+biginv
+2.220446049250313E-16
+
+
+
+public static final int
+BOTTOM
+102
+
+
+
+public static final int
+CENTER
+3
+
+
+
+public static final int
+CORNER
+0
+
+
+
+public static final String
+FONTNAME
+"Helvetica"
+
+
+
+public static final int
+INDEX_NOT_FOUND
+-1
+
+
+
+public static final int
+LEFT
+37
+
+
+
+public static final double
+LOGPI
+1.1447298858494002
+
+
+
+public static final double
+MACHEP
+1.1102230246251565E-16
+
+
+
+public static final double
+MAXGAM
+171.6243769563027
+
+
+
+public static final double
+MAXLOG
+709.782712893384
+
+
+
+public static final double
+MINLOG
+-745.1332191019412
+
+
+
+public static final int
+RIGHT
+39
+
+
+
+public static final double
+SQRTH
+0.7071067811865476
+
+
+
+public static final double
+SQTPI
+2.5066282746310007
+
+
+
+
+
+public static final int
+TOP
+101
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ Processing library papaya by
+ Adila Faruk. (C) 2014
+
+
+
+
diff --git a/drawing/papaya/reference/deprecated-list.html b/drawing/papaya/reference/deprecated-list.html
new file mode 100644
index 0000000..32cbfde
--- /dev/null
+++ b/drawing/papaya/reference/deprecated-list.html
@@ -0,0 +1,147 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+Deprecated API
+
+Contents
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ Processing library papaya by
+ Adila Faruk. (C) 2014
+
+
+
+
diff --git a/drawing/papaya/reference/help-doc.html b/drawing/papaya/reference/help-doc.html
new file mode 100644
index 0000000..77f1c9e
--- /dev/null
+++ b/drawing/papaya/reference/help-doc.html
@@ -0,0 +1,214 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+How This API Document Is Organized
+
+Package
+
+
+
+
+
+
+Class/Interface
+
+
+
+
+
+
+Each summary entry contains the first sentence from the detailed description for that item. The summary entries are alphabetical, while the detailed descriptions are in the order they appear in the source code. This preserves the logical groupings established by the programmer.
+Annotation Type
+
+
+
+
+
+
+
+Enum
+
+
+
+
+
+
+Tree (Class Hierarchy)
+
+There is a Class Hierarchy page for all packages, plus a hierarchy for each package. Each hierarchy page contains a list of classes and a list of interfaces. The classes are organized by inheritance structure starting with
+java.lang.Object
. The interfaces do not inherit from java.lang.Object
.
+
+
+Deprecated API
+
+The Deprecated API page lists all of the API that have been deprecated. A deprecated API is not recommended for use, generally due to improvements, and a replacement API is usually given. Deprecated APIs may be removed in future implementations.
+
+Index
+
+The Index contains an alphabetic list of all classes, interfaces, constructors, methods, and fields.
+
+Prev/Next
+These links take you to the next or previous class, interface, package, or related page.
+Frames/No Frames
+These links show and hide the HTML frames. All pages are available with or without frames.
+
+Serialized Form
+Each serializable or externalizable class has a description of its serialization fields and methods. This information is of interest to re-implementors, not to developers using the API. While there is no link in the navigation bar, you can get to this information by going to any serialized class and clicking "Serialized Form" in the "See also" section of the class description.
+
+Constant Field Values
+The Constant Field Values page lists the static final fields and their values.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ Processing library papaya by
+ Adila Faruk. (C) 2014
+
+
+
+
diff --git a/drawing/papaya/reference/index-all.html b/drawing/papaya/reference/index-all.html
new file mode 100644
index 0000000..a4e4669
--- /dev/null
+++ b/drawing/papaya/reference/index-all.html
@@ -0,0 +1,2076 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+A B C D E F G H I K L M N O P Q R S T U V W X Y Z
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+A
+
+
+Sum( data[i] ) / data.length
.
+auto(data, 1, mean, variance)
.
+
+
+B
+
+
+z = slope*x + intercept
by minimizing the sum of least squares
+ between z
and the y
.
+x
under the beta density
+ function.
+x
to
+ infinity) of the beta density function.
+0
through k
of the Binomial
+ probability density.
+k+1
through n
of the Binomial
+ probability density.
+
+
+C
+
+
+chi2
, or the
+ "omnibus" test statistic, and the significance, or "p-value" of this test statistic.
+x
)
+ of the Chi square probability density function with
+ v
degrees of freedom.
+x
to
+ infinity) of the Chi square probability density function
+ with v
degrees of freedom.
+a
and b
into a new array
+ c
such that c = {a,b}
.
+size
with
+ each element equal to the specified constValue
.
+size
with
+ each element equal to the specified constValue
.
+y
for a sequence of λ
+ and returns the array of linear correlation coefficients
+ between the x
and the box-cox transformed y
.
+data1
and data2
,
+ each of length N.
+
+
+D
+
+
+value
.
+ScatterPlot.drawScatterPlot(float[], float[], float, int)
.
+Correlation.auto(float[], int, float, float)
)
+ in the residuals (prediction errors) from a regression analysis.
+
+
+E
+
+
+erfc
.
+
+
+F
+
+
+x
under the F density
+ function (also known as Snedcor's density or the
+ variance ratio density); formerly named fdtr
.
+fdtrc
.
+x
of the F density is equal to p; formerly named fdtri
.
+
+
+G
+
+
+Γ(x)
+x
of the gamma probability
+ density function.
+x
to infinity of the gamma
+ probability density function:
+L
.
+data[i]
is NaN.
+data[i]
is NaN.
+U
+
+
+H
+
+
+Sum( 1.0 / data[i])
.
+
+
+I
+
+
+dimension
+xx
; formerly named ibeta
.
+igamma
.
+Cast
function.
+fromIndex
(inclusive) to toIndex
(exclusive)
+ in ascending or descending order.
+Cast
function.
+replaceZeroWith
value instead.
+Sum( 1.0 / data[i])
.
+
+
+K
+
+
+-3 + moment(data,4,mean) / standardDeviation4
.
+zb2
associated with
+ b2 and the significance, or "p-value" of the kurtosis test statistic zb2
, assuming a
+ two-tailed null hypothesis as well as .
+
+
+L
+
+
+r
+ between two datasets under the null hypothesis of no correlation.
+x
and y
into
+ a linear one.start
(inclusive) to end
(inclusive):
+ y[0] = start; y[1] = start+1; ...
+
lgamma
.
+Sum( Log(data[i])
.
+
+
+M
+
+
+U
and p-value for
+ assessing whether one of two samples of independent observations tends to have
+ larger values than the other.
+Sum( data[i] ) / data.length
.
+k
data sequences.
+k
-th order with constant c
of a data sequence,
+ which is Sum( (data[i]-c)k ) / data.size()
.
+
+
+N
+
+
+0
through k
of the Negative Binomial Distribution.
+k+1
to infinity of the Negative
+ Binomial distribution.
+N
) necessary to produce a Q-Q plot
+ for a normal distribution (or normal probability plot).
+x
(assumes mean is zero, variance is one).
+x
when the
+ data has not been standardized (i.e.
+x
, for which the area under the
+ Normal (Gaussian) probability density function (integrated from
+ minus infinity to x
) is equal to the argument y
+ (assumes mean is zero, variance is one); formerly named ndtri
.
+
+
+O
+
+
+F = variance between samples / variance within samples
.lowerLimit
+ and more than or equal to the upperLimit
+
+
+P
+
+
+k
terms of the Poisson distribution.
+k+1
to Infinity
of the Poisson distribution.
+Sum( (data[i]-c)k )
;
+ optimized for common parameters like c == 0.0
and/or k == -2 ..
+
Sum( (data[i]-c)k )
for all i = from ..
+
Sum ( data[i]k )
.
+Prod( data[i] )
.
+Prod( data[i] )
.
+Sum( x[i] * y[i])
.
+OneWayAnova.F()
.
+
+
+Q
+
+
+phi-
quantile; that is, an element elem
+ for which holds that phi
percent of data elements are less than
+ elem
.
+<= element
.
+
+
+R
+
+
+nanStrategy
and ties
+ resolved using tiesStrategy
.
+nanStrategy
and ties
+ resolved using tiesStrategy
.
+tiesStrategy
.
+tiesStrategy
.
+oldValue
with
+ the newValue
.
+oldValue
are replaced with
+ the newValue
.
+Delta_i = z_i - y_i
,
+ where
+ z_i = (slope*x_i + intercept)
is the best fit linear line.
+Delta_i = z_i - y_i
,
+ where
+ z_i = (slope*x_i + intercept)
is the best fit linear line.
+
+
+S
+
+
+ScatterPlot.setDataExtremes(float, float, float, float)
+moment(data,3,mean) / standardDeviation3
.
+zb1
associated with sqrt(b1) and
+ the significance, or "p-value" of the skew test statistic zb1
, assuming a
+ two-tailed null hypothesis as well as .
+rho
+ between two datasets under the null hypothesis of no correlation.
+rho
, between multiple columns of a matrix
+ with each column corresponding to a dataset, and each row an observation.
+rho
.
+
+
+T
+
+
+t
of the Student-t
+ distribution with k > 0
degrees of freedom.
+t
, for which the area under the
+ Student-t probability density function (integrated from
+ minus infinity to t
) is equal to p.
+x
and y
arrays according to
+ x
and going from fromIndex
(inclusive) to toIndex
(exclusive)
+ in ascending or descending order.
+
+
+U
+
+
+
+
+V
+
+
+k
data sequences.
+
+
+W
+
+
+
+
+X
+
+
+
+
+Y
+
+
+
+
+Z
+
+
+
+A B C D E F G H I K L M N O P Q R S T U V W X Y Z
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ Processing library papaya by
+ Adila Faruk. (C) 2014
+
+
+
+
diff --git a/drawing/papaya/reference/index.html b/drawing/papaya/reference/index.html
new file mode 100644
index 0000000..c231207
--- /dev/null
+++ b/drawing/papaya/reference/index.html
@@ -0,0 +1,70 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+Hierarchy For All Packages
+
+
+
+
+Class Hierarchy
+
+
+
+
+
+
+
+
+Interface Hierarchy
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV
+ NEXT
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ Processing library papaya by
+ Adila Faruk. (C) 2014
+
+
+
+
diff --git a/drawing/papaya/reference/package-list b/drawing/papaya/reference/package-list
new file mode 100644
index 0000000..cc8ed38
--- /dev/null
+++ b/drawing/papaya/reference/package-list
@@ -0,0 +1 @@
+papaya
diff --git a/drawing/papaya/reference/papaya/BoxPlot.html b/drawing/papaya/reference/papaya/BoxPlot.html
new file mode 100644
index 0000000..a49302b
--- /dev/null
+++ b/drawing/papaya/reference/papaya/BoxPlot.html
@@ -0,0 +1,498 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV CLASS
+ NEXT CLASS
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD
+
+DETAIL: FIELD | CONSTR | METHOD
+
+
+
+
+papaya
+
+
+Class BoxPlot
+java.lang.Object
+
+papaya.Visuals
+
papaya.BoxPlot
+
+
+
+
+
+
+
+public class BoxPlot
+
+
+
+
+
+
+
+Field Summary
+
+
+
+
+ float
+bottomBound
+
+
+ Specifies the space on the bottom, between the plot area and the bounding
+ rectangle.
+
+
+
+ boolean
+drawAxes
+
+
+ Specifies whether to draw the axes
+
+
+
+ boolean
+drawRectangle
+
+
+ Specifies whether to draw the bounding rectangle around the plot.
+
+
+
+ float
+leftBound
+
+
+ Specifies the space on the left between the plot area, and the
+ bounding rectangle.
+
+
+
+ float
+rightBound
+
+
+ Specifies the space on the right betweent the plot area, and the
+ bounding rectangle.
+
+
+
+ float
+topBound
+
+
+ Specifies the space on the top, between the plot area and the bounding
+ rectangle.
+
+
+
+
+Fields inherited from class papaya.Visuals
+
+
+
+bgColor
+
+
+
+
+
+
+
+Fields inherited from interface papaya.PapayaConstants
+
+
+
+BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP
+
+
+
+
+
+
+
+
+Constructor Summary
+
+
+
+BoxPlot(PApplet _theParent,
+ float _plotLeft,
+ float _plotTop,
+ float _plotWidth,
+ float _plotHeight)
+
+
+
+
+
+
+
+
+Method Summary
+
+
+
+
+ void
+drawBoxPlot(String[] _labels,
+ float _sWeight,
+ int _fColor)
+
+
+ Plot the box plots.
+
+
+
+ float[][]
+getData()
+
+
+
+
+
+
+ void
+setData(float[][] _fiveNumArr,
+ float _minVal,
+ float _maxVal)
+
+
+ Sets the data to be plotted.
+
+
+
+
+Methods inherited from class papaya.Visuals
+
+
+
+drawAxes, drawRect, drawRect, getBottom, getHeight, getLeft, getRight, getTop, getWidth, horizLine, legendHoriz, legendVert, line, mapXData, mapXData, mapYData, mapYData, setBackgroundColor, setHeight, setLeft, setTop, setupFont, setupFont, setWidth, vertLine, writeLabels, writeLabels, writeLabels, writeLabels, writeLabels, writeTitle, xLabels, xLabels, xTicks, yLabels, yLabels, YLines, yTicks
+
+
+
+
+Methods inherited from class java.lang.Object
+
+
+
+equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
+
+
+
+
+
+Field Detail
+
+drawRectangle
+
+public boolean drawRectangle
+
+
+
+
+
+
+
+drawAxes
+
+public boolean drawAxes
+
+
+
+
+
+
+
+leftBound
+
+public float leftBound
+
+
+
+
+
+
+
+rightBound
+
+public float rightBound
+
+
+
+
+
+
+
+topBound
+
+public float topBound
+
+
+
+
+
+
+
+bottomBound
+
+public float bottomBound
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Constructor Detail
+
+BoxPlot
+
+public BoxPlot(PApplet _theParent,
+ float _plotLeft,
+ float _plotTop,
+ float _plotWidth,
+ float _plotHeight)
+
+
+
+
+
+
+
+
+
+
+
+
+Method Detail
+
+setData
+
+public void setData(float[][] _fiveNumArr,
+ float _minVal,
+ float _maxVal)
+
+
+
+
+_fiveNumArr
- array of five number box plot summaries
+ with each row corresponding to a given data set. See Descriptive.tukeyFiveNum(float[])
for more info._minVal
- the minimum value to show in the plot_maxVal
- the maximum value to show in the plot
+
+
+getData
+
+public float[][] getData()
+
+
+
+
+
+
+
+
+drawBoxPlot
+
+public void drawBoxPlot(String[] _labels,
+ float _sWeight,
+ int _fColor)
+
+
+
+
+
+_labels
- labels for the box plot_sWeight
- the stroke weight to use for the lines._fColor
- the fill color for the boxplots
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV CLASS
+ NEXT CLASS
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD
+
+DETAIL: FIELD | CONSTR | METHOD
+
+
+
+ Processing library papaya by
+ Adila Faruk. (C) 2014
+
+
+
+
diff --git a/drawing/papaya/reference/papaya/Cast.html b/drawing/papaya/reference/papaya/Cast.html
new file mode 100644
index 0000000..1eca94c
--- /dev/null
+++ b/drawing/papaya/reference/papaya/Cast.html
@@ -0,0 +1,576 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV CLASS
+ NEXT CLASS
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD
+
+DETAIL: FIELD | CONSTR | METHOD
+
+
+
+
+papaya
+
+
+Class Cast
+java.lang.Object
+
+papaya.Cast
+
+
+
+
+
+public final class Cast
toArray(T[] a)
method specified in the java Collections
+ interface, but for float[] and int[] arrays (as opposed to Float[] and Integer[]).
+
+
+
+
+
+
+
+
+
+
+Method Summary
+
+
+
+
+static float[]
+arrayListToFloat(ArrayList<Float> _inputDat)
+
+
+ function for casting ArrayList to float[]
+
+
+
+static int[]
+arrayListToInt(ArrayList<Integer> _inputDat)
+
+
+ function for casting ArrayList to int[]
+
+
+
+static float[]
+doubleToFloat(double[] _inputDat)
+
+
+ function for casting double[] to float[]
+
+
+
+static float[][]
+doubleToFloat(double[][] _inputDat)
+
+
+ function for casting double[][] to float[][]
+
+
+
+static double[]
+floatToDouble(float[] _inputDat)
+
+
+ function for casting float[] to double[]
+
+
+
+static double[][]
+floatToDouble(float[][] _inputDat)
+
+
+ function for casting float[][] to double[][]
+
+
+
+static PVector[]
+floatToPVector(float[] x,
+ float[] y)
+
+
+ Returns a PVector array with the x, and coordinates set to the input values, and the z coordinates
+ set to zero.
+
+
+
+static PVector[]
+floatToPVector(float[] x,
+ float[] y,
+ float[] z)
+
+
+ Returns a PVector array with the x, y, and z coordinates set to the input values
+ (technically not a cast, but a reorganization).
+
+
+
+static float[]
+keySetToFloat(HashMap _hMap)
+
+
+ Function for casting hashMap keySet to float[]
+
+
+
+static int[]
+keySetToInt(HashMap _hMap)
+
+
+ Function for casting hashMap keySet to float[]
+
+
+
+static String[]
+keySetToString(HashMap _hMap)
+
+
+ Function for casting hashMap keySet to String[]
+
+
+
+static float[]
+setToFloat(Set<Float> theSet)
+
+
+ Function for casting a Set to float[]
+
+
+
+static int[]
+setToInt(Set<Integer> theSet)
+
+
+ Function for casting a Set to float[]
+
+
+
+static String[]
+setToString(Set<String> theSet)
+
+
+ Function for casting a Set to String array.
+
+
+
+static float[]
+vectorToFloat(Vector<Float> _inputDat)
+
+
+ function for casting Vector to float[]
+
+
+
+static int[]
+vectorToInt(Vector<Integer> _inputDat)
+
+
+ function for casting Vector to int[]
+
+
+
+
+Methods inherited from class java.lang.Object
+
+
+
+equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
+
+
+
+
+
+Method Detail
+
+arrayListToInt
+
+public static int[] arrayListToInt(ArrayList<Integer> _inputDat)
+
+
+
+
+_inputDat
- ArrayList array that needs to be cast to int[]
+
+
+
+arrayListToFloat
+
+public static float[] arrayListToFloat(ArrayList<Float> _inputDat)
+
+
+
+
+_inputDat
- ArrayList array that needs to be cast to float[]
+
+
+
+floatToDouble
+
+public static double[][] floatToDouble(float[][] _inputDat)
+
+
+
+
+_inputDat
- float[][] 2d array
+
+
+
+floatToDouble
+
+public static double[] floatToDouble(float[] _inputDat)
+
+
+
+
+_inputDat
- float[] array
+
+
+
+doubleToFloat
+
+public static float[][] doubleToFloat(double[][] _inputDat)
+
+
+
+
+_inputDat
- double[][] 2d array
+
+
+
+doubleToFloat
+
+public static float[] doubleToFloat(double[] _inputDat)
+
+
+
+
+_inputDat
- double[] array
+
+
+
+keySetToString
+
+public static String[] keySetToString(HashMap _hMap)
+
+
+
+
+_hMap
- input HashMap
+
+
+
+keySetToInt
+
+public static int[] keySetToInt(HashMap _hMap)
+
+
+
+
+_hMap
- input HashMap
+
+
+
+keySetToFloat
+
+public static float[] keySetToFloat(HashMap _hMap)
+
+
+
+
+_hMap
- input HashMap
+
+
+
+setToInt
+
+public static int[] setToInt(Set<Integer> theSet)
+
+
+
+
+
+
+
+setToFloat
+
+public static float[] setToFloat(Set<Float> theSet)
+
+
+
+
+
+
+
+setToString
+
+public static String[] setToString(Set<String> theSet)
+
+
+
+
+
+
+
+vectorToInt
+
+public static int[] vectorToInt(Vector<Integer> _inputDat)
+
+
+
+
+_inputDat
- Vector array that needs to be cast to int[]
+
+
+
+vectorToFloat
+
+public static float[] vectorToFloat(Vector<Float> _inputDat)
+
+
+
+
+_inputDat
- Vector array that needs to be cast to float[]
+
+
+
+floatToPVector
+
+public static PVector[] floatToPVector(float[] x,
+ float[] y,
+ float[] z)
+
+
+
+
+
+
+
+floatToPVector
+
+public static PVector[] floatToPVector(float[] x,
+ float[] y)
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV CLASS
+ NEXT CLASS
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD
+
+DETAIL: FIELD | CONSTR | METHOD
+
+
+
+ Processing library papaya by
+ Adila Faruk. (C) 2014
+
+
+
+
diff --git a/drawing/papaya/reference/papaya/Comparison.TTest.html b/drawing/papaya/reference/papaya/Comparison.TTest.html
new file mode 100644
index 0000000..06849d9
--- /dev/null
+++ b/drawing/papaya/reference/papaya/Comparison.TTest.html
@@ -0,0 +1,318 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Package
+ Class
+ Tree
+ Deprecated
+ Index
+ Help
+
+
+
+
+
+
+ PREV CLASS
+ NEXT CLASS
+
+ FRAMES
+ NO FRAMES
+
+
+
+
+
+
+
+
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD
+
+DETAIL: FIELD | CONSTR | METHOD
+
+
+
+
+papaya
+
+
+Class Comparison.TTest
+java.lang.Object
+
+papaya.Comparison.TTest
+
+
+
+
+
+
+
+public static class Comparison.TTest
+
+ Test statistics are available for all tests. Methods including "Test" in
+ in their names perform tests, all other methods return t-statistics. Among
+ the "Test" methods, double-
valued methods return p-values;
+ boolean-
valued methods perform fixed significance level tests.
+ Significance levels are always specified as numbers between 0 and 0.5
+ (e.g. tests at the 95% level use alpha=0.05
).
+ + Note: Inspired by the + stats.inference.TTest class available in the Apache Commons math library. +
+ +
+
+Method Summary | +|
---|---|
+static float[] |
+equalVar(float[] data1,
+ float[] data2)
+
++ Returns the t-statistic and p-value for checking whether the means + between the two datasets are different under the assumption that both + datasets have equal variances. |
+
+static float[] |
+paired(float[] before,
+ float[] after)
+
++ Returns the t-statistic and p-value for checking a pair of dependent samples. |
+
+static float[] |
+unequalVar(float[] data1,
+ float[] data2)
+
++ Returns the t-statistic and p-value for checking whether the means + between the two datasets are different under the assumption that both + datasets have unequal variances. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[] equalVar(float[] data1, + float[] data2)+
+
+public static float[] unequalVar(float[] data1, + float[] data2)+
+
+public static float[] paired(float[] before, + float[] after)+
Both datasets have to have equal lengths. +
+
before
- the dataset before treatment/measurement.after
- the dataset after treatment/measurement.
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Comparison +
public class Comparison
+Contains a number of methods for comparing more than one dataset + against each other. Rank-based methods and are powerful in the sense that they do not rely + on the data assuming a normal distribution. +
+ +
+
+Nested Class Summary | +|
---|---|
+static class |
+Comparison.TTest
+
++ Methods related to comparing two populations. |
+
+Field Summary | +
---|
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Constructor Summary | +|
---|---|
Comparison()
+
++ Makes this class non instantiable, but still let's others inherit from it. |
+
+Method Summary | +|
---|---|
+static float[] |
+mannWhitney(float[] data1,
+ float[] data2)
+
++ Computes and returns the MannWhitney (or Wilcoxon Rank Sum) test statistic, U and p-value for
+ assessing whether one of two samples of independent observations tends to have
+ larger values than the other. |
+
+static float |
+signTest(float[] x,
+ float[] y)
+
++ Performs the sign test on an input array and returns the test statistic used to + test the hypothesis that there is "no difference in medians" + between the continuous distributions of two random variables X and Y. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public Comparison()+
+
+Method Detail | +
---|
+public static float[] mannWhitney(float[] data1, + float[] data2)+
U
and p-value for
+ assessing whether one of two samples of independent observations tends to have
+ larger values than the other. That is,
+ U = min(U1,U2)+ where +
+ U1 = Sum(Ranks1) - n1*(n1+1)/2, + U2 = Sum(Ranks2) - n2*(n2+1)/2. ++ and the significance, or p-value is computed assuming + a two-tailed null-hypothesis that both samples are similar. +
+
data1
- the first datasetdata2
- the second dataset
+IllegalArgumentException
- if either of the sample sizes is less than 8 (the normal approximation
+ is no longer valid in that case. Best to consult a table instead).+public static float signTest(float[] x, + float[] y)+
To elaborate, the null hypothesis H0 states that given a random pair of measurements (X[i], Y[i]),
+ X[i] and Y[i] are equally likely to be larger than the other.
+ Now, let n
be the number of pairs for which Y[i] ≠ X[i] and w
+ be the number of pairs for which Y[i] - X[i] > 0.
+ Assuming that H0 is true, then W follows a binomial distribution W ~ b(n, 0.5).
+
+ This method returns the sum of the terms 0
through w
of the Binomial
+ probability density.
+
+ Sum_(j=0 to w) [ (n!)/(j!*(n-j)! ] * [ p^j * (1-p)^(n-j) ] + from 0 to the test-statistic, or ++ p = Probability.binomcdf(.5,w,n). +
+ For n > 100, the normal distribution cdf is returned instead. + If this is less than the critical value, then we can reject the null hypothesis that there + is no difference between the samples X and Y. ++
x
- data set containing 1st observation sequencey
- data set containing 2nd observation sequence
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Correlation.Significance +
public static class Correlation.Significance
+Contains methods used to compute the significance, or pvalue of the input correlations. + The significance is computed using the normal of student-t approximations and hence are + not to be used for small datasets(i.e. size<20). +
+ +
+
+Method Summary | +|
---|---|
+static float |
+durbinWatson(float[] data)
+
++ The Durbin-Watson statistic is a test statistic used to detect the presence + of autocorrelation ( Correlation.auto(float[], int, float, float) )
+ in the residuals (prediction errors) from a regression analysis. |
+
+static float |
+linear(float r,
+ int size)
+
++ Returns the p-value, or significance level of the linear correlation r
+ between two datasets under the null hypothesis of no correlation. |
+
+static float |
+spearman(float rho,
+ int size)
+
++ Returns the p-value, or significance level of the Spearman rank-correlation rho
+ between two datasets under the null hypothesis of no correlation. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float durbinWatson(float[] data)+
Correlation.auto(float[], int, float, float)
)
+ in the residuals (prediction errors) from a regression analysis.
+
+ If e_t
is the residual associated with the observation at time t
,
+ and there are T
observations, then the test statistic, d
is
+
+ d = Sum_{from t=2 to T} ( e_t - e_{t-1} )^2 / Sum_{from t=1 to T} ( e_t )^2 ++
+
+public static float linear(float r, + int size)+
r
+ between two datasets under the null hypothesis of no correlation.
+ That is, H_0: ρ=0
(No linear correlation; Two-tailed test). The p-value is
+ computed by transforming the correlation to create a t-statistic
+ having n-2 degrees of freedom:
+ t = r * sqrt( (n-2)/(1-r^2) ),+ where n is the size of the corresponding datasets. +
+
r
- the linear correlation coefficientsize
- the size of the corresponding datasets (i.e. number of rows/observations).
+IllegalArgumentException
- if the size is less than 20. Below this,
+ the student-T approximation is inaccurate.+public static float spearman(float rho, + int size)+
rho
+ between two datasets under the null hypothesis of no correlation.
+ That is, H_0: ρ_0=0
(No linear correlation; Two-tailed test).
+ The p-value is computed by transforming the correlation to create a t-statistic
+ having n-2 degrees of freedom:
+ t = rho * sqrt( (n-2)/(1-rho^2) ),+ where n is the size of the corresponding datasets. +
+
rho
- the Spearman-correlation coefficientsize
- the size of the corresponding datasets (i.e. number of rows/observations).
+IllegalArgumentException
- if the size is less than 20. Below this,
+ the student-T approximation is inaccurate.
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Correlation.Weighted +
public static class Correlation.Weighted
+Contains methods related to computing the correlation and covariance of weighted + datasets. +
+ +
+
+Method Summary | +|
---|---|
+static float[][] |
+corr(float[][] data,
+ float[] weights,
+ boolean unbiased)
+
++ Returns the weighted linear correlation matrix C. |
+
+static float |
+corr(float[] data1,
+ float[] data2,
+ float[] weights,
+ boolean unbiased)
+
++ Returns the weighted linear correlation of two data sequences. |
+
+static float[][] |
+cov(float[][] data,
+ float[] weights,
+ boolean unbiased)
+
++ Returns the weighted covariance matrix with element S_JK specifying the weighted + covariance between column J and column K of the input matrix. |
+
+static float |
+cov(float[] data1,
+ float[] data2,
+ float[] weights,
+ boolean unbiased)
+
++ Returns the weighted covariance between two data sequences. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[][] corr(float[][] data, + float[] weights, + boolean unbiased)+
+ C_JK = S_JK / sqrt(S_JJ,unbiasedValue)*sqrt(S_KK,unbiasedValue). ++ See
Correlation.cov(float[][],boolean)
.
++
data
- input matrix each column corresponding to a sample, each row an observation.weights
- the weights to use.unbiased
- set to true to return the unbiased weighted correlation matrix, false to return the biased version.+public static float corr(float[] data1, + float[] data2, + float[] weights, + boolean unbiased)+
+
unbiased
- set to true to return the unbiased weighted correlation, false to return the biased version.+public static float cov(float[] data1, + float[] data2, + float[] weights, + boolean unbiased)+
+
unbiased
- set to true to return the unbiased weighted covariance, false to return the biased version.+public static float[][] cov(float[][] data, + float[] weights, + boolean unbiased)+
+ The formula used to compute element S_JK
of the unbiased covariance matrix is:
+
+ S_JK = biasCorrection * Sum ( w[i] * ( dataJ[i] - mu_w(dataJ) )*( dataK[i] - mu_w(dataK) ) ) / Sum (w[i]) ++ where
mu_w
corresponds to the weighted mean of the dataset
+ and the biasCorrection term above is
+ + biasCorrection = ( Sum(w[i]) )^2 / ( ( Sum(w[i]) )^2 - Sum( w[i]^2 ) ). ++ The elements of the biased covariance matrix are computed sans the bias Correction factor. +
+ Reference: + Covariance of weighted samples, wikipedia.org. +
+
data
- input matrix each column corresponding to a sample, each row an observation.weights
- the weights to use.unbiased
- set to true to return the unbiased weighted covariance matrix, false to return the biased version.
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Correlation +
public final class Correlation
+Contains utilities related to computing covariances, as well as linear and rank correlation. + Methods relating to computing +
Correlation.Weighted
subclass.
+ Correlation.Significance
subclass.
+ + +
+
+Nested Class Summary | +|
---|---|
+static class |
+Correlation.Significance
+
++ Contains methods used to compute the significance, or pvalue of the input correlations. |
+
+static class |
+Correlation.Weighted
+
++ Contains methods related to computing the correlation and covariance of weighted + datasets. |
+
+Field Summary | +
---|
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Method Summary | +|
---|---|
+static float |
+auto(float[] data,
+ int lag,
+ float mean,
+ float variance)
+
++ Computes the sample autocorrelation by removing the sample mean from the input series, + then normalizing the sequence by the sample variance. |
+
+static float |
+autoLag1(float[] data,
+ float mean)
+
++ Returns the lag-1 autocorrelation of a dataset; + Note that this method uses computations different from + auto(data, 1, mean, variance) . |
+
+static float[][] |
+cov(float[][] data,
+ boolean unbiased)
+
++ Returns the covariance matrix of P data sequences, each of length N. |
+
+static float |
+cov(float[] data1,
+ float[] data2,
+ boolean unbiased)
+
++ Returns the covariance of two data sequences data1 and data2 ,
+ each of length N. |
+
+static float[][] |
+linear(float[][] data,
+ boolean unbiased)
+
++ Returns the (Pearson Product Moment) correlation matrix between multiple columns of a matrix + with each column corresponding to a dataset, and each row an observation. |
+
+static float |
+linear(float[] data1,
+ float[] data2,
+ boolean unbiased)
+
++ Returns the (Pearson Product Moment) linear correlation of two data sequences. |
+
+static float[][] |
+spearman(float[][] data,
+ boolean unbiased)
+
++ Computes Spearman's rank-correlation, or rho , between multiple columns of a matrix
+ with each column corresponding to a dataset, and each row an observation. |
+
+static float |
+spearman(float[] x,
+ float[] y,
+ boolean unbiased)
+
++ Computes Spearman's rank-correlation, or rho . |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float auto(float[] data, + int lag, + float mean, + float variance)+
+ R(lag) = E[ (X[t] - mu) * ( X[t+lag] - mu ) ] / variance(X). + where + E[ (X[t] - mu) * ( X[t+lag] - mu ) ] = 1/size(X) * Sum_(i=0 to size-lag)( (X[t]-mu)*X[t+lag]-mu) ). ++ Reference: Box, G. E. P., G. M. Jenkins, and G. C. Reinsel. + Time Series Analysis: Forecasting and Control. 3rd ed. + Upper Saddle River, NJ: Prentice-Hall, 1994. +
+
+
data
- the array of datalag
- the lag value. Has to be smaller than the data sequence lengthmean
- the data meanvariance
- the data variance
++public static float autoLag1(float[] data, + float mean)+
auto(data, 1, mean, variance)
.
++
+public static float[][] linear(float[][] data, + boolean unbiased)+
P
columns, data1, data2, ... , dataP
,
+ each of length n, it computes and returns the P-by-P
correlation
+ matrix, C
with each element CJK given by
+ + CJK = CKJ = corr(dataJ,dataK,unbiasedValue). ++
+
data
- The input data. Each column corresponds to a dataset;
+ each row corresponds to an observationunbiased
- set to true to return the unbiased correlation,
+ false to return the biased version.
++public static float linear(float[] data1, + float[] data2, + boolean unbiased)+
cov(float[], float[], boolean)
function via
+
+ corr = cov(x,y,unbiasedValue)/sqrt( cov(x,x, unbiasedValue)*cov(y,y,unbiasedValue) )
+
++
unbiased
- set to true to return the unbiased correlation,
+ false to return the biased version.+public static float[][] spearman(float[][] data, + boolean unbiased)+
rho
, between multiple columns of a matrix
+ with each column corresponding to a dataset, and each row an observation.
+ That is, each pair of columns are first converted to ranks rXJ, rXK
+ and the correlation between the ranks computed using the Pearson correlation coefficient
+ formula.
++
unbiased
- set to true to return the unbiased correlation,
+ false to return the biased version.+public static float spearman(float[] x, + float[] y, + boolean unbiased)+
rho
.
+ That is, the raw dataset Xi,Yi are first converted to ranks rXi, rYi
+ and the correlation between the ranks computed using the Pearson correlation coefficient
+ formula.
++
unbiased
- set to true to return the unbiased correlation,
+ false to return the biased version.+public static float cov(float[] data1, + float[] data2, + boolean unbiased)+
data1
and data2
,
+ each of length N. That is,
+ + cov(data1, data2) = E( (data1[i] - mean(data1))* (data2[i] - mean(data2)) ), ++ where E is the mathematical expectation. +
+ cov(x,y,true) normalizes by N - 1, if N > 1, where N is the number of observations. + This makes cov(x,y,true) the best unbiased estimate of the covariance matrix if the + observations are from a normal distribution. For N = 1, cov(x,y,true) normalizes by N. +
+ cov(x,y,false) normalizes by N and produces the second moment matrix of the observations + about their mean. +
+
data1
- xdata2
- yunbiased
- set to true to return the unbiased covariance (division by N-1),
+ false to return the biased version (division by N).+public static float[][] cov(float[][] data, + boolean unbiased)+
P
columns, data1, data2, ... , dataP
,
+ each of length n, it computes and returns the P-by-P
covariance
+ matrix, S
with each element SJK given by
+ + SJK = SKJ = cov(dataJ,dataK,bias). ++ + cov(data,true) normalizes by N - 1, if N > 1, where N is the number of observations + (or the number of rows in the input matrix). + This makes cov(data,true) the best unbiased estimate of the covariance matrix if the + observations are from a normal distribution. For N = 1, cov(data) normalizes by N. + +
+ cov(data,false) normalizes by N and produces the second moment matrix of the observations + about their mean. +
+
data
- Each column corresponds to a dataset; each row corresponds to an observationunbiased
- set to true to return the unbiased covariance matrix, false to return the biased version.
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Visuals +
papaya.CorrelationPlot +
public class CorrelationPlot
+Takes in a matrix and plots the data in each of the columns versus + each other. Each of the columns are normalized to their minimum and maximum values which + can be a little misleading. For example, say you are looking at columns specifying the weight + of different panda bears over 100 days. + Panda bear A (Ling-Ling)'s weight varies from 650 to 970 pounds + (she's an emotional eater), while panda bear B (Thomas)'s weight varies from 512 to 516 pounds + (he's not a big fan of bamboo; also kind of metrosexual). The correlation plot will not show you + this difference in magnitudes or ranges! + There are many ways of addressing that, and all of them involve you writing your + own code. :) +
If you absolutely insist on using this despite the warning above, then the (somewhat messy) hack + around it is to pad each column of your data with 2 additional rows containing the + minimum and maximum that you want to abide by. Just make sure it doesn't muck everything + else up though! +
+ +
+
+Field Summary | +
---|
Fields inherited from class papaya.Visuals | +
---|
bgColor |
+
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Constructor Summary | +|
---|---|
CorrelationPlot(PApplet _theParent,
+ float _plotLeft,
+ float _plotTop,
+ float _plotWidth,
+ float _plotHeight)
+
++ Setup the plot dimensions |
+
+Method Summary | +|
---|---|
+ void |
+drawBorderAndLabels(String[] categories,
+ int bgColor)
+
++ Draw the bordering lines and write the labels. |
+
+ void |
+drawPlot(float[][] data,
+ int pos,
+ int fColor)
+
++ Plots the scatter plot of each of the columns of the input data matrix vs. |
+
Methods inherited from class papaya.Visuals | +
---|
drawAxes, drawRect, drawRect, getBottom, getHeight, getLeft, getRight, getTop, getWidth, horizLine, legendHoriz, legendVert, line, mapXData, mapXData, mapYData, mapYData, setBackgroundColor, setHeight, setLeft, setTop, setupFont, setupFont, setWidth, vertLine, writeLabels, writeLabels, writeLabels, writeLabels, writeLabels, writeTitle, xLabels, xLabels, xTicks, yLabels, yLabels, YLines, yTicks |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public CorrelationPlot(PApplet _theParent, + float _plotLeft, + float _plotTop, + float _plotWidth, + float _plotHeight)+
+
+Method Detail | +
---|
+public void drawPlot(float[][] data, + int pos, + int fColor)+
If you absolutely insist on using this despite the warning above, then the (very messy) hack + around it is to pad each column of your data with 2 additional rows containing the + minimum and maximum that you want to abide by. Just make sure it doesn't muck everything + else up though! +
+
data
- the data array with each column corresponding to a datasetpos
- 0 for top right plots, 1 for bottom left plotsfColor
- the fill color for the dots on the plot.+public void drawBorderAndLabels(String[] categories, + int bgColor)+
+
categories
- the labels along the diagonalbgColor
- background rectangle color
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Descriptive.Mean +
public static class Descriptive.Mean
+Contains methods for computing the arithmetic, geometric, harmonic, trimmed, and winsorized + means (among others). +
+ +
+
+Method Summary | +|
---|---|
+static float |
+arithmetic(float[] data)
+
++ Returns the arithmetic mean of a data sequence; + That is Sum( data[i] ) / data.length . |
+
+static float[] |
+columnMean(float[][] data)
+
++ Returns an array containing the arithmetic mean of each column + of the input matrix. |
+
+static float |
+geometric(float[] data)
+
++ Returns the geometric mean of a data sequence. |
+
+static float |
+geometric(int size,
+ float sumOfLogarithms)
+
++ Returns the geometric mean of a data sequence. |
+
+static float |
+harmonic(float[] data)
+
++ Returns the harmonic mean of a data sequence as Sum( 1.0 / data[i]) . |
+
+static float |
+harmonic(int size,
+ float sumOfInversions)
+
++ Returns the harmonic mean of a data sequence. |
+
+static float[] |
+rowMean(float[][] data)
+
++ Returns an array containing the arithmetic mean of each row of the input matrix. |
+
+static float |
+trimmed(float[] sortedData,
+ float mean,
+ int left,
+ int right)
+
++ Returns the trimmed arithmetic mean of a sorted data sequence. |
+
+static float |
+winsorized(float[] sortedData,
+ float mean,
+ int left,
+ int right)
+
++ Returns the winsorized mean of a sorted data sequence. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[] columnMean(float[][] data)+
+
+public static float[] rowMean(float[][] data)+
+
+public static float arithmetic(float[] data)+
Sum( data[i] ) / data.length
.
++
+public static float geometric(int size, + float sumOfLogarithms)+
pow( Product( data[i] ), 1/size)
+ which is equivalent to Math.exp( Sum( Log(data[i]) ) / size)
.
+ +
+public static float geometric(float[] data)+
pow( Product( data[i] ), 1/data.size())
.
+ This method tries to avoid overflows at the expense of an equivalent but somewhat slow definition:
+ geometricMean = Math.exp( Sum( Log(data[i]) ) / size)
.
++
+public static float harmonic(int size, + float sumOfInversions)+
+
size
- the number of elements in the data sequence.sumOfInversions
- Sum( 1.0 / data[i])
.+public static float harmonic(float[] data)+
Sum( 1.0 / data[i])
.
++
data
- array+public static float trimmed(float[] sortedData, + float mean, + int left, + int right)+
+
sortedData
- the data sequence; must be sorted ascending.mean
- the mean of the (full) sorted data sequence.left
- the number of leading elements to trim.right
- the number of trailing elements to trim.+public static float winsorized(float[] sortedData, + float mean, + int left, + int right)+
+
sortedData
- the data sequence; must be sorted ascending.mean
- the mean of the (full) sorted data sequence.left
- the number of leading elements to trim.right
- the number of trailing elements to trim.
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Descriptive.Pooled +
public static class Descriptive.Pooled
+Class for computing the pooled mean and variance of data sequences +
+ +
+
+Method Summary | +|
---|---|
+static float |
+mean(float[] means,
+ int[] sizes)
+
++ Returns the pooled mean of a k data sequences. |
+
+static float |
+mean(float mean1,
+ int size1,
+ float mean2,
+ int size2)
+
++ Returns the pooled mean of two data sequences. |
+
+static float |
+var(float[] variances,
+ int[] sizes,
+ boolean unbiased)
+
++ Returns the pooled variance of k data sequences. |
+
+static float |
+var(float variance1,
+ int size1,
+ float variance2,
+ int size2,
+ boolean unbiased)
+
++ Returns the pooled variance of two data sequences. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float mean(float mean1, + int size1, + float mean2, + int size2)+
(size1 * mean1 + size2 * mean2) / (size1 + size2)
.
+ +
+
size1
- the number of elements in data sequence 1.mean1
- the mean of data sequence 1.size2
- the number of elements in data sequence 2.mean2
- the mean of data sequence 2.+public static float mean(float[] means, + int[] sizes)+
k
data sequences.
+ That is (size1 * mean1 + size2 * mean1 + ... + sizek * meank) / (size1 + size2 + ... + sizek)
.
+ +
+
+public static float var(float variance1, + int size1, + float variance2, + int size2, + boolean unbiased)+
+ unbiasedPooledVar = Sum( (size_i -1)*variance_i ) / Sum( (size_i-1) ), ++ while the biased pooled variance is computed using +
+ biasedPooledVar = Sum( (size_i)*variance_i ) / Sum(size_i), + + For more than 2 datasets, usevar(float[],int[],boolean)
. ++
size1
- the number of elements in data sequence 1.variance1
- the variance of data sequence 1.size2
- the number of elements in data sequence 2.variance2
- the variance of data sequence 2.unbiased
- set to true to return the unbiased pooled variance,
+ false to return the biased version.+public static float var(float[] variances, + int[] sizes, + boolean unbiased)+
k
data sequences. The unbiased estimate of the
+ pooled variance is computed as
+ + unbiasedPooledVar = Sum( (sizes[i] -1)*variances[i] ) / Sum( (sizes[i]-1) ), ++ while the biased pooled variance is computed using +
+ biasedPooledVar = Sum( (sizes[i])*variances[i] ) / Sum(sizes[i]), ++
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Descriptive.Sum +
public static class Descriptive.Sum
+Methods for computing various different sums of datasets such as sum of inversions, + logs, products, power deviations, squares, etc. +
+ +
+
+Method Summary | +|
---|---|
+static double |
+inversions(float[] data,
+ int from,
+ int to)
+
++ Returns the sum of inversions of a data sequence, + which is Sum( 1.0 / data[i]) . |
+
+static double |
+logs(float[] data,
+ int from,
+ int to)
+
++ Returns the sum of logarithms of a data sequence, which is Sum( Log(data[i]) . |
+
+static double |
+powerDeviations(float[] data,
+ int k,
+ float c)
+
++ Returns Sum( (data[i]-c)k ) ;
+ optimized for common parameters like c == 0.0 and/or k == -2 .. |
+
+static double |
+powerDeviations(float[] data,
+ int k,
+ float c,
+ int from,
+ int to)
+
++ Returns Sum( (data[i]-c)k ) for all i = from .. |
+
+static double |
+powers(float[] data,
+ int k)
+
++ Returns the sum of powers of a data sequence, which is Sum ( data[i]k ) . |
+
+static double |
+products(float[] data1,
+ float[] data2)
+
++ Returns the sum of the product of two data arrays, Sum( x[i] * y[i]) . |
+
+static float |
+squaredDeviations(int size,
+ float variance)
+
++ Returns the sum of squared mean deviation of of a data sequence. |
+
+static double |
+squares(float[] data)
+
++ Returns the sum of squares of a data sequence. |
+
+static float |
+sum(float[] data)
+
++ Returns the simple sum of a data sequence. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static double inversions(float[] data, + int from, + int to)+
Sum( 1.0 / data[i])
.
++
data
- the data sequence.from
- the index of the first data element (inclusive).to
- the index of the last data element (inclusive).+public static double logs(float[] data, + int from, + int to)+
Sum( Log(data[i])
.
++
data
- the data sequence.from
- the index of the first data element (inclusive).to
- the index of the last data element (inclusive).+public static double products(float[] data1, + float[] data2)+
Sum( x[i] * y[i])
.
++
data1
- the first data sequence.data2
- the second data sequence
++public static double powerDeviations(float[] data, + int k, + float c)+
Sum( (data[i]-c)k )
;
+ optimized for common parameters like c == 0.0
and/or k == -2 .. 4
.
++
+public static double powerDeviations(float[] data, + int k, + float c, + int from, + int to)+
Sum( (data[i]-c)k )
for all i = from .. to
;
+ optimized for common parameters like c == 0.0
and/or k == -2 .. 5
.
+ Note that no checks are made for divisions by zero (important for
+ k = -2
and k = -1
), so think twice before using this
+ if the data has elements = 0.
++
+public static double powers(float[] data, + int k)+
Sum ( data[i]k )
.
++
+public static float squaredDeviations(int size, + float variance)+
variance * (size-1) == Sum( (data[i] - mean)^2 )
.
++
size
- the number of elements of the data sequence.variance
- the variance of the data sequence.+public static double squares(float[] data)+
Sum ( data[i]*data[i] )
.
++
+public static float sum(float[] data)+
Sum( data[i] )
.
++
Mat.sum(float[])
,
+Mat.sum(float[], float[])
,
+Mat.sum(float[],float)
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Descriptive.Weighted +
public static class Descriptive.Weighted
+Contains methods related to weighted datasets. +
+ +
+
+Method Summary | +|
---|---|
+static float |
+mean(float[] data,
+ float[] weights)
+
++ Returns the weighted mean of a data sequence. |
+
+static float |
+rms(float sumOfProducts,
+ float sumOfSquaredProducts)
+
++ Returns the weighted RMS (Root-Mean-Square) of a data sequence. |
+
+static float |
+var(float[] data,
+ float[] weights,
+ boolean unbiased)
+
++ Returns the weighted variance of a data sequence of length N + There are (unfortunately) many different definitions of the unbiased weighted variance. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float mean(float[] data, + float[] weights)+
Sum (data[i] * weights[i]) / Sum ( weights[i] )
.
++
+public static float rms(float sumOfProducts, + float sumOfSquaredProducts)+
Sum( data[i] * data[i] * weights[i]) / Sum( data[i] * weights[i] )
,
+ or in other words sumOfProducts / sumOfSquaredProducts
.
++
sumOfProducts
- == Sum( data[i] * weights[i] )
.sumOfSquaredProducts
- == Sum( data[i] * data[i] * weights[i] )
.+public static float var(float[] data, + float[] weights, + boolean unbiased)+
+ weighted.var(x,w,unbiased=true) = biasCorrection * Sum ( w[i] * (x[i] - mu_w)^2 ) / Sum(w[i]). ++ where
mu_w
corresponds to the weighted mean and the biasCorrection
+ term
+ + biasCorrection = ( Sum(w[i]) )^2 / ( ( Sum(w[i]) )^2 - Sum( w[i]^2 ) ). ++ corrects for the bias in the variance. + weighted.var(x,w,unbiased=false) computes the biased weighted variance and is given by the formula above, + but without the correction factor. + +
The formula above agrees with that presented in
+
+ the NIST Information Technology Laboratory page.
+ The algorithm used, is a one-pass formula credited to
+ "D. H. D. West (1979). Communications of the ACM, 22, 9, 532-535:
+ Updating Mean and Variance Estimates: An Improved Method", and spelled out under the
+
+ weighted incremental algorithm section of the link.
+
+
unbiased
- set to true to return the unbiased variance (division by (N-1)), false to return the biased value (division by N).
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Descriptive +
public class Descriptive
+Basic descriptive statistics class for exploratory data analysis.
+ Methods for computing Correlations and Covariances are in the Correlation
+ class. Where appropriate, methods with similar functions are grouped into
+ static subclasses. For example, the Descriptive.Sum
class contains the following
+ methods:
+
Descriptive.Sum.inversions(float[], int, int)
+ Descriptive.Sum.logs(float[], int, int)
+ Descriptive.Sum.products(float[], float[])
+ Descriptive.Sum.powerDeviations(float[], int, float)
+ Descriptive.Sum.powers(float[], int)
+ Descriptive.Sum.squaredDeviations(int, float)
+ Descriptive.Sum.squares(float[])
+ Descriptive.Sum.sum(float[])
+ Descriptive.Mean
class has the
+ Descriptive.Mean.columnMean(float[][])
+ Descriptive.Mean.rowMean(float[][])
+ Descriptive.Mean.arithmetic(float[])
+ Descriptive.Mean.geometric(int, float)
+ Descriptive.Mean.harmonic(int, float)
+ Descriptive.Mean.trimmed(float[], float, int, int)
+ + +
+
+Nested Class Summary | +|
---|---|
+static class |
+Descriptive.Mean
+
++ Contains methods for computing the arithmetic, geometric, harmonic, trimmed, and winsorized + means (among others). |
+
+static class |
+Descriptive.Pooled
+
++ Class for computing the pooled mean and variance of data sequences |
+
+static class |
+Descriptive.Sum
+
++ Methods for computing various different sums of datasets such as sum of inversions, + logs, products, power deviations, squares, etc. |
+
+static class |
+Descriptive.Weighted
+
++ Contains methods related to weighted datasets. |
+
+Method Summary | +|
---|---|
+static void |
+frequencies(float[] sortedData,
+ ArrayList<Float> distinctValues,
+ ArrayList<Integer> frequencies)
+
++ Computes the frequency (number of occurances, count) of each distinct value in the given sorted data. |
+
+static float |
+kurtosis(float[] data,
+ float mean,
+ float standardDeviation)
+
++ Returns the kurtosis (aka excess) of a data sequence, which is + -3 + moment(data,4,mean) / standardDeviation4 . |
+
+static float |
+kurtosis(float moment4,
+ float standardDeviation)
+
++ Returns the kurtosis (aka excess) of a data sequence. |
+
+static double |
+max(double[] data)
+
++ Returns the largest member of a data sequence. |
+
+static double |
+max(double a,
+ double b)
+
++ |
+
+static float |
+max(float[] data)
+
++ Returns the largest member of a data sequence. |
+
+static float |
+max(float[][] data)
+
++ Returns the largest member of a matrix. |
+
+static float |
+max(float a,
+ float b)
+
++ |
+
+static int |
+max(int[] data)
+
++ Returns the largest member of a data sequence. |
+
+static int |
+max(int[][] data)
+
++ Returns the largest member of a matrix. |
+
+static int |
+max(int a,
+ int b)
+
++ |
+
+static float |
+mean(float[] data)
+
++ Returns the arithmetic mean of a data sequence; + That is Sum( data[i] ) / data.length . |
+
+static float |
+meanDeviation(float[] data,
+ float mean)
+
++ Returns the mean deviation of a dataset. |
+
+static float |
+median(float[] data,
+ boolean isSorted)
+
++ Returns the median of a data sequence. |
+
+static double |
+min(double[] data)
+
++ Returns the smallest member of a data sequence. |
+
+static double |
+min(double a,
+ double b)
+
++ |
+
+static float |
+min(float[] data)
+
++ Returns the smallest member of a data sequence. |
+
+static float |
+min(float[][] data)
+
++ Returns the smallest member of a matrix. |
+
+static float |
+min(float a,
+ float b)
+
++ |
+
+static int |
+min(int[] data)
+
++ Returns the smallest member of a data sequence. |
+
+static int |
+min(int[][] data)
+
++ Returns the smallest member of a matrix. |
+
+static int |
+min(int a,
+ int b)
+
++ |
+
+static float[] |
+mod(float[] data)
+
++ Returns the array containing the elements that appear the most in a given dataset. |
+
+static float |
+moment(float[] data,
+ int k,
+ float c)
+
++ Returns the moment of k -th order with constant c of a data sequence,
+ which is Sum( (data[i]-c)k ) / data.size() . |
+
+static float[] |
+outliers(float[] data,
+ float lowerLimit,
+ float upperLimit,
+ boolean isSorted)
+
++ Returns the array containing all elements in the dataset that are less than + or equal to the lowerLimit
+ and more than or equal to the upperLimit |
+
+static float |
+product(float[] data)
+
++ Returns the product of a data sequence, which is Prod( data[i] ) . |
+
+static float |
+product(int size,
+ float sumOfLogarithms)
+
++ Returns the product, which is Prod( data[i] ) . |
+
+static float |
+quantile(float[] sortedData,
+ float phi)
+
++ Returns the phi- quantile; that is, an element elem
+ for which holds that phi percent of data elements are less than
+ elem . |
+
+static float |
+quantileInverse(float[] sortedList,
+ float element)
+
++ Returns how many percent of the elements contained in the receiver are <= element . |
+
+static float[] |
+quantiles(float[] sortedData,
+ float[] percentages)
+
++ Returns the quantiles of the specified percentages. |
+
+static float[] |
+quartiles(float[] data,
+ boolean isSorted)
+
++ Returns the quartiles of the input data array (not necessarily sorted). |
+
+static float |
+rankInterpolated(float[] sortedList,
+ float element)
+
++ Returns the linearly interpolated number of elements in an array that + are ≤ a given element. |
+
+static float |
+rms(int size,
+ double sumOfSquares)
+
++ Returns the RMS (Root-Mean-Square) of a data sequence. |
+
+static float |
+skew(float[] data,
+ float mean,
+ float standardDeviation)
+
++ Returns the skew of a data sequence, which is moment(data,3,mean) / standardDeviation3 . |
+
+static float |
+skew(float moment3,
+ float standardDeviation)
+
++ Returns the skew of a data sequence when the 3rd moment has already been computed. |
+
+static float[] |
+std(float[][] data,
+ boolean unbiased)
+
++ Returns an array with each element of the array corresponding to + the standard deviations of each column of the input + matrix. |
+
+static float |
+std(float[] data,
+ boolean unbiased)
+
++ Returns the standard deviation of a dataset. |
+
+static float |
+stdUnbiased(int size,
+ float sampleVariance)
+
++ Returns the unbiased sample standard deviation assuming the sample is normally distributed. |
+
+static float[] |
+tukeyFiveNum(float[] data)
+
++ Return the tukey five number summary of a dataset consisting of the minimum, maximum, + and three quartile values. |
+
+static float[] |
+var(float[][] data,
+ boolean unbiased)
+
++ Returns an array containing the variance of each column of the input matrix X. |
+
+static float |
+var(float[] data,
+ boolean unbiased)
+
++ Returns the variance of a dataset, V. |
+
+static float[][] |
+zScore(float[][] X,
+ float[] means,
+ float[] standardDeviations)
+
++ Computes the standardized version of the input matrix. |
+
+static float[] |
+zScore(float[] x,
+ float mean,
+ float standardDeviation)
+
++ Returns the array of z-scores for a given data array. |
+
+static float |
+zScore(float x,
+ float mean,
+ float standardDeviation)
+
++ |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static void frequencies(float[] sortedData, + ArrayList<Float> distinctValues, + ArrayList<Integer> frequencies)+
distinctValues
and frequencies
have a new size (which is equal for both),
+ which is the number of distinct values in the sorted data.
+
+ Distinct values are filled into distinctValues
, starting at index 0.
+ The frequency of each distinct value is filled into frequencies
, starting at index 0.
+ As a result, the smallest distinct value (and its frequency) can be found at index 0,
+ the second smallest distinct value (and its frequency) at index 1, ...,
+ the largest distinct value (and its frequency) at index distinctValues.size()-1
.
+
sortedData = (5,6,6,7,8,8) --> distinctValues = (5,6,7,8), frequencies = (1,2,1,2)
+
+ Code-wise, you would write:
+ + ArrayList+distinctValues = new ArrayList (); + + ArrayList frequencies = new ArrayList (); + + frequencies(sortedData,distinctValues,frequencies);
+
+
sortedData
- the data; must be sorted ascending.distinctValues
- a ArrayList to be filled with the distinct values; can have any size.frequencies
- a ArrayList to be filled with the frequencies; can have any size;
+ set this parameter to null
to ignore it.Frequency
,
+Unique
+public static float kurtosis(float moment4, + float standardDeviation)+
+
moment4
- the fourth central moment, which is moment(data,4,mean)
.standardDeviation
- the standardDeviation.+public static float kurtosis(float[] data, + float mean, + float standardDeviation)+
-3 + moment(data,4,mean) / standardDeviation4
.
++
+public static int max(int a, + int b)+
+public static float max(float a, + float b)+
+public static double max(double a, + double b)+
+public static double max(double[] data)+
+
+public static float max(float[] data)+
+
+public static int max(int[] data)+
+
+public static float max(float[][] data)+
+
+public static int max(int[][] data)+
+
+public static float mean(float[] data)+
Sum( data[i] ) / data.length
.
+ Similar to Descriptive.Mean.arithmetic(float[])
.
++
+public static float meanDeviation(float[] data, + float mean)+
Sum( Math.abs(data[i]-mean)) ) / data.length
.
++
+public static float median(float[] data, + boolean isSorted)+
+
data
- the data sequence;isSorted
- true if the data sequence is sorted (in ascending order), else false.+public static int min(int a, + int b)+
+public static float min(float a, + float b)+
+public static double min(double a, + double b)+
+public static double min(double[] data)+
+
+public static float min(float[] data)+
+
+public static float min(float[][] data)+
+
+public static int min(int[] data)+
+
+public static int min(int[][] data)+
+
+public static float[] mod(float[] data)+
+
data
- the data array
++public static float moment(float[] data, + int k, + float c)+
k
-th order with constant c
of a data sequence,
+ which is Sum( (data[i]-c)k ) / data.size()
.
++
+public static float[] outliers(float[] data, + float lowerLimit, + float upperLimit, + boolean isSorted)+
lowerLimit
+ and more than or equal to the upperLimit
++
data
- the data arraylowerLimit
- upperLimit
- isSorted
- true if the data array has been sorted in ascending order, else set to false.+public static float product(int size, + float sumOfLogarithms)+
Prod( data[i] )
.
+ In other words: data[0]*data[1]*...*data[data.length-1]
.
+ This method uses the equivalent definition:
+ prod = pow( exp( Sum( Log(x[i]) ) / length*length)
.
++
+public static float product(float[] data)+
Prod( data[i] )
.
+ In other words: data[0]*data[1]*...*data[data.length-1]
.
+ Note that you may easily get numeric overflows. Use product(int,float)
+ instead to avoid that.
++
+public static float[] quartiles(float[] data, + boolean isSorted)+
Details:
+ The first quartile, or lower quartile (Q[0]), is the value that cuts off
+ the first 25% of the data when it is sorted in ascending order.
+ The second quartile, or median (Q[1]), is the value that cuts off the first 50%.
+ The third quartile, or upper quartile (Q[2], is the value that cuts off the first 75%.
+
+
data
- The data array.isSorted
- true if the data array has been sorted in ascending order, else set to false.
++public static float quantile(float[] sortedData, + float phi)+
phi-
quantile; that is, an element elem
+ for which holds that phi
percent of data elements are less than
+ elem
.
+ The quantile need not necessarily be contained in the data sequence,
+ it can be a linear interpolation.
++
sortedData
- the data sequence; must be sorted ascending.phi
- the percentage; must satisfy 0 <= phi <= 1
.+public static float quantileInverse(float[] sortedList, + float element)+
<= element
.
+ Does linear interpolation if the element is not contained but lies in between
+ two contained elements.
++
sortedList
- the list to be searched (must be sorted ascending).element
- the element to search for.
+phi
of elements <= element
+ (0.0 <= phi <= 1.0)
.+public static float[] quantiles(float[] sortedData, + float[] percentages)+
+
sortedData
- the data sequence; must be sorted ascending.percentages
- the percentages for which quantiles are to be computed.
+ Each percentage must be in the interval [0.0f,1.0f]
.
++public static float rankInterpolated(float[] sortedList, + float element)+
{0, 1, 2,..., sortedList.size()}
.
+ If no element is ≤ element, then the rank is zero.
+ +If the element lies in between two contained elements, then + linear interpolation is used and a non integer value is returned.
++
sortedList
- the list to be searched (must be sorted ascending).element
- the element to search for.
++public static float rms(int size, + double sumOfSquares)+
Math.sqrt(Sum( data[i]*data[i] ) / data.length)
.
+ The RMS of data sequence is the square-root of the mean of the squares of the elements in the data sequence.
+ It is a measure of the average "size" of the elements of a data sequence.
++
sumOfSquares
- sumOfSquares(data) == Sum( data[i]*data[i] )
of the data sequence.size
- the number of elements in the data sequence.+public static float skew(float moment3, + float standardDeviation)+
+
moment3
- the third central moment, which is moment(data,3,mean)
.standardDeviation
- the standardDeviation.+public static float skew(float[] data, + float mean, + float standardDeviation)+
moment(data,3,mean) / standardDeviation3
.
++
+public static float std(float[] data, + boolean unbiased)+
+ sigma_1 = 1/(N-1) * Sum( (x[i] - mean(x))^2 ) + sigma_2 = 1/(N) * Sum( (x[i] - mean(x))^2 ) ++ sigma_1 is the square root of an unbiased estimator of the variance of the population + the x is drawn, as long as x consists of independent, identically distributed samples. + sigma_2 corresponds to the second moment of the set of values about their mean. +
+ std(data,unbiased==true) returns sigma_1 above, while std(data,unbiased==false) returns sigma_2. +
+
data
- the datasetunbiased
- set to true to return the unbiased standard deviation,
+ false to return the biased version.+public static float[] std(float[][] data, + boolean unbiased)+
+ sigma_1 = 1/(N-1) * Sum( (x[i] - mean(x))^2 ) + sigma_2 = 1/(N) * Sum( (x[i] - mean(x))^2 ) ++ sigma_1 is the square root of an unbiased estimator of the variance of the population + the x is drawn, as long as x consists of independent, identically distributed samples. + sigma_2 corresponds to the second moment of the set of values about their mean. +
+ std(data,unbiased==true) returns sigma_1 above, while std(data,unbiased==false) returns sigma_2. +
+
data
- the datasetunbiased
- set to true to return the unbiased standard deviation,
+ false to return the biased version.+public static float stdUnbiased(int size, + float sampleVariance)+
See also this entry on wikipedia.org +
+
size
- the number of elements of the data sequence.sampleVariance
- the sample variance.+public static float[] tukeyFiveNum(float[] data)+
+
data
- the data array
++public static float var(float[] data, + boolean unbiased)+
+ var(x,true) normalizes V by N - 1 if N > 1, where N is the sample size. + This is an unbiased estimator of the variance of the population from which X is drawn, + as long as X consists of independent, identically distributed samples. For N = 1, + V is normalized by 1. +
+ V = var(x,false) normalizes by N and produces the second moment of the sample about its mean. + +
Reference:
+
+ Algorithms for calculating variance, Wikipedia.org
+
+ Incremental calculation of weighted mean and variance, Tony Finch
+
+
data
- the data sequence.unbiased
- set to true to return the unbiased variance (division by (N-1)), false to return the biased value (division by N).
++public static float[] var(float[][] data, + boolean unbiased)+
+ var(X,unbiased=true) normalizes V by N - 1 if N > 1, where N is the sample size. + This is an unbiased estimator of the variance of the population from which X is drawn, + as long as X consists of independent, identically distributed samples. For N = 1, + V is normalized by 1. +
+ V = var(X,unbiased=false) normalizes by N and produces the second moment of the sample about its mean. + +
Reference:
+
+ Algorithms for calculating variance, Wikipedia.org
+
+ Incremental calculation of weighted mean and variance, Tony Finch
+
+
data
- the data sequence.unbiased
- set to true to return the unbiased variance (division by (N-1)), false to return the biased value (division by N).
++public static float zScore(float x, + float mean, + float standardDeviation)+
(x-mean)/standardDeviation
+public static float[] zScore(float[] x, + float mean, + float standardDeviation)+
z[i] = ( x[i] - mean ) / standardDeviation
.
++
+public static float[][] zScore(float[][] X, + float[] means, + float[] standardDeviations)+
j
, of the output is given by
+ Z[i][j] = ( X[i][j]- mean(X[,j]) / standardDeviation(X[,j])
+ where mean(X[,j]) is the mean value of column j
of X
.
++
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Distance +
public final class Distance
+Contains methods for computing various "distance" metrics for multidimensional scaling. +
+ For an m-by-n input matrix with m observations and n variables, the output D is + the symmetric m-by-m matrix with zeros along the diagonals and element ij + specifying the distance between rows i and j. +
+ +
+
+Method Summary | +|
---|---|
+static float[][] |
+chebychev(float[][] X)
+
++ Computes the Chebychev distance matrix between pairs of objects + in the m-by-n data matrix X. |
+
+static float[][] |
+cityblock(float[][] X)
+
++ Computes the cityblock (or Manhattan) distance matrix between pairs of objects + in the m-by-n data matrix X. |
+
+static float[][] |
+correlation(float[][] X)
+
++ Computes the correlation distance matrix between pairs of objects + in the m-by-n data matrix X. |
+
+static float[][] |
+cosine(float[][] X)
+
++ Computes the cosine distance matrix between pairs of objects + in the m-by-n data matrix X. |
+
+static float[][] |
+euclidean(float[][] X)
+
++ Computes the Euclidean distance between pairs of objects + in the m-by-n data matrix X. |
+
+static float[][] |
+mahalanobis(float[][] X)
+
++ Computes the Mahalanobis distance matrix of the m-by-n input matrix X. |
+
+static float[][] |
+minkowski(float[][] X,
+ int exp)
+
++ Returns the Minkowski distance matrix of the m-by-n input matrix X. |
+
+static float[][] |
+seuclidean(float[][] X)
+
++ Computes the standardized Euclidean distance between pairs of objects + in the m-by-n data matrix X by standardizing X first prior to computing + the distances. |
+
+static float[][] |
+spearman(float[][] X)
+
++ Computes the Spearman distance matrix of the m-by-n input matrix X. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[][] cityblock(float[][] X)+
+ Dist[i][j] = (X[i][1] - X[j][1]) + (X[i][2] - X[j][2]) + ... + (X[i][n] - X[j][n]). ++
+ Rows of X correspond to observations, and columns correspond to variables. + Returns the m-by-m matrix D with D[r1][r2] corresponding to the cityblock + (or manhattan) distance between the rows of the input X. +
+
+public static float[][] chebychev(float[][] X)+
+ Dist[i][j] =Max( |X[i][1] - X[j][1]| , |X[i][2] - X[j][2]| , ... , |(X[i][n] - X[j][n]|). ++
+ Rows of X correspond to observations, and columns correspond to variables. + Returns the m-by-m matrix D with D[r1][r2] corresponding to the Chebychev + distance between the rows of the input X. +
+
+public static float[][] correlation(float[][] X)+
+ Dist[1][2] = 1 - sum_{j=1^numColumns} ( ( y1_j - mean(y1) ) * ( y2_j - mean(y2) ) / ( n * std(y1)*std(y2) ) ), ++ where n = number of columns. +
+ Rows of X correspond to observations, and columns correspond to variables. + Returns the m-by-m matrix D with D[r1][r2] corresponding to the correlation + distance between the rows of the input X. +
+
+public static float[][] cosine(float[][] X)+
+ Dist[1][2] = 1- sum_{j=1^numColumns} ( y1_j * y2_j ) / ( mag(y1)*mag(y2) ), ++ where
mag(y1) = sum_{i=1 to numColumns} (y1[j]*y1[j])
and likewise
+ for mag(y2)
.
+ etc.
+ + Rows of X correspond to observations, and columns correspond to variables. + Returns the m-by-m matrix D with D[r1][r2] corresponding to the cosine + distance between the rows of the input X. +
+
+public static float[][] euclidean(float[][] X)+
+ Dist[i][j]^2 = (X[i][1] - X[j][1]) ^2+ (X[i][2] - X[j][2])^2 + ... + (X[i][n] - X[j][n])^2. ++
+ Rows of X correspond to observations, and columns correspond to variables. + Returns the m-by-m matrix D with D[r1][r2] corresponding to the Euclidean + distance between the rows of the input X. +
+
+public static float[][] seuclidean(float[][] X)+
+ standardized value = (original value - mean)/standard deviation ++ where the mean and standard deviation correspond to the column mean and std. +
+ Rows of X correspond to observations, and columns correspond to variables. + Returns the m-by-m matrix D with D[r1][r2] corresponding to the standardized + Euclidean distance between the rows of the input X. +
+
+public static float[][] mahalanobis(float[][] X)+
+ Dist[i][j]^2 = (X[i][1] - X[j][1])^2/ invcov[1][1] + ... + (X[i][n] - X[j][n])^2/ invcov[n][n]. ++ + Rows of X correspond to observations, and columns correspond to variables. + Returns the m-by-m matrix D with D[r1][r2] corresponding to the s + mahalanobis distance between the rows of the input X. +
+
+public static float[][] minkowski(float[][] X, + int exp)+
+ Dist[i][j]^p= (X[i][1] - X[j][1])^p/ + (X[i][2] - X[j][2])^p + ... + (X[i][n] - X[j][n])^p. ++ Notice that for the special case of p = 1, the Minkowski metric + gives the city block metric, for the special case of p = 2, + the Minkowski metric gives the Euclidean distance, + and for the special case of p = &inf;, the Minkowski metric gives the Chebychev distance. + Also notice that the larger the value of p, the higher the probability of causing overflow + errors (which are, in turn, highly correlated to headaches and overall feelings of malaise). +
+
exp
- the Minkowski exponent (positive).+public static float[][] spearman(float[][] X)+
r11, r12, ... , r1n
+ r21, r22, ... , r2n
,
+ Dist[1][2] = 1 - sum_{j=1^numColumns} ( ( r1_j - mean(r1) ) * ( r2_j - mean(r2) ) / ( n * std(r1)*std(r2) ) ),
+
+ and similarly for the remaining rows.
+ + Rows of X correspond to observations, and columns correspond to variables. + Returns the m-by-m matrix D with D[r1][r2] corresponding to the Spearman + distance between the rows of the input X. +
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Eigenvalue +
public class Eigenvalue
+Eigenvalues and eigenvectors of a real matrix. +
+ If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is + diagonal and the eigenvector matrix V is orthogonal. + I.e. A = V.times(D.times(V.transpose())) and + V.times(V.transpose()) equals the identity matrix. +
+ If A is not symmetric, then the eigenvalue matrix D is block diagonal + with the real eigenvalues in 1-by-1 blocks and any complex eigenvalues, + lambda + i*mu, in 2-by-2 blocks, [lambda, mu; -mu, lambda]. The + columns of V represent the eigenvectors in the sense that A*V = V*D, + i.e. A.times(V) equals V.times(D). The matrix V may be badly + conditioned, or even singular, so the validity of the equation + A = V*D*inverse(V) depends upon V.cond(). +
+Shamelessly copied (and modified) from the
+JAMA Java
+Matrix package. To make things compatible with how most users use Processing, the
+class take in float matrices. However, to preserve the acccuracy of the computations, the algorithm
+first casts the input into a double array, prior to doing anything. All methods also return doubles; Use
+Cast.doubleToFloat(double[][])
if you want/need to cast everything back to floats for
+further (non-high-accuracy-dependant) processing (pun intended).
+
+ +
+
+Constructor Summary | +|
---|---|
Eigenvalue(float[][] Arg)
+
++ Check for symmetry, then construct the eigenvalue decomposition. |
+
+Method Summary | +|
---|---|
+ double[][] |
+getD()
+
++ Return the block diagonal eigenvalue matrix |
+
+ double[] |
+getImagEigenvalues()
+
++ Return the imaginary parts of the eigenvalues |
+
+ double[] |
+getRealEigenvalues()
+
++ Return the real parts of the eigenvalues |
+
+ double[][] |
+getV()
+
++ Return the eigenvector matrix with each column corresponding to + an eigenvector of the input matrix. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public Eigenvalue(float[][] Arg)+
+
getV()
,
+getD()
,
+getRealEigenvalues()
,
+getImagEigenvalues()
+Method Detail | +
---|
+public double[][] getV()+
+
+public double[] getRealEigenvalues()+
+
+public double[] getImagEigenvalues()+
+
+public double[][] getD()+
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Find +
public final class Find
+Static class for finding indices in an array corresponding to a given value/object. +
+ (Portions of this code were (conveniently!) copied and pasted from commons.apache.org.) :) +
++ +
+
+Field Summary | +
---|
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Method Summary | +|
---|---|
+static boolean |
+contains(byte[] array,
+ byte valueToFind)
+
++ Checks if the value is in the given array. |
+
+static boolean |
+contains(char[] array,
+ char valueToFind)
+
++ Checks if the value is in the given array. |
+
+static boolean |
+contains(double[] array,
+ double valueToFind)
+
++ Checks if the value is in the given array. |
+
+static boolean |
+contains(double[] array,
+ double valueToFind,
+ double tolerance)
+
++ Checks if a value falling within the given tolerance is in the + given array. |
+
+static boolean |
+contains(float[] array,
+ float valueToFind)
+
++ Checks if the value is in the given array. |
+
+static boolean |
+contains(int[] array,
+ int valueToFind)
+
++ Checks if the value is in the given array. |
+
+static boolean |
+contains(long[] array,
+ long valueToFind)
+
++ Checks if the value is in the given array. |
+
+static boolean |
+contains(Object[] array,
+ Object objectToFind)
+
++ Checks if the object is in the given array. |
+
+static boolean |
+contains(short[] array,
+ short valueToFind)
+
++ Checks if the value is in the given array. |
+
+static int |
+indexOf(boolean[] array,
+ boolean valueToFind)
+
++ Finds the index of the given value in the array. |
+
+static int |
+indexOf(boolean[] array,
+ boolean valueToFind,
+ int startIndex)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int |
+indexOf(byte[] array,
+ byte valueToFind)
+
++ Finds the index of the given value in the array. |
+
+static int |
+indexOf(byte[] array,
+ byte valueToFind,
+ int startIndex)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int |
+indexOf(char[] array,
+ char valueToFind)
+
++ Finds the index of the given value in the array. |
+
+static int |
+indexOf(char[] array,
+ char valueToFind,
+ int startIndex)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int |
+indexOf(double[] array,
+ double valueToFind)
+
++ Finds the index of the given value in the array. |
+
+static int |
+indexOf(double[] array,
+ double valueToFind,
+ double tolerance)
+
++ Finds the index of the given value within a given tolerance in the array. |
+
+static int |
+indexOf(double[] array,
+ double valueToFind,
+ int startIndex)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int |
+indexOf(double[] array,
+ double valueToFind,
+ int startIndex,
+ double tolerance)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int |
+indexOf(float[] array,
+ float valueToFind)
+
++ Finds the index of the given value in the array. |
+
+static int |
+indexOf(float[] array,
+ float valueToFind,
+ int startIndex)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int |
+indexOf(int[] array,
+ int valueToFind)
+
++ Finds the index of the given value in the array. |
+
+static int |
+indexOf(int[] array,
+ int valueToFind,
+ int startIndex)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int |
+indexOf(long[] array,
+ long valueToFind)
+
++ Finds the index of the given value in the array. |
+
+static int |
+indexOf(long[] array,
+ long valueToFind,
+ int startIndex)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int |
+indexOf(Object[] array,
+ Object objectToFind)
+
++ Finds the index of the given object in the array. |
+
+static int |
+indexOf(Object[] array,
+ Object objectToFind,
+ int startIndex)
+
++ Finds the index of the given object in the array starting at the given index. |
+
+static int |
+indexOf(short[] array,
+ short valueToFind)
+
++ Finds the index of the given value in the array. |
+
+static int |
+indexOf(short[] array,
+ short valueToFind,
+ int startIndex)
+
++ Finds the index of the given value in the array starting at the given index. |
+
+static int[] |
+indicesGreaterThan(float[] array,
+ float valueOfMin)
+
++ |
+
+static int[] |
+indicesGreaterThan(int[] array,
+ int valueOfMin)
+
++ |
+
+static int[] |
+indicesGreaterThanOrEqualTo(float[] array,
+ float valueOfMin)
+
++ |
+
+static int[] |
+indicesGreaterThanOrEqualTo(int[] array,
+ int valueOfMin)
+
++ |
+
+static int[] |
+indicesLessThan(float[] array,
+ float valueOfMax)
+
++ |
+
+static int[] |
+indicesLessThan(int[] array,
+ int valueOfMax)
+
++ |
+
+static int[] |
+indicesLessThanOrEqualTo(float[] array,
+ float valueOfMax)
+
++ |
+
+static int[] |
+indicesLessThanOrEqualTo(int[] array,
+ int valueOfMax)
+
++ |
+
+static int[] |
+indicesWith(boolean[] array,
+ boolean valueToFind)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(boolean[] array,
+ boolean valueToFind,
+ int startIndex)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(byte[] array,
+ byte valueToFind)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(byte[] array,
+ byte valueToFind,
+ int startIndex)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(char[] array,
+ char valueToFind)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(char[] array,
+ char valueToFind,
+ int startIndex)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(double[] array,
+ double valueToFind)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(double[] array,
+ double valueToFind,
+ int startIndex)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(float[] array,
+ float valueToFind)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(float[] array,
+ float valueToFind,
+ int startIndex)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(int[] array,
+ int valueToFind)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(int[] array,
+ int valueToFind,
+ int startIndex)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(long[] array,
+ long valueToFind)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(long[] array,
+ long valueToFind,
+ int startIndex)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(Object[] array,
+ Object objectToFind)
+
++ Finds the indices containing the specified object in the array. |
+
+static int[] |
+indicesWith(Object[] array,
+ Object objectToFind,
+ int startIndex)
+
++ Finds the indices containing the specified object in the array. |
+
+static int[] |
+indicesWith(short[] array,
+ short valueToFind)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWith(short[] array,
+ short valueToFind,
+ int startIndex)
+
++ Finds the indices containing the specified value in the array. |
+
+static int[] |
+indicesWithin(float[] array,
+ float minVal,
+ float maxVal)
+
++ Finds the indices for the array elements within the min and max value (inclusive). |
+
+static int[] |
+indicesWithin(int[] array,
+ float minVal,
+ float maxVal)
+
++ Finds the indices for the array elements within the min and max value (inclusive). |
+
+static int |
+lastIndexOf(boolean[] array,
+ boolean valueToFind)
+
++ Finds the last index of the given value within the array. |
+
+static int |
+lastIndexOf(boolean[] array,
+ boolean valueToFind,
+ int startIndex)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+lastIndexOf(byte[] array,
+ byte valueToFind)
+
++ Finds the last index of the given value within the array. |
+
+static int |
+lastIndexOf(byte[] array,
+ byte valueToFind,
+ int startIndex)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+lastIndexOf(char[] array,
+ char valueToFind)
+
++ Finds the last index of the given value within the array. |
+
+static int |
+lastIndexOf(char[] array,
+ char valueToFind,
+ int startIndex)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+lastIndexOf(double[] array,
+ double valueToFind)
+
++ Finds the last index of the given value within the array. |
+
+static int |
+lastIndexOf(double[] array,
+ double valueToFind,
+ double tolerance)
+
++ Finds the last index of the given value within a given tolerance in the array. |
+
+static int |
+lastIndexOf(double[] array,
+ double valueToFind,
+ int startIndex)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+lastIndexOf(double[] array,
+ double valueToFind,
+ int startIndex,
+ double tolerance)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+lastIndexOf(float[] array,
+ float valueToFind)
+
++ Finds the last index of the given value within the array. |
+
+static int |
+lastIndexOf(float[] array,
+ float valueToFind,
+ int startIndex)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+lastIndexOf(int[] array,
+ int valueToFind)
+
++ Finds the last index of the given value within the array. |
+
+static int |
+lastIndexOf(int[] array,
+ int valueToFind,
+ int startIndex)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+lastIndexOf(long[] array,
+ long valueToFind)
+
++ Finds the last index of the given value within the array. |
+
+static int |
+lastIndexOf(long[] array,
+ long valueToFind,
+ int startIndex)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+lastIndexOf(Object[] array,
+ Object objectToFind)
+
++ Finds the last index of the given object within the array. |
+
+static int |
+lastIndexOf(Object[] array,
+ Object objectToFind,
+ int startIndex)
+
++ Finds the last index of the given object in the array starting at the given index. |
+
+static int |
+lastIndexOf(short[] array,
+ short valueToFind)
+
++ Finds the last index of the given value within the array. |
+
+static int |
+lastIndexOf(short[] array,
+ short valueToFind,
+ int startIndex)
+
++ Finds the last index of the given value in the array starting at the given index. |
+
+static int |
+numGreaterThan(float[] array,
+ float valueOfMin)
+
++ Finds the number of elements strictly greater than the specified value. |
+
+static int |
+numGreaterThan(int[] array,
+ int valueOfMin)
+
++ Finds the number of elements strictly greater than the specified value. |
+
+static int |
+numGreaterThanOrEqualTo(float[] array,
+ float valueOfMin)
+
++ Finds the number of elements greater than or equal to the specified value. |
+
+static int |
+numGreaterThanOrEqualTo(int[] array,
+ int valueOfMin)
+
++ Finds the number of elements greater than or equal to the specified value. |
+
+static int |
+numLessThan(float[] array,
+ float valueOfMax)
+
++ Finds the number of elements strictly less than the specified value. |
+
+static int |
+numLessThan(int[] array,
+ int valueOfMax)
+
++ Finds the number of elements strictly less than the specified value. |
+
+static int |
+numLessThanOrEqualTo(float[] array,
+ float valueOfMax)
+
++ Finds the number of elements less than or equal to the specified value. |
+
+static int |
+numLessThanOrEqualTo(int[] array,
+ int valueOfMax)
+
++ Finds the number of elements less than or equal to the specified value. |
+
+static int |
+numRepeats(boolean[] array,
+ boolean valueToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static int |
+numRepeats(byte[] array,
+ byte valueToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static int |
+numRepeats(char[] array,
+ char valueToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static int |
+numRepeats(double[] array,
+ double valueToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static int |
+numRepeats(float[] array,
+ float valueToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static int |
+numRepeats(int[] array,
+ int valueToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static int |
+numRepeats(long[] array,
+ long valueToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static int |
+numRepeats(Object[] array,
+ Object objectToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static int |
+numRepeats(short[] array,
+ short valueToFind)
+
++ Finds the number of times a given value/object is present in an array. |
+
+static float[] |
+uniqueElems(float[] array)
+
++ Find the unique elements in a float array (uses HashSets) |
+
+static int[] |
+uniqueElems(int[] array)
+
++ Find the unique elements in a int array (uses HashSets) |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static int indexOf(Object[] array, + Object objectToFind)+
Finds the index of the given object in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
objectToFind
- the object to find, may be null
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(Object[] array, + Object objectToFind, + int startIndex)+
Finds the index of the given object in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
objectToFind
- the object to find, may be null
startIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(Object[] array, + Object objectToFind)+
Finds the last index of the given object within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to travers backwords looking for the object, may be null
objectToFind
- the object to find, may be null
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(Object[] array, + Object objectToFind, + int startIndex)+
Finds the last index of the given object in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than
+ the array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
objectToFind
- the object to find, may be null
startIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static boolean contains(Object[] array, + Object objectToFind)+
Checks if the object is in the given array.
+ +The method returns false
if a null
array is passed in.
+
array
- the array to search throughobjectToFind
- the object to find
+true
if the array contains the object+public static int[] indicesWith(Object[] array, + Object objectToFind)+
Finds the indices containing the specified object in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the object to find, may be null
objectToFind
- the object to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(Object[] array, + Object objectToFind, + int startIndex)+
Finds the indices containing the specified object in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the object to find, may be null
objectToFind
- the object to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int numRepeats(Object[] array, + Object objectToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectobjectToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(long[] array, + long valueToFind)+
Finds the index of the given value in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(long[] array, + long valueToFind, + int startIndex)+
Finds the index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(long[] array, + long valueToFind)+
Finds the last index of the given value within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to travers backwords looking for the object, may be null
valueToFind
- the object to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(long[] array, + long valueToFind, + int startIndex)+
Finds the last index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than the
+ array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static boolean contains(long[] array, + long valueToFind)+
Checks if the value is in the given array.
+ +The method returns false
if a null
array is passed in.
+
array
- the array to search throughvalueToFind
- the value to find
+true
if the array contains the object+public static int[] indicesWith(long[] array, + long valueToFind)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(long[] array, + long valueToFind, + int startIndex)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int numRepeats(long[] array, + long valueToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectvalueToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(int[] array, + int valueToFind)+
Finds the index of the given value in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(int[] array, + int valueToFind, + int startIndex)+
Finds the index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(int[] array, + int valueToFind)+
Finds the last index of the given value within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to travers backwords looking for the object, may be null
valueToFind
- the object to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(int[] array, + int valueToFind, + int startIndex)+
Finds the last index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than the
+ array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static boolean contains(int[] array, + int valueToFind)+
Checks if the value is in the given array.
+ +The method returns false
if a null
array is passed in.
+
array
- the array to search throughvalueToFind
- the value to find
+true
if the array contains the object+public static int[] indicesWith(int[] array, + int valueToFind)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(int[] array, + int valueToFind, + int startIndex)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWithin(int[] array, + float minVal, + float maxVal)+
Finds the indices for the array elements within the min and max value (inclusive).
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
+public static int[] indicesLessThanOrEqualTo(int[] array, + int valueOfMax)+
+public static int[] indicesLessThan(int[] array, + int valueOfMax)+
+public static int[] indicesGreaterThanOrEqualTo(int[] array, + int valueOfMin)+
+public static int[] indicesGreaterThan(int[] array, + int valueOfMin)+
+public static int numRepeats(int[] array, + int valueToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectvalueToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int numLessThanOrEqualTo(int[] array, + int valueOfMax)+
Finds the number of elements less than or equal to the specified value.
++
array
- the array to search through for the value/objectvalueOfMax
- the maximum value
++public static int numLessThan(int[] array, + int valueOfMax)+
Finds the number of elements strictly less than the specified value.
++
array
- the array to search through for the value/objectvalueOfMax
- the maximum value
++public static int numGreaterThanOrEqualTo(int[] array, + int valueOfMin)+
Finds the number of elements greater than or equal to the specified value.
++
array
- the array to search through for the value/objectvalueOfMax
- the maximum value
++public static int numGreaterThan(int[] array, + int valueOfMin)+
Finds the number of elements strictly greater than the specified value.
++
array
- the array to search through for the value/objectvalueOfMax
- the maximum value
++public static int[] uniqueElems(int[] array)+
+
+public static int indexOf(short[] array, + short valueToFind)+
Finds the index of the given value in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(short[] array, + short valueToFind, + int startIndex)+
Finds the index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(short[] array, + short valueToFind)+
Finds the last index of the given value within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to travers backwords looking for the object, may be null
valueToFind
- the object to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(short[] array, + short valueToFind, + int startIndex)+
Finds the last index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than the
+ array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static boolean contains(short[] array, + short valueToFind)+
Checks if the value is in the given array.
+ +The method returns false
if a null
array is passed in.
+
array
- the array to search throughvalueToFind
- the value to find
+true
if the array contains the object+public static int[] indicesWith(short[] array, + short valueToFind)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(short[] array, + short valueToFind, + int startIndex)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int numRepeats(short[] array, + short valueToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectvalueToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(char[] array, + char valueToFind)+
Finds the index of the given value in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(char[] array, + char valueToFind, + int startIndex)+
Finds the index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(char[] array, + char valueToFind)+
Finds the last index of the given value within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to travers backwords looking for the object, may be null
valueToFind
- the object to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(char[] array, + char valueToFind, + int startIndex)+
Finds the last index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than the
+ array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static boolean contains(char[] array, + char valueToFind)+
Checks if the value is in the given array.
+ +The method returns false
if a null
array is passed in.
+
array
- the array to search throughvalueToFind
- the value to find
+true
if the array contains the object+public static int[] indicesWith(char[] array, + char valueToFind)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(char[] array, + char valueToFind, + int startIndex)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int numRepeats(char[] array, + char valueToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectvalueToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(byte[] array, + byte valueToFind)+
Finds the index of the given value in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(byte[] array, + byte valueToFind, + int startIndex)+
Finds the index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(byte[] array, + byte valueToFind)+
Finds the last index of the given value within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to travers backwords looking for the object, may be null
valueToFind
- the object to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(byte[] array, + byte valueToFind, + int startIndex)+
Finds the last index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than the
+ array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static boolean contains(byte[] array, + byte valueToFind)+
Checks if the value is in the given array.
+ +The method returns false
if a null
array is passed in.
+
array
- the array to search throughvalueToFind
- the value to find
+true
if the array contains the object+public static int[] indicesWith(byte[] array, + byte valueToFind)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(byte[] array, + byte valueToFind, + int startIndex)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int numRepeats(byte[] array, + byte valueToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectvalueToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(double[] array, + double valueToFind)+
Finds the index of the given value in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(double[] array, + double valueToFind, + double tolerance)+
Finds the index of the given value within a given tolerance in the array. + This method will return the index of the first value which falls between the region + defined by valueToFind - tolerance and valueToFind + tolerance.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findtolerance
- tolerance of the search
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(double[] array, + double valueToFind, + int startIndex)+
Finds the index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(double[] array, + double valueToFind, + int startIndex, + double tolerance)+
Finds the index of the given value in the array starting at the given index. + This method will return the index of the first value which falls between the region + defined by valueToFind - tolerance and valueToFind + tolerance.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching attolerance
- tolerance of the search
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(double[] array, + double valueToFind)+
Finds the last index of the given value within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to travers backwords looking for the object, may be null
valueToFind
- the object to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(double[] array, + double valueToFind, + double tolerance)+
Finds the last index of the given value within a given tolerance in the array. + This method will return the index of the last value which falls between the region + defined by valueToFind - tolerance and valueToFind + tolerance.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findtolerance
- tolerance of the search
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(double[] array, + double valueToFind, + int startIndex)+
Finds the last index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than the
+ array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(double[] array, + double valueToFind, + int startIndex, + double tolerance)+
Finds the last index of the given value in the array starting at the given index. + This method will return the index of the last value which falls between the region + defined by valueToFind - tolerance and valueToFind + tolerance.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than the
+ array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards fromtolerance
- search for value within plus/minus this amount
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static boolean contains(double[] array, + double valueToFind)+
Checks if the value is in the given array.
+ +The method returns false
if a null
array is passed in.
+
array
- the array to search throughvalueToFind
- the value to find
+true
if the array contains the object+public static boolean contains(double[] array, + double valueToFind, + double tolerance)+
Checks if a value falling within the given tolerance is in the + given array. If the array contains a value within the inclusive range + defined by (value - tolerance) to (value + tolerance).
+ +The method returns false
if a null
array
+ is passed in.
+
array
- the array to searchvalueToFind
- the value to findtolerance
- the array contains the tolerance of the search
++public static int[] indicesWith(double[] array, + double valueToFind)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(double[] array, + double valueToFind, + int startIndex)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int numRepeats(double[] array, + double valueToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectvalueToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(float[] array, + float valueToFind)+
Finds the index of the given value in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(float[] array, + float valueToFind, + int startIndex)+
Finds the index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(float[] array, + float valueToFind)+
Finds the last index of the given value within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to travers backwords looking for the object, may be null
valueToFind
- the object to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(float[] array, + float valueToFind, + int startIndex)+
Finds the last index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than the
+ array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static boolean contains(float[] array, + float valueToFind)+
Checks if the value is in the given array.
+ +The method returns false
if a null
array is passed in.
+
array
- the array to search throughvalueToFind
- the value to find
+true
if the array contains the object+public static int[] indicesWith(float[] array, + float valueToFind)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(float[] array, + float valueToFind, + int startIndex)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWithin(float[] array, + float minVal, + float maxVal)+
Finds the indices for the array elements within the min and max value (inclusive).
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
+public static int numRepeats(float[] array, + float valueToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectvalueToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int[] indicesLessThanOrEqualTo(float[] array, + float valueOfMax)+
+public static int[] indicesLessThan(float[] array, + float valueOfMax)+
+public static int[] indicesGreaterThanOrEqualTo(float[] array, + float valueOfMin)+
+public static int[] indicesGreaterThan(float[] array, + float valueOfMin)+
+public static int numLessThanOrEqualTo(float[] array, + float valueOfMax)+
Finds the number of elements less than or equal to the specified value.
++
array
- the array to search through for the value/objectvalueOfMax
- the maximum value
++public static int numLessThan(float[] array, + float valueOfMax)+
Finds the number of elements strictly less than the specified value.
++
array
- the array to search through for the value/objectvalueOfMax
- the maximum value
++public static int numGreaterThanOrEqualTo(float[] array, + float valueOfMin)+
Finds the number of elements greater than or equal to the specified value.
++
array
- the array to search through for the value/objectvalueOfMax
- the maximum value
++public static int numGreaterThan(float[] array, + float valueOfMin)+
Finds the number of elements strictly greater than the specified value.
++
array
- the array to search through for the value/objectvalueOfMax
- the maximum value
++public static float[] uniqueElems(float[] array)+
+
+public static int indexOf(boolean[] array, + boolean valueToFind)+
Finds the index of the given value in the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
+
array
- the array to search through for the object, may be null
valueToFind
- the value to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int indexOf(boolean[] array, + boolean valueToFind, + int startIndex)+
Finds the index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
+ length will return PapayaConstants.INDEX_NOT_FOUND
(-1
).
+
array
- the array to search through for the object, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
+ array input+public static int lastIndexOf(boolean[] array, + boolean valueToFind)+
Finds the last index of the given value within the array.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) if
+ null
array input.
+
array
- the array to travers backwords looking for the object, may be null
valueToFind
- the object to find
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int lastIndexOf(boolean[] array, + boolean valueToFind, + int startIndex)+
Finds the last index of the given value in the array starting at the given index.
+ +This method returns PapayaConstants.INDEX_NOT_FOUND
(-1
) for a null
input array.
A negative startIndex will return PapayaConstants.INDEX_NOT_FOUND
(-1
). A startIndex larger than
+ the array length will search from the end of the array.
+
array
- the array to traverse for looking for the object, may be null
valueToFind
- the value to findstartIndex
- the start index to travers backwards from
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input+public static int[] indicesWith(boolean[] array, + boolean valueToFind)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to find
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int[] indicesWith(boolean[] array, + boolean valueToFind, + int startIndex)+
Finds the indices containing the specified value in the array.
+ +This method returns PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) for a null
input array.
+
array
- the array to search through for the value to find, may be null
valueToFind
- the value to findstartIndex
- the index to start searching at
+PapayaConstants.INDICES_NOT_FOUND
(int[] = -1
) if not found or null
array input+public static int numRepeats(boolean[] array, + boolean valueToFind)+
Finds the number of times a given value/object is present in an array.
++
array
- the array to search through for the value/objectvalueToFind
- the object to find.
+PapayaConstants.INDEX_NOT_FOUND
(-1
) if not found or null
array input
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Frequency +
public class Frequency
+Class for getting the frequency distribution, cumulative frequency distribution, and other
+ distribution-related parameters of a given array of
+ Frequency freq= new Frequency(dataArray, minimumVal,maximumVal,widthOfEachBin);
+
+ Once initialized, it stores 3 things:
+
getFrequency()
).
+ A number of different methods are available for computing other distribution-related parameters + (e.g. the relative frequency distribution, the cumulative frequency distribution, etc).
+ +See the FrequencyExample in the examples folder for an example.
++ +
+
+Constructor Summary | +|
---|---|
Frequency(float[] _inputDat,
+ float _minVal,
+ float _maxVal,
+ float _binWidth)
+
++ Initialize the class by setting the minimum value, maximum value, and the bin width. |
+|
Frequency(int[] _inputDat,
+ int _minVal,
+ int _maxVal,
+ int _binWidth)
+
++ Initialize the class by setting the minimum value, maximum value, and the bin width. |
+
+Method Summary | +|
---|---|
+ float[] |
+compCumFrequency()
+
++ Computes the cumulative frequency of the data. |
+
+ float[] |
+compRelCumFrequency()
+
++ Computes the cumulative relative frequency of the data. |
+
+ float[] |
+compRelCumFrequencyPct()
+
++ Computes the cumulative frequency of the data as a percentage. |
+
+ float[] |
+compRelFrequency()
+
++ Computes the fraction of the total data that's in each bin. |
+
+ float[] |
+compRelFrequencyPct()
+
++ Computes the percents of the total data that's in each bin. |
+
+ float[] |
+getFrequency()
+
++ Returns the pre-computed frequency array. |
+
+ int |
+getLength()
+
++ returns the length of the dataset. |
+
+ int |
+getNumBins()
+
++ Returns the number of bins. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public Frequency(float[] _inputDat, + float _minVal, + float _maxVal, + float _binWidth)+
getFrequency()
function.
+ How is the number of bins computed?
+
Like this:
+ bins = floor[ (max - min) / binWidth ] +1
,
+ as opposed to ceil[ (max - min) / binWidth ]
.
+ Why? The latter algorithm is problematic when [ (max - min) / binWidth ]
+ is a whole number.
+ E.g. let min = 0.0, max = 100.0, and binWidth = 10.0. We get 10 bins with
+
What element goes into what bin?
+
Let value = data[i];
+ value
will go into bin number floor( (_inputDat[i]-_minVal)/_binWidth );
+
+
_inputDat
- the input data_minVal
- the minimum value in the array_maxVal
- the maximum value in the array_binWidth
- the width of each of the bins+public Frequency(int[] _inputDat, + int _minVal, + int _maxVal, + int _binWidth)+
getFrequency()
function.
+ See Frequency(float[] _inputDat, float _minVal, float _maxVal, float _binWidth)
for
+ more details.
+
+
+Method Detail | +
---|
+public float[] compRelFrequency()+
+
+public float[] compRelFrequencyPct()+
+
+public float[] compCumFrequency()+
+
+public float[] compRelCumFrequency()+
compCumFrequency()
) by
+ the number of elements in the data array.
++
+public float[] compRelCumFrequencyPct()+
compCumFrequency()
) by
+ the number of elements in the data array x 100%.
++
+public float[] getFrequency()+
+
+public int getNumBins()+
+
+public int getLength()+
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Gamma +
public class Gamma
+Gamma and Beta functions. +
+ Implementation:
+ The code is mostly adapted from the CERN
+ Jet Java libraries, which in turn was adapted from the Cephes Mathematical Library. As far a
+ as I can tell, Stephen L. Moshier wrote the original C++ code for CEPHES (1989, moshier@na-net.ornl.gov)
+ and Wolfgang Hoschek (at CERN, hats off to you mate!) then adapted it for the
+ Java platform, making some significant changes along the way.
+
+
+ +
+
+Field Summary | +
---|
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Method Summary | +|
---|---|
+static float |
+beta(double a,
+ double b)
+
++ Returns the beta function of the arguments. |
+
+static double |
+gamma(double x)
+
++ Returns the Gamma function of the argument, Γ(x) |
+
+static double |
+incompleteBeta(double aa,
+ double bb,
+ double xx)
+
++ Returns the Incomplete Beta Function evaluated from zero to xx ; formerly named ibeta . |
+
+static double |
+incompleteBetaInverse(double aa,
+ double bb,
+ double yy0)
+
++ Returns the inverse of the incomplete Beta integral. |
+
+static double |
+incompleteGamma(double a,
+ double x)
+
++ Returns the Incomplete Gamma function; formerly named igamma . |
+
+static double |
+incompleteGammaComplement(double a,
+ double x)
+
++ Returns the Complemented Incomplete Gamma function; formerly named igamc. |
+
+static double |
+logGamma(double x)
+
++ Returns the natural logarithm of the gamma function; formerly named lgamma . |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float beta(double a, + double b) + throws ArithmeticException+
+ + beta( a, b ) = Γ(a) * Γ(b) ) Γ(a+b) + ++
+
ArithmeticException
+public static double gamma(double x) + throws ArithmeticException+
Γ(x)
++
ArithmeticException
+public static double incompleteBeta(double aa, + double bb, + double xx) + throws ArithmeticException+
xx
; formerly named ibeta
.
++
aa
- the alpha parameter of the beta distribution.bb
- the beta parameter of the beta distribution.xx
- the integration end point.
+ArithmeticException
+public static double incompleteBetaInverse(double aa, + double bb, + double yy0)+
y
, the function finds x
such that
+ + incompleteBeta( a, b, x ) = y. ++ The routine performs interval halving until
incompleteBeta(a,b,x) - y = 0
,
+ to within roughly 10^-13
of the true solution. If this precision is not reached, the
+ method returns the current approximation of x, and prints a warning statement
+ specifying the current error. Typically, this is on the order of 10^-12.
++
aa
- the alpha parameter of the beta distribution.bb
- the beta parameter of the beta distribution.yy0
- the value for which to solve for the corresponding x
in the
+ incomplete Beta integral.
+x
such that incompleteBeta( a, b, x ) = y
.+public static double incompleteGamma(double a, + double x) + throws ArithmeticException+
igamma
.
++
a
- the parameter of the gamma distribution.x
- the integration end point.
+ArithmeticException
+public static double incompleteGammaComplement(double a, + double x) + throws ArithmeticException+
+
a
- the parameter of the gamma distribution.x
- the integration start point.
+ArithmeticException
+public static double logGamma(double x) + throws ArithmeticException+
lgamma
.
++
ArithmeticException
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.LU +
public class LU
+LU Decomposition. +
+For an m x n
matrix A
with m > n
, the LU decomposition is an m x n
+unit lower triangular matrix L
, an n x n
upper triangular matrix U
,
+and a permutation vector piv
of length m
so that A(piv,:) = L*U
;
+If m < n
, then L
is m x m
and U
is m x n
.
+
+The LU decomposition with pivoting always exists, even if the matrix is
+singular, so the constructor will never fail. The primary use of the
+LU decomposition is in the solution of square systems of simultaneous
+linear equations. This will fail if isNonsingular()
returns false.
+
+Shamelessly copied (and modified) from the
+JAMA Java
+Matrix package. To make things compatible with how most users use Processing, the
+class take in float matrices. However, to preserve the acccuracy of the computations, the algorithm
+first casts the input into a double array, prior to doing anything. All methods also return doubles; Use
+Cast.doubleToFloat(double[][])
if you want/need to cast everything back to floats for
+further (non-high-accuracy-dependant) processing (pun intended).
+
+ +
+
+Constructor Summary | +|
---|---|
LU(float[][] A)
+
++ Constructor. |
+
+Method Summary | +|
---|---|
+ double |
+det()
+
++ Determinant |
+
+ float[] |
+getFloatPivot()
+
++ Return pivot permutation vector as a one-dimensional float array |
+
+ double[][] |
+getL()
+
++ Return lower triangular factor, L . |
+
+ int[] |
+getPivot()
+
++ Return pivot permutation vector |
+
+ double[][] |
+getU()
+
++ Return upper triangular factor, U |
+
+ boolean |
+isNonsingular()
+
++ Is the matrix nonsingular? |
+
+ double[][] |
+solve(float[][] B)
+
++ Solve A*X = B |
+
+ double[] |
+solve(float[] b,
+ boolean returnZeroesForSingularCase)
+
++ Solve A*x = b for x |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public LU(float[][] A)+
+
getL()
,
+getU()
,
+getPivot()
,
+getFloatPivot()
,
+det()
+Method Detail | +
---|
+public boolean isNonsingular()+
+
+public double[][] getL()+
L
.
++
+public double[][] getU()+
U
++
+public int[] getPivot()+
+
+public float[] getFloatPivot()+
+
+public double det()+
+
IllegalArgumentException
- Matrix must be square+public double[] solve(float[] b, + boolean returnZeroesForSingularCase)+
+
b
- An array with as many elements as the number of rows in A.
+IllegalArgumentException
- Matrix row dimensions must agree.
+RuntimeException
- Matrix is singular.+public double[][] solve(float[][] B)+
+
B
- A Matrix with as many rows as A and any number of columns.
+IllegalArgumentException
- Matrix row dimensions must agree.
+RuntimeException
- Matrix is singular.
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Linear.BoxCox +
public static class Linear.BoxCox
+Contains methods related to the Box-Cox transformation of a data set; useful in
+ determining the best transformation that will yield the best method for converting
+ a monotonic, non-linear relationship between x
and y
into
+ a linear one.
+
Reference:
+
Box, George E. P.; Cox, D. R. (1964). "An analysis of transformations".
+ Journal of the Royal Statistical Society, Series B 26 (2): 211?252.
+
+ NIST/SEMATECH e-Handbook of Statistical Methods, EDA Section 1.3.3.6: Box-Cox Linearity Plots
+ (The reader's digest version of the original.)
+
+ +
+
+Method Summary | +|
---|---|
+static float[] |
+correlation(float[] x,
+ float[] y,
+ float[] lambda)
+
++ Performs the box-cox transformation on y for a sequence of λ
+ and returns the array of linear correlation coefficients
+ between the x and the box-cox transformed y . |
+
+static float[] |
+transform(float[] data,
+ float lambda)
+
++ Performs the box-cox transformation, returning the transformed data. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[] correlation(float[] x, + float[] y, + float[] lambda)+
y
for a sequence of λ
+ and returns the array of linear correlation coefficients
+ between the x
and the box-cox transformed y
.
+ at each value of λ .
+ The value of λ corresponding to the maximum correlation
+ (or minimum for negative correlation) is the optimal choice for
+ λ such that the relationship between x and T(y) is linear.
+ Reference:
+
+ NIST/SEMATECH e-Handbook of Statistical Methods, EDA Section 1.3.3.6: Box-Cox Linearity Plots
+
+
x
- the independent data arrayy
- the dependent data arraylambda
- an array of lambda values
++public static float[] transform(float[] data, + float lambda)+
+ y[i](λ) = (y[i]^λ-1) / λ if λ ≠ 0 + + y[i](λ) = log(y[i])if λ = 0 ++ Requirements: all elements of
y
are >0.
++
data
- the data to be transformedlambda
- the lambda value
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Linear.Significance +
public static class Linear.Significance
+Contains methods used to compute the significance, or pvalue of the input correlations. + The significance is computed using the normal of student-t approximations and hence are + not to be used for small datasets(i.e. size<20). +
+ +
+
+Constructor Summary | +|
---|---|
Linear.Significance()
+
++ |
+
+Method Summary | +|
---|---|
+static float |
+intercept(float intercept,
+ float interceptStdErr,
+ int df)
+
++ Returns the p-value, or significance, of the computed intercept under the null-hypothesis + of intercept = 0 (two-tailed test). |
+
+static float |
+slope(float slope,
+ float slopeStdErr,
+ int df)
+
++ Returns the p-value, or significance, of the computed slope under the null-hypothesis + of slope = 0 (two-tailed test). |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public Linear.Significance()+
+Method Detail | +
---|
+public static float slope(float slope, + float slopeStdErr, + int df)+
t = slope/slopeStdErr,+
+
slope
- the slope of the best fit linear lineslopeStdErr
- the standard error of the slopedf
- the degrees of freedom (typically (n-2), but could also be (n-1) if the
+ intercept was previously specified.+public static float intercept(float intercept, + float interceptStdErr, + int df)+
t=intercept/interceptStdErr+
+
intercept
- the intercept of the best fit linear lineinterceptStdErr
- the standard error of the interceptdf
- the degrees of freedom (n-2)
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Linear.StdErr +
public static class Linear.StdErr
+Contains methods related to computing the standard errors of the residuals, slope and intercept + associated with the best-fit linear line. +
+ +
+
+Method Summary | +|
---|---|
+static float |
+intercept(float[] x,
+ float residualStdErr)
+
++ Returns the standard error of the computed slope given x and the + standard error in the residuals. |
+
+static float |
+residual(float[] residuals,
+ int df)
+
++ Returns the standard error of the residuals given the degrees of freedom. |
+
+static float |
+slope(float[] x,
+ float residualStdErr)
+
++ Returns the standard error of the computed slope given x and the + standard error in the residuals. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float residual(float[] residuals, + int df)+
+ residualStdErr = Sqrt ( Sum( (delta[i] - mean(delta))^2 ) / df ) ++ where df is the degrees of freedom ( (n-1) if the y-intercept was pre-specified in the + best-fit line, (n-2) if neither slope nor intercept were specified.) +
+
residuals
- df
-
++public static float slope(float[] x, + float residualStdErr)+
+ slopeStdErr = residualStdErr / Sum ( (x[i] - mean(x))^2 ). ++
+
x
- the independent variableresidualStdErr
- the standard error of the residual
++public static float intercept(float[] x, + float residualStdErr)+
+ interceptStdErr = slopeStdErr / Sqrt( Sum(x[i]*x[i])/size ). ++ where the slopeStdErr is computed using the methods specified + in
slope(float[], float)
.
++
x
- the independent variableresidualStdErr
- the standard error of the residual
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Linear +
public class Linear
+Contains methods related to determining the linear linear + relationship between two datasets (of equal arrays) such as the + best-fit linear line parameters, box-cox transformations, etc. +
+ +
+
+Nested Class Summary | +|
---|---|
+static class |
+Linear.BoxCox
+
++ Contains methods related to the Box-Cox transformation of a data set; useful in + determining the best transformation that will yield the best method for converting + a monotonic, non-linear relationship between x and y into
+ a linear one. |
+
+static class |
+Linear.Significance
+
++ Contains methods used to compute the significance, or pvalue of the input correlations. |
+
+static class |
+Linear.StdErr
+
++ Contains methods related to computing the standard errors of the residuals, slope and intercept + associated with the best-fit linear line. |
+
+Field Summary | +
---|
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Constructor Summary | +|
---|---|
Linear()
+
++ |
+
+Method Summary | +|
---|---|
+static float[] |
+bestFit(float[] x,
+ float[] y)
+
++ Returns the slope and y-intercept of the best fit linear line + z = slope*x + intercept by minimizing the sum of least squares
+ between z and the y . |
+
+static float |
+bestFit(float[] x,
+ float[] y,
+ float intercept)
+
++ Returns the slope of the best fit linear line for the prescribed y-intercept. |
+
+static float[] |
+residuals(float[] x,
+ float[] y)
+
++ Compute and return the array of residuals given by + Delta_i = z_i - y_i ,
+ where
+ z_i = (slope*x_i + intercept) is the best fit linear line. |
+
+static float[] |
+residuals(float[] x,
+ float[] y,
+ float slope,
+ float intercept)
+
++ Compute and return the array of residuals given by + Delta_i = z_i - y_i ,
+ where
+ z_i = (slope*x_i + intercept) is the best fit linear line. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public Linear()+
+Method Detail | +
---|
+public static float[] bestFit(float[] x, + float[] y)+
z = slope*x + intercept
by minimizing the sum of least squares
+ between z
and the y
.
++
x
- the x datay
- the y data
+coeff[0]
corresponding to
+ the slope, and the second element coeff[1]
equal to the y-intercept.+public static float bestFit(float[] x, + float[] y, + float intercept)+
z = slope*x + intercept
, where the intercept is specified
+ by the user. E.g. intercept = 1
will result in z = slope*x+1
.
+ The slope
is computed by minimizing the sum of least squares
+ between z
and the y
.
++
x
- the x datay
- the y dataintercept
- the intercept of the best-fit line with the y-axis.
++public static float[] residuals(float[] x, + float[] y, + float slope, + float intercept)+
Delta_i = z_i - y_i
,
+ where
+ z_i = (slope*x_i + intercept)
is the best fit linear line.
++
slope
- the slope of the best-fit linear lineintercept
- the y-intercept of the best fit linear line+public static float[] residuals(float[] x, + float[] y)+
Delta_i = z_i - y_i
,
+ where
+ z_i = (slope*x_i + intercept)
is the best fit linear line.
+ You'd basically use this to compute the spread of a best fit line (max - min)
++
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.MDS +
public final class MDS
+Contains methods for performing but classical and non-classical multidimensional scaling. +
+ For an m-by-n input matrix with m observations and n variables, the output D is + the symmetric m-by-m matrix with zeros along the diagonals and element ij + specifying the distance between rows i and j. +
+ +
+
+Method Summary | +|
---|---|
+static float[][] |
+classical(float[][] D,
+ int p,
+ boolean showEigenvalues)
+
++ Performs classical (metric) multidimensional scaling, on an input matrix of Distances (computed + using e.g. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[][] classical(float[][] D, + int p, + boolean showEigenvalues)+
Distance
) and outputs the best-fitting p-dimensional configuration of points
+ (p<N
). The output matrix Y
has p columns with rows
+ giving the coordinates of the points chosen to represent the dissimilarities.
+ When D
corresponds to the Euclidian distance matrix, the p
+ dimensional coordinates correspond exactly to the first p
principal components.
+ + Remarks +
p > k
where k
is the number of
+ positive eigenvalues of Y*Y'
, then only the
+ first k columns of Y
are computed.
+
showEigenvalues
to true to print the
+ values of the "scalar product matrix" Y*Y'
to the screen.
+ (This is a good idea if you want to know how well the first p
dimensions
+ captures most of the variation in D
.+
D
- the input distance matrix (see Distance
)p
- the dimension (number of columns) of the output matrix.showEigenvalues
- whether to print the eigenvalues to the screen or otherwise.
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Mat +
public final class Mat
+Static class for performing some basic matrix operations. +
+ Guidelines
+
+ Methods that modify an array by a value always take the array as the first parameter, and
+ the value as the second.
+ For example,
+
divide(float[] array, float numberToDivideBy)
+ multiply(float[] array, float numberToMultiplyBy)
+ sum(float[] array, float numberToAdd)
+ swap(float[] array, int index1, int index2)
,
+ populate(float[] array, int[] indices)
).
+
+ Remarks
+
+ Almost all the methods here take in float arrays or matrices, although I've also added in a few
+ methods that operate on integers. In addition, to avoid accidental array manipulation,
+ all methods return a new array or matrix instead of modifying the original one. E.g.
+ C[i][j] = A[i][j]*B[i][j] return C
+
as opposed to
+ A[i][j] = A[i][j]*B[i][j], return A
+
in dotMultiply(float[][],float[][])
.
+ (If you find it performing to the contrary though, please let me know!)
+
+
+ Some non-trivial matrix-math related methods, including those for getting + the condition number, inverse, rank, and norms of a 2D input matrix are also included. + Making everything float-based instead of double-based naturally results in some precision loss but + the internal computations are done using doubles to minimize this. + +
+
+ +
+
Cast
,
+Find
,
+NaNs
,
+Rank
,
+Sorting
+Method Summary | +|
---|---|
+static float[] |
+abs(float[] array)
+
++ Return absolute values of an array |
+
+static int[] |
+abs(int[] array)
+
++ Return absolute values of an array |
+
+static float |
+ceilToNearest(float currentVal,
+ int interval)
+
++ Function for ceiling a number to the closest interval. |
+
+static float[] |
+column(float[][] inputMat,
+ int columnIndex)
+
++ Function to get one column of a 2D matrix |
+
+static float[][] |
+column(float[][] inputMat,
+ int indexStart,
+ int indexEnd)
+
++ Function to get columns of a 2-D matrix. |
+
+static int[] |
+column(int[][] inputMat,
+ int columnIndex)
+
++ |
+
+static float |
+compare(float a,
+ float b)
+
++ Returns -1 if a is less than b, or 1 if a is greater than b, or 0. |
+
+static int |
+compare(int a,
+ int b)
+
++ Returns -1 if a is less than b, or 1 if a is greater than b, or 0. |
+
+static float[] |
+concat(float[] a,
+ float[] b)
+
++ Concatenates two arrays a and b into a new array
+ c such that c = {a,b} . |
+
+static int[] |
+concat(int[] a,
+ int[] b)
+
++ |
+
+static float[] |
+constant(float constValue,
+ int size)
+
++ Returns an array with of length size with
+ each element equal to the specified constValue . |
+
+static int[] |
+constant(int constValue,
+ int size)
+
++ Returns an array with of length size with
+ each element equal to the specified constValue . |
+
+static float[][] |
+copy(float[][] data)
+
++ Make a deep copy of a matrix |
+
+static float[] |
+copyThenSort(float[] data)
+
++ Sorts and returns a copy of the input array in ascending order |
+
+static float[] |
+cross(float[] a,
+ float[] b)
+
++ vector cross product: c = a (cross) b |
+
+static float |
+det(float[][] data)
+
++ returns the determinant of the square input matrix. |
+
+static float[][] |
+divide(float[][] A,
+ float value)
+
++ Divides each element of the input matrix A by the input value |
+
+static float[] |
+divide(float[] data,
+ float value)
+
++ Divides all elements of input array by value . |
+
+static float[] |
+divide(float[] x,
+ float[] y)
+
++ Element by element division such that z[i] = x[i]/y[i]. |
+
+static float[][] |
+dotDivide(float[][] A,
+ float[][] B)
+
++ Element by element matrix division such that C_ij = A_ij/B_ij. |
+
+static float[][] |
+dotMultiply(float[][] A,
+ float[][] B)
+
++ Element by element matrix multiplication such that C_ij = A_ij*B_ij. |
+
+static float |
+dotProduct(float[] a,
+ float[] b)
+
++ dot product of two arrays such that z = sum(a_i*b_i) |
+
+static float |
+floorToNearest(float currentVal,
+ int interval)
+
++ Function for flooring a number to the closest interval. |
+
+static float[][] |
+identity(int dimension)
+
++ returns the identity matrix of the specified dimension |
+
+static float[][] |
+inverse(float[][] A)
+
++ Matrix inverse if A is square, pseudoinverse otherwise |
+
+static float[] |
+inverse(float[] data,
+ float replaceZeroWith)
+
++ Returns the array z[i] = 1/x[i]; If x[i] is zero, it sets z[i] + to the specified replaceZeroWith value instead. |
+
+static boolean |
+isConstant(float[] a)
+
++ |
+
+static float[] |
+linspace(float start,
+ float end,
+ float stepSize)
+
++ |
+
+static int[] |
+linspace(int start,
+ int end)
+
++ Returns the array of (end-start+1) points going from + start (inclusive) to end (inclusive):
+ y[0] = start; y[1] = start+1; ... |
+
+static int[] |
+linspace(int start,
+ int end,
+ int stepSize)
+
++ |
+
+static float[] |
+log(float[] data)
+
++ Returns an array with each element equal to the natural logarithm (the base-e logarithm) + the corresponding input array. |
+
+static float[] |
+log10(float[] data)
+
++ Returns an array with each element equal to the log 10 value (base 10 logarithm) + of the corresponding input array. |
+
+static float[] |
+logToBase(float[] data,
+ float base)
+
++ Returns an array with each element equal to the log A value + of the corresponding input array. |
+
+static float |
+mag(float[] data)
+
++ Returns the magnitude of the input array = sqrt(x1^2 + x2^2 + ... |
+
+static float[] |
+map(float[] data,
+ float from,
+ float to)
+
++ Similar to map([] value, valueArrayMinimum, valueArrayMaximum, from, to) |
+
+static float[] |
+map(float[] data,
+ float low1,
+ float high1,
+ float low2,
+ float high2)
+
++ Function for mapping an input array of floats for plotting + Extends processing "map" function to accomodate + arrays. |
+
+static float |
+map(float data,
+ float low1,
+ float high1,
+ float low2,
+ float high2)
+
++ Convenience function to map a variable from one coordinate space to another. |
+
+static float[][] |
+multiply(float[][] A,
+ float a)
+
++ Multiplies the matrix A with the scalar a returning a*A = B + where B_ij = a*A_ij. |
+
+static float[] |
+multiply(float[][] A,
+ float[] x)
+
++ Multiplies the matrix A with the vector x returning A*x = y + where y_j= Sum(k=j to n) A_ij*x_j. |
+
+static float[][] |
+multiply(float[][] A,
+ float[][] B)
+
++ Multiplies two matrices A and B returning C = A*B + where C_ij = Sum(k=1 to n) A_ik*B_kj. |
+
+static float[] |
+multiply(float[] data,
+ float number)
+
++ Multiplies each element of an array by a number, and returns the multiplied array. |
+
+static float[] |
+multiply(float[] data1,
+ float[] data2)
+
++ Multiplies two arrays and returns the multiplied array. |
+
+static float |
+norm1(float[][] data)
+
++ Returns the norm1 of the input matrix, equal to the + maximum absolute column sum of the matrix. |
+
+static float |
+norm2(float[] data)
+
++ Returns the norm2 or magnitude of an array. |
+
+static float |
+norm2(float[][] data)
+
++ Returns the norm2 (or maximum singular value) of the input matrix. |
+
+static float[] |
+normalizeToMinMax(float[] data)
+
++ Returns an array with elements consisting of the data + normalized to the specified min/max; new data goes from 0 to 1. |
+
+static float[] |
+normalizeToSum(float[] data)
+
++ Returns an array with elements given by z[i] = data[i] / sum(data); |
+
+static float |
+normF(float[][] data)
+
++ /** Returns the Frobenius norm of the input matrix; + sqrt of sum of squares of all elements. |
+
+static float |
+normInf(float[][] data)
+
++ Returns the Infinity norm of the input matrix, equal to the + maximum row sum. |
+
+static double[] |
+populate(double[] fullDataset,
+ int[] indices)
+
++ Returns an array with each element corresponding to fullDataset[indices[i]]. |
+
+static float[] |
+populate(float[] fullDataset,
+ int[] indices)
+
++ Returns an array with each element corresponding to fullDataset[indices[i]]. |
+
+static int[] |
+populate(int[] fullDataset,
+ int[] indices)
+
++ Returns an array with each element corresponding to fullDataset[indices[i]]. |
+
+static String[] |
+populate(String[] fullDataset,
+ int[] indices)
+
++ Returns an array with each element corresponding to fullDataset[indices[i]]. |
+
+static void |
+print(double[][] data,
+ int d)
+
++ Print the matrix to the screen with each row of the + matrix taking up one line. |
+
+static void |
+print(double[][] data,
+ String[] columnLabels,
+ String[] rowLabels,
+ int d)
+
++ Print the matrix to the screen with the columns and rows labeled according to the + input strings. |
+
+static void |
+print(double[] data,
+ int d)
+
++ Print the array to the screen in a single line. |
+
+static void |
+print(float[][] data,
+ int d)
+
++ Print the matrix to the screen with each row of the + matrix taking up one line. |
+
+static void |
+print(float[][] data,
+ String[] columnLabels,
+ String[] rowLabels,
+ int d)
+
++ Print the matrix to the screen with the columns and rows labeled according to the + input strings. |
+
+static void |
+print(float[] data,
+ int d)
+
++ Print the array to the screen in a single line. |
+
+static void |
+print(int[][] data,
+ int d)
+
++ Print the matrix to the screen with each row of the + matrix taking up one line. |
+
+static void |
+print(int[][] data,
+ String[] columnLabels,
+ String[] rowLabels,
+ int d)
+
++ Print the matrix to the screen with the columns and rows labeled according to the + input strings. |
+
+static void |
+print(int[] data,
+ int d)
+
++ Print the array to the screen in a single line. |
+
+static int |
+rank(float[][] data)
+
++ Returns the effective numerical rank (obtained from SVD) + of the input matrix. |
+
+static float[][] |
+replace(float[][] A,
+ float oldValue,
+ float newValue)
+
++ Returns a new matrix B equal to A, except that all elements in A which are equal to oldValue with
+ the newValue . |
+
+static float[] |
+replace(float[] data,
+ float oldValue,
+ float newValue)
+
++ Returns a new array data2 which is similar to data, except that all elements in data + which are equal to oldValue are replaced with
+ the newValue . |
+
+static float[][] |
+reshape(float[][] arr,
+ int numRows,
+ int numColumns)
+
++ Reshapes a matrix into the new specified dimensions. |
+
+static double[] |
+reverse(double[] data)
+
++ reverses the order of the elements in an array. |
+
+static float[] |
+reverse(float[] data)
+
++ reverses the order of the elements in an array. |
+
+static int[] |
+reverse(int[] data)
+
++ reverses the order of the elements in an array. |
+
+static float |
+roundToDecimalPlace(float number,
+ int decimal)
+
++ Function for rounding to a given decimal place. |
+
+static float[][] |
+skewSymmetric(float[][] data)
+
++ Returns the skew symmetric part of the input matrix ((A-A')/2). |
+
+static ArrayList[] |
+split(float[] sortedList,
+ float[] splitters)
+
++ Splits (partitions) an array into sublists such that each sublist contains the elements with a given range. |
+
+static float[][] |
+subMatrix(float[][] inputMat,
+ int[] rowIndices,
+ int[] columnIndices)
+
++ Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer). |
+
+static float[][] |
+subMatrix(float[][] inputMat,
+ int[] rowIndices,
+ int columnStart,
+ int columnEnd)
+
++ Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer). |
+
+static float[][] |
+subMatrix(float[][] inputMat,
+ int rowStart,
+ int rowEnd,
+ int[] columnIndices)
+
++ Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer). |
+
+static float[][] |
+subMatrix(float[][] inputMat,
+ int rowStart,
+ int rowEnd,
+ int columnStart,
+ int columnEnd)
+
++ Get a (2D) submatrix of a (2D) matrix (a copy, not a pointer). |
+
+static int[][] |
+subMatrix(int[][] inputMat,
+ int[] rowIndices,
+ int[] columnIndices)
+
++ |
+
+static float[][] |
+subtract(float[][] x,
+ float[][] y)
+
++ Returns the matrix z = x-y where each element of z[i][j] = x[i][j] - y[i][j] |
+
+static float[] |
+subtract(float[] x,
+ float[] y)
+
++ Returns the array z = x-y where each element of z[i] = x[i] - y[i] |
+
+static float |
+sum(float[] data)
+
++ Returns the sum of a data sequence. |
+
+static float[][] |
+sum(float[][] x,
+ float[][] y)
+
++ Returns the matrix z = x+y where each element of z[i][j] = x[i][j] + y[i][j] |
+
+static float[] |
+sum(float[] data,
+ float number)
+
++ Sum of an array with a number. |
+
+static float[] |
+sum(float[] data1,
+ float[] data2)
+
++ Returns the sum of two data sequences. |
+
+static int |
+sum(int[] data)
+
++ Returns the sum of a data sequence. |
+
+static int[] |
+sum(int[] data,
+ int number)
+
++ Sum of an array with a number. |
+
+static int[] |
+sum(int[] data1,
+ int[] data2)
+
++ Returns the sum of two data sequences. |
+
+static void |
+swap(float[] data,
+ int oldIndex,
+ int newIndex)
+
++ Swap function that swaps the values in an array |
+
+static void |
+swap(int[] data,
+ int oldIndex,
+ int newIndex)
+
++ Swap function that swaps the values in an array (overload function) |
+
+static float[][] |
+symmetric(float[][] data)
+
++ Returns the symmetric part of the input matrix ((A+A')/2). |
+
+static float |
+trace(float[][] data)
+
++ Returns the matrix trace or sum of the diagonal elements. |
+
+static float[][] |
+transpose(float[][] data)
+
++ Returns the transpose of the input matrix. |
+
+static int[][] |
+transpose(int[][] data)
+
++ |
+
+static float[] |
+within(float[] numbers,
+ float min,
+ float max)
+
++ returns an array containing all numbers within the min and max (inclusive). |
+
+static boolean |
+within(float number,
+ float min,
+ float max)
+
++ Checks to see if a number is in the range specified by [min,max]. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[] abs(float[] array)+
+
+public static int[] abs(int[] array)+
+
+public static float[][] column(float[][] inputMat, + int indexStart, + int indexEnd)+
+
inputMat
- inputMatrix.indexStart
- index of the start column (inclusive)indexEnd
- index of the end column (inclusive)
++public static float[] column(float[][] inputMat, + int columnIndex)+
+
inputMat
- input matrix.columnIndex
- index of the column to get
++public static int[] column(int[][] inputMat, + int columnIndex)+
+public static int compare(int a, + int b)+
+
+public static float compare(float a, + float b)+
+
+public static float[] concat(float[] a, + float[] b)+
a
and b
into a new array
+ c
such that c = {a,b}
.
++
+public static int[] concat(int[] a, + int[] b)+
+public static float[] constant(float constValue, + int size)+
size
with
+ each element equal to the specified constValue
.
++
Arrays.fill
+public static int[] constant(int constValue, + int size)+
size
with
+ each element equal to the specified constValue
.
++
Arrays.fill
+public static float[][] copy(float[][] data)+
+
+public static float[] copyThenSort(float[] data)+
+
+public static float[] cross(float[] a, + float[] b)+
+
+public static float det(float[][] data)+
+
IllegalArgumentException
- if the matrix is not square.+public static float[] divide(float[] data, + float value)+
value
.
+ That is, z[i] = x[i] / value
.
++
IllegalArgumentException
- if value
is zero.+public static float[] divide(float[] x, + float[] y)+
+
+public static float[][] divide(float[][] A, + float value)+
+
+public static float[][] dotDivide(float[][] A, + float[][] B)+
+
+public static float[][] dotMultiply(float[][] A, + float[][] B)+
+
+public static float dotProduct(float[] a, + float[] b)+
+
+public static float[][] identity(int dimension)+
dimension
++
+public static float[] inverse(float[] data, + float replaceZeroWith)+
replaceZeroWith
value instead.
++
+public static float[][] inverse(float[][] A)+
+
+public static boolean isConstant(float[] a)+
+public static float[] log(float[] data)+
+
+public static float[] log10(float[] data)+
+
+public static float[] logToBase(float[] data, + float base)+
log_A(x) = log(x) / log(A)
where
+ log is the natural logarithm (base e), and A is the new log base.
+ This function expects the values greater than 0.0; No internal checks are done.
++
base
- the log base. E.g. to get log2, we would use logToBase(data,2).+public static int[] linspace(int start, + int end)+
start
(inclusive) to end
(inclusive):
+ y[0] = start; y[1] = start+1; ... , y[n] = end;
.
+ int[] ii = linspace(2,5);
+ produces the array ii = {2,3,4,5};
with length 4.
+
+ It is similar to this:
+
+ for(int i=0; i<=end-start+1; i++){
+
y[i] = i + start;
+
}
+
+
++
start
- the start index (inclusive)end
- the end index (inclusive);+public static int[] linspace(int start, + int end, + int stepSize)+
start
- the start index (inclusive)end
- the end index (inclusive);stepSize
- the stepsize to take
++public static float[] linspace(float start, + float end, + float stepSize)+
start
- the start index (inclusive)end
- the end index (inclusive);stepSize
- the stepsize to take
++public static float mag(float[] data)+
+
+public static float map(float data, + float low1, + float high1, + float low2, + float high2)+
map(value, low1, high1, low2, high2) = low2 + (value - low1) * ( (high2-low2) / (high1-low1) )
+
+ Similar to Processing's "map" function.
++
+public static float[] map(float[] data, + float from, + float to)+
+
+public static float[] map(float[] data, + float low1, + float high1, + float low2, + float high2)+
+
+public static float[][] multiply(float[][] A, + float[][] B)+
+
+public static float[] multiply(float[][] A, + float[] x)+
+
+public static float[][] multiply(float[][] A, + float a)+
+
+public static float[] multiply(float[] data1, + float[] data2)+
z[i] = x[i] * y[i]
.
++
+public static float[] multiply(float[] data, + float number)+
z[i] = k * x[i]
.
++
+public static float norm1(float[][] data)+
+
+public static float norm2(float[][] data)+
+
+public static float norm2(float[] data)+
mag(float[])
.
++
+public static float normF(float[][] data)+
+
+public static float normInf(float[][] data)+
+
+public static float[] normalizeToMinMax(float[] data)+
+
+public static float[] normalizeToSum(float[] data)+
+
+public static double[] populate(double[] fullDataset, + int[] indices)+
+ y[0] = fullDataset[ indices[0] ]; + y[1] = fullDataset[ indices[1] ]; + ... + ... + y[n-1] = fullDataset[ indices[n-1] ]; ++
+
indices
- the indices in the fullDataset you want to extract and store.fullDataset
- the full dataset.+public static float[] populate(float[] fullDataset, + int[] indices)+
+ y[0] = fullDataset[ indices[0] ]; + y[1] = fullDataset[ indices[1] ]; + ... + ... + y[n-1] = fullDataset[ indices[n-1] ]; ++
+
indices
- the indices in the fullDataset you want to extract and store.fullDataset
- the full dataset.+public static int[] populate(int[] fullDataset, + int[] indices)+
+ y[0] = fullDataset[ indices[0] ]; + y[1] = fullDataset[ indices[1] ]; + ... + ... + y[n-1] = fullDataset[ indices[n-1] ]; ++
+
indices
- the indices in the fullDataset you want to extract and store.fullDataset
- the full dataset.+public static String[] populate(String[] fullDataset, + int[] indices)+
+ y[0] = fullDataset[ indices[0] ]; + y[1] = fullDataset[ indices[1] ]; + ... + ... + y[n-1] = fullDataset[ indices[n-1] ]; ++
+
indices
- the indices in the fullDataset you want to extract and store.fullDataset
- the full dataset.+public static void print(double[] data, + int d)+
print
,
+ except that the entire array is printed on a single line on the screen and
+ you can format the number of decimal digits directly instead of using Processing's
+ nf
or one of the other variants of it.
++
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static void print(double[][] data, + int d)+
println
, except that
+ each row of the matrix is printed on a single line on the screen and
+ you can format the number of decimal digits directly instead of using Processing's
+ nf
or one of the other variants of it.
++
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static void print(double[][] data, + String[] columnLabels, + String[] rowLabels, + int d)+
+
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static void print(float[] data, + int d)+
print
,
+ except that the entire array is printed on a single line on the screen and
+ you can format the number of decimal digits directly instead of using Processing's
+ nf
or one of the other variants of it.
++
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static void print(float[][] data, + int d)+
println
, except that
+ each row of the matrix is printed on a single line on the screen and
+ you can format the number of decimal digits directly instead of using Processing's
+ nf
or one of the other variants of it.
++
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static void print(float[][] data, + String[] columnLabels, + String[] rowLabels, + int d)+
+
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static void print(int[] data, + int d)+
print
,
+ except that the entire array is printed on a single line on the screen and
+ you can format the number of decimal digits directly instead of using Processing's
+ nf
or one of the other variants of it.
++
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static void print(int[][] data, + int d)+
println
, except that
+ each row of the matrix is printed on a single line on the screen and
+ you can format the number of decimal digits directly instead of using Processing's
+ nf
or one of the other variants of it.
++
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static void print(int[][] data, + String[] columnLabels, + String[] rowLabels, + int d)+
+
data
- the matrix to be printed.d
- Number of digits after the decimal to display..+public static int rank(float[][] data)+
Rank
for ranking the elements of an input 1D array.
++
+public static float[][] replace(float[][] A, + float oldValue, + float newValue)+
oldValue
with
+ the newValue
. Useful, for example, in replacing zeros with
+ some other number (for dotDivide(float[][], float[][])
), or for wall-street related book-keeping...
++
+public static float[] replace(float[] data, + float oldValue, + float newValue)+
oldValue
are replaced with
+ the newValue
. Useful, for example, in replacing zeros with
+ some other number (for divide(float[], float[])
), or for wall-street related book-keeping...
++
+public static float[][] reshape(float[][] arr, + int numRows, + int numColumns)+
+
+public static double[] reverse(double[] data)+
+
+public static float[] reverse(float[] data)+
+
+public static int[] reverse(int[] data)+
+
+public static float roundToDecimalPlace(float number, + int decimal)+
+ Examples:
+
roundToDecimalPlace(1.234567, 2) --> 1.23;
+
roundToDecimalPlace(100, 2) --> 100;
+
+
number
- number to round.decimal
- decimal place to round to.
++public static float floorToNearest(float currentVal, + int interval)+
+ Examples:
+
floorToNearest(173.2,5) --> 170;
+
floorToNearest(28,10) --> 20;
+
+
currentVal
- number to floor.interval
- to floor to. E.g. in fives, twos, tens, etc.
++public static float ceilToNearest(float currentVal, + int interval)+
+ Examples:
+
ceilToNearest(173.2,5) --> 175;
+
ceilToNearest(28,10) --> 30;
+
+
currentVal
- number to ceil.interval
- to ceil to. E.g. in fives, twos, tens, etc.
++public static ArrayList[] split(float[] sortedList, + float[] splitters)+
splitters=(a,b,c,...,y,z)
defines the ranges [-inf,a), [a,b), [b,c), ..., [y,z), [z,inf]
.
+ Examples:
+
data = (1,2,3,4,5,8,8,8,10,11)
.
+ splitters=(2,8)
yields 3 bins: (1), (2,3,4,5) (8,8,8,10,11)
.
+ splitters=()
yields 1 bin: (1,2,3,4,5,8,8,8,10,11)
.
+ splitters=(-5)
yields 2 bins: (), (1,2,3,4,5,8,8,8,10,11)
.
+ splitters=(100)
yields 2 bins: (1,2,3,4,5,8,8,8,10,11), ()
.
+ +
sortedList
- the list to be partitioned (must be sorted ascending).splitters
- the points at which the list shall be partitioned (must be sorted ascending).
+length == splitters.length + 1
.
+ Each sublist is returned sorted ascending.+public static float[][] subMatrix(float[][] inputMat, + int rowStart, + int rowEnd, + int columnStart, + int columnEnd)+
+
inputMat
- input matrixrowStart
- row to start at (inclusive)rowEnd
- row to end at (inclusive)columnStart
- column to start at (inclusive)columnEnd
- column to end at (inclusive)
+A(rowStart:rowEnd, columnStart:columnEnd)
.
+IllegalArgumentException
- if the rowStart, rowEnd, columnStart, or columnEnd parameters
+ are not within the inputMatrix size bounds.+public static float[][] subMatrix(float[][] inputMat, + int[] rowIndices, + int columnStart, + int columnEnd)+
+
inputMat
- input matrixrowIndices
- the row indicescolumnStart
- column to start at (inclusive)columnEnd
- column to end at (inclusive)
+A(rowIndices(:), columnStart:columnEnd)
.
+IllegalArgumentException
- if the inputs are not within the inputMatrix size bounds.+public static float[][] subMatrix(float[][] inputMat, + int rowStart, + int rowEnd, + int[] columnIndices)+
+
inputMat
- input matrixrowStart
- row to start at (inclusive)rowEnd
- column to end at (inclusive)columnIndices
- the column indices
+A(rowIndices(:), columnStart:columnEnd)
.
+IllegalArgumentException
- if the inputs are not within the inputMatrix size bounds.+public static float[][] subMatrix(float[][] inputMat, + int[] rowIndices, + int[] columnIndices)+
+
inputMat
- input matrixrowIndices
- the row indicescolumnIndices
- the column indices
+A(rowIndices(:), columnStart:columnEnd)
.
+IllegalArgumentException
- if the inputs are not within the inputMatrix size bounds.+public static int[][] subMatrix(int[][] inputMat, + int[] rowIndices, + int[] columnIndices)+
+public static float[] subtract(float[] x, + float[] y)+
+
+public static float[][] subtract(float[][] x, + float[][] y)+
+
+public static float sum(float[] data)+
+
+public static float[] sum(float[] data1, + float[] data2)+
+
+public static float[] sum(float[] data, + float number)+
+
+public static int sum(int[] data)+
+
+public static int[] sum(int[] data1, + int[] data2)+
+
+public static int[] sum(int[] data, + int number)+
+
+public static float[][] sum(float[][] x, + float[][] y)+
+
+public static void swap(float[] data, + int oldIndex, + int newIndex)+
+
data
- is an array of floatsoldIndex
- newIndex
- e.g. data[newIndex,oldIndex] = data[oldIndex,newIndex]+public static void swap(int[] data, + int oldIndex, + int newIndex)+
+
data
- is an array of integersoldIndex
- newIndex
- e.g. data[newIndex,oldIndex] = data[oldIndex,newIndex]+public static float[][] symmetric(float[][] data)+
+
+public static float[][] skewSymmetric(float[][] data)+
+
+public static float trace(float[][] data)+
+
+public static float[][] transpose(float[][] data)+
+
+public static int[][] transpose(int[][] data)+
+public static boolean within(float number, + float min, + float max)+
+
min
- left-most limit (inclusive)max
- right-most limit (inclusive)number
- the value to check for.
++public static float[] within(float[] numbers, + float min, + float max)+
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.NaNs +
public final class NaNs
+
Contains various methods for dealing with NaNs in your data.
+ Currently only accommodates float and double arrays. Java's Integer.isNaN doesn't + seem to exist or I'd include that as well. ++ +
+
+Method Summary | +|
---|---|
+static boolean |
+containsNaNs(double[] data)
+
++ Checks for presence of NaNs in the data array. |
+
+static boolean |
+containsNaNs(float[] data)
+
++ Checks for presence of NaNs in the data array. |
+
+static double[] |
+eliminate(double[] data)
+
++ Eliminate the NaNs from the input array. |
+
+static float[] |
+eliminate(float[] data)
+
++ Eliminate the NaNs from the input array. |
+
+static ArrayList<Integer> |
+getNaNPositions(double[] data)
+
++ Returns an array list of indexes where data[i] is NaN. |
+
+static ArrayList<Integer> |
+getNaNPositions(float[] data)
+
++ Returns an array list of indexes where data[i] is NaN. |
+
+static double[] |
+replaceNewWith(double[] data,
+ double newNaN)
+
++ Returns a new array with NaN elements in the original data set replaced with the value + specified by newNaN. |
+
+static float[] |
+replaceNewWith(float[] data,
+ float newNaN)
+
++ Returns a new array with NaN elements in the original data set replaced with the value + specified by newNaN. |
+
+static void |
+replaceOriginalWith(double[] data,
+ double newNaN)
+
++ Modifies the original data array by replacing the NaN elements in the data set with the value + specified by newNaN. |
+
+static void |
+replaceOriginalWith(float[] data,
+ float newNaN)
+
++ Modifies the original data array by replacing the NaN elements in the data set with the value + specified by newNaN. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static double[] eliminate(double[] data)+
+
data
- the array containing NaNs
++public static double[] replaceNewWith(double[] data, + double newNaN)+
+
data
- the array containing NaNsnewNaN
- the value to replace the NaNs with.
++public static void replaceOriginalWith(double[] data, + double newNaN)+
+
data
- the array containing NaNsnewNaN
- the value to replace the NaNs with.+public static boolean containsNaNs(double[] data)+
+
data
- array to be searched for NaNs
++public static ArrayList<Integer> getNaNPositions(double[] data)+
data[i]
is NaN.
++
data
- array to search for NaNs
+data[i] = NaN
+public static float[] eliminate(float[] data)+
+
data
- the array containing NaNs
++public static float[] replaceNewWith(float[] data, + float newNaN)+
+
data
- the array containing NaNsnewNaN
- the value to replace the NaNs with.
++public static void replaceOriginalWith(float[] data, + float newNaN)+
+
data
- the array containing NaNsnewNaN
- the value to replace the NaNs with.+public static boolean containsNaNs(float[] data)+
+
data
- array to be searched for NaNs
++public static ArrayList<Integer> getNaNPositions(float[] data)+
data[i]
is NaN.
++
data
- array to search for NaNs
+data[i] = NaN
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Normality.Dago +
public static class Normality.Dago
+Methods for computing the skewnewss, kurtosis, and D'Agostino-Peasrson K^2 "omnibus" test-statistics (that + combine the former two), and accompanying significance (or p-values) + for testing the underlying population normality. + +
Implementation:
+
+
Let mk
denote the k
th moment,
+ mk = ∑_(i=1 to n)(x_i - mean(x))/n.
Then, we can define the following:
+
Sample estimate of the third standardized moment: b1 = m3/(m2)^(3/2)
+
Sample estimate of the fourth standardized moment: b2 = m4/m2
+
The test statistics Z(b1), Z(b2) are approximately normally distributed under
+ the null hypothesis of population normality. Both Z(b1) and Z(b2) can be used to
+ test one-sided and two-sided alternative hypothesis.
+
+
+ D'Agostino and Pearson (1973) also introduced the following "omnibus" test statistic,
+ K^2 = (Z(b1))^2 + (Z(b2))^2
,
+ which is able to detect deviations from normality due to either skewness or kurtosis
+ This K^2 statistic has an approximately Chi-Squared distribution with 2 degrees of
+ freedom when the population is normally distributed.
+ Similar to Z(b1) and Z(b2), K^2 can be used to test one-sided and two-sided alternative hypothesis.
+
+
Requirements
+
data set has to have more than 20 elements. An error is thrown otherwise since the normal
+ approximation is no longer valid (technically, the it is valid for Zb1, as long as n > 8, but
+ since we're computing everything, the requirement has been set a little higher.
+
+
Remarks: +
g1
+ and g2
used in SAS, and SPSS but they are related by a linear transformation (see
+ cited paper for details).
+ dagoTest
class in the fBasics library albeit with some implementation.
+
+ References:
+
D'Agostino, R., Albert Belanger, A., D'Agostino Jr, R., 1990
+ A Suggestion for Using Powerful and Informative Tests of Normality
+
+
D?Agostino, R., Pearson, E., 1973. Tests for departures from normality.
+ Empirical results for the distribution of b1 and b2.Biometrika 60, 613?622.
+
+ +
+
+Method Summary | +|
---|---|
+static float[] |
+chi2(float[] data)
+
++ Computes and returns an array containing the test statistic chi2 , or the
+ "omnibus" test statistic, and the significance, or "p-value" of this test statistic. |
+
+static float[] |
+kurtosis(float[] data)
+
++ Computes and returns an array containing the test statistic zb2 associated with
+ b2 and the significance, or "p-value" of the kurtosis test statistic zb2 , assuming a
+ two-tailed null hypothesis as well as . |
+
+static float[] |
+pValues(float[] data)
+
++ Returns an array containing the three significance, or p-values, for testing normality. |
+
+static float[] |
+skew(float[] data)
+
++ Computes and returns an array containing the test statistic zb1 associated with sqrt(b1) and
+ the significance, or "p-value" of the skew test statistic zb1 , assuming a
+ two-tailed null hypothesis as well as . |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[] skew(float[] data)+
zb1
associated with sqrt(b1) and
+ the significance, or "p-value" of the skew test statistic zb1
, assuming a
+ two-tailed null hypothesis as well as . The first element of the output array is the
+ test statistic zb1
associated with sqrt(b1) = m3/m2^(3/2) where mk is
+ the kth momement as specified in Descriptive.moment(float[], int, float)
and the second element is
+ the p-value for the two-tailed null hypothesis with
+ + pValueSkew = 2* ( 1-Probability.normcdf( zb1 ) ). ++ (D'Agostino et al. (1990)) . +
+
+public static float[] kurtosis(float[] data)+
zb2
associated with
+ b2 and the significance, or "p-value" of the kurtosis test statistic zb2
, assuming a
+ two-tailed null hypothesis as well as . The first element of the output array is the
+ test statistic zb2
associated with b2 = m4/m2^2 where mk is
+ the kth momement as specified in Descriptive.moment(float[], int, float)
. and the second element is
+ the p-value for the two-tailed null hypothesis with
+ + pValueKurtosis = 2* ( 1-Probability.normcdf( zb2 ) ). ++ (D'Agostino et al. (1990)) . +
+
+public static float[] chi2(float[] data)+
chi2
, or the
+ "omnibus" test statistic, and the significance, or "p-value" of this test statistic.
+ The first element of the output array is the test statistic the ombibus test
+ statistic chi2
and the second element is
+ the p-value:
+ + pValueChiSquared = ( 1-Probability.chi2cdf( chi2 ) ). ++ (D'Agostino et al. (1990)) . +
+
+public static float[] pValues(float[] data)+
chi2(float[])
), the second corresponds to D'Agostino's
+ skew test (skew(float[])
), and the
+ third corresponds to D'Agostino's kurtosis test (kurtosis(float[])
).
++
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Normality +
public class Normality
+Contains various utilities for checking if the dataset comes from a normal distribution. +
+ +
+
+Nested Class Summary | +|
---|---|
+static class |
+Normality.Dago
+
++ Methods for computing the skewnewss, kurtosis, and D'Agostino-Peasrson K^2 "omnibus" test-statistics (that + combine the former two), and accompanying significance (or p-values) + for testing the underlying population normality. |
+
+Field Summary | +
---|
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Method Summary | +|
---|---|
+static float[] |
+normalProbability(float[] data)
+
++ Return the Normal order statistic medians ( N ) necessary to produce a Q-Q plot
+ for a normal distribution (or normal probability plot). |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[] normalProbability(float[] data)+
N
) necessary to produce a Q-Q plot
+ for a normal distribution (or normal probability plot).
+ That is, given a data array, this method computes the Normal order statistic medians
+ and the ordered response values or z
-scores of the input.
+ A plot of the N
vs z
should form an approximate straight line if the
+ data is normally distributed;
+ departures from this straight line indicate departures from normality with the shape of the line
+ providing clues as to the distribution of the data.
+
+ The normal order statistic medians are computed as
+ N[i] = Probability.norminv(U[i])
+ where U[i]
are the uniform order statistic medians, given by
+
Reference: + NIST/SEMATECH e-Handbook of Statistical Methods, EDA Section 1.3.3.21: Normal Probability Plots +
+
data
- the data array
+Descriptive.zScore(float[],float,float)
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.OneWayAnova +
public class OneWayAnova
+Computes the one-way ANOVA p-value to test the equality of two or more sample
+ means by analyzing the sample variances using the test statistic
+ F = variance between samples / variance within samples
.
+ Once initialized, the following values are computed and stored:
+
dfbg()
).
+ ssbg()
).
+ dfwg()
).
+ sswg()
).
+ F()
).
+ pValue()
).
+ Preconditions:
Collection
must contain
+ float[]
arrays.float[]
arrays in the
+ categoryData
collection and each of these arrays must
+ contain at least two values.This implementation is based on a description provided in Chapter 13 of the VassarStats
+ online textbook
+ Mean values are not computed directly in the algorithm as this tends to cause round-off error.
+ Rather, we use the more stable SumOfSquares - Sum^2
:
+
+ Sum_(i=1 to n) (x_i - mean(x) )^2 = Sum_(i=1 to n)(x_i)^2 - (Sum_(i=1 to n) x_i)^2/n. ++ +
+ +
+
+Constructor Summary | +|
---|---|
OneWayAnova(Collection<float[]> categoryData)
+
++ Performs an Analysis of Variance (ANOVA) for a collection of float arrays. |
+
+Method Summary | +|
---|---|
+ int |
+dfbg()
+
++ Returns the degrees of freedom between groups. |
+
+ int |
+dfwg()
+
++ Returns the degrees of freedom within each group. |
+
+ float |
+etasqrd()
+
++ Computes eta^2: a measure of the strength of the curvilinear relationship + between the independent and dependent variable. |
+
+ float |
+F()
+
++ Returns the test statistic F. |
+
+ float |
+pValue()
+
++ Returns the significance, or "p-value" of the test statistic F() . |
+
+ float |
+ssbg()
+
++ Returns the sum of squared deviates between each group. |
+
+ float |
+sswg()
+
++ Returns the sum of squared deviates of the data within each group. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public OneWayAnova(Collection<float[]> categoryData)+
+
categoryData
- the data coresponding to the different groupsCollection
+Method Detail | +
---|
+public float etasqrd()+
+
+public int dfbg()+
+
+public int dfwg()+
+
+public float F()+
+ F = (ssbg/dfbg) / (sswg/dfwg); ++
+
+public float pValue()+
F()
.
+ That is,
+ + p = 1 - Probability.fcdf(F,dfbg,dfwg); ++
+
+public float sswg()+
+ sswg = Sum_(i=1 to n1) (xi1-mean(x1))^2 + Sum_(i=1 to n2) (xi2-mean(xk))^2 + ... + Sum_(i=1 to nk) (xik-mean(xk))^2; ++
+
+public float ssbg()+
+ ssbg = Sum_(i=1 to k) (y_i)-mean(y)) ++
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
public interface PapayaConstants
+PapayaConstants stores some of the constants used in plotting. + I try to follow processing's conventions where possible + as specified in processing.core.PConstants + The Processing Javadoc has more details. +
+ +
+
+Field Summary | +|
---|---|
+static int |
+BASELINE
+
++ |
+
+static double |
+big
+
++ |
+
+static double |
+biginv
+
++ |
+
+static int |
+BOTTOM
+
++ |
+
+static int |
+CENTER
+
++ |
+
+static int |
+CORNER
+
++ |
+
+static String |
+FONTNAME
+
++ Default font used. |
+
+static int[] |
+GRAY
+
++ |
+
+static int |
+INDEX_NOT_FOUND
+
++ Index to return if a given value/object is not found in the Cast function. |
+
+static int[] |
+INDICES_NOT_FOUND
+
++ Index array to return if a given value/object is not found in the Cast function. |
+
+static int |
+LEFT
+
++ |
+
+static double |
+LOGPI
+
++ |
+
+static double |
+MACHEP
+
++ machine constants |
+
+static double |
+MAXGAM
+
++ |
+
+static double |
+MAXLOG
+
++ |
+
+static double |
+MINLOG
+
++ |
+
+static int |
+RIGHT
+
++ |
+
+static double |
+SQRTH
+
++ |
+
+static double |
+SQTPI
+
++ |
+
+static float[] |
+STROKEWEIGHT
+
++ |
+
+static int[] |
+TEXTSIZE
+
++ |
+
+static int |
+TOP
+
++ |
+
+Field Detail | +
---|
+static final int BASELINE+
+static final int CORNER+
+static final int BOTTOM+
+static final int CENTER+
+static final int LEFT+
+static final int TOP+
+static final int RIGHT+
+static final String FONTNAME+
+
+static final float[] STROKEWEIGHT+
+static final int[] GRAY+
+static final int[] TEXTSIZE+
+static final int INDEX_NOT_FOUND+
Cast
function.
++
+static final int[] INDICES_NOT_FOUND+
Cast
function.
++
+static final double MACHEP+
+
+static final double MAXLOG+
+static final double MINLOG+
+static final double MAXGAM+
+static final double SQTPI+
+static final double SQRTH+
+static final double LOGPI+
+static final double big+
+static final double biginv+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Polynomial +
public final class Polynomial
+Static Class for casting one variable type to another. +
+ +
+
+Method Summary | +|
---|---|
+static double[] |
+polyval(double[] x,
+ double[] coeff)
+
++ y = polyval(x,coeff) returns the array values of a polynomial of degree n evaluated at + each element of x. |
+
+static double |
+polyval(double x,
+ double[] coeff)
+
++ y = polyval(x,coeff) returns the value of a polynomial of degree n evaluated at x. |
+
+static float[] |
+polyval(float[] x,
+ float[] coeff)
+
++ y = polyval(x,coeff) returns the value of a polynomial of degree n evaluated at x. |
+
+static float |
+polyval(float x,
+ float[] coeff)
+
++ y = polyval(x,coeff) returns the value of a polynomial of degree n evaluated at x. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static double polyval(double x, + double[] coeff) + throws ArithmeticException+
+ y = coeff[0]*x^n + coeff[1]*x^(n-1) + .... + coeff[n-1]*x + coeff[n]. +
++ E.g. for a quadratic function, y = coeff[0]*x^2 + coeff[1]*x + coeff[2]. +
++
x
- argument to the polynomial.coeff
- the coefficients of the polynomial.
+ArithmeticException
+public static double[] polyval(double[] x, + double[] coeff) + throws ArithmeticException+
+ y = coeff[0]*x^n + coeff[1]*x^(n-1) + .... + coeff[n-1]*x + coeff[n]. +
++ E.g. for a quadratic function, y = coeff[0]*x^2 + coeff[1]*x + coeff[2]. +
++
x
- array argument to the polynomial.coeff
- the coefficients of the polynomial.
+ArithmeticException
+public static float polyval(float x, + float[] coeff) + throws ArithmeticException+
+ y = coeff[0]*x^n + coeff[1]*x^(n-1) + .... + coeff[n-1]*x + coeff[n]. +
++ E.g. for a quadratic function, y = coeff[0]*x^2 + coeff[1]*x + coeff[2]. +
++
x
- argument to the polynomial.coeff
- the coefficients of the polynomial.
+ArithmeticException
+public static float[] polyval(float[] x, + float[] coeff) + throws ArithmeticException+
+ y = coeff[0]*x^n + coeff[1]*x^(n-1) + .... + coeff[n-1]*x + coeff[n]. +
++ E.g. for a quadratic function, y = coeff[0]*x^2 + coeff[1]*x + coeff[2]. +
++
x
- array argument to the polynomial.coeff
- the coefficients of the polynomial.
+ArithmeticException
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Probability +
public class Probability
+Cumulative distribution functions and corresponding inverses of certain probability distributions. +
+ Description:
+ ...cdf
signifies the cumulative distribution function and is similar to
+ the q...
functions in R
and the ...cdf
functions in
+ MATLAB
. The cdf represent the integral, from -infinity to 0
of the
+ corresponding continuous probability density function (pdf), or, the sum from 0
to x
+ of an associated finite pdf.
+
+ ...inv
signifies the inverse of the cumulative distribution function and is similar to
+ the p...
functions in R
and the ...inv
functions in
+ MATLAB
.
+
+
Implementation: +
normcdf(1.96) = 0.975
--> 97.5% of the curve is within +/-1.96 of 0.
+ norminv(0.975) = 1.96
--> finds the point x (=1.96) such that normcdf(x) = .975
.
+ norminv( normcdf(x) ) = x
+ alpha = 0.05
and H_0: μ=0
+ --> p = 1-alpha/2 = 0.975
+ --> Reject null hypothesis μ=0
if
+ the test statistic is greater or less than than +/-1.96 (= +/- norminv(p) )
.
+
+ alpha = 0.05
and H_0: μ < 0
+ --> p = 1-alpha = 0.95
+ --> Reject null hypothesis μ < 0
if
+ the test statistic is greater than 1.645 (= norminv(p) )
.
+ alpha = 0.05
and H_0: μ > 0
+ --> p = 1-alpha = 0.95
+ --> Reject null hypothesis μ > 0
if
+ the test statistic is less than -1.645 (= -norminv(p) )
.
+ + +
+
+Field Summary | +
---|
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Method Summary | +|
---|---|
+static double |
+betacdf(double x,
+ double a,
+ double b)
+
++ Returns the area from zero to x under the beta density
+ function. |
+
+static double |
+betacdfComplemented(double x,
+ double a,
+ double b)
+
++ Returns the area under the right hand tail (from x to
+ infinity) of the beta density function. |
+
+static double |
+betainv(double p,
+ double a,
+ double b)
+
++ Returns the inverse of the beta cumulative distribution function. |
+
+static double |
+binomcdf(double p,
+ int k,
+ int n)
+
++ Returns the sum of the terms 0 through k of the Binomial
+ probability density. |
+
+static double |
+binomcdfComplemented(double p,
+ int k,
+ int n)
+
++ Returns the sum of the terms k+1 through n of the Binomial
+ probability density. |
+
+static double |
+binominv(double y,
+ int k,
+ int n)
+
++ Finds the event probability p such that the sum of the + terms 0 through k of the Binomial probability density + is equal to the given cumulative probability y. |
+
+static double |
+chi2cdf(double x,
+ double v)
+
++ Returns the area under the left hand tail (from 0 to x )
+ of the Chi square probability density function with
+ v degrees of freedom. |
+
+static double |
+chi2cdfComplemented(double x,
+ double v)
+
++ Returns the area under the right hand tail (from x to
+ infinity) of the Chi square probability density function
+ with v degrees of freedom. |
+
+static double |
+erf(double x)
+
++ Returns the error function of the normal distribution. |
+
+static double |
+erfComplemented(double a)
+
++ Returns the complementary Error function of the normal distribution; formerly named erfc . |
+
+static double |
+fcdf(double x,
+ int df1,
+ int df2)
+
++ Returns the area from zero to x under the F density
+ function (also known as Snedcor's density or the
+ variance ratio density); formerly named fdtr . |
+
+static double |
+fcdfComplemented(double x,
+ int df1,
+ int df2)
+
++ Returns the area from x to infinity under the F density + function (also known as Snedcor's density or the + variance ratio density); formerly named fdtrc . |
+
+static double |
+finv(double p,
+ int df1,
+ int df2)
+
++ Finds the F density argument x such that the integral + from 0 to x of the F density is equal to p; formerly named fdtri . |
+
+static double |
+gammacdf(double x,
+ double a,
+ double b)
+
++ Returns the integral from zero to x of the gamma probability
+ density function. |
+
+static double |
+gammacdfComplemented(double x,
+ double a,
+ double b)
+
++ Returns the integral from x to infinity of the gamma
+ probability density function: |
+
+static double |
+nbinomcdf(double p,
+ int k,
+ int n)
+
++ Returns the sum of the terms 0 through k of the Negative Binomial Distribution. |
+
+static double |
+nbinomcdfComplemented(double p,
+ int k,
+ int n)
+
++ Returns the sum of the terms k+1 to infinity of the Negative
+ Binomial distribution. |
+
+static double |
+normcdf(double a)
+
++ Returns the area under the Normal (Gaussian) probability density + function, integrated from minus infinity to x (assumes mean is zero, variance is one). |
+
+static double |
+normcdf(double x,
+ double mean,
+ double variance)
+
++ Returns the area under the Normal (Gaussian) probability density + function, integrated from minus infinity to x when the
+ data has not been standardized (i.e. |
+
+static double |
+norminv(double y0)
+
++ Returns the value, x , for which the area under the
+ Normal (Gaussian) probability density function (integrated from
+ minus infinity to x ) is equal to the argument y
+ (assumes mean is zero, variance is one); formerly named ndtri . |
+
+static double |
+poissoncdf(double mean,
+ int k)
+
++ Returns the sum of the first k terms of the Poisson distribution. |
+
+static double |
+poissoncdfComplemented(int k,
+ double mean)
+
++ Returns the sum of the terms k+1 to Infinity of the Poisson distribution. |
+
+static double |
+tcdf(double t,
+ double k)
+
++ Returns the integral from minus infinity to t of the Student-t
+ distribution with k > 0 degrees of freedom. |
+
+static double |
+tinv(double p,
+ double k)
+
++ Returns the value, t , for which the area under the
+ Student-t probability density function (integrated from
+ minus infinity to t ) is equal to p. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static double betacdf(double x, + double a, + double b)+
x
under the beta density
+ function.
+ + + P(x) = Γ(a+b) / [ Γ(a) * Γ(b) ] * Integral_(0 to x) [ t^(a-1) * (1-t)^(b-1) ] dt + ++ This function is identical to the incomplete beta + integral function + +
Gamma.incompleteBeta(a, b, x)
.
+
+ The complemented function is betacdfComplemented
, specified by
+
+ 1 - P(1-x) = Gamma.incompleteBeta( b, a, x )
;
++
+public static double betacdfComplemented(double x, + double a, + double b)+
x
to
+ infinity) of the beta density function.
+
+ This function is identical to the incomplete beta
+ integral function Gamma.incompleteBeta(b, a, x)
.
++
+public static double betainv(double p, + double a, + double b)+
p
, the function finds x
such that
+ + Gamma.incompleteBeta( a, b, x ) = beta(x, a, b) = p; ++ + This function is identical to the incomplete beta inverse + function
Gamma.incompleteBetaInverse(aa, bb, yy0)
.
++
a
- the alpha parameter of the beta distribution.b
- the beta parameter of the beta distribution.p
- the value for which to solve for the corresponding x
in the
+ incomplete Beta integral.
+x
such that beta( a, b, x ) = p
.+public static double binomcdf(double p, + int k, + int n)+
0
through k
of the Binomial
+ probability density.
+ + + Sum_(j=0 to k) [ (n!)/(j!*(n-j)! ] * [ p^j * (1-p)^(n-j) ] + ++ The terms are not summed directly; instead the incomplete + beta integral is employed, according to the formula +
+ y = binomcdf( p, k, n) = Gamma.incompleteBeta( n-k, k+1, 1-p )
.
+
+ All arguments must be positive, +
+
k
- end term.n
- the number of trials.p
- the probability of success (must be in (0.0,1.0)
).+public static double binomcdfComplemented(double p, + int k, + int n)+
k+1
through n
of the Binomial
+ probability density.
+ + + Sum_(j=k+1 to n) [ (n!)/(j!*(n-j)! ] * [ p^j * (1-p)^(n-j) ] + ++ The terms are not summed directly; instead the incomplete + beta integral is employed, according to the formula +
+ y = binomcdfComplemented( p, k, n ) = Gamma.incompleteBeta( k+1, n-k, p )
.
+
+ All arguments must be positive, +
+
k
- (start-1) term.n
- the number of trials.p
- the probability of success (must be in (0.0,1.0)
).+public static double binominv(double y, + int k, + int n)+
+ binomcdf( p, k, n ) = y; ++ This is accomplished using the inverse beta integral + function and the relation +
+ 1 - p = Gamma.incompleteBetaInverse( n-k, k+1, y ). +
+ All arguments must be positive. ++
k
- end term.n
- the number of trials.y
- the value to solve for such that binomcdf( p, k, n ) = y
.+public static double chi2cdf(double x, + double v) + throws ArithmeticException+
x
)
+ of the Chi square probability density function with
+ v
degrees of freedom.
+ + + P( x | v ) = 1/ [ 2^(v/2) * Γ(v/2) ] * Integral_(from 0 to x) [ t^(v/2-1) * e^(-t/2) ] dt + ++ where
x
is the Chi-square variable.
+ + The incomplete gamma integral is used, according to the + formula +
+ y = chi2cdf( x, v ) = Gamma.incompleteGamma( v/2.0, x/2.0 )
.
+
+ The arguments must both be positive. +
+
v
- degrees of freedom.x
- integration end point.
+ArithmeticException
+public static double chi2cdfComplemented(double x, + double v) + throws ArithmeticException+
x
to
+ infinity) of the Chi square probability density function
+ with v
degrees of freedom.
+ + + P( x | v ) = 1/ [ 2^(v/2) * Γ(v/2) ] * Integral_(from x to infinity) [ t^(v/2-1) * e^(-t/2) ] dt + ++ where
x
is the Chi-square variable.
+
+ The incomplete gamma integral is used, according to the
+ formula
+
+ y = chi2cdfComplemented( x, v ) = incompleteGammaComplement( v/2.0, x/2.0 )
.
+
+ The arguments must both be positive.
++
v
- degrees of freedom.x
- integration start point.
+ArithmeticException
+public static double erf(double x) + throws ArithmeticException+
+ + erf(x) = 2/sqrt(PI) * Integral_(0 to x) [ e^(-t^2) ] dt + ++ Implementation: + For
0 <= |x| < 1, erf(x) = x * P4(x^2)/Q5(x^2)
; otherwise
+ erf(x) = 1 - erfc(x)
.
+ +
+
x
- the integral end point.
+ArithmeticException
+public static double erfComplemented(double a) + throws ArithmeticException+
erfc
.
+ + + erfc(x) = 2/sqrt(PI) * Integral_(x to inf) [ e^(-t^2) ] dt + = 1 - 2/sqrt(PI) * Integral_(0 to x) [ e^(-t^2) ] dt + = 1 - erf(x). + ++ Implementation: + For small x,
erfc(x) = 1 - erf(x)
; otherwise rational
+ approximations are computed.
+ +
+
a
- corresponds to x
above. An internal check is performed to see if
+ a
is < 0 or otherwise. If a < 0 --> x = -a.
Else, x = a
.
+ArithmeticException
+public static double fcdf(double x, + int df1, + int df2)+
x
under the F density
+ function (also known as Snedcor's density or the
+ variance ratio density); formerly named fdtr
.
+ Corresponds to the left hand tail of the F-distribution.
+
+ The incomplete beta integral is used, according to the formula
+ + + P(x) = Gamma.incompleteBeta( df1/2, df2/2, (df1*x/(df2 + df1*x) ). + ++
+
df1
- ( greater than or equal to 1 )df2
- ( greater than or equal to 1 )x
- the integration end point (greater than or equal to 0).+public static double fcdfComplemented(double x, + int df1, + int df2)+
fdtrc
.
+ Corresponds to the right hand tail of the F-distribution ( and
+ to the values presented in F-distribution tables).
+
+ The incomplete beta integral is used, according to the
+ relation
++ + P(x) = Gamma.incompleteBeta( df2/2, df1/2, (df2/(df2 + df1*x) ) + ++
+
df1
- df1 ( greater than or equal to 1 )df2
- df2 ( greater than or equal to 1 )x
- the left integration point. Corresponds to the F value in look-up tables.
+x
to inf. under the F density function.+public static double finv(double p, + int df1, + int df2)+
x
of the F density is equal to p; formerly named fdtri
.
+ That is, it solves for the value of x
, given p
such that
+ + fcdf(x,df1,df2) - p = 0. ++ + This is accomplished using the inverse beta integral + function and the relations +
+ z = incompleteBetaInverse( df2/2, df1/2, p ), + x = 1 - df2 * (1-z) / (df1 z), ++ if
p > Gamma.incompleteBeta( 0.5*df1, 0.5*df2, 0.5 );
, or p < 0.001
and,
++ z = incompleteBetaInverse( df1/2, df2/2, p ), + x = 1 - df2 * z / (df1 (1-z)), ++ otherwise. + The significance level for a one-sided test (or α in look-up tables) is then
1-x
++
df1
- degrees of freedom of the first datasetdf2
- degrees of freedom of the second datasetp
- the fcdf value for which to solve for x
+IllegalArgumentException
- if df1 or df2 are less than 1, or p is not in (0,1].+public static double gammacdf(double x, + double a, + double b)+
x
of the gamma probability
+ density function.
+ + + y = a^b / Γ(b) * Integral_(0 to x) [ t^(b-1) e^(-a*t) ]dt + ++ The incomplete gamma integral is used, according to the + relation + +
y = Gamma.incompleteGamma( b, a*x )
.
++
a
- the paramater a (alpha) of the gamma distribution.b
- the paramater b (beta, lambda) of the gamma distribution.x
- integration end point.
+x
of the gamma pdf.+public static double gammacdfComplemented(double x, + double a, + double b)+
x
to infinity of the gamma
+ probability density function:
+ + + y = a^b / Γ(b) * Integral_(from x to infinity) [t^(b-1) * e^(-a*t) ] dt + ++ The incomplete gamma integral is used, according to the + relation +
+ y = Gamma.incompleteGammaComplement( b, a*x ). +
+
a
- the paramater a (alpha) of the gamma distribution.b
- the paramater b (beta, lambda) of the gamma distribution.x
- integration start point.+public static double nbinomcdf(double p, + int k, + int n)+
0
through k
of the Negative Binomial Distribution.
+ + + Sum_(from j=0 to k) [ (n+j-1)! / ( j! * (n-1)! ) ] * [ p^n* (1-p)^j ] + ++ In a sequence of Bernoulli trials, this is the probability + that
k
or fewer failures precede the n
-th success.
+ + The terms are not computed individually; instead the incomplete + beta integral is employed, according to the formula +
+ y = nbinomcdf( p, k, n ) = Gamma.incompleteBeta( n, k+1, p )
.
+
+ All arguments must be positive,
+
+
k
- end term.n
- the number of trials.p
- the probability of success (must be in (0.0,1.0)
).+public static double nbinomcdfComplemented(double p, + int k, + int n)+
k+1
to infinity of the Negative
+ Binomial distribution.
+ + + Sum_(from j=k+1 to inf) [ (n+j-1)! / ( j! * (n-1)! ) ] * [ p^n* (1-p)^j ] + ++ The terms are not computed individually; instead the incomplete + beta integral is employed, according to the formula +
+ y = nbinomcdfComplemented( p, k, n ) = Gamma.incompleteBeta( k+1, n, 1-p ). + + All arguments must be positive, +
+
k
- (start-1) term.n
- the number of trials.p
- the probability of success (must be in (0.0,1.0)
).+public static double normcdf(double a) + throws ArithmeticException+
x
(assumes mean is zero, variance is one).
+ + + normcdf(x) = 1/sqrt(2pi) * Integral_(-inf to x) [ e^(-t^2/2) ] dt + = ( 1 + erf(z) ) / 2 + = erfc(z) / 2 + ++ where
z = x/sqrt(2)
.
+ Computation is via the functions erf
and erfComplement
.
+ + Relation to alpha value (or confidence level): +
alpha = 2*( 1-normcdf(x) )
.
+ alpha = ( 1-normcdf(x) )
.
+ +
a
- x
in the equation above.
+ArithmeticException
+public static double normcdf(double x, + double mean, + double variance) + throws ArithmeticException+
x
when the
+ data has not been standardized (i.e. mean ≠0, variance ≠1).
+ + + normcdf(x,mean,variance) = 1/sqrt(2pi*v) * Integral_(-inf to x) [ e^(-(t-mean)^2 / 2v) ] dt + ++ where
v = variance
.
+ Computation is via the functions erf
.
++
mean
- the mean of the normal distribution.variance
- the variance of the normal distribution.x
- the integration limit.
+ArithmeticException
+public static double norminv(double y0) + throws ArithmeticException+
x
, for which the area under the
+ Normal (Gaussian) probability density function (integrated from
+ minus infinity to x
) is equal to the argument y
+ (assumes mean is zero, variance is one); formerly named ndtri
.
+ That is,
+ + normcdf(x) = y0. +
+
+ For small arguments 0 < y < exp(-2)
, the program computes
+ z = sqrt( -2.0 * log(y) )
; then the approximation is
+ x = z - log(z)/z - (1/z) P(1/z) / Q(1/z)
.
+ There are two rational functions P/Q, one for 0 < y < exp(-32)
+ and the other for y
up to exp(-2)
.
+ For larger arguments,
+ w = y - 0.5
, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2))
.
+ Finally, this returns -infinity
for y0 = 0
+ and +infinity for y0 = 1
.
+
+
y0
- the argument to the function
+ArithmeticException
+public static double poissoncdf(double mean, + int k) + throws ArithmeticException+
k
terms of the Poisson distribution.
+ + + Sum_(j=0 to k) [ e^(-m) * m^j /j! ] + ++ The terms are not summed directly; instead the incomplete + gamma integral is employed, according to the relation +
+ y = poissoncdf( mean, k m ) = Gamma.incompleteGammaComplement( k+1, mean )
.
+
+ The arguments must both be positive.
+
+
k
- number of terms.mean
- the mean of the poisson distribution.
+ArithmeticException
+public static double poissoncdfComplemented(int k, + double mean) + throws ArithmeticException+
k+1
to Infinity
of the Poisson distribution.
+ + + Sum_(j=k+1 to inf.) [ e^(-m) * m^j /j! ] + ++ The terms are not summed directly; instead the incomplete + gamma integral is employed, according to the formula +
+ y = poissoncdfComplemented( k, m ) = Gamma.incompleteGamma( k+1, m )
.
+
+ The arguments must both be positive.
+
+
k
- (start-1) term.mean
- the mean of the poisson distribution.
+ArithmeticException
+public static double tcdf(double t, + double k) + throws ArithmeticException+
t
of the Student-t
+ distribution with k > 0
degrees of freedom.
+ + + Γ((k+1)/2) / [ sqrt(k*pi) * Γ(k/2) ] * Integral_(-inf to t) [ ( 1+x^2/k )^( (-k+1)/2 ) ] dx + ++ Relation to incomplete beta integral: +
+ tcdf(t,k) = 0.5 * Gamma.incompleteBeta( k/2, 1/2, z )
, when t < 0
,
+ tcdf(t,k) = 1-0.5 * Gamma.incompleteBeta( k/2, 1/2, z )
, when t > 0
,
+
z = k/(k + t^2)
.
+ Relation to alpha value (or confidence level):
+ alpha = 2*( 1-tcdf(t,k) )
.
+ alpha = ( 1-tcdf(t,k) )
.
+ +
k
- degrees of freedom.t
- integration end point.
+ArithmeticException
+public static double tinv(double p, + double k)+
t
, for which the area under the
+ Student-t probability density function (integrated from
+ minus infinity to t
) is equal to p.
+ The function uses the tcdf function to determine the return
+ value iteratively.
++
p
- the fcdf value for which to solve for xk
- the degrees of freedom
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.QR +
public class QR
+QR Decomposition. +
+ For an m-by-n matrix A with m ≥ n, the QR decomposition is an m-by-n + orthogonal matrix Q and an n-by-n upper triangular matrix R so that + A = Q*R. +
+ The QR decompostion always exists, even if the matrix does not have + full rank, so the constructor will never fail. The primary use of the + QR decomposition is in the least squares solution of nonsquare systems + of simultaneous linear equations. This will fail if isFullRank() + returns false. +
+Shamelessly copied (and modified) from the
+JAMA Java
+Matrix package. To make things compatible with how most users use Processing, the
+class take in float matrices. However, to preserve the acccuracy of the computations, the algorithm
+first casts the input into a double array, prior to doing anything. All methods also return doubles; Use
+Cast.doubleToFloat(double[][])
if you want/need to cast everything back to floats for
+further (non-high-accuracy-dependant) processing (pun intended).
+
+ +
+
+Constructor Summary | +|
---|---|
QR(float[][] A)
+
++ QR Decomposition, computed by Householder reflections. |
+
+Method Summary | +|
---|---|
+ double[][] |
+getH()
+
++ Return the Householder vectors |
+
+ double[][] |
+getQ()
+
++ Generate and return the (economy-sized) orthogonal factor |
+
+ double[][] |
+getR()
+
++ Return the upper triangular factor |
+
+ boolean |
+isFullRank()
+
++ Is the matrix full rank? |
+
+ double[] |
+solve(float[] b)
+
++ Least squares solution of A*X = b |
+
+ double[][] |
+solve(float[][] B)
+
++ Least squares solution of A*X = B |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public QR(float[][] A)+
+
A
- Rectangular matrix+Method Detail | +
---|
+public boolean isFullRank()+
+
+public double[][] getH()+
+
+public double[][] getR()+
+
+public double[][] getQ()+
+
+public double[] solve(float[] b)+
+
b
- An array with the same length as the number of rows in A.
+IllegalArgumentException
- Matrix row dimensions must agree.
+RuntimeException
- Matrix is rank deficient.+public double[][] solve(float[][] B)+
+
B
- A Matrix with as many rows as A and any number of columns.
+IllegalArgumentException
- Matrix row dimensions must agree.
+RuntimeException
- Matrix is rank deficient.
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Rank +
public class Rank
+
Ranking based on the natural ordering on floats for a sequence of data that may also + contain NaNs.
+When present, NaNs are treated according to the configured NaNStrategy constants and ties + are handled using the configured tiesStrategy constants as follows: + +
Strategies for handling NaN values in rank transformations. +
Float.NEGATIVE_INFINITY
.Float.POSITIVE_INFINITY
Strategies for handling tied values in rank transformations: +
Examples: +
+ Input data: (20, 17, 30, 42.3, 17, 50, Float.NaN, Float.NEGATIVE_INFINITY, 17) + | ||
---|---|---|
NaNStrategy | TiesStrategy | +rank(data) |
+
0 (default = NaNs removed) | +0 (default = ties averaged) | +(5, 3, 6, 7, 3, 8, 1, 3) |
0 (default = NaNs removed) | +1 (MINIMUM) | +(5, 2, 6, 7, 2, 8, 1, 2) |
1 (MINIMAL) | +0 (default = ties averaged) | +(6, 4, 7, 8, 4, 9, 1.5, 1.5, 4) |
1 (MINIMAL) | +2 (MAXIMUM) | +(6, 5, 7, 8, 5, 9, 2, 2, 5) |
2 (MAXIMAL) | +2 (MAXIMUM)/td> + | (5, 4, 6, 7, 4, 8, 9, 1, 4) |
+ +
+
+Method Summary | +|
---|---|
+static float[] |
+rank(float[] data,
+ int tiesStrategy)
+
++ Rank an array (with no NaNs) using the natural ordering on Floats with ties + resolved using tiesStrategy . |
+
+static float[] |
+rank(float[] data,
+ int tiesStrategy,
+ int nanStrategy)
+
++ Rank an array containing NaN values using the natural ordering on Floats, with + NaN values handled according to nanStrategy and ties
+ resolved using tiesStrategy . |
+
+static float[] |
+rank(int[] data,
+ int tiesStrategy)
+
++ Rank an array (with no NaNs) using the natural ordering on Floats with ties + resolved using tiesStrategy . |
+
+static float[] |
+rank(int[] data,
+ int tiesStrategy,
+ int nanStrategy)
+
++ Rank an array containing NaN values using the natural ordering on Floats, with + NaN values handled according to nanStrategy and ties
+ resolved using tiesStrategy . |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static float[] rank(int[] data, + int tiesStrategy, + int nanStrategy)+
nanStrategy
and ties
+ resolved using tiesStrategy
.
+
+ Input values that specify which strategy to use for handling tied values in the + rank transformations: +
Input values that specify which strategy to use for handling NaN values in the + rank transformations: +
Float.NEGATIVE_INFINITY
.Float.POSITIVE_INFINITY
rank(float[], int)
instead. It is quicker.
++
data
- array to be ranked. This is cast to a float array prior to ranking.nanStrategy
- 0,1,2 or 3 corresponding to the NaN strategy to employ.tiesStrategy
- 0,1,2 or 3 corresponding to the ties strategy to employ.
++public static float[] rank(float[] data, + int tiesStrategy, + int nanStrategy)+
nanStrategy
and ties
+ resolved using tiesStrategy
.
+
+ Input values that specify which strategy to use for handling tied values in the + rank transformations: +
Input values that specify which strategy to use for handling NaN values in the + rank transformations: +
Float.NEGATIVE_INFINITY
.Float.POSITIVE_INFINITY
rank(float[], int)
instead. It is quicker.
++
data
- array to be rankednanStrategy
- 0,1,2 or 3 corresponding to the NaN strategy to employ.tiesStrategy
- 0,1,2 or 3 corresponding to the ties strategy to employ.
++public static float[] rank(int[] data, + int tiesStrategy)+
tiesStrategy
.
+ Input values that specify which strategy to use for handling NaN values in the + rank transformations: +
+
data
- array to be ranked. The array is cast to a float array prior to rankingtiesStrategy
- the strategy to employ for ties.
++public static float[] rank(float[] data, + int tiesStrategy)+
tiesStrategy
.
+ Input values that specify which strategy to use for handling NaN values in the + rank transformations: +
+
data
- array to be ranked.tiesStrategy
- the strategy to employ for ties.
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.SVD +
public class SVD
+Singular Value Decomposition. +
+For an m x n
matrix A
with m > n
,
+the singular value decomposition is an m-by-n
orthogonal matrix
+U
, an n-by-n
diagonal matrix S
, and
+an n-by-n
orthogonal matrix V
so that A = U*S*V'
.
+
+The singular values, sigma[k] = S[k][k]
, are ordered so that
+sigma[0] > sigma[1] > ... > sigma[n-1]
.
+
+The singular value decompostion always exists, so the constructor will +never fail. The matrix condition number and the effective numerical +rank can be computed from this decomposition. +
+Shamelessly copied (and modified) from the
+JAMA Java
+Matrix package. To make things compatible with how most users use Processing, the
+class take in float matrices. However, to preserve the acccuracy of the computations, the algorithm
+first casts the input into a double array, prior to doing anything. All methods also return doubles; Use
+Cast.doubleToFloat(double[][])
if you want/need to cast everything back to floats for
+further (non-high-accuracy-dependant) processing (pun intended).
+
+ +
+
+Constructor Summary | +|
---|---|
SVD(float[][] AA)
+
++ Construct the singular value decomposition. |
+
+Method Summary | +|
---|---|
+ double |
+cond()
+
++ Two norm condition number |
+
+ double[][] |
+getS()
+
++ Return the diagonal matrix of singular values |
+
+ double[] |
+getSingularValues()
+
++ Return the one-dimensional array of singular values |
+
+ double[][] |
+getU()
+
++ Return the left singular vectors |
+
+ double[][] |
+getV()
+
++ Return the right singular vectors |
+
+ double |
+norm2()
+
++ Two norm |
+
+ int |
+rank()
+
++ Effective numerical matrix rank |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Constructor Detail | +
---|
+public SVD(float[][] AA)+
+
AA
- Rectangular matrix+Method Detail | +
---|
+public double[][] getU()+
+
+public double[][] getV()+
+
+public double[] getSingularValues()+
+
+public double[][] getS()+
+
+public double norm2()+
+
+public double cond()+
+
+public int rank()+
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Visuals +
papaya.ScatterPlot +
public class ScatterPlot
+A simple class to plot x vs y data as a + scatter plot. Default is to draw the points as ellipses, + but you can also use rectangles, and connect the points with + lines. +
+ +
+
+Field Summary | +|
---|---|
+ boolean |
+asConnectedLines
+
++ |
+
+ boolean |
+asEllipses
+
++ |
+
+ boolean |
+asRectangles
+
++ |
+
+ float |
+bottomBound
+
++ Specifies the space on the bottom, between the plot area and the bounding + rectangle. |
+
+ boolean |
+drawAxes
+
++ Specifies whether to draw the axes |
+
+ boolean |
+drawRectangle
+
++ Specifies whether to draw the bounding rectangle around the plot. |
+
+ float |
+leftBound
+
++ Specifies the space on the left between the plot area, and the + bounding rectangle. |
+
+ float |
+rightBound
+
++ Specifies the space on the right betweent the plot area, and the + bounding rectangle. |
+
+ float |
+topBound
+
++ Specifies the space on the top, between the plot area and the bounding + rectangle. |
+
+ boolean[] |
+XYLabels
+
++ |
+
+ boolean[] |
+XYTicks
+
++ |
+
Fields inherited from class papaya.Visuals | +
---|
bgColor |
+
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Constructor Summary | +|
---|---|
ScatterPlot(PApplet _theParent,
+ float _plotLeft,
+ float _plotTop,
+ float _plotWidth,
+ float _plotHeight)
+
++ |
+
+Method Summary | +|
---|---|
+ void |
+drawRectangle()
+
++ Draws a rectangle around the plot. |
+
+ void |
+drawScatterPlot(float[] _xDat,
+ float[] _yDat,
+ float _sWeight,
+ int _sColor)
+
++ Parent function to plot scatter plot of the data. |
+
+ void |
+setDataExtremes(float _minXDat,
+ float _maxXDat,
+ float _minYDat,
+ float _maxYDat)
+
++ Set the minimum/maximum values on the x and y axis to nice numbers. |
+
Methods inherited from class papaya.Visuals | +
---|
drawAxes, drawRect, drawRect, getBottom, getHeight, getLeft, getRight, getTop, getWidth, horizLine, legendHoriz, legendVert, line, mapXData, mapXData, mapYData, mapYData, setBackgroundColor, setHeight, setLeft, setTop, setupFont, setupFont, setWidth, vertLine, writeLabels, writeLabels, writeLabels, writeLabels, writeLabels, writeTitle, xLabels, xLabels, xTicks, yLabels, yLabels, YLines, yTicks |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Field Detail | +
---|
+public boolean asConnectedLines+
+public boolean asRectangles+
+public boolean asEllipses+
+public boolean[] XYLabels+
+public boolean[] XYTicks+
+public boolean drawRectangle+
+
+public boolean drawAxes+
+
+public float leftBound+
+
+public float rightBound+
+
+public float topBound+
+
+public float bottomBound+
+
+Constructor Detail | +
---|
+public ScatterPlot(PApplet _theParent, + float _plotLeft, + float _plotTop, + float _plotWidth, + float _plotHeight)+
+Method Detail | +
---|
+public void setDataExtremes(float _minXDat, + float _maxXDat, + float _minYDat, + float _maxYDat)+
+
_minXDat
- the minimum x value to use. Set to more than the x data minimum._maxXDat
- the maximum x value to use. Set to more than the x data maximum._minYDat
- the minimum y value to use. Set to more than the y data minimum._maxYDat
- the maximum y value to use. Set to more than the y data maximum.+public void drawRectangle()+
+
+public void drawScatterPlot(float[] _xDat, + float[] _yDat, + float _sWeight, + int _sColor)+
+
_xDat
- x Data array_yDat
- y Data array_sWeight
- stroke weight_sColor
- stroke color
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Sorting +
public final class Sorting
+Class for getting the array of indices that can be used to sort the array in ascending or + descending order. This is especially useful if you want to preserve the ordering of + your original array, or if you want to manipulate more than one array according to the + sorted order of only one of them. +
+ For example, assume we have three arrays X, Y and Z. We want to sort all three arrays by
+ X (or some arbitrary comparison function). For example, we have
+ X={3, 2, 1}, Y={3.0, 1.0, 2.0}, Z={6.0, 7.0, 8.0}
. The output should
+ be
+
.
+ To do this, we can use the indices corresponding to the elements of
+ X={1, 2, 3}, Y={2.0, 1.0, 3.0}, Z={8.0, 7.0, 6.0}X
sorted
+ in ascending order:
+
+ X={3,2,1}
so indicesForSorting = {2,1,0}
+ (X[2]
holds the smallest value of X
,
+ X[1]
holds the next smallest value of X
, and X[0]
+ holds the largest value of X
).
+ The indicesForSorting
can then be used to arrange Y
and Z
+ in the same order.
+
+
+ The algorithm used to obtain these indices is a modified version of the + tuned quicksort proposed by Jon L. Bentley and M. Douglas McIlroy's "Engineering a + Sort Function", Software-Practice and Experience, Vol. 23(11) + P. 1249-1265 (November 1993) (program 7 of the paper; currently used in the Arrays.sort() method + to sort arrays). +
It is a modified version in the sense that the original array remains unsorted. Instead, + a copy of the original array is used in the sorting routine, and the array of + indices [0,1,2,...N-1] sorted concurrently with the array being sorted. +
+ +
+
+Method Summary | +|
---|---|
+static int[] |
+indices(double[] a,
+ boolean isAscending)
+
++ |
+
+static int[] |
+indices(double[] a,
+ int fromIndex,
+ int toIndex,
+ boolean isAscending)
+
++ |
+
+static int[] |
+indices(float[] a,
+ boolean isAscending)
+
++ Gets the array of indices that can be used to sort the array in ascending or + descending order. |
+
+static int[] |
+indices(float[] a,
+ int fromIndex,
+ int toIndex,
+ boolean isAscending)
+
++ Gets the array of indices that can be used to sort the section of the + input array going from fromIndex (inclusive) to toIndex (exclusive)
+ in ascending or descending order. |
+
+static int[] |
+indices(int[] a,
+ boolean isAscending)
+
++ |
+
+static int[] |
+indices(int[] a,
+ int fromIndex,
+ int toIndex,
+ boolean isAscending)
+
++ |
+
+static void |
+twoArrays(double[] x,
+ double[] y,
+ int fromIndex,
+ int toIndex,
+ boolean isAscending)
+
++ |
+
+static void |
+twoArrays(float[] x,
+ float[] y,
+ int fromIndex,
+ int toIndex,
+ boolean isAscending)
+
++ Sorts the input x and y arrays according to
+ x and going from fromIndex (inclusive) to toIndex (exclusive)
+ in ascending or descending order. |
+
+static void |
+twoArrays(int[] x,
+ int[] y,
+ int fromIndex,
+ int toIndex,
+ boolean isAscending)
+
++ |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Method Detail | +
---|
+public static int[] indices(float[] a, + boolean isAscending)+
+ The algorithm used to obtain these indices is a modified version of the + tuned quicksort proposed by Jon L. Bentley and M. Douglas McIlroy's "Engineering a + Sort Function", Software-Practice and Experience, Vol. 23(11) + P. 1249-1265 (November 1993) (program 7 of the paper; currently used in the Arrays.sort() method + to sort arrays). +
It is a modified version in the sense that the original array remains unsorted. Instead, + a copy of the original array is used in the sorting routine, and the array of + indices [0,1,2,...N-1] sorted concurrently with the array being sorted. +
+
a
- the input arrayisAscending
- true for data sorted in increasing order.
+fromIndex > toIndex
+ArrayIndexOutOfBoundsException
- if fromIndex < 0
or
+ toIndex > a.length
+public static int[] indices(double[] a, + boolean isAscending)+
+public static int[] indices(int[] a, + boolean isAscending)+
+public static int[] indices(float[] a, + int fromIndex, + int toIndex, + boolean isAscending)+
fromIndex
(inclusive) to toIndex
(exclusive)
+ in ascending or descending order.
+
+ + The algorithm used to obtain these indices is a modified version of the + tuned quicksort proposed by Jon L. Bentley and M. Douglas McIlroy's "Engineering a + Sort Function", Software-Practice and Experience, Vol. 23(11) + P. 1249-1265 (November 1993) (program 7 of the paper; currently used in the Arrays.sort() method + to sort arrays). +
It is a modified version in the sense that the original array remains unsorted. Instead, + a copy of the original array is used in the sorting routine, and the array of + indices [fromIndex,fromIndex+1, ... , toIndex-1] sorted concurrently with the array being sorted. +
+
a
- the array to be sorted.fromIndex
- the index of the first element (inclusive) to be
+ sorted.toIndex
- the index of the last element (exclusive) to be sorted.isAscending
- true for data sorted in increasing order.
+IllegalArgumentException
- if fromIndex > toIndex
+ArrayIndexOutOfBoundsException
- if fromIndex < 0
or
+ toIndex > a.length
+public static int[] indices(double[] a, + int fromIndex, + int toIndex, + boolean isAscending)+
+public static int[] indices(int[] a, + int fromIndex, + int toIndex, + boolean isAscending)+
+public static void twoArrays(float[] x, + float[] y, + int fromIndex, + int toIndex, + boolean isAscending)+
x
and y
arrays according to
+ x
and going from fromIndex
(inclusive) to toIndex
(exclusive)
+ in ascending or descending order.
+
+ + The algorithm used to obtain these indices is a modified version of the + tuned quicksort proposed by Jon L. Bentley and M. Douglas McIlroy's "Engineering a + Sort Function", Software-Practice and Experience, Vol. 23(11) + P. 1249-1265 (November 1993) (program 7 of the paper; currently used in the Arrays.sort() method + to sort arrays). +
It is a modified version in the sense that two arrays are sorted according to the first
+ array. At the end of the method, the section of both arrays going from
+ fromIndex
(inclusive) to toIndex
(exclusive) are sorted according
+ to the first array's order. Use a copy of both arrays if you want to preserve the natural order of
+ the original arrays.
+
+
x
- the array to sort.y
- the second array, to sort according to x
.fromIndex
- the index of the first element (inclusive) to be
+ sorted.toIndex
- the index of the last element (exclusive) to be sorted.isAscending
- true for data sorted in increasing order.
+IllegalArgumentException
- if fromIndex > toIndex
+ArrayIndexOutOfBoundsException
- if fromIndex < 0
or
+ toIndex > a.length
Arrays.sort(float[])
+public static void twoArrays(double[] x, + double[] y, + int fromIndex, + int toIndex, + boolean isAscending)+
+public static void twoArrays(int[] x, + int[] y, + int fromIndex, + int toIndex, + boolean isAscending)+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Visuals +
papaya.SubPlot +
public class SubPlot
+Convenient class for drawing multiple scatter plots. +
+ +
+
+Field Summary | +|
---|---|
+ float |
+bottomBound
+
++ Specifies the space on the bottom, between the plot area and the bounding + rectangle. |
+
+ boolean |
+drawAxes
+
++ Specifies whether to draw the axes |
+
+ boolean |
+drawRectangle
+
++ Specifies whether to draw the bounding rectangle around the plot. |
+
+ float |
+leftBound
+
++ Specifies the space on the left between the plot area, and the + bounding rectangle. |
+
+ float |
+rightBound
+
++ Specifies the space on the right betweent the plot area, and the + bounding rectangle. |
+
+ ScatterPlot[][] |
+s
+
++ |
+
+ float |
+topBound
+
++ Specifies the space on the top, between the plot area and the bounding + rectangle. |
+
+ float[][] |
+xLefts
+
++ |
+
+ float |
+xSpacing
+
++ |
+
+ float |
+xW
+
++ |
+
+ float |
+yH
+
++ |
+
+ float |
+ySpacing
+
++ |
+
+ float[][] |
+yTops
+
++ |
+
Fields inherited from class papaya.Visuals | +
---|
bgColor |
+
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Constructor Summary | +|
---|---|
SubPlot(PApplet theParent,
+ float xLeft,
+ float yTop,
+ float xWidth,
+ float yHeight,
+ int numX,
+ int numY)
+
++ Creates numX*numY plots, each of with xWidth, and height yHeight. |
+
+Method Summary | +|
---|---|
+ void |
+drawScatterPlot(float[] xDat,
+ float[] yDat,
+ int fColor,
+ int xnum,
+ int ynum)
+
++ Draws the scatterplot for subplot(xnum,ynum) by calling ScatterPlot.drawScatterPlot(float[], float[], float, int) . |
+
+ void |
+legendHoriz(String where,
+ int[] colors,
+ String[] labels)
+
++ Draws a legend going to the right with the first element situated at (x,y). |
+
+ void |
+legendVert(float x,
+ float y,
+ int[] colors,
+ String[] labels)
+
++ Draws a legend going downwards with the first element situated at (x,y). |
+
+ void |
+setDataExtremes(float minXDat,
+ float maxXDat,
+ float minYDat,
+ float maxYDat,
+ int xnum,
+ int ynum)
+
++ Sets the data extremes for subplot(xnum,ynum) by calling ScatterPlot.setDataExtremes(float, float, float, float) |
+
+ void |
+writeTitle(PFont pFont,
+ String title,
+ int xnum,
+ int ynum)
+
++ Writes the title for subplot(xnum,ynum). |
+
+ void |
+writeTitle(String title,
+ int xnum,
+ int ynum)
+
++ Writes the title for subplot(xnum,ynum). |
+
+ void |
+xLabels(int _numDiv,
+ int xnum,
+ int ynum)
+
++ Writes the x labels for subplot(xnum,ynum). |
+
+ void |
+yLabels(int _numDiv,
+ int xnum,
+ int ynum)
+
++ Writes the y labels for subplot(xnum,ynum). |
+
Methods inherited from class papaya.Visuals | +
---|
drawAxes, drawRect, drawRect, getBottom, getHeight, getLeft, getRight, getTop, getWidth, horizLine, legendHoriz, line, mapXData, mapXData, mapYData, mapYData, setBackgroundColor, setHeight, setLeft, setTop, setupFont, setupFont, setWidth, vertLine, writeLabels, writeLabels, writeLabels, writeLabels, writeLabels, writeTitle, xLabels, xLabels, xTicks, yLabels, yLabels, YLines, yTicks |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Field Detail | +
---|
+public boolean drawRectangle+
+
+public boolean drawAxes+
+
+public float leftBound+
+
+public float rightBound+
+
+public float topBound+
+
+public float bottomBound+
+
+public float[][] xLefts+
+public float[][] yTops+
+public float xW+
+public float yH+
+public float ySpacing+
+public float xSpacing+
+public ScatterPlot[][] s+
+Constructor Detail | +
---|
+public SubPlot(PApplet theParent, + float xLeft, + float yTop, + float xWidth, + float yHeight, + int numX, + int numY)+
+
+Method Detail | +
---|
+public void setDataExtremes(float minXDat, + float maxXDat, + float minYDat, + float maxYDat, + int xnum, + int ynum)+
ScatterPlot.setDataExtremes(float, float, float, float)
++
+public void drawScatterPlot(float[] xDat, + float[] yDat, + int fColor, + int xnum, + int ynum)+
ScatterPlot.drawScatterPlot(float[], float[], float, int)
. For
+ more control, use s[xnum][ynum].drawScatterPlot directly.
++
+public void writeTitle(PFont pFont, + String title, + int xnum, + int ynum)+
+
+public void writeTitle(String title, + int xnum, + int ynum)+
+
+public void legendVert(float x, + float y, + int[] colors, + String[] labels)+
Visuals.legendVert(float, float, int[], java.lang.String[])
.
++
legendVert
in class Visuals
colors
- the legend colorslabels
- labels that go with each color+public void legendHoriz(String where, + int[] colors, + String[] labels)+
+
where
- "top" if you want to place the legend at the top, otherwise, it
+ will default to placing it at the bottom of the plots.colors
- the legend colorslabels
- labels that go with each color+public void yLabels(int _numDiv, + int xnum, + int ynum)+
+
+public void xLabels(int _numDiv, + int xnum, + int ynum)+
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Unique +
public class Unique
+Class for getting and storing an unsorted array's unique elements, + the indices of these elements, and the number of times the elements occur. +
This class is overkill if:
+
+ you want to store only the unique elements. Use a Java
+ Set instead.
+
+ you don't want to store everything in a class. Use the
+ Descriptive.frequencies(float[], java.util.ArrayList
method instead.
+
+ +
+
+Field Summary | +|
---|---|
+ ArrayList<Integer> |
+frequencies
+
++ ArrayList containing the frequencies of the corresponding unique value |
+
+ int[][] |
+idx
+
++ Class containing the integer arrays that hold the indices of each unique value. |
+
+ ArrayList<Float> |
+values
+
++ ArrayList containing the unique values |
+
+Constructor Summary | +|
---|---|
Unique(float[] data,
+ boolean storeIndices)
+
++ Class for storing the unique values in an unsorted data array. |
+
+Method Summary | +
---|
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Field Detail | +
---|
+public ArrayList<Float> values+
+
+public ArrayList<Integer> frequencies+
+
+public int[][] idx+
+
+Constructor Detail | +
---|
+public Unique(float[] data, + boolean storeIndices)+
true
.
+
+ Example:
+
+ data = (8,5,6,7,8,5,5) --> values = (5,6,7,8), frequencies = (3,1,1,2),
+ idx[0] = {1,5,6}, idx[1] = {2}, idx[2] = {3}, idx[3] = {0,4}
+
+
data
- the data arraystoreIndices
- set to true
if you want to store the indices,
+ else set to false
+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+java.lang.Object ++papaya.Visuals +
public class Visuals
+Visuals is the parent class behind most of the other plotting classes. + +
+ It: + + 1. Contains some useful functions for the visual representation + of your data (e.g. setting up your font, writing labels, mapping + arrays to the plot right-left-top-bottom parameters, xTicks, yTicks, + etc.) that should help you reduce the amount of code you end up + writing. + + 2. Is extended by the available "Plotting" classes used to quickly give a + visual snapshot of your data. +
++ +
+
+Field Summary | +|
---|---|
+ int |
+bgColor
+
++ Specifies the plot background color |
+
Fields inherited from interface papaya.PapayaConstants | +
---|
BASELINE, big, biginv, BOTTOM, CENTER, CORNER, FONTNAME, GRAY, INDEX_NOT_FOUND, INDICES_NOT_FOUND, LEFT, LOGPI, MACHEP, MAXGAM, MAXLOG, MINLOG, RIGHT, SQRTH, SQTPI, STROKEWEIGHT, TEXTSIZE, TOP |
+
+Constructor Summary | +|
---|---|
Visuals(PApplet _theParent,
+ float _plotLeft,
+ float _plotTop,
+ float _plotWidth,
+ float _plotHeight)
+
++ The parent class behind the other plotting classes. |
+
+Method Summary | +|
---|---|
+ void |
+drawAxes(int _sColor,
+ float _sWeight)
+
++ Draws the plot axes using a line with color _sColor, and weight _sWeight |
+
+ void |
+drawRect()
+
++ Draw the plot background rectangle using the default plot + dimensions and background color. |
+
+ void |
+drawRect(float _left,
+ float _top,
+ float _width,
+ float _height,
+ int _rectColor)
+
++ Draws the plot background rectangle using the input dimensions and + background color. |
+
+ float |
+getBottom()
+
++ |
+
+ float |
+getHeight()
+
++ |
+
+ float |
+getLeft()
+
++ |
+
+ float |
+getRight()
+
++ |
+
+ float |
+getTop()
+
++ |
+
+ float |
+getWidth()
+
++ |
+
+ void |
+horizLine(float _xFrom,
+ float _xTo,
+ float _y,
+ int _sColor,
+ float _sWeight)
+
++ draws a horizonal line with color _sColor, and weight _sWeight |
+
+ void |
+legendHoriz(float x,
+ float y,
+ int[] colors,
+ String[] labels)
+
++ Draws a legend going to the right with the first element situated at (x,y). |
+
+ void |
+legendVert(float x,
+ float y,
+ int[] colors,
+ String[] labels)
+
++ Draws a legend going downwards with the first element situated at (x,y). |
+
+ void |
+line(float _x1,
+ float _y1,
+ float _x2,
+ float _y2,
+ int _sColor,
+ float _sWeight)
+
++ Draws a straight line going from (x[0],y[0]) to (x[1],y[1]) + with color _sColor, and weight _sWeight |
+
+ float[] |
+mapXData(float[] data)
+
++ Maps the x-data from the range min(data) to max(data) to + the plotLeft and plotLeft+plotWidth. |
+
+ float[] |
+mapXData(float[] data,
+ float minXDat,
+ float maxXDat)
+
++ Maps the x-data from the range minXDat to maxXDat to + the plotLeft and plotRight. |
+
+ float[] |
+mapYData(float[] data)
+
++ Maps the y-data from the range min(data) to max(data) to + the plotBottom and plotTop. |
+
+ float[] |
+mapYData(float[] data,
+ float minYDat,
+ float maxYDat)
+
++ Maps the y-data from the range minYDat to maxYDat to + the plotBottom and plotTop. |
+
+ void |
+setBackgroundColor(int _backgroundColor)
+
++ |
+
+ void |
+setHeight(float _plotHeight)
+
++ |
+
+ void |
+setLeft(float _plotLeft)
+
++ |
+
+ void |
+setTop(float _plotTop)
+
++ |
+
+ void |
+setupFont(String _fontName,
+ int _xAlign,
+ int _yAlign,
+ int _tSize)
+
++ function for setting the font up for writing the labels |
+
+ void |
+setupFont(String _fontName,
+ int _xAlign,
+ int _yAlign,
+ int _tSize,
+ int _fontColor)
+
++ overload setupFont function that takes in font color |
+
+ void |
+setWidth(float _plotWidth)
+
++ |
+
+ void |
+vertLine(float _yFrom,
+ float _yTo,
+ float _x,
+ int _sColor,
+ float _sWeight)
+
++ draws a vertical line with color _sColor, and weight _sWeight |
+
+ void |
+writeLabels(float _text,
+ float _xPos,
+ float _yPos)
+
++ |
+
+ void |
+writeLabels(PFont _plotFont,
+ float _text,
+ float _xPos,
+ float _yPos)
+
++ |
+
+ void |
+writeLabels(PFont _plotFont,
+ String[] _text,
+ float[] _xPos,
+ float[] _yPos)
+
++ |
+
+ void |
+writeLabels(PFont _plotFont,
+ String _text,
+ float _xPos,
+ float _yPos)
+
++ |
+
+ void |
+writeLabels(String _text,
+ float _xPos,
+ float _yPos)
+
++ |
+
+ void |
+writeTitle(String _title,
+ PFont _plotFont)
+
++ titles the plot given the String _title and the PFont |
+
+ void |
+xLabels(float _left,
+ float _width,
+ float _xDatMin,
+ float _xDatMax,
+ int _numDiv)
+
++ Write the x-axis labels. |
+
+ void |
+xLabels(float _left,
+ float _width,
+ String[] _xLabels)
+
++ Write the x-axis labels. |
+
+ void |
+xTicks(float _left,
+ float _width,
+ int _numDiv,
+ int _tickHeight)
+
++ Draws the x-axis tickmarks. |
+
+ void |
+yLabels(float _top,
+ float _height,
+ float _yDatMin,
+ float _yDatMax,
+ int _numDiv)
+
++ Write the y-axis labels. |
+
+ void |
+yLabels(float _top,
+ float _height,
+ String[] _yLabels)
+
++ Write the y-axis labels. |
+
+ void |
+YLines(int _numDiv)
+
++ Draws horizontal y-lines on the plot |
+
+ void |
+yTicks(float _top,
+ float _height,
+ int _numDiv,
+ int _tickWidth)
+
++ Draws the y-axis tickmarks. |
+
Methods inherited from class java.lang.Object | +
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
+
+Field Detail | +
---|
+public int bgColor+
+
+Constructor Detail | +
---|
+public Visuals(PApplet _theParent, + float _plotLeft, + float _plotTop, + float _plotWidth, + float _plotHeight)+
+ Visuals contains some useful functions for the (elementary!) visual representation + of your data (e.g. setting up your font, writing labels, mapping + arrays to the plot right-left-top-bottom parameters, xTicks, yTicks,legends, + etc). Nothing very fancy like writing x-labels at an angle or computing your + tax returns for you though so don't get your hopes up. Sorry. +
+
_theParent
- the Processing papplet. Just "this" in your sketch._plotLeft
- the plot left_plotTop
- the plot top (in Processing coordinates!)_plotWidth
- the plot width_plotHeight
- the plot height.
+ The bottom of the plot area is then given by _plotTop + _plotHeight
+ while the right is _plotLeft + _plotWidth
.
+Method Detail | +
---|
+public float[] mapXData(float[] data)+
mappedData[i] = plotLeft + (data[i] - min) / (max-min) * plotWidth
.
+ This is not done directly though, but rather, the
+ Mat.map(float[] data, float low1, float high1, float low2, float high2)
+ function is used.
++
data
- the x data to map.
++public float[] mapXData(float[] data, + float minXDat, + float maxXDat)+
mappedData[i] = plotLeft + (data[i] - minXDat) / (maxXDat-minXDat) * plotWidth
.
+ This is not done directly though, but rather, the
+ Mat.map(float[] data, float low1, float high1, float low2, float high2)
+ function is used.
+ + Note: No internal checks are done to make sure that the min/max values are in the + data range. +
++
data
- the x data to map.minXDat
- the minimum value to use in the data mapping.maxXDat
- the maximum value to use in the data mapping.
++public float[] mapYData(float[] data)+
mappedData[i] = plotBottom + (data[i] - min) / (max-min) * (-plotHeight)
.
+ (The minus sign above accounts for the difference in Processing's coordinate system
+ and what we associate with Top/Bottom).
+ The computation above is not done directly though, but rather, the
+ Mat.map(float[] data, float low1, float high1, float low2, float high2)
+ function is used.
++
data
- the y data to map.
++public float[] mapYData(float[] data, + float minYDat, + float maxYDat)+
mappedData[i] = plotBottom + (data[i] - minYDat) / (maxYDat-minYDat) * (-plotHeight)
.
+ (The minus sign above accounts for the difference in Processing's coordinate system
+ and what we associate with Top/Bottom).
+ The computation above is not done directly though, but rather, the
+ Mat.map(float[] data, float low1, float high1, float low2, float high2)
+ function is used.
+ + Note: No internal checks are done to make sure that the min/max values are in the + data range. +
++
data
- the x data to map.minYDat
- the minimum value to use in the data mapping.maxYDat
- the maximum value to use in the data mapping.
++public void drawAxes(int _sColor, + float _sWeight)+
+
+public void drawRect()+
+
+public void drawRect(float _left, + float _top, + float _width, + float _height, + int _rectColor)+
+
_left
- the left-most point of the rectangle_top
- the top-most point of the rectangle_width
- the width of the rectangle_height
- the height of the rectangle_rectColor
- the rectangle color.+public void legendVert(float x, + float y, + int[] colors, + String[] labels)+
+
colors
- the legend colorslabels
- labels that go with each color+public void legendHoriz(float x, + float y, + int[] colors, + String[] labels)+
+
colors
- the legend colorslabels
- labels that go with each color+public void xTicks(float _left, + float _width, + int _numDiv, + int _tickHeight)+
+
_left
- left most tick position. Set to negative to make it default
+ to the plotLeft)_width
- (_left + _width) = right most tick position. Set to negative_numDiv
- the number of divisions. The number of tick marks drawn = _numDiv+1._tickHeight
- the height of the tick mark.+public void xLabels(float _left, + float _width, + float _xDatMin, + float _xDatMax, + int _numDiv)+
+
_left
- left most label position. Set to negative to make it default
+ to the plotLeft)_width
- (_left + _width) = right most label position. Set to negative
+ to make it default to the plotWidth_xDatMin
- minimum data value to show in the label_xDatMax
- maximum data value to show in the label_numDiv
- the number of divisions.+public void xLabels(float _left, + float _width, + String[] _xLabels)+
+
_left
- left most label position. Set to negative to make it default
+ to the plotLeft)_width
- (_left + _width) = right most label position. Set to negative
+ to make it default to the plotWidth_xLabels
- the String array of labels.+public void yTicks(float _top, + float _height, + int _numDiv, + int _tickWidth)+
+
_top
- top most tick position in Processing's coordinate system.
+ Set to negative to make it default to the plotTop)_height
- (top + height) = bottom most label position in Processing's coordinate system.
+ Set to negative to make it default to the plotHeight._numDiv
- the number of divisions. The number of tick marks drawn = _numDiv+1._tickWidth
- the width of the tick mark.+public void yLabels(float _top, + float _height, + float _yDatMin, + float _yDatMax, + int _numDiv)+
+
_top
- top most label position in Processing's coordinate system.
+ Set to negative to make it default to the plotTop)_height
- (top + height) = bottom most label positionin Processing's coordinate system.
+ Set to negative to make it default to the plotHeight._yDatMin
- minimum data value to show in the label_yDatMax
- maximum data value to show in the label_numDiv
- the number of divisions.+public void yLabels(float _top, + float _height, + String[] _yLabels)+
+
_top
- top most label position in Processing's coordinate system.
+ Set to negative to make it default to the plotTop)_height
- (top + height) = bottom most label positionin Processing's coordinate system.
+ Set to negative to make it default to the plotHeight._yLabels
- the String array of labels.+public void YLines(int _numDiv)+
+
_numDiv
- number of divisions. The number of lines drawn = numDiv+1+public void horizLine(float _xFrom, + float _xTo, + float _y, + int _sColor, + float _sWeight)+
+
+public void vertLine(float _yFrom, + float _yTo, + float _x, + int _sColor, + float _sWeight)+
+
+public void line(float _x1, + float _y1, + float _x2, + float _y2, + int _sColor, + float _sWeight)+
+
+public void writeTitle(String _title, + PFont _plotFont)+
+
+public void writeLabels(PFont _plotFont, + String[] _text, + float[] _xPos, + float[] _yPos)+
+public void writeLabels(PFont _plotFont, + String _text, + float _xPos, + float _yPos)+
+public void writeLabels(PFont _plotFont, + float _text, + float _xPos, + float _yPos)+
+public void writeLabels(float _text, + float _xPos, + float _yPos)+
+public void writeLabels(String _text, + float _xPos, + float _yPos)+
+public void setupFont(String _fontName, + int _xAlign, + int _yAlign, + int _tSize)+
+
+public void setupFont(String _fontName, + int _xAlign, + int _yAlign, + int _tSize, + int _fontColor)+
+
+public void setBackgroundColor(int _backgroundColor)+
+public float getLeft()+
+public float getRight()+
+public float getTop()+
+public float getBottom()+
+public float getWidth()+
+public float getHeight()+
+public void setLeft(float _plotLeft)+
+public void setWidth(float _plotWidth)+
+public void setTop(float _plotTop)+
+public void setHeight(float _plotHeight)+
+
+
|
++ + | +|||||||
+ PREV CLASS + NEXT CLASS | ++ FRAMES + NO FRAMES + + + + + | +|||||||
+ SUMMARY: NESTED | FIELD | CONSTR | METHOD | ++DETAIL: FIELD | CONSTR | METHOD | +
+Interfaces
+
+ +PapayaConstants |
+
+Classes
+
+ +BoxPlot + +Cast + +Comparison + +Comparison.TTest + +Correlation + +Correlation.Significance + +Correlation.Weighted + +CorrelationPlot + +Descriptive + +Descriptive.Mean + +Descriptive.Pooled + +Descriptive.Sum + +Descriptive.Weighted + +Distance + +Eigenvalue + +Find + +Frequency + +Gamma + +Linear + +Linear.BoxCox + +Linear.Significance + +Linear.StdErr + +LU + +Mat + +MDS + +NaNs + +Normality + +Normality.Dago + +OneWayAnova + +Polynomial + +Probability + +QR + +Rank + +ScatterPlot + +Sorting + +SubPlot + +SVD + +Unique + +Visuals |
+
+
+
|
++ + | +|||||||
+ PREV PACKAGE + NEXT PACKAGE | ++ FRAMES + NO FRAMES + + + + + | +
+Interface Summary | +|
---|---|
PapayaConstants | +PapayaConstants stores some of the constants used in plotting. | +
+ +
+Class Summary | +|
---|---|
BoxPlot | +BoxPlot class | +
Cast | +Static Class for casting Object arrays to their corresponding primitive type. | +
Comparison | +Contains a number of methods for comparing more than one dataset + against each other. | +
Comparison.TTest | +Methods related to comparing two populations. | +
Correlation | +Contains utilities related to computing covariances, as well as linear and rank correlation. | +
Correlation.Significance | +Contains methods used to compute the significance, or pvalue of the input correlations. | +
Correlation.Weighted | +Contains methods related to computing the correlation and covariance of weighted + datasets. | +
CorrelationPlot | +Takes in a matrix and plots the data in each of the columns versus + each other. | +
Descriptive | +Basic descriptive statistics class for exploratory data analysis. | +
Descriptive.Mean | +Contains methods for computing the arithmetic, geometric, harmonic, trimmed, and winsorized + means (among others). | +
Descriptive.Pooled | +Class for computing the pooled mean and variance of data sequences | +
Descriptive.Sum | +Methods for computing various different sums of datasets such as sum of inversions, + logs, products, power deviations, squares, etc. | +
Descriptive.Weighted | +Contains methods related to weighted datasets. | +
Distance | +Contains methods for computing various "distance" metrics for multidimensional scaling. | +
Eigenvalue | +Eigenvalues and eigenvectors of a real matrix. | +
Find | +Static class for finding indices in an array corresponding to a given value/object. | +
Frequency | +Class for getting the frequency distribution, cumulative frequency distribution, and other
+ distribution-related parameters of a given array of |
+
Gamma | +Gamma and Beta functions. | +
Linear | +Contains methods related to determining the linear linear + relationship between two datasets (of equal arrays) such as the + best-fit linear line parameters, box-cox transformations, etc. | +
Linear.BoxCox | +Contains methods related to the Box-Cox transformation of a data set; useful in
+ determining the best transformation that will yield the best method for converting
+ a monotonic, non-linear relationship between x and y into
+ a linear one. |
+
Linear.Significance | +Contains methods used to compute the significance, or pvalue of the input correlations. | +
Linear.StdErr | +Contains methods related to computing the standard errors of the residuals, slope and intercept + associated with the best-fit linear line. | +
LU | +LU Decomposition. | +
Mat | +Static class for performing some basic matrix operations. | +
MDS | +Contains methods for performing but classical and non-classical multidimensional scaling. | +
NaNs | +Contains various methods for dealing with NaNs in your data. | +
Normality | +Contains various utilities for checking if the dataset comes from a normal distribution. | +
Normality.Dago | +Methods for computing the skewnewss, kurtosis, and D'Agostino-Peasrson K^2 "omnibus" test-statistics (that + combine the former two), and accompanying significance (or p-values) + for testing the underlying population normality. | +
OneWayAnova | +Computes the one-way ANOVA p-value to test the equality of two or more sample
+ means by analyzing the sample variances using the test statistic
+ F = variance between samples / variance within samples . |
+
Polynomial | +Static Class for casting one variable type to another. | +
Probability | +Cumulative distribution functions and corresponding inverses of certain probability distributions. | +
QR | +QR Decomposition. | +
Rank | +Ranking based on the natural ordering on floats for a sequence of data that may also + contain NaNs. | +
ScatterPlot | +A simple class to plot x vs y data as a + scatter plot. | +
Sorting | +Class for getting the array of indices that can be used to sort the array in ascending or + descending order. | +
SubPlot | +Convenient class for drawing multiple scatter plots. | +
SVD | +Singular Value Decomposition. | +
Unique | +Class for getting and storing an unsorted array's unique elements, + the indices of these elements, and the number of times the elements occur. | +
Visuals | +Visuals is the parent class behind most of the other plotting classes. | +
+
+
+
|
++ + | +|||||||
+ PREV PACKAGE + NEXT PACKAGE | ++ FRAMES + NO FRAMES + + + + + | +
+
+
|
++ + | +|||||||
+ PREV + NEXT | ++ FRAMES + NO FRAMES + + + + + | +
+
+
|
++ + | +|||||||
+ PREV + NEXT | ++ FRAMES + NO FRAMES + + + + + | +
+ * + *
+ * @author Nur Adila Faruk Senan + */ +public final class Cast { + + /** + * Makes this class non instantiable, but still let's others inherit from it. + */ + protected Cast(){ + } + + /** + * function for casting ArrayList to int[] + * @param _inputDat ArrayList array that needs to be cast to int[] + * @return the resulting int[] array + */ + public static int[] arrayListToInt(ArrayListU = min(U1,U2)+ * where + *
+ * U1 = Sum(Ranks1) - n1*(n1+1)/2, + * U2 = Sum(Ranks2) - n2*(n2+1)/2. + *+ * and the significance, or p-value is computed assuming + * a two-tailed null-hypothesis that both samples are similar. + * @param data1 the first dataset + * @param data2 the second dataset + * @return a 2 element array with first element corresponding to the test statistic, and second + * equal to the p-value or significance. + * @throws IllegalArgumentException if either of the sample sizes is less than 8 (the normal approximation + * is no longer valid in that case. Best to consult a table instead). + */ + public static float[] mannWhitney(float[] data1, float[] data2){ + int size1 = data1.length, size2 = data2.length; + if(size1 < 8 || size2 < 8) + throw new IllegalArgumentException("size1 and size2 need to be greater than 8."); + + float[] allData = Mat.concat(data1,data2); + float[] ranksAll = Rank.rank(allData,0); + + /* sum up the ranks of the first dataset*/ + float ranks1Sum = ranksAll[0]; + for(int i=1; i
To elaborate, the null hypothesis H0 states that given a random pair of measurements (X[i], Y[i]),
+ * X[i] and Y[i] are equally likely to be larger than the other.
+ * Now, let n
be the number of pairs for which Y[i] ≠ X[i] and w
+ * be the number of pairs for which Y[i] - X[i] > 0.
+ * Assuming that H0 is true, then W follows a binomial distribution W ~ b(n, 0.5).
+ *
+ * This method returns the sum of the terms 0
through w
of the Binomial
+ * probability density.
+ *
+ * Sum_(j=0 to w) [ (n!)/(j!*(n-j)! ] * [ p^j * (1-p)^(n-j) ] + * from 0 to the test-statistic, or + *+ * @param unbiased set to true to return the unbiased correlation, + * false to return the biased version. + */ + public static float linear(float[] data1, float[] data2, boolean unbiased) { + float denom = cov(data1,data1,unbiased)*cov(data2,data2,unbiased); + return cov(data1,data2,unbiased)/(float)Math.sqrt(denom); + } + + /** + * Computes Spearman's rank-correlation, or+ * p = Probability.binomcdf(.5,w,n). + *
+ * For n > 100, the normal distribution cdf is returned instead. + * If this is less than the critical value, then we can reject the null hypothesis that there + * is no difference between the samples X and Y. + * + * @param x data set containing 1st observation sequence + * @param y data set containing 2nd observation sequence + * @return the significance, or p-value, assuming a two-tailed null hypothesis (i.e. no difference) + */ + public static float signTest(float[] x, float[] y){ + int size = x.length; + Check.equalLengths(size,y); + int numMore=0,numLess=0; + for(int i=0; i0) numMore++; + else if (val<0) numLess++; + } + int numTotal = numMore + numLess; + double p; + int minOfTwo = Math.min(numMore,numLess); + if(numTotal<100){ + p = Probability.binomcdf(.5, minOfTwo, numTotal); + } else{ + double z = (2*minOfTwo + 1 - numTotal)/Math.sqrt(numTotal); + p = Probability.normcdf(z); + } + return (float)p; + } + + /** Methods related to comparing two populations. Each method returns an array with the + * first element containing the t-statistic, and the second corresponding to the + * p-value, or significance, of rejecting the null hypothesis. + * + * Tests can be:
+ *
+ *- One-sample or two-sample
+ *- One-sided or two-sided
+ *- Paired or unpaired (for two-sample tests)
+ *- Homoscedastic (equal variance assumption) or heteroscedastic + * (for two sample tests)
+ *- Fixed significance level (boolean-valued) or returning p-values. + *
+ * Test statistics are available for all tests. Methods including "Test" in + * in their names perform tests, all other methods return t-statistics. Among + * the "Test" methods,
+ *double-
valued methods return p-values; + *boolean-
valued methods perform fixed significance level tests. + * Significance levels are always specified as numbers between 0 and 0.5 + * (e.g. tests at the 95% level usealpha=0.05
).+ * + * Note: Inspired by the + * stats.inference.TTest class available in the Apache Commons math library. */ + public static class TTest{ + protected TTest(){} + + /** + * Returns the t-statistic and p-value for checking whether the means + * between the two datasets are different under the assumption that both + * datasets have equal variances. The p-value corresponds to the + * null hypothesis of no-difference (i.e. two-tailed). + * @return an array with the first element = t and the second element = p. + */ + public static float[] equalVar(float[] data1, float[] data2){ + int[] sizes = new int[]{data1.length,data2.length}; + float[] means = new float[]{Descriptive.mean(data1),Descriptive.mean(data2)}; + float[] variances = new float[]{Descriptive.var(data1,true),Descriptive.var(data2,true)}; + float pooledVariance = Descriptive.Pooled.var(variances,sizes,true); + float sizeSum = 1f/sizes[0] + 1f/sizes[1]; + double t = (means[0]-means[1])/Math.sqrt(pooledVariance*sizeSum); + int df = sizes[0] + sizes[1] - 2; + float p = (float)twoTailedStudentT(t,df); + return new float[]{(float)t,p}; + } + + /** Returns the t-statistic and p-value for checking whether the means + * between the two datasets are different under the assumption that both + * datasets have unequal variances. The p-value corresponds to the + * null hypothesis of no-difference (i.e. two-tailed). + * This is also known as Welch's t-test. + * @return an array with the first element = t and the second element = p. + */ + public static float[] unequalVar(float[] data1, float[] data2){ + int size1 = data1.length; int size2 = data2.length; + float mean1 = Descriptive.mean(data1); float mean2 = Descriptive.mean(data2); + float var1 = Descriptive.var(data1,true); float var2 = Descriptive.var(data2,true); + float var1n1 = var1/size1; float var2n2 = var2/size2; + // test statistic + double t = (mean1-mean2)/Math.sqrt(var1n1 + var2n2); + // df + double numerator = (var1n1 + var2n2)*(var1n1 + var2n2); + double denominator = var1n1*var1n1/(size1-1) + var2n2*var2n2/(size2-1); + double df = (numerator/denominator); + float p = (float)twoTailedStudentT(t,df); + return new float[]{(float)t,p}; + } + + /** + * Returns the t-statistic and p-value for checking a pair of dependent samples. + * That is, when there is only one sample that has been tested twice + * (repeated measures) or when there are two samples that have been + * matched or "paired". The p-value corresponds to the + * null hypothesis of no-difference in both measurements (i.e. two-tailed). + *
Both datasets have to have equal lengths. + * @param before the dataset before treatment/measurement. + * @param after the dataset after treatment/measurement. + * @return an array with the first element = t and the second element = p. + */ + public static float[] paired(float[] before, float[] after){ + int size = before.length; + Check.equalLengths(size,after); + float[] diff =new float[size]; + float sum = 0; + for (int i = 0; i < size; i++) { + diff[i] += after[i]-before[i]; + sum+=diff[i]; + } + float mean = sum/size; + float sd = Descriptive.std(diff,false) / (float)Math.sqrt(size); + double t = mean/sd; + float p = (float)twoTailedStudentT(t,size-1); + return new float[]{(float)t,p}; + } + + + /** Returns the welch's t-test degree of freedom */ + protected static float welchdf(float[] data1, float[] data2){ + int size1 = data1.length; int size2 = data2.length; + float var1 = Descriptive.var(data1,true); + float var2 = Descriptive.var(data2,true); + float var1n1 = var1/size1; float var2n2 = var2/size2; + double numerator = (var1n1 + var2n2)*(var1n1 + var2n2); + double denominator = var1n1*var1n1/(size1-1) + var2n2*var2n2/(size2-1); + return (float)(numerator/denominator); + } + + } + private static double twoTailedNormal(double z){ + if(z>0) return ( 2*(1.0- (Probability.normcdf(z)) ) ); + else return (2 * (Probability.normcdf(z))); + } + + private static double twoTailedStudentT(double t, double df){ + if(t>0) return ( 2*(1.0- (Probability.tcdf(t,df)) ) ); + else return (2 * (Probability.tcdf(t,df))); + } + + + /** + * Non-parametric statistical hypothesis test used when comparing two related samples + * or repeated measurements on a single sample to assess whether their population mean + * ranks differ (i.e. it's a paired difference test). + * It can be used as an alternative to the paired Student's t-test when the + * population cannot be assumed to be normally distributed or the data is on the ordinal scale. + *
Note: + * If we wish to make an inference about the mean (or about the median) difference, + * then we assume the distribution of the differences is symmetric. + * If we only want to test the hypothesis that the probability that the sum of a randomly + * chosen pair of differences exceeding zero is 0.5 then no distributional assumption is needed. + *
+ */ + //Wilcoxon signed-rank test + + /** A non-parametric method for testing whether samples originate from the same distribution. + * It is used for comparing more than two samples that are independent, or not related. + * The parametric equivalence of the Kruskal-Wallis test is the one-way analysis of variance (ANOVA). + * The factual null hypothesis is that the populations from which the samples originate, + * have the same median. + * When the Kruskal-Wallis test leads to significant results, + * then at least one of the samples is different from the other samples. + * The test does not identify where the differences occur or how many + * differences actually occur. + * It is an extension of the Mann?Whitney U test to 3 or more groups. + * The Mann-Whitney would help analyze the specific sample pairs for significant differences. + * + *
Since it is a non-parametric method, the Kruskal?Wallis test does not assume a normal distribution, + * unlike the analogous one-way analysis of variance. + * However, the test does assume an identically-shaped and scaled distribution for each group, + * except for any difference in medians. + * */ + // Kruskal-Wallis test. + +} diff --git a/drawing/papaya/src/papaya/Correlation.java b/drawing/papaya/src/papaya/Correlation.java new file mode 100644 index 0000000..b094040 --- /dev/null +++ b/drawing/papaya/src/papaya/Correlation.java @@ -0,0 +1,546 @@ +/** + * papaya: A collection of utilities for Statistics and Matrix-related manipulations + * http://adilapapaya.com/papayastatistics/, 1.1.0 + * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014 + * + * + * Copyright (C) 2014 Adila Faruk http://adilapapaya.com + * Copyright 1999 CERN - European Organization for Nuclear Research. + * Copyright 1995 Stephen L.Moshier. + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA + */ + +package papaya; +import java.util.*; +import java.io.*; +import processing.core.*; + +/** + * Contains utilities related to computing covariances, as well as linear and rank correlation. + * Methods relating to computing + *+ *
+ * Whatever it is, remember that correlation does not always imply causation. + */ +public final class Correlation implements PapayaConstants { + /** + * Makes this class non instantiable, but still let's others inherit from it. + */ + protected Correlation() {} + /** + * Computes the sample autocorrelation by removing the sample mean from the input series, + * then normalizing the sequence by the sample variance. That is, it returns + *- normal correlations and covariances (biased or unbiased) are in the main class. + *
- weighted correlation and covariances are in the {@link Weighted} subclass. + *
- significance of the correlations (i.e. p-values) are in the {@link Significance} subclass. + *
+ * R(lag) = E[ (X[t] - mu) * ( X[t+lag] - mu ) ] / variance(X). + * where + * E[ (X[t] - mu) * ( X[t+lag] - mu ) ] = 1/size(X) * Sum_(i=0 to size-lag)( (X[t]-mu)*X[t+lag]-mu) ). + *+ * Reference: Box, G. E. P., G. M. Jenkins, and G. C. Reinsel. + * Time Series Analysis: Forecasting and Control. 3rd ed. + * Upper Saddle River, NJ: Prentice-Hall, 1994. + *+ * @param data the array of data + * @param lag the lag value. Has to be smaller than the data sequence length + * @param mean the data mean + * @param variance the data variance + * @return the autocorrelation value + */ + public static float auto(float[] data, int lag, float mean, float variance) { + int size = data.length; + if (lag >= size) throw new IllegalArgumentException("Lag is too large"); + if(lag==0) return 1f; + double run = 0; + for( int i=0; i
=1) return 1f; + if(autocorr<=-1) return -1f; + return autocorr; + } + + /** + * Returns the lag-1 autocorrelation of a dataset; + * Note that this method uses computations different from + * auto(data, 1, mean, variance)
. + */ + public static float autoLag1(float[] data, float mean) { + int size = data.length; + double r1 ; + double q = 0 ; + double v = (data[0] - mean) * (data[0] - mean) ; + + for (int i = 1; i < size ; i++) { + double delta0 = (data[i-1] - mean); + double delta1 = (data[i] - mean); + q += (delta0 * delta1 - q)/(i + 1); + v += (delta1 * delta1 - v)/(i + 1); + } + r1 = q / v ; + return (float)r1; + } + /** + * Returns the (Pearson Product Moment) correlation matrix between multiple columns of a matrix + * with each column corresponding to a dataset, and each row an observation. + * That is, givenP
columns,data1, data2, ... , dataP
, + * each of length n, it computes and returns theP-by-P
correlation + * matrix,C
with each element CJK given by + *+ * CJK = CKJ = corr(dataJ,dataK,unbiasedValue). + *+ * @param data The input data. Each column corresponds to a dataset; + * each row corresponds to an observation + * @param unbiased set to true to return the unbiased correlation, + * false to return the biased version. + * @return the correlation matrix (symmetric, by definition) + */ + public static float[][] linear(float[][] data, boolean unbiased){ + int numColumns = data[0].length; + int size = data.length; + float[][] corrMat = new float[numColumns][numColumns]; + float[] data1 = new float[size], data2 = new float[size]; + for(int c1=0; c1+ * corr = cov(x,y,unbiasedValue)/sqrt( cov(x,x, unbiasedValue)*cov(y,y,unbiasedValue) )
+ *
rho
, between multiple columns of a matrix
+ * with each column corresponding to a dataset, and each row an observation.
+ * That is, each pair of columns are first converted to ranks rXJ, rXK
+ * and the correlation between the ranks computed using the Pearson correlation coefficient
+ * formula.
+ * @param unbiased set to true to return the unbiased correlation,
+ * false to return the biased version.
+ */
+ public static float[][] spearman(float[][] data, boolean unbiased){
+
+ int numColumns = data[0].length;
+ int size = data.length;
+ float[][] corrMat = new float[numColumns][numColumns];
+ float[] data1 = new float[size], data2 = new float[size];
+ for(int c1=0; c1tau
between two datasets.
+ * That is, the raw dataset Xi,Yi are first converted to ranks rXi, rYi
+ * and tau
computed as
+ * tau = (nC - nD)/(sqrt(n0-n1)*sqrt(n0-n2))+ * where + *
nC
is the number of concordant pairs
+ * nD
is the number of discordant pairs
+ * n0
is the sum of ranks = n*(n-1)/2
+ * n1
is the sum of tied ranks in dataset 1
+ * n2
is the sum of tied ranks in dataset 2
+ * @param unbiased set to true to return the unbiased correlation,
+ * false to return the biased version.
+ */
+// public static float kendall(float[] x, float[] y, boolean unbiased){
+// int size = x.length;
+// Check.equalLengths(size,y);
+// float[] rX = Rank.rank(x,0);
+// float[] rY = Rank.rank(y,0);
+// //int nConcordant =
+// float rs = linear(rX,rY,unbiased);
+// return rs;
+// }
+
+ /**
+ * Returns the covariance of two data sequences data1
and data2
,
+ * each of length N. That is,
+ * + * cov(data1, data2) = E( (data1[i] - mean(data1))* (data2[i] - mean(data2)) ), + *+ * where E is the mathematical expectation. + *
+ * cov(x,y,true) normalizes by N - 1, if N > 1, where N is the number of observations. + * This makes cov(x,y,true) the best unbiased estimate of the covariance matrix if the + * observations are from a normal distribution. For N = 1, cov(x,y,true) normalizes by N. + *
+ * cov(x,y,false) normalizes by N and produces the second moment matrix of the observations
+ * about their mean.
+ * @param data1 x
+ * @param data2 y
+ * @param unbiased set to true to return the unbiased covariance (division by N-1),
+ * false to return the biased version (division by N).
+ */
+ public static float cov(float[] data1, float[] data2, boolean unbiased) {
+ int size = data1.length;
+ Check.equalLengths(size,data2);
+ Check.size0(size);
+
+ float sumx = data1[0], sumy = data2[0], Sxy=0;
+ for (int i=1; i
+ * cov(data,false) normalizes by N and produces the second moment matrix of the observations
+ * about their mean.
+ * @param data Each column corresponds to a dataset; each row corresponds to an observation
+ * @param unbiased set to true to return the unbiased covariance matrix, false to return the biased version.
+ */
+ public static float[][] cov(float[][] data, boolean unbiased) {
+ int numColumns = data[0].length;
+ int size = data.length;
+ float[] data1 = new float[size], data2 = new float[size];
+ float[][] covMat = new float[numColumns][numColumns];
+ for(int c1=0; c1
+ * Reference:
+ * Covariance of weighted samples, wikipedia.org.
+ * @param data input matrix each column corresponding to a sample, each row an observation.
+ * @param weights the weights to use.
+ * @param unbiased set to true to return the unbiased weighted covariance matrix, false to return the biased version.
+ */
+ public static float[][] cov(float[][] data, float[] weights, boolean unbiased){
+ int size = data.length;
+ int numColumns = data[0].length;
+ float[][] cov = new float[numColumns][numColumns];
+ for(int c1=0; c1
+ * If If you absolutely insist on using this despite the warning above, then the (somewhat messy) hack
+ * around it is to pad each column of your data with 2 additional rows containing the
+ * minimum and maximum that you want to abide by. Just make sure it doesn't muck everything
+ * else up though!
+ */
+public class CorrelationPlot extends Visuals{
+
+
+ protected PApplet myParent;
+
+ /**width and height of the individual plots */
+ private float w;
+ private float h;
+ private int numColumns;
+
+ //CENTER = 3; BOTTOM = 102; TOP = 101; BASELINE = 0; LEFT = 37; RIGHT = 39;
+
+ /** Setup the plot dimensions */
+ public CorrelationPlot(PApplet _theParent, float _plotLeft, float _plotTop, float _plotWidth, float _plotHeight){
+ super(_theParent,_plotLeft,_plotTop,_plotWidth,_plotHeight);
+ myParent = _theParent;
+ bgColor = super.bgColor;
+ }
+
+ /**
+ * Plots the scatter plot of each of the columns of the input data matrix vs. the
+ * other columns. Each of the columns are normalized to their minimum and maximum values which
+ * can be a little misleading. For example, say you are looking at columns specifying the weight
+ * of different panda bears over 100 days.
+ * Panda bear A (Ling-Ling)'s weight varies from 450 to 670 pounds
+ * (she's an emotional eater), while panda bear B (Thomas)'s weight varies from 512 to 516 pounds
+ * (he's not a big fan of bamboo). The correlation plot will not show you this difference
+ * since both could follow the same sort of distribution.
+ *
+ * There are many ways of addressing that, and all of them involve you writing your
+ * own code. :)
+ * If you absolutely insist on using this despite the warning above, then the (very messy) hack
+ * around it is to pad each column of your data with 2 additional rows containing the
+ * minimum and maximum that you want to abide by. Just make sure it doesn't muck everything
+ * else up though!
+ *
+ * @param data the data array with each column corresponding to a dataset
+ * @param pos 0 for top right plots, 1 for bottom left plots
+ * @param fColor the fill color for the dots on the plot.
+ */
+ public void drawPlot(float[][] data, int pos, int fColor){
+ if(pos!=0 && pos!=1)
+ throw new IllegalArgumentException("Set pos to 0 for top right plots or 1 for bottom left plots.");
+ if(numColumns!=data[0].length)
+ throw new IllegalArgumentException("Number of categories doesn't match the number of columns in the matrix.");
+
+ w = plotWidth/numColumns;
+ h = plotHeight/numColumns;
+
+ float x,y;
+
+ for(int r=0; r
+ * Distinct values are filled into
+ *
+ * @return the updated values filled into the
+ * This method computes those updated values without needing to know the already recorded elements.
+ * This is interesting for interactive online monitoring and/or applications that cannot keep the entire huge data sequence in memory.
+ * For example, the incremental computation of moments is based upon such sums of powers:
+ *
+ * The moment of
+ *
+ * @param data the additional elements to be incorporated into min, max, etc.
+ * @param from the index of the first element within
+ * This method computes those updated values without needing to know the already recorded elements.
+ * This is interesting for interactive online monitoring and/or applications that cannot keep the entire huge data sequence in memory.
+ *
+ *
+ * @param size1 the number of elements in data sequence 1.
+ * @param mean1 the mean of data sequence 1.
+ * @param size2 the number of elements in data sequence 2.
+ * @param mean2 the mean of data sequence 2.
+ */
+ public static float mean(float mean1, int size1, float mean2, int size2) {
+ return (size1 * mean1 + size2 * mean2) / (size1 + size2);
+ }
+ /**
+ * Returns the pooled mean of a
+ *
+ */
+ public static float mean(float[] means, int[] sizes) {
+ int size = sizes.length;
+ Check.equalLengths(size,means);
+ float sum=0, sumSizes=0;
+ for(int i=0; i Details: If no element is ≤ element, then the rank is zero. If the element lies in between two contained elements, then
+ * linear interpolation is used and a non integer value is returned.
+ * Ref: R.R. Sokal, F.J. Rohlf, Biometry: the principles and practice of statistics
+ * in biological research (W.H. Freeman and Company, New York, 1998, 3rd edition)
+ * p. 114-115.
+ *
+ * @param size the number of elements of the data sequence.
+ * @param moment4 the fourth central moment, which is
+ * Ref: R.R. Sokal, F.J. Rohlf, Biometry: the principles and practice of statistics
+ * in biological research (W.H. Freeman and Company, New York, 1998, 3rd edition)
+ * p. 138.
+ * @param size the number of elements of the data sequence.
+ */
+ public static float sampleKurtosisStandardError(int size) {
+ float n=size; // set to float to avoid NaNs.
+ return (float)Math.sqrt( 24.0*n*(n-1)*(n-1)/((n-3)*(n-2)*(n+3)*(n+5)) );
+ }
+ /**
+ * Returns the sample skew of a data sequence. That is
+ *
+ * Ref: R.R. Sokal, F.J. Rohlf, Biometry: the principles and practice of statistics
+ * in biological research (W.H. Freeman and Company, New York, 1998, 3rd edition)
+ * p. 114-115.
+ * @param size the number of elements of the data sequence.
+ * @param moment3 the third central moment, which is
+ * Ref: R.R. Sokal, F.J. Rohlf, Biometry: the principles and practice of statistics
+ * in biological research (W.H. Freeman and Company, New York, 1998, 3rd edition)
+ * p. 138.
+ *
+ * @param size the number of elements of the data sequence.
+ */
+ public static float sampleSkewStandardError(int size) {
+ float n=size;
+ return (float)Math.sqrt( 6.0*n*(n-1)/((n-2)*(n+1)*(n+3)) );
+ }
+ }
+
+ /**
+ * Returns the skew of a data sequence when the 3rd moment has already been computed.
+ * @param moment3 the third central moment, which is
+ * std(data,unbiased==true) returns sigma_1 above, while std(data,unbiased==false) returns sigma_2.
+ * @param data the dataset
+ * @param unbiased set to true to return the unbiased standard deviation,
+ * false to return the biased version.
+ */
+ public static float std(float[] data, boolean unbiased) {
+ return (float)Math.sqrt(var(data,unbiased));
+ }
+
+
+ /**
+ * Returns an array with each element of the array corresponding to
+ * the standard deviations of each column of the input
+ * matrix. Each column of the matrix corresponds to a dataset, and each row an observation.
+ *
+ * There are two definitions of the standard deviation:
+ *
+ * std(data,unbiased==true) returns sigma_1 above, while std(data,unbiased==false) returns sigma_2.
+ *
+ * @param data the dataset
+ * @param unbiased set to true to return the unbiased standard deviation,
+ * false to return the biased version.
+ */
+ public static float[] std(float[][] data, boolean unbiased) {
+ int numColumns = data[0].length;
+ float[] std = new float[numColumns];
+ float[] dataEach = new float[data.length];
+ float variance = 0;
+ for(int i=0; i See also this entry on wikipedia.org
+ *
+ * @param size the number of elements of the data sequence.
+ * @param sampleVariance the sample variance.
+ */
+ public static float stdUnbiased(int size, float sampleVariance) {
+ double s, Cn;
+ int n=size;
+ // The standard deviation calculated as the sqrt of the variance underestimates
+ // the unbiased standard deviation.
+ s=Math.sqrt(sampleVariance);
+ // It needs to be multiplied by this correction factor.
+ if (n>30) {
+ Cn = 1 + 1.0/(4*(n-1)); // Cn = 1+1/(4*(n-1));
+ } else {
+ Cn = Math.sqrt((n-1)*0.5)*Gamma.gamma((n-1)*0.5)/Gamma.gamma(n*0.5);
+ }
+ return (float) (Cn*s);
+ }
+
+ /** Methods for computing various different sums of datasets such as sum of inversions,
+ * logs, products, power deviations, squares, etc. */
+
+ public static class Sum{
+ protected Sum(){};
+ /**
+ * Returns the sum of inversions of a data sequence,
+ * which is
+ * var(x,true) normalizes V by N - 1 if N > 1, where N is the sample size.
+ * This is an unbiased estimator of the variance of the population from which X is drawn,
+ * as long as X consists of independent, identically distributed samples. For N = 1,
+ * V is normalized by 1.
+ *
+ * V = var(x,false) normalizes by N and produces the second moment of the sample about its mean.
+ *
+ * Reference:
+ *
+ * var(X,unbiased=true) normalizes V by N - 1 if N > 1, where N is the sample size.
+ * This is an unbiased estimator of the variance of the population from which X is drawn,
+ * as long as X consists of independent, identically distributed samples. For N = 1,
+ * V is normalized by 1.
+ *
+ * V = var(X,unbiased=false) normalizes by N and produces the second moment of the sample about its mean.
+ *
+ * Reference:
+ *
+ * formula: var(data) = (Sum.squares(data) - mean(data)^2) / size
+ */
+ protected static float varianceNotNormalized(float[] data){
+ float size = data.length;
+ float mean = data[0];
+ float varianceSum = 0;
+ for(int i = 1; i < size; ++i) {
+ float stepSum = data[i]-mean; // deviation of data from previous mean
+ mean += stepSum/(i+1); // mean_at_step_k = mean_at_step_k-1 + (data[k] - mean_at_step_k-1) / k
+ varianceSum+=stepSum*(data[i]-mean);
+ }
+ return varianceSum;
+ }
+
+
+ /**
+ * Contains methods related to weighted datasets.
+ */
+ public static class Weighted{
+ protected Weighted(){}
+ /**
+ * Returns the weighted mean of a data sequence. Used when the values in a set of
+ * data do not share equal importance.
+ * That is The formula above agrees with that presented in
+ *
+ * the NIST Information Technology Laboratory page.
+ * The algorithm used, is a one-pass formula credited to
+ * "D. H. D. West (1979). Communications of the ACM, 22, 9, 532-535:
+ * Updating Mean and Variance Estimates: An Improved Method", and spelled out under the
+ *
+ * Rows of X correspond to observations, and columns correspond to variables.
+ * Returns the m-by-m matrix D with D[r1][r2] corresponding to the cityblock
+ * (or manhattan) distance between the rows of the input X.
+ */
+ public static float[][] cityblock(float[][] X){
+ int numRows = X.length; int numColumns = X[0].length;
+ float[][] distMatrix = new float[numRows][numRows];
+ double val = 0.0;
+ for(int r = 0; r
+ * Rows of X correspond to observations, and columns correspond to variables.
+ * Returns the m-by-m matrix D with D[r1][r2] corresponding to the Chebychev
+ * distance between the rows of the input X.
+ */
+ public static float[][] chebychev(float[][] X){
+ int numRows = X.length; int numColumns = X[0].length;
+ float[][] distMatrix = new float[numRows][numRows];
+ double val = 0.0;
+ for(int r = 0; r
+ * Rows of X correspond to observations, and columns correspond to variables.
+ * Returns the m-by-m matrix D with D[r1][r2] corresponding to the correlation
+ * distance between the rows of the input X.
+ */
+ public static float[][] correlation(float[][] X){
+ int numRows = X.length; int numColumns = X[0].length;
+ float[][] distMatrix = new float[numRows][numRows];
+ float[] mean = new float[numRows]; float[] std = new float[numRows];
+ for(int i=0; i
+ * Rows of X correspond to observations, and columns correspond to variables.
+ * Returns the m-by-m matrix D with D[r1][r2] corresponding to the cosine
+ * distance between the rows of the input X.
+ */
+ public static float[][] cosine(float[][] X){
+ int numRows = X.length; int numColumns = X[0].length;
+ float[][] distMatrix = new float[numRows][numRows];
+ float[] mag = new float[numRows];
+ for(int i=0; i
+ * Rows of X correspond to observations, and columns correspond to variables.
+ * Returns the m-by-m matrix D with D[r1][r2] corresponding to the Euclidean
+ * distance between the rows of the input X.
+ */
+ public static float[][] euclidean(float[][] X){
+ int numRows = X.length; int numColumns = X[0].length;
+ float[][] distMatrix = new float[numRows][numRows];
+ double val = 0.0;
+ for(int r = 0; r
+ * Rows of X correspond to observations, and columns correspond to variables.
+ * Returns the m-by-m matrix D with D[r1][r2] corresponding to the standardized
+ * Euclidean distance between the rows of the input X.
+ */
+ public static float[][] seuclidean(float[][] X){
+ float[] means = Descriptive.Mean.columnMean(X);
+ float[] stds = Descriptive.std(X,true);//unbiased
+ float[][] distMatrix = euclidean(Descriptive.zScore(X,means,stds));
+ return distMatrix;
+ }
+ /**
+ * Computes the Mahalanobis distance matrix of the m-by-n input matrix X. That is,
+ *
+ * Rows of X correspond to observations, and columns correspond to variables.
+ * Returns the m-by-m matrix D with D[r1][r2] corresponding to the Spearman
+ * distance between the rows of the input X.
+ */
+ public static float[][] spearman(float[][] X){
+ int numRows = X.length; int numColumns = X[0].length;
+ float[][] distMatrix = new float[numRows][numRows];
+ // get the rank for all rows
+ float[][] rank = new float[numRows][numColumns];
+ float mean = (numColumns+1)/2;
+ double[] std = new double[numRows];
+ for(int i=0; i
+ If A is symmetric, then A = V*D*V' where the eigenvalue matrix D is
+ diagonal and the eigenvector matrix V is orthogonal.
+ I.e. A = V.times(D.times(V.transpose())) and
+ V.times(V.transpose()) equals the identity matrix.
+
+ If A is not symmetric, then the eigenvalue matrix D is block diagonal
+ with the real eigenvalues in 1-by-1 blocks and any complex eigenvalues,
+ lambda + i*mu, in 2-by-2 blocks, [lambda, mu; -mu, lambda]. The
+ columns of V represent the eigenvectors in the sense that A*V = V*D,
+ i.e. A.times(V) equals V.times(D). The matrix V may be badly
+ conditioned, or even singular, so the validity of the equation
+ A = V*D*inverse(V) depends upon V.cond().
+
+Shamelessly copied (and modified) from the
+JAMA Java
+Matrix package. To make things compatible with how most users use Processing, the
+class take in float matrices. However, to preserve the acccuracy of the computations, the algorithm
+first casts the input into a double array, prior to doing anything. All methods also return doubles; Use
+{@link Cast#doubleToFloat(double[][])} if you want/need to cast everything back to floats for
+further (non-high-accuracy-dependant) processing (pun intended).
+ */
+
+
+public class Eigenvalue {
+
+/* ------------------------
+ Class variables
+ * ------------------------ */
+
+ /** Row and column dimension (square matrix).
+ @serial matrix dimension.
+ */
+ private int n;
+
+ /** Symmetry flag.
+ @serial internal symmetry flag.
+ */
+ private boolean issymmetric;
+
+ /** Arrays for internal storage of eigenvalues.
+ @serial internal storage of eigenvalues.
+ */
+ private double[] d, e;
+
+ /** Array for internal storage of eigenvectors.
+ @serial internal storage of eigenvectors.
+ */
+ private double[][] V;
+
+ /** Array for internal storage of nonsymmetric Hessenberg form.
+ @serial internal storage of nonsymmetric Hessenberg form.
+ */
+ private double[][] H;
+
+ /** Working storage for nonsymmetric algorithm.
+ @serial working storage for nonsymmetric algorithm.
+ */
+ private double[] ort;
+
+/* ------------------------
+ Private Methods
+ * ------------------------ */
+
+ // Symmetric Householder reduction to tridiagonal form.
+
+ private void tred2 () {
+
+ // This is derived from the Algol procedures tred2 by
+ // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
+ // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ for (int j = 0; j < n; j++) {
+ d[j] = V[n-1][j];
+ }
+
+ // Householder reduction to tridiagonal form.
+
+ for (int i = n-1; i > 0; i--) {
+
+ // Scale to avoid under/overflow.
+
+ double scale = 0.0;
+ double h = 0.0;
+ for (int k = 0; k < i; k++) {
+ scale = scale + Math.abs(d[k]);
+ }
+ if (scale == 0.0) {
+ e[i] = d[i-1];
+ for (int j = 0; j < i; j++) {
+ d[j] = V[i-1][j];
+ V[i][j] = 0.0;
+ V[j][i] = 0.0;
+ }
+ } else {
+
+ // Generate Householder vector.
+
+ for (int k = 0; k < i; k++) {
+ d[k] /= scale;
+ h += d[k] * d[k];
+ }
+ double f = d[i-1];
+ double g = Math.sqrt(h);
+ if (f > 0) {
+ g = -g;
+ }
+ e[i] = scale * g;
+ h = h - f * g;
+ d[i-1] = f - g;
+ for (int j = 0; j < i; j++) {
+ e[j] = 0.0;
+ }
+
+ // Apply similarity transformation to remaining columns.
+
+ for (int j = 0; j < i; j++) {
+ f = d[j];
+ V[j][i] = f;
+ g = e[j] + V[j][j] * f;
+ for (int k = j+1; k <= i-1; k++) {
+ g += V[k][j] * d[k];
+ e[k] += V[k][j] * f;
+ }
+ e[j] = g;
+ }
+ f = 0.0;
+ for (int j = 0; j < i; j++) {
+ e[j] /= h;
+ f += e[j] * d[j];
+ }
+ double hh = f / (h + h);
+ for (int j = 0; j < i; j++) {
+ e[j] -= hh * d[j];
+ }
+ for (int j = 0; j < i; j++) {
+ f = d[j];
+ g = e[j];
+ for (int k = j; k <= i-1; k++) {
+ V[k][j] -= (f * e[k] + g * d[k]);
+ }
+ d[j] = V[i-1][j];
+ V[i][j] = 0.0;
+ }
+ }
+ d[i] = h;
+ }
+
+ // Accumulate transformations.
+
+ for (int i = 0; i < n-1; i++) {
+ V[n-1][i] = V[i][i];
+ V[i][i] = 1.0;
+ double h = d[i+1];
+ if (h != 0.0) {
+ for (int k = 0; k <= i; k++) {
+ d[k] = V[k][i+1] / h;
+ }
+ for (int j = 0; j <= i; j++) {
+ double g = 0.0;
+ for (int k = 0; k <= i; k++) {
+ g += V[k][i+1] * V[k][j];
+ }
+ for (int k = 0; k <= i; k++) {
+ V[k][j] -= g * d[k];
+ }
+ }
+ }
+ for (int k = 0; k <= i; k++) {
+ V[k][i+1] = 0.0;
+ }
+ }
+ for (int j = 0; j < n; j++) {
+ d[j] = V[n-1][j];
+ V[n-1][j] = 0.0;
+ }
+ V[n-1][n-1] = 1.0;
+ e[0] = 0.0;
+ }
+
+ // Symmetric tridiagonal QL algorithm.
+
+ private void tql2 () {
+
+ // This is derived from the Algol procedures tql2, by
+ // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for
+ // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ for (int i = 1; i < n; i++) {
+ e[i-1] = e[i];
+ }
+ e[n-1] = 0.0;
+
+ double f = 0.0;
+ double tst1 = 0.0;
+ double eps = Math.pow(2.0,-52.0);
+ for (int l = 0; l < n; l++) {
+
+ // Find small subdiagonal element
+
+ tst1 = Math.max(tst1,Math.abs(d[l]) + Math.abs(e[l]));
+ int m = l;
+ while (m < n) {
+ if (Math.abs(e[m]) <= eps*tst1) {
+ break;
+ }
+ m++;
+ }
+
+ // If m == l, d[l] is an eigenvalue,
+ // otherwise, iterate.
+
+ if (m > l) {
+ int iter = 0;
+ do {
+ iter = iter + 1; // (Could check iteration count here.)
+
+ // Compute implicit shift
+
+ double g = d[l];
+ double p = (d[l+1] - g) / (2.0 * e[l]);
+ double r = SVD.hypot(p,1.0);
+ if (p < 0) {
+ r = -r;
+ }
+ d[l] = e[l] / (p + r);
+ d[l+1] = e[l] * (p + r);
+ double dl1 = d[l+1];
+ double h = g - d[l];
+ for (int i = l+2; i < n; i++) {
+ d[i] -= h;
+ }
+ f = f + h;
+
+ // Implicit QL transformation.
+
+ p = d[m];
+ double c = 1.0;
+ double c2 = c;
+ double c3 = c;
+ double el1 = e[l+1];
+ double s = 0.0;
+ double s2 = 0.0;
+ for (int i = m-1; i >= l; i--) {
+ c3 = c2;
+ c2 = c;
+ s2 = s;
+ g = c * e[i];
+ h = c * p;
+ r = SVD.hypot(p,e[i]);
+ e[i+1] = s * r;
+ s = e[i] / r;
+ c = p / r;
+ p = c * d[i] - s * g;
+ d[i+1] = h + s * (c * g + s * d[i]);
+
+ // Accumulate transformation.
+
+ for (int k = 0; k < n; k++) {
+ h = V[k][i+1];
+ V[k][i+1] = s * V[k][i] + c * h;
+ V[k][i] = c * V[k][i] - s * h;
+ }
+ }
+ p = -s * s2 * c3 * el1 * e[l] / dl1;
+ e[l] = s * p;
+ d[l] = c * p;
+
+ // Check for convergence.
+
+ } while (Math.abs(e[l]) > eps*tst1);
+ }
+ d[l] = d[l] + f;
+ e[l] = 0.0;
+ }
+
+ // Sort eigenvalues and corresponding vectors.
+
+ for (int i = 0; i < n-1; i++) {
+ int k = i;
+ double p = d[i];
+ for (int j = i+1; j < n; j++) {
+ if (d[j] < p) {
+ k = j;
+ p = d[j];
+ }
+ }
+ if (k != i) {
+ d[k] = d[i];
+ d[i] = p;
+ for (int j = 0; j < n; j++) {
+ p = V[j][i];
+ V[j][i] = V[j][k];
+ V[j][k] = p;
+ }
+ }
+ }
+ }
+
+ // Nonsymmetric reduction to Hessenberg form.
+
+ private void orthes () {
+
+ // This is derived from the Algol procedures orthes and ortran,
+ // by Martin and Wilkinson, Handbook for Auto. Comp.,
+ // Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutines in EISPACK.
+
+ int low = 0;
+ int high = n-1;
+
+ for (int m = low+1; m <= high-1; m++) {
+
+ // Scale column.
+
+ double scale = 0.0;
+ for (int i = m; i <= high; i++) {
+ scale = scale + Math.abs(H[i][m-1]);
+ }
+ if (scale != 0.0) {
+
+ // Compute Householder transformation.
+
+ double h = 0.0;
+ for (int i = high; i >= m; i--) {
+ ort[i] = H[i][m-1]/scale;
+ h += ort[i] * ort[i];
+ }
+ double g = Math.sqrt(h);
+ if (ort[m] > 0) {
+ g = -g;
+ }
+ h = h - ort[m] * g;
+ ort[m] = ort[m] - g;
+
+ // Apply Householder similarity transformation
+ // H = (I-u*u'/h)*H*(I-u*u')/h)
+
+ for (int j = m; j < n; j++) {
+ double f = 0.0;
+ for (int i = high; i >= m; i--) {
+ f += ort[i]*H[i][j];
+ }
+ f = f/h;
+ for (int i = m; i <= high; i++) {
+ H[i][j] -= f*ort[i];
+ }
+ }
+
+ for (int i = 0; i <= high; i++) {
+ double f = 0.0;
+ for (int j = high; j >= m; j--) {
+ f += ort[j]*H[i][j];
+ }
+ f = f/h;
+ for (int j = m; j <= high; j++) {
+ H[i][j] -= f*ort[j];
+ }
+ }
+ ort[m] = scale*ort[m];
+ H[m][m-1] = scale*g;
+ }
+ }
+
+ // Accumulate transformations (Algol's ortran).
+
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < n; j++) {
+ V[i][j] = (i == j ? 1.0 : 0.0);
+ }
+ }
+
+ for (int m = high-1; m >= low+1; m--) {
+ if (H[m][m-1] != 0.0) {
+ for (int i = m+1; i <= high; i++) {
+ ort[i] = H[i][m-1];
+ }
+ for (int j = m; j <= high; j++) {
+ double g = 0.0;
+ for (int i = m; i <= high; i++) {
+ g += ort[i] * V[i][j];
+ }
+ // Double division avoids possible underflow
+ g = (g / ort[m]) / H[m][m-1];
+ for (int i = m; i <= high; i++) {
+ V[i][j] += g * ort[i];
+ }
+ }
+ }
+ }
+ }
+
+
+ // Complex scalar division.
+
+ private transient double cdivr, cdivi;
+ private void cdiv(double xr, double xi, double yr, double yi) {
+ double r,d;
+ if (Math.abs(yr) > Math.abs(yi)) {
+ r = yi/yr;
+ d = yr + r*yi;
+ cdivr = (xr + r*xi)/d;
+ cdivi = (xi - r*xr)/d;
+ } else {
+ r = yr/yi;
+ d = yi + r*yr;
+ cdivr = (r*xr + xi)/d;
+ cdivi = (r*xi - xr)/d;
+ }
+ }
+
+
+ // Nonsymmetric reduction from Hessenberg to real Schur form.
+
+ private void hqr2 () {
+
+ // This is derived from the Algol procedure hqr2,
+ // by Martin and Wilkinson, Handbook for Auto. Comp.,
+ // Vol.ii-Linear Algebra, and the corresponding
+ // Fortran subroutine in EISPACK.
+
+ // Initialize
+
+ int nn = this.n;
+ int n = nn-1;
+ int low = 0;
+ int high = nn-1;
+ double eps = Math.pow(2.0,-52.0);
+ double exshift = 0.0;
+ double p=0,q=0,r=0,s=0,z=0,t,w,x,y;
+
+ // Store roots isolated by balanc and compute matrix norm
+
+ double norm = 0.0;
+ for (int i = 0; i < nn; i++) {
+ if (i < low | i > high) {
+ d[i] = H[i][i];
+ e[i] = 0.0;
+ }
+ for (int j = Math.max(i-1,0); j < nn; j++) {
+ norm = norm + Math.abs(H[i][j]);
+ }
+ }
+
+ // Outer loop over eigenvalue index
+
+ int iter = 0;
+ while (n >= low) {
+
+ // Look for single small sub-diagonal element
+
+ int l = n;
+ while (l > low) {
+ s = Math.abs(H[l-1][l-1]) + Math.abs(H[l][l]);
+ if (s == 0.0) {
+ s = norm;
+ }
+ if (Math.abs(H[l][l-1]) < eps * s) {
+ break;
+ }
+ l--;
+ }
+
+ // Check for convergence
+ // One root found
+
+ if (l == n) {
+ H[n][n] = H[n][n] + exshift;
+ d[n] = H[n][n];
+ e[n] = 0.0;
+ n--;
+ iter = 0;
+
+ // Two roots found
+
+ } else if (l == n-1) {
+ w = H[n][n-1] * H[n-1][n];
+ p = (H[n-1][n-1] - H[n][n]) / 2.0;
+ q = p * p + w;
+ z = Math.sqrt(Math.abs(q));
+ H[n][n] = H[n][n] + exshift;
+ H[n-1][n-1] = H[n-1][n-1] + exshift;
+ x = H[n][n];
+
+ // Real pair
+
+ if (q >= 0) {
+ if (p >= 0) {
+ z = p + z;
+ } else {
+ z = p - z;
+ }
+ d[n-1] = x + z;
+ d[n] = d[n-1];
+ if (z != 0.0) {
+ d[n] = x - w / z;
+ }
+ e[n-1] = 0.0;
+ e[n] = 0.0;
+ x = H[n][n-1];
+ s = Math.abs(x) + Math.abs(z);
+ p = x / s;
+ q = z / s;
+ r = Math.sqrt(p * p+q * q);
+ p = p / r;
+ q = q / r;
+
+ // Row modification
+
+ for (int j = n-1; j < nn; j++) {
+ z = H[n-1][j];
+ H[n-1][j] = q * z + p * H[n][j];
+ H[n][j] = q * H[n][j] - p * z;
+ }
+
+ // Column modification
+
+ for (int i = 0; i <= n; i++) {
+ z = H[i][n-1];
+ H[i][n-1] = q * z + p * H[i][n];
+ H[i][n] = q * H[i][n] - p * z;
+ }
+
+ // Accumulate transformations
+
+ for (int i = low; i <= high; i++) {
+ z = V[i][n-1];
+ V[i][n-1] = q * z + p * V[i][n];
+ V[i][n] = q * V[i][n] - p * z;
+ }
+
+ // Complex pair
+
+ } else {
+ d[n-1] = x + p;
+ d[n] = x + p;
+ e[n-1] = z;
+ e[n] = -z;
+ }
+ n = n - 2;
+ iter = 0;
+
+ // No convergence yet
+
+ } else {
+
+ // Form shift
+
+ x = H[n][n];
+ y = 0.0;
+ w = 0.0;
+ if (l < n) {
+ y = H[n-1][n-1];
+ w = H[n][n-1] * H[n-1][n];
+ }
+
+ // Wilkinson's original ad hoc shift
+
+ if (iter == 10) {
+ exshift += x;
+ for (int i = low; i <= n; i++) {
+ H[i][i] -= x;
+ }
+ s = Math.abs(H[n][n-1]) + Math.abs(H[n-1][n-2]);
+ x = y = 0.75 * s;
+ w = -0.4375 * s * s;
+ }
+
+ // MATLAB's new ad hoc shift
+
+ if (iter == 30) {
+ s = (y - x) / 2.0;
+ s = s * s + w;
+ if (s > 0) {
+ s = Math.sqrt(s);
+ if (y < x) {
+ s = -s;
+ }
+ s = x - w / ((y - x) / 2.0 + s);
+ for (int i = low; i <= n; i++) {
+ H[i][i] -= s;
+ }
+ exshift += s;
+ x = y = w = 0.964;
+ }
+ }
+
+ iter = iter + 1; // (Could check iteration count here.)
+
+ // Look for two consecutive small sub-diagonal elements
+
+ int m = n-2;
+ while (m >= l) {
+ z = H[m][m];
+ r = x - z;
+ s = y - z;
+ p = (r * s - w) / H[m+1][m] + H[m][m+1];
+ q = H[m+1][m+1] - z - r - s;
+ r = H[m+2][m+1];
+ s = Math.abs(p) + Math.abs(q) + Math.abs(r);
+ p = p / s;
+ q = q / s;
+ r = r / s;
+ if (m == l) {
+ break;
+ }
+ if (Math.abs(H[m][m-1]) * (Math.abs(q) + Math.abs(r)) <
+ eps * (Math.abs(p) * (Math.abs(H[m-1][m-1]) + Math.abs(z) +
+ Math.abs(H[m+1][m+1])))) {
+ break;
+ }
+ m--;
+ }
+
+ for (int i = m+2; i <= n; i++) {
+ H[i][i-2] = 0.0;
+ if (i > m+2) {
+ H[i][i-3] = 0.0;
+ }
+ }
+
+ // Double QR step involving rows l:n and columns m:n
+
+ for (int k = m; k <= n-1; k++) {
+ boolean notlast = (k != n-1);
+ if (k != m) {
+ p = H[k][k-1];
+ q = H[k+1][k-1];
+ r = (notlast ? H[k+2][k-1] : 0.0);
+ x = Math.abs(p) + Math.abs(q) + Math.abs(r);
+ if (x != 0.0) {
+ p = p / x;
+ q = q / x;
+ r = r / x;
+ }
+ }
+ if (x == 0.0) {
+ break;
+ }
+ s = Math.sqrt(p * p + q * q + r * r);
+ if (p < 0) {
+ s = -s;
+ }
+ if (s != 0) {
+ if (k != m) {
+ H[k][k-1] = -s * x;
+ } else if (l != m) {
+ H[k][k-1] = -H[k][k-1];
+ }
+ p = p + s;
+ x = p / s;
+ y = q / s;
+ z = r / s;
+ q = q / p;
+ r = r / p;
+
+ // Row modification
+
+ for (int j = k; j < nn; j++) {
+ p = H[k][j] + q * H[k+1][j];
+ if (notlast) {
+ p = p + r * H[k+2][j];
+ H[k+2][j] = H[k+2][j] - p * z;
+ }
+ H[k][j] = H[k][j] - p * x;
+ H[k+1][j] = H[k+1][j] - p * y;
+ }
+
+ // Column modification
+
+ for (int i = 0; i <= Math.min(n,k+3); i++) {
+ p = x * H[i][k] + y * H[i][k+1];
+ if (notlast) {
+ p = p + z * H[i][k+2];
+ H[i][k+2] = H[i][k+2] - p * r;
+ }
+ H[i][k] = H[i][k] - p;
+ H[i][k+1] = H[i][k+1] - p * q;
+ }
+
+ // Accumulate transformations
+
+ for (int i = low; i <= high; i++) {
+ p = x * V[i][k] + y * V[i][k+1];
+ if (notlast) {
+ p = p + z * V[i][k+2];
+ V[i][k+2] = V[i][k+2] - p * r;
+ }
+ V[i][k] = V[i][k] - p;
+ V[i][k+1] = V[i][k+1] - p * q;
+ }
+ } // (s != 0)
+ } // k loop
+ } // check convergence
+ } // while (n >= low)
+
+ // Backsubstitute to find vectors of upper triangular form
+
+ if (norm == 0.0) {
+ return;
+ }
+
+ for (n = nn-1; n >= 0; n--) {
+ p = d[n];
+ q = e[n];
+
+ // Real vector
+
+ if (q == 0) {
+ int l = n;
+ H[n][n] = 1.0;
+ for (int i = n-1; i >= 0; i--) {
+ w = H[i][i] - p;
+ r = 0.0;
+ for (int j = l; j <= n; j++) {
+ r = r + H[i][j] * H[j][n];
+ }
+ if (e[i] < 0.0) {
+ z = w;
+ s = r;
+ } else {
+ l = i;
+ if (e[i] == 0.0) {
+ if (w != 0.0) {
+ H[i][n] = -r / w;
+ } else {
+ H[i][n] = -r / (eps * norm);
+ }
+
+ // Solve real equations
+
+ } else {
+ x = H[i][i+1];
+ y = H[i+1][i];
+ q = (d[i] - p) * (d[i] - p) + e[i] * e[i];
+ t = (x * s - z * r) / q;
+ H[i][n] = t;
+ if (Math.abs(x) > Math.abs(z)) {
+ H[i+1][n] = (-r - w * t) / x;
+ } else {
+ H[i+1][n] = (-s - y * t) / z;
+ }
+ }
+
+ // Overflow control
+
+ t = Math.abs(H[i][n]);
+ if ((eps * t) * t > 1) {
+ for (int j = i; j <= n; j++) {
+ H[j][n] = H[j][n] / t;
+ }
+ }
+ }
+ }
+
+ // Complex vector
+
+ } else if (q < 0) {
+ int l = n-1;
+
+ // Last vector component imaginary so matrix is triangular
+
+ if (Math.abs(H[n][n-1]) > Math.abs(H[n-1][n])) {
+ H[n-1][n-1] = q / H[n][n-1];
+ H[n-1][n] = -(H[n][n] - p) / H[n][n-1];
+ } else {
+ cdiv(0.0,-H[n-1][n],H[n-1][n-1]-p,q);
+ H[n-1][n-1] = cdivr;
+ H[n-1][n] = cdivi;
+ }
+ H[n][n-1] = 0.0;
+ H[n][n] = 1.0;
+ for (int i = n-2; i >= 0; i--) {
+ double ra,sa,vr,vi;
+ ra = 0.0;
+ sa = 0.0;
+ for (int j = l; j <= n; j++) {
+ ra = ra + H[i][j] * H[j][n-1];
+ sa = sa + H[i][j] * H[j][n];
+ }
+ w = H[i][i] - p;
+
+ if (e[i] < 0.0) {
+ z = w;
+ r = ra;
+ s = sa;
+ } else {
+ l = i;
+ if (e[i] == 0) {
+ cdiv(-ra,-sa,w,q);
+ H[i][n-1] = cdivr;
+ H[i][n] = cdivi;
+ } else {
+
+ // Solve complex equations
+
+ x = H[i][i+1];
+ y = H[i+1][i];
+ vr = (d[i] - p) * (d[i] - p) + e[i] * e[i] - q * q;
+ vi = (d[i] - p) * 2.0 * q;
+ if (vr == 0.0 & vi == 0.0) {
+ vr = eps * norm * (Math.abs(w) + Math.abs(q) +
+ Math.abs(x) + Math.abs(y) + Math.abs(z));
+ }
+ cdiv(x*r-z*ra+q*sa,x*s-z*sa-q*ra,vr,vi);
+ H[i][n-1] = cdivr;
+ H[i][n] = cdivi;
+ if (Math.abs(x) > (Math.abs(z) + Math.abs(q))) {
+ H[i+1][n-1] = (-ra - w * H[i][n-1] + q * H[i][n]) / x;
+ H[i+1][n] = (-sa - w * H[i][n] - q * H[i][n-1]) / x;
+ } else {
+ cdiv(-r-y*H[i][n-1],-s-y*H[i][n],z,q);
+ H[i+1][n-1] = cdivr;
+ H[i+1][n] = cdivi;
+ }
+ }
+
+ // Overflow control
+
+ t = Math.max(Math.abs(H[i][n-1]),Math.abs(H[i][n]));
+ if ((eps * t) * t > 1) {
+ for (int j = i; j <= n; j++) {
+ H[j][n-1] = H[j][n-1] / t;
+ H[j][n] = H[j][n] / t;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // Vectors of isolated roots
+
+ for (int i = 0; i < nn; i++) {
+ if (i < low | i > high) {
+ for (int j = i; j < nn; j++) {
+ V[i][j] = H[i][j];
+ }
+ }
+ }
+
+ // Back transformation to get eigenvectors of original matrix
+
+ for (int j = nn-1; j >= low; j--) {
+ for (int i = low; i <= high; i++) {
+ z = 0.0;
+ for (int k = low; k <= Math.min(j,high); k++) {
+ z = z + V[i][k] * H[k][j];
+ }
+ V[i][j] = z;
+ }
+ }
+ }
+
+
+/* ------------------------
+ Constructor
+ * ------------------------ */
+
+ /** Check for symmetry, then construct the eigenvalue decomposition.
+ * Upon initialization, the matrices D and V, as well as the vectors containing the
+ * real and imaginary eigenvalues are computed and stored.
+ * @see getV()
+ * @see getD()
+ * @see getRealEigenvalues()
+ * @see getImagEigenvalues()
+ */
+
+ public Eigenvalue (float[][] Arg) {
+ double[][] A = Cast.floatToDouble(Arg);
+ n = Arg[0].length;
+ V = new double[n][n];
+ d = new double[n];
+ e = new double[n];
+
+ issymmetric = true;
+ for (int j = 0; (j < n) & issymmetric; j++) {
+ for (int i = 0; (i < n) & issymmetric; i++) {
+ issymmetric = (A[i][j] == A[j][i]);
+ }
+ }
+
+ if (issymmetric) {
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < n; j++) {
+ V[i][j] = A[i][j];
+ }
+ }
+
+ // Tridiagonalize.
+ tred2();
+
+ // Diagonalize.
+ tql2();
+
+ } else {
+ H = new double[n][n];
+ ort = new double[n];
+
+ for (int j = 0; j < n; j++) {
+ for (int i = 0; i < n; i++) {
+ H[i][j] = A[i][j];
+ }
+ }
+
+ // Reduce to Hessenberg form.
+ orthes();
+
+ // Reduce Hessenberg to real Schur form.
+ hqr2();
+ }
+ }
+
+/* ------------------------
+ Public Methods
+ * ------------------------ */
+
+ /** Return the eigenvector matrix with each column corresponding to
+ * an eigenvector of the input matrix.
+ @return V
+ */
+
+ public double[][] getV () {
+ return V;
+ }
+
+ /** Return the real parts of the eigenvalues
+ @return real(diag(D))
+ */
+ public double[] getRealEigenvalues () {
+ return d;
+ }
+
+ /** Return the imaginary parts of the eigenvalues
+ @return imag(diag(D))
+ */
+
+ public double[] getImagEigenvalues () {
+ return e;
+ }
+
+ /** Return the block diagonal eigenvalue matrix
+ @return D
+ */
+
+ public double[][] getD () {
+ double[][] D = new double[n][n];
+ for (int i = 0; i < n; i++) {
+ D[i][i] = d[i];
+ if (e[i] > 0) {
+ D[i][i+1] = e[i];
+ } else if (e[i] < 0) {
+ D[i][i-1] = e[i];
+ }
+ }
+ return D;
+ }
+}
+
+
+
+
+
diff --git a/drawing/papaya/src/papaya/Find.java b/drawing/papaya/src/papaya/Find.java
new file mode 100644
index 0000000..d3eee6d
--- /dev/null
+++ b/drawing/papaya/src/papaya/Find.java
@@ -0,0 +1,1945 @@
+/**
+ * papaya: A collection of utilities for Statistics and Matrix-related manipulations
+ * http://adilapapaya.com/papayastatistics/, 1.1.0
+ * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014
+ *
+ *
+ * Copyright (C) 2014 Adila Faruk http://adilapapaya.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+
+package papaya;
+
+import java.util.*;
+import java.io.*;
+import processing.core.*;
+
+/**
+ * Static class for finding indices in an array corresponding to a given value/object.
+ *
+ * (Portions of this code were (conveniently!) copied and pasted from commons.apache.org.) :)
+ * Finds the index of the given object in the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the index of the given object in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). Finds the last index of the given object within the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the last index of the given object in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than
+ * the array length will search from the end of the array. Checks if the object is in the given array. The method returns {@code false} if a {@code null} array is passed in. Finds the indices containing the specified object in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the indices containing the specified object in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the number of times a given value/object is present in an array. Finds the index of the given value in the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). Finds the last index of the given value within the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the last index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array. Checks if the value is in the given array. The method returns {@code false} if a {@code null} array is passed in. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the number of times a given value/object is present in an array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). Finds the last index of the given value within the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the last index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array. Checks if the value is in the given array. The method returns {@code false} if a {@code null} array is passed in. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the indices for the array elements within the min and max value (inclusive). This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the number of times a given value/object is present in an array. Finds the number of elements strictly greater than the specified value. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). Finds the last index of the given value within the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the last index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array. Checks if the value is in the given array. The method returns {@code false} if a {@code null} array is passed in. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the number of times a given value/object is present in an array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). Finds the last index of the given value within the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the last index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array. Checks if the value is in the given array. The method returns {@code false} if a {@code null} array is passed in. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the number of times a given value/object is present in an array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). Finds the last index of the given value within the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the last index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array. Checks if the value is in the given array. The method returns {@code false} if a {@code null} array is passed in. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the number of times a given value/object is present in an array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the index of the given value within a given tolerance in the array.
+ * This method will return the index of the first value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). Finds the index of the given value in the array starting at the given index.
+ * This method will return the index of the first value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex is treated as zero. A startIndex larger than the array
+ * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). Finds the last index of the given value within the array. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the last index of the given value within a given tolerance in the array.
+ * This method will return the index of the last value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. Finds the last index of the given value in the array starting at the given index. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array. Finds the last index of the given value in the array starting at the given index.
+ * This method will return the index of the last value which falls between the region
+ * defined by valueToFind - tolerance and valueToFind + tolerance. This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the
+ * array length will search from the end of the array. Checks if the value is in the given array. The method returns {@code false} if a {@code null} array is passed in. Checks if a value falling within the given tolerance is in the
+ * given array. If the array contains a value within the inclusive range
+ * defined by (value - tolerance) to (value + tolerance). The method returns {@code false} if a {@code null} array
+ * is passed in. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the indices containing the specified value in the array. This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array. Finds the number of times a given value/object is present in an array.data1, data2, ... , dataP
,
+ * each of length n, it computes and returns the P-by-P
covariance
+ * matrix, S
with each element SJK given by
+ *
+ * SJK = SKJ = cov(dataJ,dataK,bias).
+ *
+ *
+ * cov(data,true) normalizes by N - 1, if N > 1, where N is the number of observations
+ * (or the number of rows in the input matrix).
+ * This makes cov(data,true) the best unbiased estimate of the covariance matrix if the
+ * observations are from a normal distribution. For N = 1, cov(data) normalizes by N.
+ *
+ * S_JK
of the unbiased covariance matrix is:
+ *
+ * S_JK = biasCorrection * Sum ( w[i] * ( dataJ[i] - mu_w(dataJ) )*( dataK[i] - mu_w(dataK) ) ) / Sum (w[i])
+ *
+ * where mu_w
corresponds to the weighted mean of the dataset
+ * and the biasCorrection term above is
+ *
+ * biasCorrection = ( Sum(w[i]) )^2 / ( ( Sum(w[i]) )^2 - Sum( w[i]^2 ) ).
+ *
+ * The elements of the biased covariance matrix are computed sans the bias Correction factor.
+ * e_t
is the residual associated with the observation at time t
,
+ * and there are T
observations, then the test statistic, d
is
+ *
+ * d = Sum_{from t=2 to T} ( e_t - e_{t-1} )^2 / Sum_{from t=1 to T} ( e_t )^2
+ *
+ * @return d the Durbin Watson test statistic
+ */
+ public static float durbinWatson(float[] data) {
+ int size = data.length;
+ if (size < 2) throw new IllegalArgumentException("data sequence must contain at least two values.");
+
+ float run = 0;
+ float run_sq = 0;
+ run_sq = data[0] * data[0];
+ for(int i=1; iH_0: ρ=0
(No linear correlation; Two-tailed test). The p-value is
+ * computed by transforming the correlation to create a t-statistic
+ * having n-2 degrees of freedom:
+ * t = r * sqrt( (n-2)/(1-r^2) ),
+ * where n is the size of the corresponding datasets.
+ * @param r the linear correlation coefficient
+ * @param size the size of the corresponding datasets (i.e. number of rows/observations).
+ * @throws IllegalArgumentException if the size is less than 20. Below this,
+ * the student-T approximation is inaccurate.
+ */
+ public static float linear(float r, int size){
+ double t = studentT(r,size-2);
+ return (float)twoTailedStudentT(t,size-2);
+
+ }
+ /**
+ * Returns the p-value, or significance level of the Spearman rank-correlation rho
+ * between two datasets under the null hypothesis of no correlation.
+ * That is, H_0: ρ_0=0
(No linear correlation; Two-tailed test).
+ * The p-value is computed by transforming the correlation to create a t-statistic
+ * having n-2 degrees of freedom:
+ * t = rho * sqrt( (n-2)/(1-rho^2) ),
+ * where n is the size of the corresponding datasets.
+ * @param rho the Spearman-correlation coefficient
+ * @param size the size of the corresponding datasets (i.e. number of rows/observations).
+ * @throws IllegalArgumentException if the size is less than 20. Below this,
+ * the student-T approximation is inaccurate.
+ */
+ public static float spearman(float rho, int size){
+ Check.size20(size);
+ double t = studentT(rho,size-2);
+ return (float)twoTailedStudentT(t,size-2);
+ }
+
+ /**
+ * Returns the Fisher transformation of the input correlation coefficient. That is,
+ * F(r) = atanh(r);
+ * @param corrcoef the correlation coefficient, r
+ * @return F(r)
+ */
+ private static double fisher(float corrcoef){
+ if(corrcoef==1) return MAXLOG;
+ if(corrcoef==-1) return MINLOG;
+ if(corrcoef==0) return 0.0f;
+ double log = Math.log((1+corrcoef)/(1-corrcoef));
+ return (.5*log);
+ }
+ /**
+ * Returns the Student-T test statistic of the input correlation coefficient. That is,
+ * t = abs(r) * sqrt( df/(1-r^2) ), df = size-2.
+ * @param corrcoef the correlation coefficient, r
+ * @param df the degrees of freedom. Corresponds to size-2
+ */
+ private static double studentT(float corrcoef, int df){
+ if(corrcoef==1) return MAXLOG;
+ if(corrcoef==-1) return MINLOG;
+ if(corrcoef==0) return 0.0f;
+ return corrcoef*Math.sqrt( (df)/(1-corrcoef*corrcoef) );
+ }
+
+ private static double twoTailedNormal(double z){
+ if(z>0) return ( 2*(1.0- (Probability.normcdf(z)) ) );
+ else return (2 * (Probability.normcdf(z)));
+ }
+
+ private static double twoTailedStudentT(double t, int df){
+ if(t>0) return ( 2*(1.0- (Probability.tcdf(t,df)) ) );
+ else return (2 * (Probability.tcdf(t,df)));
+ }
+
+ /**
+ * Returns the z value of the Spearman rank-correlation.
+ * That is,
+ * z = sqrt((n-3)(1.06)) * F(r), F(r) = atanh(r);
+ * @param corrcoef the correlation coefficient, r
+ * @param size the size of the dataset
+ */
+ private static double zSpearman(float corrcoef, int size){
+ Check.size3(size);
+ double f = fisher(corrcoef);
+ return Math.sqrt((size-3)/1.06)*f;
+ }
+ }// end test class
+}// end Correlation class
diff --git a/drawing/papaya/src/papaya/CorrelationPlot.java b/drawing/papaya/src/papaya/CorrelationPlot.java
new file mode 100644
index 0000000..75e49de
--- /dev/null
+++ b/drawing/papaya/src/papaya/CorrelationPlot.java
@@ -0,0 +1,161 @@
+/**
+ * papaya: A collection of utilities for Statistics and Matrix-related manipulations
+ * http://adilapapaya.com/papayastatistics/, 1.1.0
+ * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014
+ *
+ *
+ * Copyright (C) 2014 Adila Faruk http://adilapapaya.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+package papaya;
+import java.util.Arrays;
+import java.io.*;
+import java.lang.Object;
+import java.lang.String;
+
+import processing.core.*;
+
+/**
+ * Takes in a matrix and plots the data in each of the columns versus
+ * each other. Each of the columns are normalized to their minimum and maximum values which
+ * can be a little misleading. For example, say you are looking at columns specifying the weight
+ * of different panda bears over 100 days.
+ * Panda bear A (Ling-Ling)'s weight varies from 650 to 970 pounds
+ * (she's an emotional eater), while panda bear B (Thomas)'s weight varies from 512 to 516 pounds
+ * (he's not a big fan of bamboo; also kind of metrosexual). The correlation plot will not show you
+ * this difference in magnitudes or ranges!
+ * There are many ways of addressing that, and all of them involve you writing your
+ * own code. :)
+ *
+ *
+ * while the {@link Mean} class has the
+ *
+ *
+ * utilities.
+ */
+public class Descriptive extends Object {
+ /**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+ protected Descriptive() {}
+
+ /**
+ * Checks if the given range is within the contained array's bounds.
+ * @throws IndexOutOfBoundsException if to!=from-1 || from<0 || from>to || to>=size
.
+ */
+ protected static void checkRangeFromTo(int from, int to, int theSize) {
+ if (to==from-1) return;
+ if (from<0 || from>to || to>=theSize)
+ throw new IndexOutOfBoundsException("from: "+from+", to: "+to+", size="+theSize);
+ }
+ /**
+ *
+ * Computes the frequency (number of occurances, count) of each distinct value in the given sorted data.
+ * After this call, both distinctValues
and frequencies
have a new size (which is equal for both),
+ * which is the number of distinct values in the sorted data.
+ * distinctValues
, starting at index 0.
+ * The frequency of each distinct value is filled into frequencies
, starting at index 0.
+ * As a result, the smallest distinct value (and its frequency) can be found at index 0,
+ * the second smallest distinct value (and its frequency) at index 1, ...,
+ * the largest distinct value (and its frequency) at index distinctValues.size()-1
.
+ * sortedData = (5,6,6,7,8,8) --> distinctValues = (5,6,7,8), frequencies = (1,2,1,2)
+ *
+ * Code-wise, you would write:
+ *
+ * ArrayList
+ *
+ * @param sortedData the data; must be sorted ascending.
+ * @param distinctValues a ArrayList to be filled with the distinct values; can have any size.
+ * @param frequencies a ArrayList to be filled with the frequencies; can have any size;
+ * set this parameter to frequencies(sortedData,distinctValues,frequencies);
+ * null
to ignore it.
+ *
+ * @see Frequency
+ * @see Unique
+ */
+ public static void frequencies(float[] sortedData, ArrayList
Definition of sumOfSquares: sumOfSquares(data) = Sum ( data[i] * data[i] )
.
+ *
+ * @param data the additional elements to be incorporated into min, max, etc.
+ * @param from the index of the first element within data
to consider.
+ * @param to the index of the last element within data
to consider.
+ * The method incorporates elements data[from], ..., data[to]
.
+ * @param inOut the old values in the following format:
+ *
+ *
+ * If no data sequence elements have so far been recorded set the values as follows
+ * inOut[0]
is the old minimum.
+ * inOut[1]
is the old maximum.
+ * inOut[2]
is the old sum.
+ * inOut[3]
is the old sum of squares.
+ *
+ *
+ * inOut[0] = Float.POSITIVE_INFINITY
as the old minimum.
+ * inOut[1] = Float.NEGATIVE_INFINITY
as the old maximum.
+ * inOut[2] = 0.0
as the old sum.
+ * inOut[3] = 0.0
as the old sum of squares.
+ * inOut
array.
+ */
+
+ public static void sumOfSquares(float[] data, int from, int to, double[] inOut) {
+ checkRangeFromTo(from,to,data.length);
+
+ // read current values
+ double min = inOut[0];
+ double max = inOut[1];
+ double sum = inOut[2];
+ double sumSquares = inOut[3];
+
+ for (int i=from; i<=to; i++) {
+ float element = data[i];
+ sum += element;
+ sumSquares += element*element;
+ if (element < min) min = element;
+ if (element > max) max = element;
+
+ /*
+ double oldDeviation = element - mean;
+ mean += oldDeviation / (N+1);
+ sumSquaredDeviations += (element-mean)*oldDeviation; // cool, huh?
+ */
+
+ /*
+ double oldMean = mean;
+ mean += (element - mean)/(N+1);
+ if (N > 0) {
+ sumSquaredDeviations += (element-mean)*(element-oldMean); // cool, huh?
+ }
+ */
+
+ }
+
+ // store new values
+ inOut[0] = min;
+ inOut[1] = max;
+ inOut[2] = sum;
+ inOut[3] = sumSquares;
+
+ // At this point of return the following postcondition holds:
+ // data.length-from elements have been consumed by this call.
+ }
+
+ /**
+ * Incrementally maintains and updates various sums of powers of the form Sum(data[i]k)
.
+ *
+ * Assume we have already recorded some data sequence elements data[i]
+ * and know the values of Sum(data[i]from), Sum(data[i]from+1), ..., Sum(data[i]to)
.
+ * Assume further, we are to record some more elements
+ * and to derive updated values of these sums.
+ * k
-th order with constant c
of a data sequence,
+ * is given by Sum( (data[i]-c)k ) / data.size()
.
+ * It can incrementally be computed by using the equivalent formula
+ * moment(k,c) = m(k,c) / data.size()
where
+ * m(k,c) = Sum( -1i * b(k,i) * ci * sumOfPowers(k-i))
for i = 0 .. k
and
+ * b(k,i) =
{@link cern.jet.math.Arithmetic#binomial(long,long) binomial(k,i)} and
+ * sumOfPowers(k) = Sum( data[i]k )
.
+ * data
to consider.
+ * @param to the index of the last element within data
to consider.
+ * The method incorporates elements data[from], ..., data[to]
.
+ *
+ * @param inOut the old values of the sums in the following format:
+ *
+ *
+ * If no data sequence elements have so far been recorded set all old values of the sums to sumOfPowers[0]
is the old Sum(data[i]fromSumIndex)
.
+ * sumOfPowers[1]
is the old Sum(data[i]fromSumIndex+1)
.
+ * sumOfPowers[toSumIndex-fromSumIndex]
is the old Sum(data[i]toSumIndex)
.
+ * 0.0
.
+ *
+ * @return the updated values filled into the sumOfPowers
array.
+ */
+ public static void sumOfPowers(float[] data, int from, int to, int fromSumIndex, int toSumIndex, double[] sumOfPowers) {
+ int size = data.length;
+ int lastIndex = toSumIndex - fromSumIndex;
+ if (from > size || lastIndex+1 > sumOfPowers.length) throw new IllegalArgumentException();
+
+ // optimized for common parameters
+ if (fromSumIndex==1) { // handle quicker
+ if (toSumIndex==2) {
+ double sum = sumOfPowers[0];
+ double sumSquares = sumOfPowers[1];
+ for (int i=from-1; ++i<=to; ) {
+ float element = data[i];
+ sum += element;
+ sumSquares += element*element;
+ //if (element < min) min = element;
+ //else if (element > max) max = element;
+ }
+ sumOfPowers[0] += sum;
+ sumOfPowers[1] += sumSquares;
+ return;
+ }
+ else if (toSumIndex==3) {
+ double sum = sumOfPowers[0];
+ double sumSquares = sumOfPowers[1];
+ double sum_xxx = sumOfPowers[2];
+ for (int i=from-1; ++i<=to; ) {
+ float element = data[i];
+ sum += element;
+ sumSquares += element*element;
+ sum_xxx += element*element*element;
+ //if (element < min) min = element;
+ //else if (element > max) max = element;
+ }
+ sumOfPowers[0] += sum;
+ sumOfPowers[1] += sumSquares;
+ sumOfPowers[2] += sum_xxx;
+ return;
+ }
+ else if (toSumIndex==4) { // handle quicker
+ double sum = sumOfPowers[0];
+ double sumSquares = sumOfPowers[1];
+ double sum_xxx = sumOfPowers[2];
+ double sum_xxxx = sumOfPowers[3];
+ for (int i=from-1; ++i<=to; ) {
+ float element = data[i];
+ sum += element;
+ sumSquares += element*element;
+ sum_xxx += element*element*element;
+ sum_xxxx += element*element*element*element;
+ //if (element < min) min = element;
+ //else if (element > max) max = element;
+ }
+ sumOfPowers[0] += sum;
+ sumOfPowers[1] += sumSquares;
+ sumOfPowers[2] += sum_xxx;
+ sumOfPowers[3] += sum_xxxx;
+ return;
+ }
+ }
+
+ if (fromSumIndex==toSumIndex || (fromSumIndex >= -1 && toSumIndex <= 5)) { // handle quicker
+ for (int i=fromSumIndex; i<=toSumIndex; i++) {
+ sumOfPowers[i-fromSumIndex] += Sum.powerDeviations(data,i,0.0f,from,to);
+ }
+ return;
+ }
+ // now the most general case:
+ // optimized for maximum speed, but still not quite quick
+ for (int i=from-1; ++i<=to; ) {
+ float element = data[i];
+ double pow = Math.pow(element,fromSumIndex);
+
+ int j=0;
+ for (int m=lastIndex; --m >= 0; ) {
+ sumOfPowers[j++] += pow;
+ pow *= element;
+ }
+ sumOfPowers[j] += pow;
+ }
+ // At this point of return the following postcondition holds:
+ // data.size()-fromIndex elements have been consumed by this call.
+ }
+ /**
+ * Incrementally maintains and updates sum and sum of squares of a weighted data sequence.
+ *
+ * Assume we have already recorded some data sequence elements
+ * and know their sum and sum of squares.
+ * Assume further, we are to record some more elements
+ * and to derive updated values of sum and sum of squares.
+ *
Definition of sum: sum = Sum ( data[i] * weights[i] )
.
+ *
Definition of sumOfSquares: sumOfSquares = Sum ( data[i] * data[i] * weights[i])
.
+ *
+ *
+ * @param data the additional elements to be incorporated into min, max, etc.
+ * @param weights the weight of each element within data
.
+ * @param from the index of the first element within data
(and weights
) to consider.
+ * @param to the index of the last element within data
(and weights
) to consider.
+ * The method incorporates elements data[from], ..., data[to]
.
+ * @param inOut the old values in the following format:
+ *
+ *
+ * If no data sequence elements have so far been recorded set the values as follows
+ * inOut[0]
is the old sum.
+ * inOut[1]
is the old sum of squares.
+ *
+ *
+ *
+ * @return the updated values filled into the inOut[0] = 0.0
as the old sum.
+ * inOut[1] = 0.0
as the old sum of squares.
+ * inOut
array.
+ */
+ public static void sumOfSquaresWeighted(float[] data, float[] weights, int from, int to, float[] inOut) {
+ int dataSize = data.length;
+ checkRangeFromTo(from,to,dataSize);
+ if (dataSize != weights.length) throw new IllegalArgumentException("from="+from+", to="+to+", data.length="+dataSize+", weights.length="+weights.length);
+
+ // read current values
+ float sum = inOut[0];
+ float sumOfSquares = inOut[1];
+
+ for (int i=from-1; ++i<=to; ) {
+ float element = data[i];
+ float weight = weights[i];
+ float prod = element*weight;
+
+ sum += prod;
+ sumOfSquares += element * prod;
+ }
+
+ // store new values
+ inOut[0] = sum;
+ inOut[1] = sumOfSquares;
+
+ // At this point of return the following postcondition holds:
+ // data.size()-from elements have been consumed by this call.
+ }
+
+ }// end IncrUpdate class
+ /**
+ * Returns the kurtosis (aka excess) of a data sequence.
+ * @param moment4 the fourth central moment, which is moment(data,4,mean)
.
+ * @param standardDeviation the standardDeviation.
+ */
+ public static float kurtosis(float moment4, float standardDeviation) {
+ return -3 + moment4 / (standardDeviation * standardDeviation * standardDeviation * standardDeviation);
+ }
+ /**
+ * Returns the kurtosis (aka excess) of a data sequence, which is
+ * -3 + moment(data,4,mean) / standardDeviation4
.
+ */
+ public static float kurtosis(float[] data, float mean, float standardDeviation) {
+ return kurtosis(moment(data,4,mean), standardDeviation);
+ }
+
+ public static int max(int a, int b) {
+ return (a > b) ? a : b;
+ }
+
+ public static float max(float a, float b) {
+ return (a > b) ? a : b;
+ }
+
+
+ public static double max(double a, double b) {
+ return (a > b) ? a : b;
+ }
+
+
+ /**
+ * Returns the largest member of a data sequence.
+ */
+ public static double max(double[] data) {
+ int size = data.length;
+ Check.size0(size);
+ double max = data[size-1];
+ for (int i = size-1; --i >= 0;) {
+ if (data[i] > max) max = data[i];
+ }
+ return max;
+ }
+ /**
+ * Returns the largest member of a data sequence.
+ */
+ public static float max(float[] data) {
+ int size = data.length;
+ Check.size0(size);
+ float max = data[0];
+ for (int i = 0; i
+ * The geometric mean is given by pow( Product( data[i] ), 1/size)
+ * which is equivalent to Math.exp( Sum( Log(data[i]) ) / size)
.
+ *
+ * The latter version is used here is the former easily results in overflows.
+ */
+ public static float geometric(int size, float sumOfLogarithms) {
+ return (float)Math.exp(sumOfLogarithms/size);
+ // this version would easily results in overflows
+ //return Math.pow(product, 1/size);
+ }
+ /**
+ * Returns the geometric mean of a data sequence.
+ * Note that for a geometric mean to be meaningful, the minimum of the data sequence must not be less or equal to zero.
+ *
+ * The geometric mean is given by pow( Product( data[i] ), 1/data.size())
.
+ * This method tries to avoid overflows at the expense of an equivalent but somewhat slow definition:
+ * geometricMean = Math.exp( Sum( Log(data[i]) ) / size)
.
+ */
+ public static float geometric(float[] data) {
+ return geometric(data.length, (float)Sum.logs(data,0,data.length-1));
+ }
+ /**
+ * Returns the harmonic mean of a data sequence.
+ *
+ * @param size the number of elements in the data sequence.
+ * @param sumOfInversions Sum( 1.0 / data[i])
.
+ */
+ public static float harmonic(int size, float sumOfInversions) {
+ return size / sumOfInversions;
+ }
+
+ /**
+ * Returns the harmonic mean of a data sequence as Sum( 1.0 / data[i])
.
+ *
+ * @param data array
+ */
+ public static float harmonic(float[] data) {
+ return (float)data.length / (float)Sum.inversions(data, 0, data.length-1);
+ }
+ /**
+ * Returns the trimmed arithmetic mean of a sorted data sequence.
+ * @param sortedData the data sequence; must be sorted ascending.
+ * @param mean the mean of the (full) sorted data sequence.
+ * @param left the number of leading elements to trim.
+ * @param right the number of trailing elements to trim.
+ * */
+ public static float trimmed(float[] sortedData, float mean, int left, int right) {
+ int size = sortedData.length;
+ Check.size0(size);
+ if (left+right >= size) throw new IllegalArgumentException("Not enough data. left: "+left+", right: "+right+", data length: "+ size);
+ int origLn=size;
+ for(int i=0; iSum( data[i] ) / data.length
.
+ * Similar to {@link Mean#arithmetic}.
+ */
+ public static float mean(float[] data) {
+ return Mean.arithmetic(data);
+ }
+ /**
+ * Returns the mean deviation of a dataset.
+ * That is Sum( Math.abs(data[i]-mean)) ) / data.length
.
+ */
+ public static float meanDeviation(float[] data, float mean) {
+
+ int size = data.length;
+ double sum=0;
+ for (int i=size; --i >= 0;) sum += Math.abs(data[i]-mean);
+ return (float)sum/size;
+ }
+
+ /**
+ * Returns the median of a data sequence.
+ * @param data the data sequence;
+ * @param isSorted true if the data sequence is sorted (in ascending order), else false.
+ */
+ public static float median(float[] data, boolean isSorted) {
+ if(isSorted) return quantile(data, 0.5f);
+ else{
+ float[] sortedData = Mat.copyThenSort(data);
+ return quantile(sortedData, 0.5f);
+ }
+ }
+
+ public static int min(int a, int b) {
+ return (a < b) ? a : b;
+ }
+
+ public static float min(float a, float b) {
+ return (a < b) ? a : b;
+ }
+ public static double min(double a, double b) {
+ return (a < b) ? a : b;
+ }
+ /**
+ * Returns the smallest member of a data sequence.
+ */
+ public static double min(double[] data) {
+ int size = data.length;
+ Check.size0(size);
+ double min = data[0];
+ for (int i = 1; ic
of a data sequence,
+ * which is Sum( (data[i]-c)k ) / data.length
.
+ *
+ * @param sumOfPowers sumOfPowers[m] == Sum( data[i]m) )
for m = 0,1,..,k
as returned by method
+ * {@link #incrementalUpdateSumsOfPowers(DoubleArrayList,int,int,int,int,double[])}.
+ * In particular there must hold sumOfPowers.length == k+1
.
+ * @param size the number of elements of the data sequence.
+ */
+ // public static float moment(int k, float c, int size, float[] sumOfPowers) {
+ // double sum=0;
+ // int sign = 1;
+ // for (int i=0; i<=k; i++) {
+ // double y;
+ // if (i==0) y = 1;
+ // else if (i==1) y = c;
+ // else if (i==2) y = c*c;
+ // else if (i==3) y = c*c*c;
+ // else y = Math.pow(c, i);
+ // //sum += sign *
+ // sum += sign * cern.jet.math.Arithmetic.binomial(k,i) * y * sumOfPowers[k-i];
+ // sign = -sign;
+ // }
+ // /*
+ // for (int i=0; i<=k; i++) {
+ // sum += sign * cern.jet.math.Arithmetic.binomial(k,i) * Math.pow(c, i) * sumOfPowers[k-i];
+ // sign = -sign;
+ // }
+ // */
+ // return sum/size;
+ // }
+ /**
+ * Returns the moment of k
-th order with constant c
of a data sequence,
+ * which is Sum( (data[i]-c)k ) / data.size()
.
+ */
+ public static float moment(float[] data, int k, float c) {
+ return (float)Sum.powerDeviations(data,k,c) / data.length;
+ }
+ /**
+ * Returns the array containing all elements in the dataset that are less than
+ * or equal to the lowerLimit
+ * and more than or equal to the upperLimit
+ * @param data the data array
+ * @param lowerLimit
+ * @param upperLimit
+ * @param isSorted true if the data array has been sorted in ascending order, else set to false.
+ */
+ public static float[] outliers(float[] data, float lowerLimit, float upperLimit, boolean isSorted){
+ int size = data.length;
+ float[] sortedData = new float[size];
+ if(isSorted){
+ sortedData = data;
+ }
+ else{
+ sortedData = Mat.copyThenSort(data);
+ }
+ float max = sortedData[size-1]; float min = sortedData[0];
+ float[] tempArray = new float[size];
+ int i=0;
+ if(lowerLimit>=min){
+ while(sortedData[i]<=lowerLimit){
+ tempArray[i] = sortedData[i];
+ i++;
+ }
+ }
+ int newSize = i;
+ i = size-1;
+ if(upperLimit<=max){
+ while(sortedData[i]>=upperLimit){
+ tempArray[i] = sortedData[i];
+ i--;
+ newSize++;
+ }
+ }
+ float[] outliers = Arrays.copyOf(tempArray,newSize);
+ //Arrays.sort(outliers);
+ return outliers;
+ }
+
+ /** Class for computing the pooled mean and variance of data sequences */
+ public static class Pooled{
+ protected Pooled(){}
+
+ /**
+ * Returns the pooled mean of two data sequences.
+ * That is (size1 * mean1 + size2 * mean2) / (size1 + size2)
.
+ * k
data sequences.
+ * That is (size1 * mean1 + size2 * mean1 + ... + sizek * meank) / (size1 + size2 + ... + sizek)
.
+ *
+ * biasedPooledVar = Sum( (size_i)*variance_i ) / Sum(size_i),
+ *
+ * For more than 2 datasets, use {@link Pooled#var(float[],int[],boolean)}.
+ *
+ * @param size1 the number of elements in data sequence 1.
+ * @param variance1 the variance of data sequence 1.
+ * @param size2 the number of elements in data sequence 2.
+ * @param variance2 the variance of data sequence 2.
+ * @param unbiased set to true to return the unbiased pooled variance,
+ * false to return the biased version.
+ */
+ public static float var(float variance1, int size1, float variance2, int size2, boolean unbiased) {
+ if(!unbiased) return (size1 * variance1 + size2 * variance2) / (size1 + size2);
+ else return((size1-1)*variance1+(size2-1)*variance2)/(size1+size2-2);
+ }
+ /**
+ * Returns the pooled variance of
+ * where n = number of columns.
+ * k
data sequences. The unbiased estimate of the
+ * pooled variance is computed as
+ *
+ * unbiasedPooledVar = Sum( (sizes[i] -1)*variances[i] ) / Sum( (sizes[i]-1) ),
+ *
+ * while the biased pooled variance is computed using
+ *
+ * biasedPooledVar = Sum( (sizes[i])*variances[i] ) / Sum(sizes[i]),
+ */
+ public static float var(float[] variances, int[] sizes, boolean unbiased) {
+ int size = sizes.length;
+ Check.equalLengths(size, variances);
+ float sizeSum = sizes[0];
+ float varianceSum=0;
+ float pooledVar = 0;
+ if(unbiased){
+ for(int i=0; i
+ * data[0]*data[1]*...*data[data.length-1]
.
+ * This method uses the equivalent definition:
+ * prod = pow( exp( Sum( Log(x[i]) ) / length*length)
.
+ */
+ public static float product(int size, float sumOfLogarithms) {
+ return (float)Math.pow(Math.exp(sumOfLogarithms/size), size);
+ }
+ /**
+ * Returns the product of a data sequence, which is Prod( data[i] )
.
+ * In other words: data[0]*data[1]*...*data[data.length-1]
.
+ * Note that you may easily get numeric overflows. Use {@link product(int,float)}
+ * instead to avoid that.
+ */
+ public static float product(float[] data) {
+ int size = data.length;
+ float product = 1;
+ for (int i=size; --i >= 0;) product *= data[i];
+ return product;
+ }
+
+ /**
+ * Returns the quartiles of the input data array (not necessarily sorted).
+ *
+ *
+ * The first quartile, or lower quartile (Q[0]), is the value that cuts off
+ * the first 25% of the data when it is sorted in ascending order.
+ * The second quartile, or median (Q[1]), is the value that cuts off the first 50%.
+ * The third quartile, or upper quartile (Q[2], is the value that cuts off the first 75%.
+ * phi-
quantile; that is, an element elem
+ * for which holds that phi
percent of data elements are less than
+ * elem
.
+ * The quantile need not necessarily be contained in the data sequence,
+ * it can be a linear interpolation.
+ * @param sortedData the data sequence; must be sorted ascending.
+ * @param phi the percentage; must satisfy 0 <= phi <= 1
.
+ */
+ public static float quantile(float[] sortedData, float phi) {
+
+ int size = sortedData.length;
+
+ float index = phi * (size - 1) ;
+ int lhs = (int)index ;
+ float delta = index - lhs ;
+ float result;
+
+ if (size == 0) return 0.0f ;
+
+ if (lhs == size - 1) {
+ result = sortedData[lhs] ;
+ }
+ else {
+ result = (1 - delta) * sortedData[lhs] + delta * sortedData[lhs + 1] ;
+ }
+ return result ;
+ }
+ /**
+ * Returns how many percent of the elements contained in the receiver are <= element
.
+ * Does linear interpolation if the element is not contained but lies in between
+ * two contained elements.
+ *
+ * @param sortedList the list to be searched (must be sorted ascending).
+ * @param element the element to search for.
+ * @return the percentage phi
of elements <= element
+ * (0.0 <= phi <= 1.0)
.
+ */
+ public static float quantileInverse(float[] sortedList, float element) {
+ return rankInterpolated(sortedList,element) / sortedList.length;
+ }
+ /**
+ * Returns the quantiles of the specified percentages.
+ * The quantiles need not necessarily be contained in the data sequence, it can be a linear interpolation.
+ * @param sortedData the data sequence; must be sorted ascending.
+ * @param percentages the percentages for which quantiles are to be computed.
+ * Each percentage must be in the interval [0.0f,1.0f]
.
+ * @return the quantiles.
+ */
+ public static float[] quantiles(float[] sortedData, float[] percentages) {
+ int s = percentages.length;
+ ArrayList{0, 1, 2,..., sortedList.size()}
.
+ * Math.sqrt(Sum( data[i]*data[i] ) / data.length).
+ * The RMS of data sequence is the square-root of the mean of the squares of the elements in the data sequence.
+ * It is a measure of the average "size" of the elements of a data sequence.
+ *
+ * @param sumOfSquares sumOfSquares(data) == Sum( data[i]*data[i] )
of the data sequence.
+ * @param size the number of elements in the data sequence.
+ */
+ public static float rms(int size, double sumOfSquares) {
+ return (float)Math.sqrt(sumOfSquares/size);
+ }
+
+ /**
+ * Contains sample-related statistical methods.
+ */
+ private static class Sample{
+ protected Sample(){}
+ /**
+ * Returns the sample kurtosis (aka excess) of a data sequence.
+ * That is,
+ *
+ * γ_2 = moment4*n^2*(n+1)/ ((n-1)*(n-2)*(n-3)*var^2) - 3*(n-1)^2/ ( (n-2)*(n-3) )
+ * = Sum( (y_i-ymean)^4 )*n*(n+1)/ ((n-1)*(n-2)*(n-3)*var^2) - 3*(n-1)^2/ ( (n-2)*(n-3) ),
+ *
+ * where n is the length of the input data array.
+ * moment(data,4,mean)
.
+ * @param sampleVariance the sample variance.
+ */
+ public static float sampleKurtosis(int size, float moment4, float sampleVariance) {
+ float n=size; // set to float to avoid NaNs.
+ float s2=sampleVariance; // (y-ymean)^2/(n-1)
+ float m4 = moment4*n; // (y-ymean)^4
+ return m4*n*(n+1) / ( (n-1)*(n-2)*(n-3)*s2*s2 )
+ - 3.0f*(n-1)*(n-1)/((n-2)*(n-3));
+ }
+ /**
+ * Returns the sample kurtosis (aka excess) of a data sequence.
+ */
+ public static float sampleKurtosis(float[] data, float mean, float sampleVariance) {
+ return sampleKurtosis(data.length,moment(data,4,mean), sampleVariance);
+ }
+ /**
+ * Return the standard error of the sample kurtosis. That is,
+ *
+ * Sqrt( 24n*(n-1)*(n-1)/((n-3)*(n-2)*(n+3)*(n+5)) ),
+ *
+ * where n is the length of the input array.
+ *
+ * γ_1 = moment3*n^2 / ((n-1)*(n-2)*stdev^3)
+ * = Sum( (y_i-ymean)^3 )/stdev^3 * n / ((n-1)*(n-2)),
+ *
+ * where n is the length of the input data array.
+ * moment(data,3,mean)
.
+ * @param sampleVariance the sample variance.
+ */
+ public static float sampleSkew(int size, float moment3, float sampleVariance) {
+ float n=size; // set to float to avoid NaNs
+ float s=(float)Math.sqrt(sampleVariance); // sqrt( (y-ymean)^2/(n-1) )
+ float m3 = moment3*n; // (y-ymean)^3
+ return n*m3 / ((n-1)*(n-2)*s*s*s);
+ }
+ /**
+ * Returns the sample skew of a data sequence.
+ */
+ public static float sampleSkew(float[] data, float mean, float sampleVariance) {
+ return sampleSkew(data.length, moment(data,3,mean), sampleVariance);
+ }
+ /**
+ * Return the standard error of the sample skew. That is,
+ *
+ * Sqrt( 6n*(n-1)/((n-2)*(n+1)*(n+3)) ),
+ *
+ * where n is the length of the input array.
+ * moment(data,3,mean)
.
+ * @param standardDeviation the standardDeviation.
+ */
+ public static float skew(float moment3, float standardDeviation) {
+ return moment3 / (standardDeviation * standardDeviation * standardDeviation);
+ }
+ /**
+ * Returns the skew of a data sequence, which is moment(data,3,mean) / standardDeviation3
.
+ */
+ public static float skew(float[] data, float mean, float standardDeviation) {
+ return skew(moment(data,3,mean), standardDeviation);
+ }
+
+ /**
+ * Returns the standard deviation of a dataset.
+ *
+ * There are two definitions of the standard deviation:
+ *
+ * sigma_1 = 1/(N-1) * Sum( (x[i] - mean(x))^2 )
+ * sigma_2 = 1/(N) * Sum( (x[i] - mean(x))^2 )
+ *
+ * sigma_1 is the square root of an unbiased estimator of the variance of the population
+ * the x is drawn, as long as x consists of independent, identically distributed samples.
+ * sigma_2 corresponds to the second moment of the set of values about their mean.
+ *
+ * sigma_1 = 1/(N-1) * Sum( (x[i] - mean(x))^2 )
+ * sigma_2 = 1/(N) * Sum( (x[i] - mean(x))^2 )
+ *
+ * sigma_1 is the square root of an unbiased estimator of the variance of the population
+ * the x is drawn, as long as x consists of independent, identically distributed samples.
+ * sigma_2 corresponds to the second moment of the set of values about their mean.
+ * Sum( 1.0 / data[i])
.
+ * @param data the data sequence.
+ * @param from the index of the first data element (inclusive).
+ * @param to the index of the last data element (inclusive).
+ */
+ public static double inversions(float[] data, int from, int to) {
+ return powerDeviations(data,-1,0.0f,from,to);
+ }
+ /**
+ * Returns the sum of logarithms of a data sequence, which is Sum( Log(data[i])
.
+ * @param data the data sequence.
+ * @param from the index of the first data element (inclusive).
+ * @param to the index of the last data element (inclusive).
+ */
+ public static double logs(float[] data, int from, int to) {
+ double logsum = 0;
+ for (int i=from-1; ++i <= to;) logsum += Math.log(data[i]);
+ return (float)logsum;
+ }
+ /**
+ * Returns the sum of the product of two data arrays, Sum( x[i] * y[i])
.
+ * @param data1 the first data sequence.
+ * @param data2 the second data sequence
+ * @return the sum of the product of the two data sequences.
+ */
+ public static double products(float[] data1, float[] data2) {
+ int size = data1.length;
+ Check.equalLengths(size,data2);
+
+ double sumOfProduct = data1[0]*data2[0];
+ for (int i=1; ic == 0.0
and/or k == -2 .. 4
.
+ */
+ public static double powerDeviations(float[] data, int k, float c) {
+ return powerDeviations(data,k,c,0,data.length-1);
+ }
+ /**
+ * Returns Sum( (data[i]-c)k )
for all i = from .. to
;
+ * optimized for common parameters like c == 0.0
and/or k == -2 .. 5
.
+ * Note that no checks are made for divisions by zero (important for
+ * k = -2
and k = -1
), so think twice before using this
+ * if the data has elements = 0.
+ */
+
+ public static double powerDeviations(final float[] data, final int k, final float c, final int from, final int to) {
+
+ double sum = 0.0;
+ double v;
+ int i;
+ switch (k) { // optimized for speed
+ case -2:
+ if (c==0.0) for (i=from-1; ++i<=to; ) { v = data[i]; sum += 1/(v*v); }
+ else for (i=from-1; ++i<=to; ) { v = data[i]-c; sum += 1/(v*v); }
+ break;
+ case -1:
+ if (c==0.0) for (i=from-1; ++i<=to; ) sum += 1/(data[i]);
+ else for (i=from-1; ++i<=to; ) sum += 1/(data[i]-c);
+ break;
+ case 0:
+ sum += to-from+1;
+ break;
+ case 1:
+ if (c==0.0) for (i=from-1; ++i<=to; ) sum += data[i];
+ else for (i=from-1; ++i<=to; ) sum += data[i]-c;
+ break;
+ case 2:
+ if (c==0.0) for (i=from-1; ++i<=to; ) { v = data[i]; sum += v*v; }
+ else for (i=from-1; ++i<=to; ) { v = data[i]-c; sum += v*v; }
+ break;
+ case 3:
+ if (c==0.0) for (i=from-1; ++i<=to; ) { v = data[i]; sum += v*v*v; }
+ else for (i=from-1; ++i<=to; ) { v = data[i]-c; sum += v*v*v; }
+ break;
+ case 4:
+ if (c==0.0) for (i=from-1; ++i<=to; ) { v = data[i]; sum += v*v*v*v; }
+ else for (i=from-1; ++i<=to; ) { v = data[i]-c; sum += v*v*v*v; }
+ break;
+ case 5:
+ if (c==0.0) for (i=from-1; ++i<=to; ) { v = data[i]; sum += v*v*v*v*v; }
+ else for (i=from-1; ++i<=to; ) { v = data[i]-c; sum += v*v*v*v*v; }
+ break;
+ default:
+ for (i=from-1; ++i<=to; ) sum += Math.pow(data[i]-c, k);
+ break;
+ }
+ return sum;
+ }
+ /**
+ * Returns the sum of powers of a data sequence, which is Sum ( data[i]k )
.
+ */
+ public static double powers(float[] data, int k) {
+ return powerDeviations(data,k,0);
+ }
+ /**
+ * Returns the sum of squared mean deviation of of a data sequence.
+ * That is variance * (size-1) == Sum( (data[i] - mean)^2 )
.
+ *
+ * @param size the number of elements of the data sequence.
+ * @param variance the variance of the data sequence.
+ */
+ public static float squaredDeviations(int size, float variance) {
+ return variance * (size-1);
+ }
+ /**
+ * Returns the sum of squares of a data sequence.
+ * That is Sum ( data[i]*data[i] )
.
+ */
+ public static double squares(float[] data) {
+ return powerDeviations(data,2,0.0f);
+ }
+ /**
+ * Returns the simple sum of a data sequence.
+ * That is Sum( data[i] )
.
+ * @see Mat#sum(float[])
+ * @see Mat#sum(float[], float[])
+ * @see Mat#sum(float[],float)
+ */
+ public static float sum(float[] data) {
+ return (float)Sum.powerDeviations(data,1,0.0f);
+ }
+ } // end Sum class
+
+ /**
+ * Return the tukey five number summary of a dataset consisting of the minimum, maximum,
+ * and three quartile values.
+ * @param data the data array
+ * @return the array of five numbers.
+ */
+ public static float[] tukeyFiveNum(float[] data){
+ int size = data.length;
+ float[] sortedArray = Mat.copyThenSort(data);
+ float[] Q = quartiles(sortedArray, true);
+ float[] fiveNum = new float[5];
+ fiveNum[0] = sortedArray[0];
+ fiveNum[4]=sortedArray[size-1];
+ fiveNum[1] = Q[0]; fiveNum[2] = Q[1]; fiveNum[3] = Q[2];
+ return fiveNum;
+ }
+
+ /**
+ * Returns the variance of a dataset, V.
+ * For matrices, var(X,unbiased=true) returns an array containing the variance of each column of X.
+ * The result V is an unbiased estimator of the variance of the population from which
+ * X is drawn, as long as X consists of independent, identically distributed samples.
+ *
+ * Algorithms for calculating variance, Wikipedia.org
+ *
+ * Incremental calculation of weighted mean and variance, Tony Finch
+ * @param data the data sequence.
+ * @param unbiased set to true to return the unbiased variance (division by (N-1)), false to return the biased value (division by N).
+ * @return the variance in the data.
+ */
+ public static float var(float[] data, boolean unbiased) {
+ int size = data.length;
+ float varianceSum = varianceNotNormalized(data);
+ if(unbiased && size>1) return varianceSum/(size-1);
+ return varianceSum/size;
+ }
+
+ /**
+ * Returns an array containing the variance of each column of the input matrix X.
+ * The result V is an unbiased estimator of the variance of the population from which
+ * X is drawn, as long as X consists of independent, identically distributed samples.
+ *
+ * Algorithms for calculating variance, Wikipedia.org
+ *
+ * Incremental calculation of weighted mean and variance, Tony Finch
+ * @param data the data sequence.
+ * @param unbiased set to true to return the unbiased variance (division by (N-1)), false to return the biased value (division by N).
+ * @return the variance in the data.
+ */
+ public static float[] var(float[][] data, boolean unbiased) {
+ int numColumns = data[0].length;
+ float[] variances = new float[numColumns];
+ float[] dataEach = new float[data.length];
+ for(int i=0; i Sum (data[i] * weights[i]) / Sum ( weights[i] )
.
+ */
+ public static float mean(float[] data, float[] weights) {
+ int size = data.length;
+ Check.equalLengths(size,weights);
+ float sum = 0.0f;
+ float weightsSum = 0.0f;
+ for (int i=size; --i >= 0; ) {
+ sum += data[i] * weights[i];
+ weightsSum += weights[i];
+ }
+ return sum/weightsSum;
+ }
+ /**
+ * Returns the weighted RMS (Root-Mean-Square) of a data sequence.
+ * That is Sum( data[i] * data[i] * weights[i]) / Sum( data[i] * weights[i] )
,
+ * or in other words sumOfProducts / sumOfSquaredProducts
.
+ *
+ * @param sumOfProducts == Sum( data[i] * weights[i] )
.
+ * @param sumOfSquaredProducts == Sum( data[i] * data[i] * weights[i] )
.
+ */
+ public static float rms(float sumOfProducts, float sumOfSquaredProducts) {
+ return sumOfProducts / sumOfSquaredProducts;
+ }
+
+
+ /**
+ * Returns the weighted variance of a data sequence of length N
+ * There are (unfortunately) many different definitions of the unbiased weighted variance.
+ * Here, we use the following formula
+ *
+ * weighted.var(x,w,unbiased=true) = biasCorrection * Sum ( w[i] * (x[i] - mu_w)^2 ) / Sum(w[i]).
+ *
+ * where mu_w
corresponds to the weighted mean and the biasCorrection
+ * term
+ *
+ * biasCorrection = ( Sum(w[i]) )^2 / ( ( Sum(w[i]) )^2 - Sum( w[i]^2 ) ).
+ *
+ * corrects for the bias in the variance.
+ * weighted.var(x,w,unbiased=false) computes the biased weighted variance and is given by the formula above,
+ * but without the correction factor.
+ *
+ *
+ * weighted incremental algorithm section of the link.
+ *
+ * @param unbiased set to true to return the unbiased variance (division by (N-1)), false to return the biased value (division by N).
+ * @return the weighted variance.
+ */
+ public static float var(float[] data, float[] weights, boolean unbiased) {
+ int size = data.length;
+ Check.equalLengths(size,weights);
+ if(size==1) return 0;
+ float weightSum = weights[0]; // Sum of weights from 0 to i
+ float varianceSum = 0;
+ float mean = data[0];
+ float weightSumSquared = weights[0]*weights[0];
+
+ /** at step i:
+ mean= meanOld +weights[i]/weightSum * (data[i] - meanOld)
+ S[i]+= weights[i]*(data[i] - meanOld)*(data[i] - mean)
+ */
+ for(int i=1; iz[i] = ( x[i] - mean ) / standardDeviation
.
+ * @return the standardized array, z
+ */
+ public static float[] zScore(float[] x, float mean, float standardDeviation){
+ float[] z = new float[x.length];
+ for(int i=0; iZ[i][j] = ( X[i][j]- mean(X[,j]) / standardDeviation(X[,j])
+ * where mean(X[,j]) is the mean value of column j
of X
.
+ */
+ public static float[][] zScore(float[][] X, float[] means, float[] standardDeviations){
+ int numRows = X.length; int numColumns = X[0].length;
+ float[][] z = new float[numRows][numColumns];
+ Check.equalLengths(numColumns,means);
+ Check.equalLengths(numColumns,standardDeviations);
+ for(int i=0; i
+ * Dist[i][j] = (X[i][1] - X[j][1]) + (X[i][2] - X[j][2]) + ... + (X[i][n] - X[j][n]).
+ *
+ * mag(y1) = sum_{i=1 to numColumns} (y1[j]*y1[j])
and likewise
+ * for mag(y2)
.
+ * etc.
+ *
+ * Dist[i][j]^2 = (X[i][1] - X[j][1])^2/ invcov[1][1] + ... + (X[i][n] - X[j][n])^2/ invcov[n][n].
+ *
+ *
+ * Rows of X correspond to observations, and columns correspond to variables.
+ * Returns the m-by-m matrix D with D[r1][r2] corresponding to the s
+ * mahalanobis distance between the rows of the input X.
+ */
+ public static float[][] mahalanobis(float[][] X){
+ int numRows = X.length; int numColumns = X[0].length;
+ double[][] invCov = new LU(Correlation.cov(X,true)).solve(Mat.identity(numColumns));
+ float[][] distMatrix = new float[numRows][numRows];
+ for(int r = 0; rr11, r12, ... , r1n
+ *
+ * and
+ * r21, r22, ... , r2n
,
then
+ *
+ * and similarly for the remaining rows.
+ *
+ * Dist[1][2] = 1 - sum_{j=1^numColumns} ( ( r1_j - mean(r1) ) * ( r2_j - mean(r2) ) / ( n * std(r1)*std(r2) ) ),
+ *
This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return the index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int indexOf(float[] array, float valueToFind) { + return indexOf(array, valueToFind, 0); + } + + /** + *Finds the index of the given value in the array starting at the given index.
+ * + *This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * + *A negative startIndex is treated as zero. A startIndex larger than the array + * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int indexOf(float[] array, float valueToFind, int startIndex) { + if (array.length == 0) { + return INDEX_NOT_FOUND; + } + if (startIndex < 0) { + startIndex = 0; + } + for (int i = startIndex; i < array.length; i++) { + if (valueToFind == array[i]) { + return i; + } + } + return INDEX_NOT_FOUND; + } + + + /** + *Finds the last index of the given value within the array.
+ * + *This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * + * @param array the array to travers backwords looking for the object, may be {@code null} + * @param valueToFind the object to find + * @return the last index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int lastIndexOf(float[] array, float valueToFind) { + return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); + } + + /** + *Finds the last index of the given value in the array starting at the given index.
+ * + *This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * + *A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than the + * array length will search from the end of the array.
+ * + * @param array the array to traverse for looking for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the start index to travers backwards from + * @return the last index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int lastIndexOf(float[] array, float valueToFind, int startIndex) { + if (array.length == 0) { + return INDEX_NOT_FOUND; + } + if (startIndex < 0) { + return INDEX_NOT_FOUND; + } else if (startIndex >= array.length) { + startIndex = array.length - 1; + } + for (int i = startIndex; i >= 0; i--) { + if (valueToFind == array[i]) { + return i; + } + } + return INDEX_NOT_FOUND; + } + + /** + *Checks if the value is in the given array.
+ * + *The method returns {@code false} if a {@code null} array is passed in.
+ * + * @param array the array to search through + * @param valueToFind the value to find + * @return {@code true} if the array contains the object + */ + public static boolean contains(float[] array, float valueToFind) { + return indexOf(array, valueToFind) != INDEX_NOT_FOUND; + } + + /** + *Finds the indices containing the specified value in the array.
+ * + *This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array.
+ * + * @param array the array to search through for the value to find, may be {@code null} + * @param valueToFind the value to find + * @return the indices containing the value within the array, + * {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) if not found or {@code null} array input + */ + public static int[] indicesWith(float[] array, float valueToFind) { + return indicesWith(array, valueToFind, 0); + } + + /** + *Finds the indices containing the specified value in the array.
+ * + *This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array.
+ * + * @param array the array to search through for the value to find, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the indices containing the value within the array, + * {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) if not found or {@code null} array input + */ + public static int[] indicesWith(float[] array, float valueToFind, int startIndex) { + if (array == null) { + return INDICES_NOT_FOUND; + } + if (startIndex < 0) { + startIndex = 0; + } + Vector indices = new Vector(); + for (int i = startIndex; i < array.length; i++) { + if (array[i] == valueToFind) { + indices.add(i); + } + } + return checkIndices(indices); + } + + /** + *Finds the indices for the array elements within the min and max value (inclusive).
+ * + *This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array.
+ */ + public static int[] indicesWithin(float[] array, float minVal, float maxVal) { + if (array == null) { + return INDICES_NOT_FOUND; + } + Vector indices = new Vector(); + for (int i = 0; i < array.length; i++) { + if (array[i] >= minVal && array[i]<= maxVal) { + indices.add(i); + } + } + return checkIndices(indices); + } + + /** + *Finds the number of times a given value/object is present in an array.
+ * + * @param array the array to search through for the value/object + * @param valueToFind the object to find. + * @return the number of times that value/object appears in the array + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int numRepeats(float[] array, float valueToFind) { + int count=0; + for (int i=0; iFinds the number of elements less than or equal to the specified value.
+ * + * @param array the array to search through for the value/object + * @param valueOfMax the maximum value + * @return the number of elements ? that value + */ + public static int numLessThanOrEqualTo(float[] array, float valueOfMax) { + int count=0; + for (int i=0; iFinds the number of elements strictly greater than the specified value.
+ * + * @param array the array to search through for the value/object + * @param valueOfMax the maximum value + * @return the number of elements strictly greater than that value + */ + public static int numGreaterThan(float[] array, float valueOfMin) { + int count=0; + for (int i=0; iThis method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @return the index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int indexOf(boolean[] array, boolean valueToFind) { + return indexOf(array, valueToFind, 0); + } + + /** + *Finds the index of the given value in the array starting at the given index.
+ * + *This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * + *A negative startIndex is treated as zero. A startIndex larger than the array + * length will return {@link #INDEX_NOT_FOUND} ({@code -1}).
+ * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} + * array input + */ + public static int indexOf(boolean[] array, boolean valueToFind, int startIndex) { + if (array.length == 0) { + return INDEX_NOT_FOUND; + } + if (startIndex < 0) { + startIndex = 0; + } + for (int i = startIndex; i < array.length; i++) { + if (valueToFind == array[i]) { + return i; + } + } + return INDEX_NOT_FOUND; + } + + /** + *Finds the last index of the given value within the array.
+ * + *This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) if + * {@code null} array input.
+ * + * @param array the array to travers backwords looking for the object, may be {@code null} + * @param valueToFind the object to find + * @return the last index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int lastIndexOf(boolean[] array, boolean valueToFind) { + return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); + } + + /** + *Finds the last index of the given value in the array starting at the given index.
+ * + *This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
+ * + *A negative startIndex will return {@link #INDEX_NOT_FOUND} ({@code -1}). A startIndex larger than + * the array length will search from the end of the array.
+ * + * @param array the array to traverse for looking for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the start index to travers backwards from + * @return the last index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int lastIndexOf(boolean[] array, boolean valueToFind, int startIndex) { + if (array.length == 0) { + return INDEX_NOT_FOUND; + } + if (startIndex < 0) { + return INDEX_NOT_FOUND; + } else if (startIndex >= array.length) { + startIndex = array.length - 1; + } + for (int i = startIndex; i >= 0; i--) { + if (valueToFind == array[i]) { + return i; + } + } + return INDEX_NOT_FOUND; + } + + /** + *Finds the indices containing the specified value in the array.
+ * + *This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array.
+ * + * @param array the array to search through for the value to find, may be {@code null} + * @param valueToFind the value to find + * @return the indices containing the value within the array, + * {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) if not found or {@code null} array input + */ + public static int[] indicesWith(boolean[] array, boolean valueToFind) { + return indicesWith(array, valueToFind, 0); + } + + /** + *Finds the indices containing the specified value in the array.
+ * + *This method returns {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) for a {@code null} input array.
+ * + * @param array the array to search through for the value to find, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the indices containing the value within the array, + * {@link #INDICES_NOT_FOUND} ({@code int[] = -1}) if not found or {@code null} array input + */ + public static int[] indicesWith(boolean[] array, boolean valueToFind, int startIndex) { + if (array == null) { + return INDICES_NOT_FOUND; + } + if (startIndex < 0) { + startIndex = 0; + } + Vector indices = new Vector(); + for (int i = startIndex; i < array.length; i++) { + if (array[i] == valueToFind) { + indices.add(i); + } + } + return checkIndices(indices); + } + + /** + *Finds the number of times a given value/object is present in an array.
+ * + * @param array the array to search through for the value/object + * @param valueToFind the object to find. + * @return the number of times that value/object appears in the array + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ + public static int numRepeats(boolean[] array, boolean valueToFind) { + int count=0; + for (int i=0; i
+ * Frequency freq= new Frequency(dataArray, minimumVal,maximumVal,widthOfEachBin);
+ *
+ * Once initialized, it stores 3 things:
+ *
A number of different methods are available for computing other distribution-related parameters + * (e.g. the relative frequency distribution, the cumulative frequency distribution, etc).
+ * + *See the FrequencyExample in the examples folder for an example.
+ * + * @author Adila Faruk + */ + +// TO DO: OBJECTS!!!! +public class Frequency{ + + private int numBins = 0; + private int ln = 0; + // stored as a float so that we don't mess things up when dividing! + private float[] frequency; + + /** + * Initialize the class by setting the minimum value, maximum value, and the bin width. + * Upon initialization, the number of bins are determined and the + * frequency distribution array is computed for the given input data and histogram bin width. + * This frequency distribution can be accessed using the {@link getFrequency()} function. + *How is the number of bins computed?
+ *
Like this:
+ * bins = floor[ (max - min) / binWidth ] +1
,
+ * as opposed to ceil[ (max - min) / binWidth ]
.
+ * Why? The latter algorithm is problematic when [ (max - min) / binWidth ]
+ * is a whole number.
+ * E.g. let min = 0.0, max = 100.0, and binWidth = 10.0. We get 10 bins with
+ *
What element goes into what bin?
+ *
Let value = data[i];
+ * value
will go into bin number floor( (_inputDat[i]-_minVal)/_binWidth );
+ *
See {@link Frequency(float[] _inputDat, float _minVal, float _maxVal, float _binWidth)} for + * more details. + *
+ */ + public Frequency(int[] _inputDat, int _minVal, int _maxVal, int _binWidth){ + numBins = ( (_maxVal - _minVal)/_binWidth) +1; + frequency = new float[numBins]; + for (int i=0; i<_inputDat.length; i++) { + frequency[ (_inputDat[i]-_minVal)/_binWidth ]++; + } + ln = _inputDat.length; + } + + /** + * Computes the fraction of the total data that's in each bin. + * This is similar to dividing the number in each bin by the total + * number of elements in the data array. + */ + public float[] compRelFrequency() { + return Mat.multiply( frequency, (float)1/ln ); + } + + /** + * Computes the percents of the total data that's in each bin. + * This isimilar to dividing the number in each bin by the total + * number of elements in the data array x 100% + */ + public float[] compRelFrequencyPct() { + return Mat.multiply( frequency, (float)100/ln ); + } + + /** + * Computes the cumulative frequency of the data. + */ + public float[] compCumFrequency() { + float[] cumFreq = new float[frequency.length]; + cumFreq[0] = frequency[0]; + for(int i=1; i+ * + * beta( a, b ) = Γ(a) * Γ(b) ) Γ(a+b) + * + *+ */ + static public float beta(double a, double b) throws ArithmeticException { + double y; + + y = a + b; + y = gamma(y); + if( y == 0.0 ) return 1.0f; + + if( a > b ) { + y = gamma(a)/y; + y *= gamma(b); + } + else { + y = gamma(b)/y; + y *= gamma(a); + } + + return((float)y); + } + /** + * Returns the Gamma function of the argument,
Γ(x)
+ */
+ static public double gamma(double x) throws ArithmeticException {
+
+ double P[] = {
+ 1.60119522476751861407E-4,
+ 1.19135147006586384913E-3,
+ 1.04213797561761569935E-2,
+ 4.76367800457137231464E-2,
+ 2.07448227648435975150E-1,
+ 4.94214826801497100753E-1,
+ 9.99999999999999996796E-1
+ };
+ double Q[] = {
+ -2.31581873324120129819E-5,
+ 5.39605580493303397842E-4,
+ -4.45641913851797240494E-3,
+ 1.18139785222060435552E-2,
+ 3.58236398605498653373E-2,
+ -2.34591795718243348568E-1,
+ 7.14304917030273074085E-2,
+ 1.00000000000000000320E0
+ };
+ //double MAXGAM = 171.624376956302725;
+ //double LOGPI = 1.14472988584940017414;
+
+ double p, z;
+ int i;
+
+ double q = Math.abs(x);
+
+ if( q > 33.0 ) {
+ if( x < 0.0 ) {
+ p = Math.floor(q);
+ if( p == q ) throw new ArithmeticException("gamma: overflow");
+ i = (int)p;
+ z = q - p;
+ if( z > 0.5 ) {
+ p += 1.0;
+ z = q - p;
+ }
+ z = q * Math.sin( Math.PI * z );
+ if( z == 0.0 ) throw new ArithmeticException("gamma: overflow");
+ z = Math.abs(z);
+ z = Math.PI/(z * stirlingFormula(q) );
+
+ return -z;
+ } else {
+ return stirlingFormula(x);
+ }
+ }
+
+ z = 1.0;
+ while( x >= 3.0 ) {
+ x -= 1.0;
+ z *= x;
+ }
+
+ while( x < 0.0 ) {
+ if( x == 0.0 ) {
+ throw new ArithmeticException("gamma: singular");
+ } else
+ if( x > -1.E-9 ) {
+ return( z/((1.0 + 0.5772156649015329 * x) * x) );
+ }
+ z /= x;
+ x += 1.0;
+ }
+
+ while( x < 2.0 ) {
+ if( x == 0.0 ) {
+ throw new ArithmeticException("gamma: singular");
+ } else
+ if( x < 1.e-9 ) {
+ return( z/((1.0 + 0.5772156649015329 * x) * x) );
+ }
+ z /= x;
+ x += 1.0;
+ }
+
+ if( (x == 2.0) || (x == 3.0) ) return z;
+
+ x -= 2.0;
+ p = Polynomial.polyval( x, P );
+ q = Polynomial.polyval( x, Q );
+ return z * p / q;
+
+ }
+ /**
+ * Returns the Incomplete Beta Function evaluated from zero to xx
; formerly named ibeta
.
+ *
+ * @param aa the alpha parameter of the beta distribution.
+ * @param bb the beta parameter of the beta distribution.
+ * @param xx the integration end point.
+ */
+ public static double incompleteBeta( double aa, double bb, double xx ) throws ArithmeticException {
+ double a, b, t, x, xc, w, y;
+ boolean flag;
+
+ if( aa <= 0.0 || bb <= 0.0 ) throw new
+ ArithmeticException("incompleteBeta: Domain error!");
+
+ if( (xx <= 0.0) || ( xx >= 1.0) ) {
+ if( xx == 0.0 ) return 0.0;
+ if( xx == 1.0 ) return 1.0;
+ throw new ArithmeticException("incompleteBeta: Domain error!");
+ }
+
+ flag = false;
+ if( (bb * xx) <= 1.0 && xx <= 0.95) {
+ t = powerSeries(aa, bb, xx);
+ return t;
+ }
+
+ w = 1.0 - xx;
+
+ /* Reverse a and b if x is greater than the mean. */
+ if( xx > (aa/(aa+bb)) ) {
+ flag = true;
+ a = bb;
+ b = aa;
+ xc = xx;
+ x = w;
+ } else {
+ a = aa;
+ b = bb;
+ xc = w;
+ x = xx;
+ }
+
+ if( flag && (b * x) <= 1.0 && x <= 0.95) {
+ t = powerSeries(a, b, x);
+ if( t <= MACHEP ) t = 1.0 - MACHEP;
+ else t = 1.0 - t;
+ return t;
+ }
+
+ /* Choose expansion for better convergence. */
+ y = x * (a+b-2.0) - (a-1.0);
+ if( y < 0.0 )
+ w = incompleteBetaFraction1( a, b, x );
+ else
+ w = incompleteBetaFraction2( a, b, x ) / xc;
+
+ /* Multiply w by the factor
+ x^a (1-x)^b Γ(a+b) / ( a *Γ(a) *Γ(b) ) . */
+
+ y = a * Math.log(x);
+ t = b * Math.log(xc);
+ if( (a+b) < MAXGAM && Math.abs(y) < MAXLOG && Math.abs(t) < MAXLOG ) {
+ t = Math.pow(xc,b);
+ t *= Math.pow(x,a);
+ t /= a;
+ t *= w;
+ t *= gamma(a+b) / (gamma(a) * gamma(b));
+ if( flag ) {
+ if( t <= MACHEP ) t = 1.0 - MACHEP;
+ else t = 1.0 - t;
+ }
+ return t;
+ }
+ /* Resort to logarithms. */
+ y += t + logGamma(a+b) - logGamma(a) - logGamma(b);
+ y += Math.log(w/a);
+ if( y < MINLOG )
+ t = 0.0;
+ else
+ t = Math.exp(y);
+
+ if( flag ) {
+ if( t <= MACHEP ) t = 1.0 - MACHEP;
+ else t = 1.0 - t;
+ }
+ return t;
+ }
+
+ /**
+ * Returns the inverse of the incomplete Beta integral.
+ * That is, given y
, the function finds x
such that
+ * + * incompleteBeta( a, b, x ) = y. + *+ * The routine performs interval halving until
incompleteBeta(a,b,x) - y = 0
,
+ * to within roughly 10^-13
of the true solution. If this precision is not reached, the
+ * method returns the current approximation of x, and prints a warning statement
+ * specifying the current error. Typically, this is on the order of 10^-12.
+ *
+ * @param aa the alpha parameter of the beta distribution.
+ * @param bb the beta parameter of the beta distribution.
+ * @param yy0 the value for which to solve for the corresponding x
in the
+ * incomplete Beta integral.
+ * @return the value x
such that incompleteBeta( a, b, x ) = y
.
+ */
+ public static double incompleteBetaInverse(double aa,double bb,double yy0 )
+ {
+ double a=0, b=0, y0=0, d, y=0, x=0, x0, x1, lgm=0, yp, di=0, dithresh=0, yl, yh, xt;
+ int i=0, rflg=0, dir=0, nflg;
+
+ //ap_error::make_assertion(y>=0&&y<=1);
+ if( yy0 <= 0 )
+ return 0.0;
+ if( yy0 >= 1.0 )
+ return 1.0;
+ x0 = 0.0;
+ yl = 0.0; // ylow
+ x1 = 1.0;
+ yh = 1.0; // yhigh
+
+ int count = 0;
+ double precision = 1e-8;
+
+ /* Make an initial approximation for the inverse function */
+
+ if( aa <= 1.0 || bb <= 1.0 )
+ {
+ dithresh = 1.0e-6;
+ rflg = 0;
+ a = aa;
+ b = bb;
+ y0 = yy0;
+ x = a/(a+b); // approximate x using the mean value
+ y = incompleteBeta( a, b, x );
+ }
+ else{
+ a=aa;
+ b=bb;
+ y0 = yy0;
+
+ yp = -Probability.norminv(yy0);
+ if( yy0 > 0.5 )
+ {
+ yp = -yp; // since the normal distribution is symmetric
+ }
+ // log gamma
+ lgm = (yp * yp - 3.0)/6.0;
+ x = 2.0/( 1.0/(2.0*a-1.0) + 1.0/(2.0*b-1.0) );
+
+ d = yp * Math.sqrt( x + lgm ) / x
+ - ( 1.0/(2.0*b-1.0) - 1.0/(2.0*a-1.0) )
+ * (lgm + 5.0/6.0 - 2.0/(3.0*x));
+ d = 2.0 * d;
+ if( d < MINLOG )
+ {
+ x = 0.0;
+ return x;
+ }
+ x = a/( a + b * Math.exp(d) );
+ y = incompleteBeta( a, b, x );
+ }
+
+ /* This only works because the incompleteBeta function is an increasing function */
+ while (Math.abs(y-y0) > precision && count<1000)
+ {
+ // if y>y0, we're too far right so move x1 to x
+ if (y > y0)
+ {
+ x1 = x;
+ }
+ else // y<=y0 we're too far left so move x0 to the right.
+ {
+ x0 = x;
+ }
+ x = (x1 + x0) / 2;
+ y = incompleteBeta( a, b, x );
+ count++; // so we don't end up in a never-ending loop.
+ }
+ if(count>=1000){
+ System.out.println("Warning: incompleteBetaInverse: Could not solve to specified precision."+
+ "Returning closest match ( error = " + Math.abs(y-y0)+" )");
+ }
+ return x;
+ }
+
+ /**
+ * Continued fraction expansion #1 for incomplete beta integral; formerly named incbcf
.
+ */
+ static double incompleteBetaFraction1( double a, double b, double x ) throws ArithmeticException {
+ double xk, pk, pkm1, pkm2, qk, qkm1, qkm2;
+ double k1, k2, k3, k4, k5, k6, k7, k8;
+ double r, t, ans, thresh;
+ int n;
+
+ k1 = a;
+ k2 = a + b;
+ k3 = a;
+ k4 = a + 1.0;
+ k5 = 1.0;
+ k6 = b - 1.0;
+ k7 = k4;
+ k8 = a + 2.0;
+
+ pkm2 = 0.0;
+ qkm2 = 1.0;
+ pkm1 = 1.0;
+ qkm1 = 1.0;
+ ans = 1.0;
+ r = 1.0;
+ n = 0;
+ thresh = 3.0 * MACHEP;
+ do {
+ xk = -( x * k1 * k2 )/( k3 * k4 );
+ pk = pkm1 + pkm2 * xk;
+ qk = qkm1 + qkm2 * xk;
+ pkm2 = pkm1;
+ pkm1 = pk;
+ qkm2 = qkm1;
+ qkm1 = qk;
+
+ xk = ( x * k5 * k6 )/( k7 * k8 );
+ pk = pkm1 + pkm2 * xk;
+ qk = qkm1 + qkm2 * xk;
+ pkm2 = pkm1;
+ pkm1 = pk;
+ qkm2 = qkm1;
+ qkm1 = qk;
+
+ if( qk != 0 ) r = pk/qk;
+ if( r != 0 ) {
+ t = Math.abs( (ans - r)/r );
+ ans = r;
+ } else
+ t = 1.0;
+
+ if( t < thresh ) return ans;
+
+ k1 += 1.0;
+ k2 += 1.0;
+ k3 += 2.0;
+ k4 += 2.0;
+ k5 += 1.0;
+ k6 -= 1.0;
+ k7 += 2.0;
+ k8 += 2.0;
+
+ if( (Math.abs(qk) + Math.abs(pk)) > big ) {
+ pkm2 *= biginv;
+ pkm1 *= biginv;
+ qkm2 *= biginv;
+ qkm1 *= biginv;
+ }
+ if( (Math.abs(qk) < biginv) || (Math.abs(pk) < biginv) ) {
+ pkm2 *= big;
+ pkm1 *= big;
+ qkm2 *= big;
+ qkm1 *= big;
+ }
+ } while( ++n < 300 );
+
+ return ans;
+ }
+ /**
+ * Continued fraction expansion #2 for incomplete beta integral; formerly named incbd
.
+ */
+ static double incompleteBetaFraction2( double a, double b, double x ) throws ArithmeticException {
+ double xk, pk, pkm1, pkm2, qk, qkm1, qkm2;
+ double k1, k2, k3, k4, k5, k6, k7, k8;
+ double r, t, ans, z, thresh;
+ int n;
+
+ k1 = a;
+ k2 = b - 1.0;
+ k3 = a;
+ k4 = a + 1.0;
+ k5 = 1.0;
+ k6 = a + b;
+ k7 = a + 1.0;;
+ k8 = a + 2.0;
+
+ pkm2 = 0.0;
+ qkm2 = 1.0;
+ pkm1 = 1.0;
+ qkm1 = 1.0;
+ z = x / (1.0-x);
+ ans = 1.0;
+ r = 1.0;
+ n = 0;
+ thresh = 3.0 * MACHEP;
+ do {
+ xk = -( z * k1 * k2 )/( k3 * k4 );
+ pk = pkm1 + pkm2 * xk;
+ qk = qkm1 + qkm2 * xk;
+ pkm2 = pkm1;
+ pkm1 = pk;
+ qkm2 = qkm1;
+ qkm1 = qk;
+
+ xk = ( z * k5 * k6 )/( k7 * k8 );
+ pk = pkm1 + pkm2 * xk;
+ qk = qkm1 + qkm2 * xk;
+ pkm2 = pkm1;
+ pkm1 = pk;
+ qkm2 = qkm1;
+ qkm1 = qk;
+
+ if( qk != 0 ) r = pk/qk;
+ if( r != 0 ) {
+ t = Math.abs( (ans - r)/r );
+ ans = r;
+ } else
+ t = 1.0;
+
+ if( t < thresh ) return ans;
+
+ k1 += 1.0;
+ k2 -= 1.0;
+ k3 += 2.0;
+ k4 += 2.0;
+ k5 += 1.0;
+ k6 += 1.0;
+ k7 += 2.0;
+ k8 += 2.0;
+
+ if( (Math.abs(qk) + Math.abs(pk)) > big ) {
+ pkm2 *= biginv;
+ pkm1 *= biginv;
+ qkm2 *= biginv;
+ qkm1 *= biginv;
+ }
+ if( (Math.abs(qk) < biginv) || (Math.abs(pk) < biginv) ) {
+ pkm2 *= big;
+ pkm1 *= big;
+ qkm2 *= big;
+ qkm1 *= big;
+ }
+ } while( ++n < 300 );
+
+ return ans;
+ }
+ /**
+ * Returns the Incomplete Gamma function; formerly named igamma
.
+ * @param a the parameter of the gamma distribution.
+ * @param x the integration end point.
+ */
+ static public double incompleteGamma(double a, double x)
+ throws ArithmeticException {
+
+
+ double ans, ax, c, r;
+
+ if( x <= 0 || a <= 0 ) return 0.0;
+
+ if( x > 1.0 && x > a ) return 1.0 - incompleteGammaComplement(a,x);
+
+ /* Compute x**a * exp(-x) / gamma(a) */
+ ax = a * Math.log(x) - x - logGamma(a);
+ if( ax < -MAXLOG ) return( 0.0 );
+
+ ax = Math.exp(ax);
+
+ /* power series */
+ r = a;
+ c = 1.0;
+ ans = 1.0;
+
+ do {
+ r += 1.0;
+ c *= x/r;
+ ans += c;
+ }
+ while( c/ans > MACHEP );
+
+ return( ans * ax/a );
+
+ }
+ /**
+ * Returns the Complemented Incomplete Gamma function; formerly named igamc.
+ * @param a the parameter of the gamma distribution.
+ * @param x the integration start point.
+ */
+ static public double incompleteGammaComplement( double a, double x ) throws ArithmeticException {
+ double ans, ax, c, yc, r, t, y, z;
+ double pk, pkm1, pkm2, qk, qkm1, qkm2;
+
+ if( x <= 0 || a <= 0 ) return 1.0;
+
+ if( x < 1.0 || x < a ) return 1.0 - incompleteGamma(a,x);
+
+ ax = a * Math.log(x) - x - logGamma(a);
+ if( ax < -MAXLOG ) return 0.0;
+
+ ax = Math.exp(ax);
+
+ /* continued fraction */
+ y = 1.0 - a;
+ z = x + y + 1.0;
+ c = 0.0;
+ pkm2 = 1.0;
+ qkm2 = x;
+ pkm1 = x + 1.0;
+ qkm1 = z * x;
+ ans = pkm1/qkm1;
+
+ do {
+ c += 1.0;
+ y += 1.0;
+ z += 2.0;
+ yc = y * c;
+ pk = pkm1 * z - pkm2 * yc;
+ qk = qkm1 * z - qkm2 * yc;
+ if( qk != 0 ) {
+ r = pk/qk;
+ t = Math.abs( (ans - r)/r );
+ ans = r;
+ } else
+ t = 1.0;
+
+ pkm2 = pkm1;
+ pkm1 = pk;
+ qkm2 = qkm1;
+ qkm1 = qk;
+ if( Math.abs(pk) > big ) {
+ pkm2 *= biginv;
+ pkm1 *= biginv;
+ qkm2 *= biginv;
+ qkm1 *= biginv;
+ }
+ } while( t > MACHEP );
+
+ return ans * ax;
+ }
+ /**
+ * Returns the natural logarithm of the gamma function; formerly named lgamma
.
+ */
+ public static double logGamma(double x) throws ArithmeticException {
+ double p, q, w, z;
+
+ double A[] = {
+ 8.11614167470508450300E-4,
+ -5.95061904284301438324E-4,
+ 7.93650340457716943945E-4,
+ -2.77777777730099687205E-3,
+ 8.33333333333331927722E-2
+ };
+ double B[] = {
+ -1.37825152569120859100E3,
+ -3.88016315134637840924E4,
+ -3.31612992738871184744E5,
+ -1.16237097492762307383E6,
+ -1.72173700820839662146E6,
+ -8.53555664245765465627E5
+ };
+ double C[] = {
+ 1.00000000000000000000E0,
+ -3.51815701436523470549E2,
+ -1.70642106651881159223E4,
+ -2.20528590553854454839E5,
+ -1.13933444367982507207E6,
+ -2.53252307177582951285E6,
+ -2.01889141433532773231E6
+ };
+
+ if( x < -34.0 ) {
+ q = -x;
+ w = logGamma(q);
+ p = Math.floor(q);
+ if( p == q ) throw new ArithmeticException("logGamma: Overflow");
+ z = q - p;
+ if( z > 0.5 ) {
+ p += 1.0;
+ z = p - q;
+ }
+ z = q * Math.sin( Math.PI * z );
+ if( z == 0.0 ) throw new
+ ArithmeticException("logGamma: Overflow");
+ z = LOGPI - Math.log( z ) - w;
+ return z;
+ }
+
+ if( x < 13.0 ) {
+ z = 1.0;
+ while( x >= 3.0 ) {
+ x -= 1.0;
+ z *= x;
+ }
+ while( x < 2.0 ) {
+ if( x == 0.0 ) throw new
+ ArithmeticException("logGamma: Overflow");
+ z /= x;
+ x += 1.0;
+ }
+ if( z < 0.0 ) z = -z;
+ if( x == 2.0 ) return Math.log(z);
+ x -= 2.0;
+ p = x * Polynomial.polyval( x, B) / Polynomial.polyval( x, C);
+ return( Math.log(z) + p );
+ }
+
+ if( x > 2.556348e305 ) throw new
+ ArithmeticException("logGamma: Overflow");
+
+ q = ( x - 0.5 ) * Math.log(x) - x + 0.91893853320467274178;
+ //if( x > 1.0e8 ) return( q );
+ if( x > 1.0e8 ) return( q );
+
+ p = 1.0/(x*x);
+ if( x >= 1000.0 )
+ q += (( 7.9365079365079365079365e-4 * p
+ - 2.7777777777777777777778e-3) *p
+ + 0.0833333333333333333333) / x;
+ else
+ q += Polynomial.polyval( p, A ) / x;
+ return q;
+ }
+ /**
+ * Power series for incomplete beta integral; formerly named pseries.
+ * Use when b*x is small and x not too close to 1.
+ */
+ static double powerSeries( double a, double b, double x ) throws ArithmeticException {
+ double s, t, u, v, n, t1, z, ai;
+
+ ai = 1.0 / a;
+ u = (1.0 - b) * x;
+ v = u / (a + 1.0);
+ t1 = v;
+ t = u;
+ n = 2.0;
+ s = 0.0;
+ z = MACHEP * ai;
+ while( Math.abs(v) > z ) {
+ u = (n - b) * x / n;
+ t *= u;
+ v = t / (a + n);
+ s += v;
+ n += 1.0;
+ }
+ s += t1;
+ s += ai;
+
+ u = a * Math.log(x);
+ if( (a+b) < MAXGAM && Math.abs(u) < MAXLOG ) {
+ t = Gamma.gamma(a+b)/(Gamma.gamma(a)*Gamma.gamma(b));
+ s = s * t * Math.pow(x,a);
+ } else {
+ t = Gamma.logGamma(a+b) - Gamma.logGamma(a) - Gamma.logGamma(b) + u + Math.log(s);
+ if( t < MINLOG ) s = 0.0;
+ else s = Math.exp(t);
+ }
+ return s;
+ }
+ /**
+ * Returns the Gamma function computed by Stirling's formula; formerly named stirf.
+ * The polynomial STIR is valid for 33 <= x <= 172.
+ */
+ static double stirlingFormula(double x) throws ArithmeticException {
+ double STIR[] = {
+ 7.87311395793093628397E-4,
+ -2.29549961613378126380E-4,
+ -2.68132617805781232825E-3,
+ 3.47222221605458667310E-3,
+ 8.33333333333482257126E-2,
+ };
+ double MAXSTIR = 143.01608;
+
+ double w = 1.0/x;
+ double y = Math.exp(x);
+
+ w = 1.0 + w * Polynomial.polyval( w, STIR);
+
+ if( x > MAXSTIR ) {
+ /* Avoid overflow in Math.pow() */
+ double v = Math.pow( x, 0.5 * x - 0.25 );
+ y = v * (v / y);
+ } else {
+ y = Math.pow( x, x - 0.5 ) / y;
+ }
+ y = SQTPI * y * w;
+ return y;
+ }
+}
diff --git a/drawing/papaya/src/papaya/LU.java b/drawing/papaya/src/papaya/LU.java
new file mode 100644
index 0000000..0ac1b42
--- /dev/null
+++ b/drawing/papaya/src/papaya/LU.java
@@ -0,0 +1,410 @@
+/**
+ * papaya: A collection of utilities for Statistics and Matrix-related manipulations
+ * http://adilapapaya.com/papayastatistics/, 1.1.0
+ * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014
+ *
+ *
+ * Copyright (C) 2014 Adila Faruk http://adilapapaya.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+package papaya;
+
+/** LU Decomposition.
+
+For an m x n
matrix A
with m > n
, the LU decomposition is an m x n
+unit lower triangular matrix L
, an n x n
upper triangular matrix U
,
+and a permutation vector piv
of length m
so that A(piv,:) = L*U
;
+If m < n
, then L
is m x m
and U
is m x n
.
+
+The LU decomposition with pivoting always exists, even if the matrix is
+singular, so the constructor will never fail. The primary use of the
+LU decomposition is in the solution of square systems of simultaneous
+linear equations. This will fail if isNonsingular()
returns false.
+
+Shamelessly copied (and modified) from the +JAMA Java +Matrix package. To make things compatible with how most users use Processing, the +class take in float matrices. However, to preserve the acccuracy of the computations, the algorithm +first casts the input into a double array, prior to doing anything. All methods also return doubles; Use +{@link Cast#doubleToFloat(double[][])} if you want/need to cast everything back to floats for +further (non-high-accuracy-dependant) processing (pun intended). + */ + +public class LU { + + /* ------------------------ +Class variables + * ------------------------ */ + + /** Array for internal storage of decomposition. +@serial internal array storage. + */ + private double[][] LU; + + /** Row and column dimensions, and pivot sign. +@serial column dimension. +@serial row dimension. +@serial pivot sign. + */ + private int numRows, numColumns, pivsign; + + /** Internal storage of pivot vector. +@serial pivot vector. + */ + private int[] piv; + + /* ------------------------ +Constructor + * ------------------------ */ + + /** Constructor. Takes in a matrix and computes the LU matrix. Additional + * parameters can be obtained by calling one of the available methods. + * @see getL() + * @see getU() + * @see getPivot() + * @see getFloatPivot() + * @see det() + */ + public LU (float[][] A) { + + // Use a "left-looking", dot-product, Crout/Doolittle algorithm. + LU = Cast.floatToDouble(A); + numRows = A.length; + numColumns = A[0].length; + piv = new int[numRows]; + for (int i = 0; i < numRows; i++) { + piv[i] = i; + } + pivsign = 1; + double[] LUrowi; + double[] LUcolj = new double[numRows]; + + // Outer loop. + + for (int j = 0; j < numColumns;j++) { + + // Make a copy of the j-th column to localize references. + + for (int i = 0; i < numRows; i++) { + LUcolj[i] = LU[i][j]; + } + + // Apply previous transformations. + + for (int i = 0; i < numRows; i++) { + LUrowi = LU[i]; + + // Most of the time is spent in the following dot product. + + int kmax = Math.min(i,j); + float s = 0.0f; + for (int k = 0; k < kmax; k++) { + s += LUrowi[k]*LUcolj[k]; + } + + LUrowi[j] = LUcolj[i] -= s; + } + + // Find pivot and exchange if necessary. + + int p = j; + for (int i = j+1; i < numRows; i++) { + if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) { + p = i; + } + } + if (p != j) { + for (int k = 0; k < numColumns; k++) { + double t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t; + } + int k = piv[p]; piv[p] = piv[j]; piv[j] = k; + pivsign = -pivsign; + } + + // Compute multipliers. + + if (j < numRows & LU[j][j] != 0.0) { + for (int i = j+1; i < numRows; i++) { + LU[i][j] /= LU[j][j]; + } + } + } + } + + /* ------------------------ +Temporary, experimental code. +------------------------ *\ + +\** LU Decomposition, computed by Gaussian elimination. +
+This constructor computes L and U with the "daxpy"-based elimination +algorithm used in LINPACK and MATLAB. In Java, we suspect the dot-product, +Crout algorithm will be faster. We have temporarily included this +constructor until timing experiments confirm this suspicion. +
+@param A Rectangular matrix
+@param linpackflag Use Gaussian elimination. Actual value ignored.
+@return Structure to access L, U and piv.
+ *\
+
+public LUDecomposition (Matrix A, int linpackflag) {
+ // Initialize.
+ LU = A.getArrayCopy();
+ m = A.getRowDimension();
+ n = A.getColumnDimension();
+ piv = new int[m];
+ for (int i = 0; i < numRows; i++) {
+ piv[i] = i;
+ }
+ pivsign = 1;
+ // Main loop.
+ for (int k = 0; k < numColumns k++) {
+ // Find pivot.
+ int p = k;
+ for (int i = k+1; i < numRows; i++) {
+ if (Math.abs(LU[i][k]) > Math.abs(LU[p][k])) {
+ p = i;
+ }
+ }
+ // Exchange if necessary.
+ if (p != k) {
+ for (int j = 0; j < numColumns;j++) {
+ double t = LU[p][j]; LU[p][j] = LU[k][j]; LU[k][j] = t;
+ }
+ int t = piv[p]; piv[p] = piv[k]; piv[k] = t;
+ pivsign = -pivsign;
+ }
+ // Compute multipliers and eliminate k-th column.
+ if (LU[k][k] != 0.0) {
+ for (int i = k+1; i < numRows; i++) {
+ LU[i][k] /= LU[k][k];
+ for (int j = k+1; j < numColumns;j++) {
+ LU[i][j] -= LU[i][k]*LU[k][j];
+ }
+ }
+ }
+ }
+}
+
+\* ------------------------
+End of temporary code.
+ * ------------------------ */
+
+ /* ------------------------
+Public Methods
+ * ------------------------ */
+
+ /** Is the matrix nonsingular?
+@return true if U, and hence A, is nonsingular.
+ */
+
+ public boolean isNonsingular () {
+ for (int j = 0; j < numColumns;j++) {
+ if (LU[j][j] == 0)
+ return false;
+ }
+ return true;
+ }
+
+ /** Return lower triangular factor, Reference:
+ * Reference:
+ *
+ * For an m-by-n input matrix with m observations and n variables, the output D is
+ * the symmetric m-by-m matrix with zeros along the diagonals and element ij
+ * specifying the distance between rows i and j.
+ *
+ */
+public final class MDS {
+
+ /**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+ protected MDS(){
+
+ }
+ static double MACHEP = PapayaConstants.MACHEP;
+
+ /**
+ * Performs classical (metric) multidimensional scaling, on an input matrix of Distances (computed
+ * using e.g. {@link Distance} ) and outputs the best-fitting p-dimensional configuration of points
+ * (
+ * Remarks
+ *
+ * Guidelines
+ *
+ * Remarks
+ *
+ * Some non-trivial matrix-math related methods, including those for getting
+ * the condition number, inverse, rank, and norms of a 2D input matrix are also included.
+ * Making everything float-based instead of double-based naturally results in some precision loss but
+ * the internal computations are done using doubles to minimize this.
+ *
+ *
+ *
+ * @see Cast
+ * @see Find
+ * @see NaNs
+ * @see Rank
+ * @see Sorting
+ *
+ */
+public final class Mat {
+
+ /**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+ protected Mat(){
+
+ }
+ /**
+ * Appends the part of the specified list between
+ * Examples:
+ *
+ * Examples:
+ *
+ * Examples:
+ * Examples: Contains various methods for dealing with NaNs in your data.
+ * The normal order statistic medians are computed as
+ * Reference:
+ * NIST/SEMATECH e-Handbook of Statistical Methods, EDA Section 1.3.3.21: Normal Probability Plots
+ * @param data the data array
+ * @return the Normal order statistic medians
+ * @see Descriptive#zScore(float[],float,float)
+ */
+ public static float[] normalProbability(float[] data){
+ int size = data.length;
+ float[] N = new float[size];
+ double factor = (double)1/size;
+ double U = Math.pow(.5,factor); // U_n
+ N[0] = (float)Probability.norminv(1-U);
+ N[size-1] = (float)Probability.norminv(U);
+ //U(i) = (i - 0.3175)/(n + 0.365) for i = 2, 3, ..., n-1
+ for(int i=1; i
+ * D'Agostino and Pearson (1973) also introduced the following "omnibus" test statistic,
+ * Requirements
+ * Remarks:
+ *
+ * References:
+ * Preconditions: This implementation is based on a description provided in Chapter 13 of the VassarStats
+ * online textbook
+ * Mean values are not computed directly in the algorithm as this tends to cause round-off error.
+ * Rather, we use the more stable
+ * y = coeff[0]*x^n + coeff[1]*x^(n-1) + .... + coeff[n-1]*x + coeff[n].
+ *
+ * E.g. for a quadratic function, y = coeff[0]*x^2 + coeff[1]*x + coeff[2].
+ *
+ * E.g. for a quadratic function, y = coeff[0]*x^2 + coeff[1]*x + coeff[2].
+ * L
.*/
+ public double[][] getL () {
+ double[][] L = new double[numRows][numColumns];
+ for (int i = 0; i < numRows; i++) {
+ for (int j = 0; j < numColumns;j++) {
+ if (i > j) {
+ L[i][j] = LU[i][j];
+ } else if (i == j) {
+ L[i][j] = 1.0f;
+ } else {
+ L[i][j] = 0.0f;
+ }
+ }
+ }
+ return L;
+ }
+
+ /** Return upper triangular factor, U
*/
+ public double[][] getU () {
+ double[][] U = new double[numColumns][numColumns];
+ for (int i = 0; i < numColumns; i++) {
+ for (int j = 0; j < numColumns;j++) {
+ if (i <= j) {
+ U[i][j] = LU[i][j];
+ } else {
+ U[i][j] = 0.0f;
+ }
+ }
+ }
+ return U;
+ }
+
+ /** Return pivot permutation vector
+@return piv
+ */
+
+ public int[] getPivot () {
+ int[] p = new int[numRows];
+ for (int i = 0; i < numRows; i++) {
+ p[i] = piv[i];
+ }
+ return p;
+ }
+
+ /** Return pivot permutation vector as a one-dimensional float array
+ * @return piv
+ */
+
+ public float[] getFloatPivot () {
+ float[] vals = new float[numRows];
+ for (int i = 0; i < numRows; i++) {
+ vals[i] = (float) piv[i];
+ }
+ return vals;
+ }
+
+ /** Determinant
+ *@return det(A)
+ *@exception IllegalArgumentException Matrix must be square
+ */
+ public double det () {
+ if (numRows != numColumns) {
+ throw new IllegalArgumentException("Matrix must be square.");
+ }
+ double d = (double) pivsign;
+ for (int j = 0; j < numColumns;j++) {
+ d *= LU[j][j];
+ }
+ return d;
+ }
+
+ /** Solve A*x = b for x
+ * @param b An array with as many elements as the number of rows in A.
+ * @return x so that L*U*x = b(piv,:)
+ * @exception IllegalArgumentException Matrix row dimensions must agree.
+ * @exception RuntimeException Matrix is singular.
+ */
+ public double[] solve (float[] b, boolean returnZeroesForSingularCase) {
+ if (b.length != numRows) {
+ throw new IllegalArgumentException("Matrix row dimensions must agree.");
+ }
+ if (!this.isNonsingular()) {
+ if(returnZeroesForSingularCase) return new double[numColumns];
+ else throw new RuntimeException("Matrix is singular.");
+ }
+
+ // Copy right hand side with pivoting
+ double[] X = Cast.floatToDouble(Mat.populate(b,piv));
+ //System.out.println(X.length+","+b.length+","+LU[0].length);
+ // Solve L*Y = B(piv,:)
+ for (int k = 0; k < numColumns; k++) {
+ for (int i = k+1; i < numColumns; i++) {
+ X[i] -= X[k]*LU[i][k];
+ }
+ }
+ // Solve U*X = Y;
+ for (int k = numColumns-1; k >= 0; k--) {
+ X[k] /= LU[k][k];
+ for (int i = 0; i < k; i++) {
+ X[i] -= X[k]*LU[i][k];
+ }
+ }
+ //Mat.print(X,3); System.out.print("\n");
+ return X;
+ }
+
+ /** Solve A*X = B
+ * @param B A Matrix with as many rows as A and any number of columns.
+ * @return X so that L*U*X = B(piv,:)
+ * @exception IllegalArgumentException Matrix row dimensions must agree.
+ * @exception RuntimeException Matrix is singular.
+ */
+ public double[][] solve (float[][] B) {
+ if (B.length != numRows) {
+ throw new IllegalArgumentException("Matrix row dimensions must agree.");
+ }
+ if (!this.isNonsingular()) {
+ throw new RuntimeException("Matrix is singular.");
+ }
+
+ // Copy right hand side with pivoting
+ int nx = B[0].length;
+ double[][] X = subMatrix(B,piv,0,nx-1);
+ // Solve L*Y = B(piv,:)
+ for (int k = 0; k < numColumns; k++) {
+ for (int i = k+1; i < numColumns; i++) {
+ for (int j = 0; j < nx; j++) {
+ X[i][j] -= X[k][j]*LU[i][k];
+ }
+ }
+ }
+ // Solve U*X = Y;
+ for (int k = numColumns-1; k >= 0; k--) {
+ for (int j = 0; j < nx; j++) {
+ X[k][j] /= LU[k][k];
+ }
+ for (int i = 0; i < k; i++) {
+ for (int j = 0; j < nx; j++) {
+ X[i][j] -= X[k][j]*LU[i][k];
+ }
+ }
+ }
+ return X;
+ }
+
+ /**
+ * Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).
+ * @param inputMat input matrix
+ * @param rowIndices the row indices
+ * @param columnStart column to start at (inclusive)
+ * @param columnEnd column to end at (inclusive)
+ * @return the submatrix containing a copy of the input matrix's specified rows from
+ * columnStart to columnEnd; copy of A(rowIndices(:), columnStart:columnEnd)
.
+ * @throws IllegalArgumentException if the inputs are not within the inputMatrix size bounds.
+ */
+ private static double[][] subMatrix(float[][] inputMat, int[] rowIndices, int columnStart, int columnEnd) {
+ int numRows = rowIndices.length;
+ int numColumns = columnEnd-columnStart+1;
+ double[][] outputMat = new double[numRows][numColumns];
+ for (int i = 0; iz
and the y
.
+ * @param x the x data
+ * @param y the y data
+ * @return a 2-by-1 array with the first element coeff[0]
corresponding to
+ * the slope, and the second element coeff[1]
equal to the y-intercept.
+ */
+ public static float[] bestFit(float[] x, float[] y){
+ int size = x.length;
+ Check.equalLengths(size,y);
+ if(size<3) throw new IllegalArgumentException("Each data array needs to hold at least 3 values.");
+
+ // the best-fit linear line coefficients.
+ double sumX = 0; double sumY = 0; double sumXY = 0; double sumXX = 0;
+ for(int i=0; i intercept = 1
will result in z = slope*x+1
.
+ * The slope
is computed by minimizing the sum of least squares
+ * between z
and the y
.
+ * @param x the x data
+ * @param y the y data
+ * @param intercept the intercept of the best-fit line with the y-axis.
+ *
+ * @return the slope of the best-fit linear line.
+ */
+ public static float bestFit(float[] x, float[] y, float intercept){
+ int size = x.length;
+ Check.equalLengths(size,y);
+ if(size<3) throw new IllegalArgumentException("Each data array needs to hold at least 3 values.");
+ // the best-fit linear line coefficients.
+ double sumX = 0; double sumY = 0; double sumXY = 0; double sumXX = 0;
+ for(int i=0; i z_i = (slope*x_i + intercept)
is the best fit linear line.
+ * @param slope the slope of the best-fit linear line
+ * @param intercept the y-intercept of the best fit linear line
+ */
+ public static float[] residuals(float[] x, float[] y, float slope, float intercept){
+ int size = x.length; Check.equalLengths(size,y);
+ float[] delta = new float[size];
+ for(int i=0; i z_i = (slope*x_i + intercept)
is the best fit linear line.
+ * You'd basically use this to compute the spread of a best fit line (max - min)
+ */
+ public static float[] residuals(float[] x, float[] y){
+ int size = x.length; Check.equalLengths(size,y);
+ float[] params = bestFit(x,y);
+ float[] delta = new float[size];
+ for(int i=0; i
+ * slopeStdErr = residualStdErr / Sum ( (x[i] - mean(x))^2 ).
+ *
+ * @param x the independent variable
+ * @param residualStdErr the standard error of the residual
+ * @return the error in the slope.
+ */
+ public static float slope(float[] x, float residualStdErr ){
+ float v = Descriptive.varianceNotNormalized(x);
+ double slopeError = residualStdErr/Math.sqrt(v);
+ return (float)slopeError;
+ }
+ /**
+ * Returns the standard error of the computed slope given x and the
+ * standard error in the residuals. That is,
+ *
+ * interceptStdErr = slopeStdErr / Sqrt( Sum(x[i]*x[i])/size ).
+ *
+ * where the slopeStdErr is computed using the methods specified
+ * in {@link StdErr#slope}.
+ * @param x the independent variable
+ * @param residualStdErr the standard error of the residual
+ * @return error in the slope.
+ */
+ public static float intercept(float[] x, float residualStdErr ){
+ int size = x.length;
+ float slopeError = slope(x,residualStdErr);
+ double sumXX = 0;
+ for(int i=0; i t = slope/slopeStdErr,
+ * @param slope the slope of the best fit linear line
+ * @param slopeStdErr the standard error of the slope
+ * @param df the degrees of freedom (typically (n-2), but could also be (n-1) if the
+ * intercept was previously specified.
+ */
+ public static float slope(float slope, float slopeStdErr, int df){
+ double t = slope/slopeStdErr;
+ System.out.println("t slope: "+t);
+ return (float)twoTailedStudentT(t,df);
+ }
+ /**
+ * Returns the p-value, or significance, of the computed intercept under the null-hypothesis
+ * of intercept = 0 (two-tailed test). The p-value is computed using the student-T distribution
+ * with df degrees of freedom and test statistic
+ * t=intercept/interceptStdErr
+ * @param intercept the intercept of the best fit linear line
+ * @param interceptStdErr the standard error of the intercept
+ * @param df the degrees of freedom (n-2)
+ */
+ public static float intercept(float intercept, float interceptStdErr, int df){
+ double t = intercept/interceptStdErr;
+ System.out.println("t intercept: "+t);
+ return (float)twoTailedStudentT(t,df);
+ }
+ }
+
+ /**
+ * Contains methods related to the Box-Cox transformation of a data set; useful in
+ * determining the best transformation that will yield the best method for converting
+ * a monotonic, non-linear relationship between x
and y
into
+ * a linear one.
+ *
Box, George E. P.; Cox, D. R. (1964). "An analysis of transformations".
+ * Journal of the Royal Statistical Society, Series B 26 (2): 211?252.
+ *
+ * NIST/SEMATECH e-Handbook of Statistical Methods, EDA Section 1.3.3.6: Box-Cox Linearity Plots
+ * (The reader's digest version of the original.)
+ *
+ */
+ public static class BoxCox{
+ protected BoxCox(){}
+
+ /**
+ * Performs the box-cox transformation on y
for a sequence of λ
+ * and returns the array of linear correlation coefficients
+ * between the x
and the box-cox transformed y
.
+ * at each value of λ .
+ * The value of λ corresponding to the maximum correlation
+ * (or minimum for negative correlation) is the optimal choice for
+ * λ such that the relationship between x and T(y) is linear.
+ *
+ * NIST/SEMATECH e-Handbook of Statistical Methods, EDA Section 1.3.3.6: Box-Cox Linearity Plots
+ *
+ * @param x the independent data array
+ * @param y the dependent data array
+ * @param lambda an array of lambda values
+ * @return the array containing the linear correlation coefficients, r.
+ */
+ public static float[] correlation(float[] x, float[] y, float []lambda){
+ /* Check that the data is all positive */
+ if(Descriptive.min(x)<=0) throw new IllegalArgumentException("y (the dependent variable) has to be strictly > 0.");
+ int size = x.length;
+ Check.equalLengths(size,y);
+ float[] transformed = new float[size];
+ int numSteps = lambda.length;
+ float[] corrcoef = new float[numSteps];
+ for(int i=0; iy
are >0.
+ * @param data the data to be transformed
+ * @param lambda the lambda value
+ * @return the transformed data, or y(λ) above.
+ * */
+ public static float[] transform(float[] data, float lambda){
+ /* Check that the data is all positive */
+ if(Descriptive.min(data)<=0) throw new IllegalArgumentException("data has to be strictly > 0.");
+ int size = data.length;
+ float[] transformed = new float[size];
+ if(lambda==0){
+ for(int i=0; ip<N
). The output matrix Y
has p columns with rows
+ * giving the coordinates of the points chosen to represent the dissimilarities.
+ * When D
corresponds to the Euclidian distance matrix, the p
+ * dimensional coordinates correspond exactly to the first p
principal components.
+ *
+ *
>
+ * @param D the input distance matrix (see {@link Distance})
+ * @param p the dimension (number of columns) of the output matrix.
+ * @param showEigenvalues whether to print the eigenvalues to the screen or otherwise.
+ * @return Y, N-by-p (or N-by-k) matrix consisting of N points in each of the p (or k) dimensions.
+ */
+ public static float[][] classical(float[][] D, int p, boolean showEigenvalues){
+ Check.square(D);
+ int n = D.length;
+ float[][] P = createP(n);
+ float[][] B = new float[n][n];
+ double sum = 0.0;
+ // Doubly center D
+ for(int i=0; ip > k
where k
is the number of
+ * positive eigenvalues of Y*Y'
, then only the
+ * first k
columns of
Y
are computed.
+ * showEigenvalues
to true to print the
+ * values of the "scalar product matrix" Y*Y'
to the screen.
+ * (This is a good idea if you want to know how well the first p
dimensions
+ * captures most of the variation in D
.
+ * Methods that modify an array by a value always take the array as the first parameter, and
+ * the value as the second.
+ * For example,
+ *
+ *
+ * I try to follow that guiding principle with all methods that take in an input
+ * array (e.g. {@link swap(float[] array, int index1, int index2)},
+ * {@link populate(float[] array, int[] indices)}).
+ *
{@link divide(float[] array, float numberToDivideBy)}
+ *
{@link multiply(float[] array, float numberToMultiplyBy)}
+ *
{@link sum(float[] array, float numberToAdd)}
+ *
+ * Almost all the methods here take in float arrays or matrices, although I've also added in a few
+ * methods that operate on integers. In addition, to avoid accidental array manipulation,
+ * all methods return a new array or matrix instead of modifying the original one. E.g.
+ * C[i][j] = A[i][j]*B[i][j] return C
+ *
as opposed to
+ * A[i][j] = A[i][j]*B[i][j], return A
+ *
in {@link dotMultiply(float[][],float[][])}.
+ * (If you find it performing to the contrary though, please let me know!)
+ *
+ * from
(inclusive) and
+ * to
(inclusive) to the receiver.
+ *
+ *@param listToAddTo ArrayList to add the elements to. Clears this list before
+ * adding the elements in.
+ * @param data the array to be added to the listToAddTo.
+ * @param from the index of the first element to be appended (inclusive).
+ * @param to the index of the last element to be appended (inclusive).
+ * @exception IndexOutOfBoundsException index is out of range
+ * ((from<0 || from>to || to>=sortedData.length)
).
+ */
+ private static void addAllOfFromTo(ArrayList listToAddTo, float[] sortedData,int from, int to){
+ if (from<0 || from>to || to>sortedData.length) {
+ throw new IndexOutOfBoundsException("from: "+from+", to: "+to+", sortedData.length: "+sortedData.length);
+ }
+ // print a warning just in case.
+ if(listToAddTo.size()>0){
+ System.out.println("Warning: Input ArrayList has size "+listToAddTo.size()+
+ ". Appending the data to the end of the end");
+ }
+ for(int i = from; i<=to; i++){
+ listToAddTo.add(sortedData[i]);
+ }
+ }
+
+ /** Return absolute values of an array */
+ public static float[] abs(float[] array){
+ float[] output = new float[array.length];
+ for(int i=0; ia
and b
into a new array
+ * c
such that c = {a,b}
.
+ */
+ public static float[] concat(float[] a, float[] b) {
+ float[] c= new float[a.length+b.length];
+ System.arraycopy(a, 0, c, 0, a.length);
+ System.arraycopy(b, 0, c, a.length, b.length);
+ return c;
+ }
+ public static int[] concat(int[] a, int[] b) {
+ int[] c= new int[a.length+b.length];
+ System.arraycopy(a, 0, c, 0, a.length);
+ System.arraycopy(b, 0, c, a.length, b.length);
+ return c;
+ }
+
+ /**
+ * Returns an array with of length size
with
+ * each element equal to the specified constValue
.
+ * @see Arrays.fill
+ */
+ public static float[] constant(float constValue, int size){
+ float[] constant = new float[size];
+ for(int i=0; iconstValue
.
+ * @see Arrays.fill
+ */
+ public static int[] constant(int constValue, int size){
+ int[] constant = new int[size];
+ for(int i=0; ivalue
.
+ * That is, z[i] = x[i] / value
.
+ * @throws IllegalArgumentException if value
is zero.
+ */
+ public static float[] divide(float[] data, float value){
+ if(value==0) throw new IllegalArgumentException("Division by zero");
+ float[] divided = new float[data.length];
+ for(int i=0; iend
(inclusive):
+ * y[0] = start; y[1] = start+1; ... , y[n] = end;
.
+ *
+ * For example, int[] ii = linspace(2,5);
+ * produces the array ii = {2,3,4,5};
with length 4.
+ *
+ * It is similar to this:
+ *
+ * @param start the start index (inclusive)
+ * @param end the end index (inclusive);
+ */
+ public static int[] linspace(int start, int end){
+ int[] indices = new int[end-start+1];
+ for(int i=0; i
+ * for(int i=0; i<=end-start+1; i++){
+ *
+ *
y[i] = i + start;
+ *
}
+ * nf
or one of the other variants of it.
+ * @param data the matrix to be printed.
+ * @param d Number of digits after the decimal to display..
+ */
+ public static void print(double[] data, int d) {
+ if(d<0) throw new IllegalArgumentException("Can't display "+d+" decimal digits without recreating" +
+ "the number system.");
+ int numColumns = data.length;
+ // number format
+ DecimalFormat format = setupFormat(d);
+ for (int j=0; jnf
or one of the other variants of it.
+ * @param data the matrix to be printed.
+ * @param d Number of digits after the decimal to display..
+ */
+ public static void print(double[][] data, int d) {
+ if(d<0) throw new IllegalArgumentException("Can't display "+d+" decimal digits without recreating" +
+ "the number system.");
+ int numRows = data.length;
+ // number format
+ DecimalFormat format = setupFormat(d);
+ for (int i=0; inf
or one of the other variants of it.
+ * @param data the matrix to be printed.
+ * @param d Number of digits after the decimal to display..
+ */
+ public static void print(float[] data, int d) {
+ if(d<0) throw new IllegalArgumentException("Can't display "+d+" decimal digits without recreating" +
+ "the number system.");
+ int numColumns = data.length;
+ // number format
+ DecimalFormat format = setupFormat(d);
+ for (int j=0; jnf
or one of the other variants of it.
+ * @param data the matrix to be printed.
+ * @param d Number of digits after the decimal to display..
+ */
+ public static void print(float[][] data, int d) {
+ if(d<0) throw new IllegalArgumentException("Can't display "+d+" decimal digits without recreating" +
+ "the number system.");
+ int numRows = data.length;
+ // number format
+ DecimalFormat format = setupFormat(d);
+ for (int i=0; inf
or one of the other variants of it.
+ * @param data the matrix to be printed.
+ * @param d Number of digits after the decimal to display..
+ */
+ public static void print(int[] data, int d) {
+ if(d<0) throw new IllegalArgumentException("Can't display "+d+" decimal digits without recreating" +
+ "the number system.");
+ int numColumns = data.length;
+ // number format
+ DecimalFormat format = setupFormat(d);
+ for (int j=0; jnf
or one of the other variants of it.
+ * @param data the matrix to be printed.
+ * @param d Number of digits after the decimal to display..
+ */
+ public static void print(int[][] data, int d) {
+ if(d<0) throw new IllegalArgumentException("Can't display "+d+" decimal digits without recreating" +
+ "the number system.");
+ int numRows = data.length;
+ // number format
+ DecimalFormat format = setupFormat(d);
+ for (int i=0; inewValue
. Useful, for example, in replacing zeros with
+ * some other number (for {@link dotDivide(float[][], float[][])}), or for wall-street related book-keeping...
+ */
+ public static float[][] replace(float[][] A, float oldValue, float newValue){
+ int numRows = A.length;
+ int numColumns = A[0].length;
+ float[][] C = new float[numRows][numColumns];
+ for(int i=0; inewValue
. Useful, for example, in replacing zeros with
+ * some other number (for {@link divide(float[], float[])}), or for wall-street related book-keeping...
+ */
+ public static float[] replace(float[] data, float oldValue, float newValue){
+ int numRows = data.length;
+ float[]C = new float[numRows];
+ for(int i=0; i
roundToDecimalPlace(1.234567, 2) --> 1.23;
+ *
roundToDecimalPlace(100, 2) --> 100;
+ *
floorToNearest(173.2,5) --> 170;
+ *
floorToNearest(28,10) --> 20;
+ *
ceilToNearest(173.2,5) --> 175;
+ *
ceilToNearest(28,10) --> 30;
+ * splitters=(a,b,c,...,y,z)
defines the ranges [-inf,a), [a,b), [b,c), ..., [y,z), [z,inf]
.
+ *
+ *
+ *
+ * @param sortedList the list to be partitioned (must be sorted ascending).
+ * @param splitters the points at which the list shall be partitioned (must be sorted ascending).
+ * @return the sublists (an array with data = (1,2,3,4,5,8,8,8,10,11)
.
+ * splitters=(2,8)
yields 3 bins: (1), (2,3,4,5) (8,8,8,10,11)
.
+ * splitters=()
yields 1 bin: (1,2,3,4,5,8,8,8,10,11)
.
+ * splitters=(-5)
yields 2 bins: (), (1,2,3,4,5,8,8,8,10,11)
.
+ * splitters=(100)
yields 2 bins: (1,2,3,4,5,8,8,8,10,11), ()
.
+ * length == splitters.length + 1
.
+ * Each sublist is returned sorted ascending.
+ */
+ public static ArrayList[] split(float[] sortedList, float[] splitters) {
+ // assertion: data is sorted ascending.
+ // assertion: splitValues is sorted ascending.
+ int numBins = splitters.length + 1;
+
+ ArrayList[] bins = new ArrayList[numBins];
+ for (int i=numBins; --i >= 0;) bins[i] = new ArrayList();
+
+ int listSize = sortedList.length;
+ int nextStart = 0;
+ int i=0;
+ while (nextStart < listSize && i < numBins-1) {
+ float splitValue = splitters[i];
+ int index = Arrays.binarySearch( sortedList,splitValue );
+ if (index < 0) { // splitValue not found
+ int insertionPosition = -index - 1;
+ addAllOfFromTo(bins[i], sortedList, nextStart,insertionPosition-1);
+ nextStart = insertionPosition;
+ }
+ else { // splitValue found
+ // For multiple identical elements ("runs"), binarySearch does not define which of all valid indexes is returned.
+ // Thus, skip over to the first element of a run.
+ do {
+ index--;
+ } while (index >= 0 && sortedList[index] == splitValue);
+
+ addAllOfFromTo(bins[i], sortedList, nextStart, index);
+ nextStart = index + 1;
+ }
+ i++;
+ }
+ // now fill the remainder
+ addAllOfFromTo(bins[numBins-1] , sortedList, nextStart,sortedList.length-1);
+ return bins;
+ }
+
+ /**
+ * Get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).
+ * @param inputMat input matrix
+ * @param rowStart row to start at (inclusive)
+ * @param rowEnd row to end at (inclusive)
+ * @param columnStart column to start at (inclusive)
+ * @param columnEnd column to end at (inclusive)
+ * @return submatrix containing (rowEnd-rowStart+1) rows and (columnEnd-columnStart+1) columns;
+ * copy of A(rowStart:rowEnd, columnStart:columnEnd)
.
+ * @throws IllegalArgumentException if the rowStart, rowEnd, columnStart, or columnEnd parameters
+ * are not within the inputMatrix size bounds.
+ */
+ public static float[][] subMatrix(float[][] inputMat, int rowStart, int rowEnd, int columnStart, int columnEnd) {
+ int numRows = rowEnd-rowStart+1;
+ int numColumns = columnEnd-columnStart+1;
+ if(numRows>inputMat.length || rowStart<0 || rowEnd>inputMat.length-1) throw new IllegalArgumentException("Row indices are not within the matrix bounds");
+ if(numColumns>inputMat[0].length || columnStart<0 || columnEnd>inputMat[0].length-1) throw new IllegalArgumentException("Column indices are not within the matrix bounds");
+ float[][] outputMat = new float[numRows][numColumns];
+
+ for (int i = 0; iNaN.
+ *
+ * @param data array to search for NaNs
+ * @return list of indexes i such that data[i] = NaN
+ */
+ public static ArrayListNaN.
+ *
+ * @param data array to search for NaNs
+ * @return list of indexes i such that data[i] = NaN
+ */
+ public static ArrayListNaN.
+// *
+// * @param data array to search for NaNs
+// * @return list of indexes i such that data[i] = NaN
+// */
+//public static ArrayListN
) necessary to produce a Q-Q plot
+ * for a normal distribution (or normal probability plot).
+ * That is, given a data array, this method computes the Normal order statistic medians
+ * and the ordered response values or z
-scores of the input.
+ * A plot of the N
vs z
should form an approximate straight line if the
+ * data is normally distributed;
+ * departures from this straight line indicate departures from normality with the shape of the line
+ * providing clues as to the distribution of the data.
+ * N[i] = Probability.norminv(U[i])
+ * where U[i]
are the uniform order statistic medians, given by
+ *
+ *
+ *
U[i] = 1 - U(n) for i = 1
+ *
U[i] = (i - 0.3175)/(n + 0.365) for i = 2, 3, ..., n-1
+ *
U[i] = 0.5^(1/n) for i = n
+ *
Let mk
denote the k
th moment,
+ * mk = ∑_(i=1 to n)(x_i - mean(x))/n.
Then, we can define the following:
+ *
Sample estimate of the third standardized moment: b1 = m3/(m2)^(3/2)
+ *
Sample estimate of the fourth standardized moment: b2 = m4/m2
+ *
The test statistics Z(b1), Z(b2) are approximately normally distributed under
+ * the null hypothesis of population normality. Both Z(b1) and Z(b2) can be used to
+ * test one-sided and two-sided alternative hypothesis.
+ *
+ * K^2 = (Z(b1))^2 + (Z(b2))^2
,
+ * which is able to detect deviations from normality due to either skewness or kurtosis
+ * This K^2 statistic has an approximately Chi-Squared distribution with 2 degrees of
+ * freedom when the population is normally distributed.
+ * Similar to Z(b1) and Z(b2), K^2 can be used to test one-sided and two-sided alternative hypothesis.
+ *
+ *
data set has to have more than 20 elements. An error is thrown otherwise since the normal
+ * approximation is no longer valid (technically, the it is valid for Zb1, as long as n > 8, but
+ * since we're computing everything, the requirement has been set a little higher.
+ *
+ *
+ *
+ *g1
+ * and g2
used in SAS, and SPSS but they are related by a linear transformation (see
+ * cited paper for details).
+ * dagoTest
class in the fBasics library albeit with some implementation.
+ *
D'Agostino, R., Albert Belanger, A., D'Agostino Jr, R., 1990
+ * A Suggestion for Using Powerful and Informative Tests of Normality
+ *
+ *
D?Agostino, R., Pearson, E., 1973. Tests for departures from normality.
+ * Empirical results for the distribution of b1 and b2.Biometrika 60, 613?622.
+ */
+ public static class Dago {
+
+ protected Dago(){};
+
+ /**
+ * Computes and returns an array containing the test statistic zb1
associated with sqrt(b1) and
+ * the significance, or "p-value" of the skew test statistic zb1
, assuming a
+ * two-tailed null hypothesis as well as . The first element of the output array is the
+ * test statistic zb1
associated with sqrt(b1) = m3/m2^(3/2) where mk is
+ * the kth momement as specified in {@link Descriptive#moment} and the second element is
+ * the p-value for the two-tailed null hypothesis with
+ *
+ * pValueSkew = 2* ( 1-Probability.normcdf( zb1 ) ).
+ *
+ * (D'Agostino et al. (1990)) .
+ */
+ public static float[] skew(float[] data){
+ float n = data.length;
+ checkLength20((int)n);
+
+ float mean = Descriptive.mean(data);
+ float m2 = Descriptive.moment(data,2,mean);
+ double sd = Math.sqrt(m2);
+ double m3 = Descriptive.moment(data,3,mean);
+ double b1 = m3/(sd*sd*sd);
+ double y = b1* Math.sqrt( (n+1)*(n+3)/( 6*(n-2) ) ); // standardized b1
+ // standardized moment
+ double beta2 = 3*(n*n+27*n-70)*(n+1)*(n+3)/ ( (n-2)*(n+5)*(n+7)*(n+9) );
+ double Wsquared = -1 + Math.sqrt(2*(beta2-1));
+ double W = Math.sqrt(Wsquared);
+ double delta = 1/Math.sqrt(Math.log(W));
+ double alpha = Math.sqrt(2/(Wsquared-1));
+ double yalpha = y/alpha;
+ double Zb1 = (delta*Math.log(yalpha + Math.sqrt(yalpha*yalpha+1)));
+
+ return new float[]{(float)Zb1,(float)twoTailedNormal(Zb1)};
+ }
+
+ /**
+ * Computes and returns an array containing the test statistic zb2
associated with
+ * b2 and the significance, or "p-value" of the kurtosis test statistic zb2
, assuming a
+ * two-tailed null hypothesis as well as . The first element of the output array is the
+ * test statistic zb2
associated with b2 = m4/m2^2 where mk is
+ * the kth momement as specified in {@link Descriptive#moment}. and the second element is
+ * the p-value for the two-tailed null hypothesis with
+ *
+ * pValueKurtosis = 2* ( 1-Probability.normcdf( zb2 ) ).
+ *
+ * (D'Agostino et al. (1990)) .
+ */
+ public static float[] kurtosis(float[] data){
+ float n = data.length;
+ checkLength20((int)n);
+ float mean = Descriptive.mean(data);
+ float m2 = Descriptive.moment(data,2,mean);
+ double m4 = Descriptive.moment(data,4,mean);
+
+ double b2 = m4/(m2*m2);
+ double b2Mean = 3*(n-1)/(n+1);
+ double b2Var = 24*n*(n-2)*(n-3)/( (n+1)*(n+1)*(n+3)*(n+5) );
+ double x = (b2-b2Mean)/Math.sqrt(b2Var);
+ double beta1 = 6*(n*n-5*n+2)/((n+7)*(n+9)) * Math.sqrt( (6*(n+3)*(n+5)) / (n*(n-2)*(n-3)) ); // third standardized moment of b2
+ double A = 6+8/beta1* ( 2/beta1 + Math.sqrt(1+ 4/(beta1*beta1)) );
+ double nineA = 9*A;
+ double temp = (1-2/A)/( 1+x*Math.sqrt(2/(A-4)) );
+ double Zb2 = ( (1-2/(nineA) - Math.pow (temp,1.0/3.0) )/ Math.sqrt(2/(nineA)));
+ return new float[]{(float)Zb2,(float)twoTailedNormal(Zb2)};
+ }
+
+ /**
+ * Computes and returns an array containing the test statistic chi2
, or the
+ * "omnibus" test statistic, and the significance, or "p-value" of this test statistic.
+ * The first element of the output array is the test statistic the ombibus test
+ * statistic chi2
and the second element is
+ * the p-value:
+ *
+ * pValueChiSquared = ( 1-Probability.chi2cdf( chi2 ) ).
+ *
+ * (D'Agostino et al. (1990)) .
+ */
+ public static float[] chi2(float[] data){
+ float n = data.length;
+ checkLength20((int)n);
+ float Zb1 = skew(data)[0]; float Zb2 = kurtosis(data)[0];
+ double Ksquared = Zb1*Zb1 + Zb2*Zb2;
+ float p = (float)( 1-Probability.chi2cdf(Ksquared,2.0) );
+ return new float[]{(float)Ksquared,p};
+ }
+ /**
+ * Returns an array containing the three significance, or p-values, for testing normality.
+ * The first element corresponds to the p-value associated with D'Agostino's Chi-Squared
+ * test statistic (or omnibus test, {@link Dago#chi2}), the second corresponds to D'Agostino's
+ * skew test ({@link Dago#skew}), and the
+ * third corresponds to D'Agostino's kurtosis test ({@link Dago#kurtosis}).
+ */
+ public static float[] pValues(float[] data){
+ float n = data.length;
+ if(n<20) throw new IllegalArgumentException("Dataset contains "+n+" elements which is" +
+ "less than the minimum of 20.");
+ float mean = Descriptive.mean(data);
+ float m2 = Descriptive.moment(data,2,mean);
+ double sd = Math.sqrt(m2);
+ double m3 = Descriptive.moment(data,3,mean);
+ double m4 = Descriptive.moment(data,4,mean);
+ double b1 = m3/(sd*sd*sd);
+ double y = b1* Math.sqrt( (n+1)*(n+3)/( 6*(n-2) ) ); // standardized b1
+ // standardized moment
+ double beta2 = 3*(n*n+27*n-70)*(n+1)*(n+3)/ ( (n-2)*(n+5)*(n+7)*(n+9) );
+ double Wsquared = -1 + Math.sqrt(2*(beta2-1));
+ double W = Math.sqrt(Wsquared);
+ double delta = 1/Math.sqrt(Math.log(W));
+ double alpha = Math.sqrt(2/(Wsquared-1));
+ double yalpha = y/alpha;
+ double Zb1 = (delta*Math.log(yalpha + Math.sqrt(yalpha*yalpha+1)));
+ // kurtosis
+ double b2 = m4/(m2*m2);
+ double b2Mean = 3*(n-1)/(n+1);
+ double b2Var = 24*n*(n-2)*(n-3)/( (n+1)*(n+1)*(n+3)*(n+5) );
+ double x = (b2-b2Mean)/Math.sqrt(b2Var);
+ double beta1 = 6*(n*n-5*n+2)/((n+7)*(n+9)) * Math.sqrt( (6*(n+3)*(n+5)) / (n*(n-2)*(n-3)) ); // third standardized moment of b2
+ double A = 6+8/beta1* ( 2/beta1 + Math.sqrt(1+ 4/(beta1*beta1)) );
+ double nineA = 9*A;
+ double temp = (1-2/A)/( 1+x*Math.sqrt(2/(A-4)) );
+ double Zb2 = ( (1-2/(nineA) - Math.pow (temp,1.0/3.0) )/ Math.sqrt(2/(nineA)));
+ // omnibus K-squared
+ double Ksquared = Zb1*Zb1 + Zb2*Zb2;
+
+ float pSkew = (float)twoTailedNormal(Zb1);
+ float pKurtosis = (float)twoTailedNormal(Zb2);
+ float pKSquared = (float)( 1-Probability.chi2cdf(Ksquared,2.0) );
+ return new float[]{pKSquared,pSkew,pKurtosis};
+ }
+ }
+
+
+private static double twoTailedNormal(double z){
+ if(z>0) return ( 2*(1.0- (Probability.normcdf(z)) ) );
+ else return (2 * (Probability.normcdf(z)));
+}
+
+
+}
diff --git a/drawing/papaya/src/papaya/OneWayAnova.java b/drawing/papaya/src/papaya/OneWayAnova.java
new file mode 100644
index 0000000..f654ece
--- /dev/null
+++ b/drawing/papaya/src/papaya/OneWayAnova.java
@@ -0,0 +1,196 @@
+/**
+ * papaya: A collection of utilities for Statistics and Matrix-related manipulations
+ * http://adilapapaya.com/papayastatistics/, 1.1.0
+ * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014
+ *
+ *
+ * Copyright (C) 2014 Adila Faruk http://adilapapaya.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+package papaya;
+
+import java.util.*;
+import java.io.*;
+import processing.core.*;
+/**
+
+ * Computes the one-way ANOVA p-value to test the equality of two or more sample
+ * means by analyzing the sample variances using the test statistic
+ * F = variance between samples / variance within samples
.
+ * Once initialized, the following values are computed and stored:
+ *
+ *
+ *
+ *
+ *
Collection
must contain
+ * float[]
arrays.float[]
arrays in the
+ * categoryData
collection and each of these arrays must
+ * contain at least two values.SumOfSquares - Sum^2
:
+ *
+ * Sum_(i=1 to n) (x_i - mean(x) )^2 = Sum_(i=1 to n)(x_i)^2 - (Sum_(i=1 to n) x_i)^2/n.
+ *
+ *
+ * F = (ssbg/dfbg) / (sswg/dfwg);
+ *
+ */
+ public float F(){
+ return (float)(F);
+
+ }
+
+ /**
+ * Returns the significance, or "p-value" of the test statistic {@link F()}.
+ * That is,
+ *
+ * p = 1 - Probability.fcdf(F,dfbg,dfwg);
+ *
+ */
+ public float pValue(){
+ return (float)(p);
+
+ }
+
+ /**
+ * Returns the sum of squared deviates of the data within each group. That is,
+ * if there are k groups, and each have got n_i elements,
+ *
+ * sswg = Sum_(i=1 to n1) (xi1-mean(x1))^2 + Sum_(i=1 to n2) (xi2-mean(xk))^2 + ... + Sum_(i=1 to nk) (xik-mean(xk))^2;
+ *
+ */
+ public float sswg(){
+ return (float)sswg;
+
+ }
+
+ /**
+ * Returns the sum of squared deviates between each group. That is,
+ * if there are k groups, each with mean value y_i, then
+ *
+ * ssbg = Sum_(i=1 to k) (y_i)-mean(y))
+ *
+ */
+ public float ssbg(){
+ return (float)ssbg;
+
+ }
+
+
+}
diff --git a/drawing/papaya/src/papaya/PapayaConstants.java b/drawing/papaya/src/papaya/PapayaConstants.java
new file mode 100644
index 0000000..0ce44ad
--- /dev/null
+++ b/drawing/papaya/src/papaya/PapayaConstants.java
@@ -0,0 +1,94 @@
+/**
+ * papaya: A collection of utilities for Statistics and Matrix-related manipulations
+ * http://adilapapaya.com/papayastatistics/, 1.1.0
+ * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014
+ *
+ *
+ * Copyright (C) 2014 Adila Faruk http://adilapapaya.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+package papaya;
+
+import java.awt.event.KeyEvent;
+
+import processing.core.*;
+import processing.core.PApplet;
+import processing.core.PFont;
+import processing.core.PConstants;
+
+/**
+ * PapayaConstants stores some of the constants used in plotting.
+ * I try to follow processing's conventions where possible
+ * as specified in processing.core.PConstants
+ * The Processing Javadoc has more details.
+ *
+ */
+public interface PapayaConstants{
+
+ public final static int BASELINE = 0;
+
+ public final static int CORNER = 0;
+
+ public final static int BOTTOM = 102;
+
+ public final static int CENTER = 3;
+
+ public final static int LEFT = 37;
+
+ public final static int TOP = 101;
+
+ public final static int RIGHT = 39;
+
+ /**
+ * Default font used.
+ */
+ public final static String FONTNAME = "Helvetica";
+
+ public final static float[] STROKEWEIGHT ={0.25f, .5f, .75f, 1};
+
+ public final static int[] GRAY ={100, 150, 200, 240, 250};
+
+ public final static int[] TEXTSIZE = {6,8,10,12};
+
+
+ /**
+ * Index to return if a given value/object is not found in the {@link Cast} function.
+ */
+ public static final int INDEX_NOT_FOUND = -1;
+
+ /**
+ * Index array to return if a given value/object is not found in the {@link Cast} function.
+ */
+ public static final int[] INDICES_NOT_FOUND = new int[]{-1};
+
+ /**
+ * machine constants
+ */
+ public static final double MACHEP = 1.11022302462515654042E-16;
+ public static final double MAXLOG = 7.09782712893383996732E2;
+ public static final double MINLOG = -7.451332191019412076235E2;
+ public static final double MAXGAM = 171.624376956302725;
+ public static final double SQTPI = 2.50662827463100050242E0;
+ public static final double SQRTH = 7.07106781186547524401E-1;
+ public static final double LOGPI = 1.14472988584940017414;
+
+ public static final double big = 4.503599627370496e15;
+ public static final double biginv = 2.22044604925031308085e-16;
+
+}
+
diff --git a/drawing/papaya/src/papaya/Polynomial.java b/drawing/papaya/src/papaya/Polynomial.java
new file mode 100644
index 0000000..78f3bd6
--- /dev/null
+++ b/drawing/papaya/src/papaya/Polynomial.java
@@ -0,0 +1,129 @@
+/**
+ * papaya: A collection of utilities for Statistics and Matrix-related manipulations
+ * http://adilapapaya.com/papayastatistics/, 1.1.0
+ * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014
+ *
+ *
+ * Copyright (C) 2014 Adila Faruk http://adilapapaya.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+package papaya;
+
+import java.util.*;
+import java.io.*;
+import processing.core.*;
+import processing.core.PApplet;
+import processing.core.PImage;
+
+/**
+ * Static Class for casting one variable type to another.
+ *
+ * @author Nur Adila Faruk Senan
+ */
+public final class Polynomial {
+
+ /**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+ protected Polynomial() {}
+
+ /**
+ * y = polyval(x,coeff) returns the value of a polynomial of degree n evaluated at x.
+ * The input argument coeff is an array of length n+1 whose elements are the coefficients
+ * in descending powers of the polynomial to be evaluated.
+ *
+ * E.g. for a quadratic function, y = coeff[0]*x^2 + coeff[1]*x + coeff[2]. + *
+ * @param x argument to the polynomial. + * @param coeff the coefficients of the polynomial. + */ + public static float polyval(float x , float coeff[]) throws ArithmeticException { + int n = coeff.length; + float ans = coeff[0]; + for(int i=1; i+ * E.g. for a quadratic function, y = coeff[0]*x^2 + coeff[1]*x + coeff[2]. + *
+ * @param x array argument to the polynomial. + * @param coeff the coefficients of the polynomial. + */ + public static float[] polyval(float[] x, float coeff[] ) throws ArithmeticException { + int size = x.length; + float[] y = new float[size]; + for(int i=0; i...cdf
signifies the cumulative distribution function and is similar to
+ * the q...
functions in R
and the ...cdf
functions in
+ * MATLAB
. The cdf represent the integral, from -infinity to 0
of the
+ * corresponding continuous probability density function (pdf), or, the sum from 0
to x
+ * of an associated finite pdf.
+ *
+ * ...inv
signifies the inverse of the cumulative distribution function and is similar to
+ * the p...
functions in R
and the ...inv
functions in
+ * MATLAB
.
+ *
+ * Implementation: + *
normcdf(1.96) = 0.975
--> 97.5% of the curve is within +/-1.96 of 0.
+ * norminv(0.975) = 1.96
--> finds the point x (=1.96) such that normcdf(x) = .975
.
+ * norminv( normcdf(x) ) = x
+ * alpha = 0.05
and H_0: μ=0
+ * --> p = 1-alpha/2 = 0.975
+ * --> Reject null hypothesis μ=0
if
+ * the test statistic is greater or less than than +/-1.96 (= +/- norminv(p) )
.
+ *
+ * alpha = 0.05
and H_0: μ < 0
+ * --> p = 1-alpha = 0.95
+ * --> Reject null hypothesis μ < 0
if
+ * the test statistic is greater than 1.645 (= norminv(p) )
.
+ * alpha = 0.05
and H_0: μ > 0
+ * --> p = 1-alpha = 0.95
+ * --> Reject null hypothesis μ > 0
if
+ * the test statistic is less than -1.645 (= -norminv(p) )
.
+ * x
under the beta density
+ * function.
+ * + * + * P(x) = Γ(a+b) / [ Γ(a) * Γ(b) ] * Integral_(0 to x) [ t^(a-1) * (1-t)^(b-1) ] dt + * + *+ * This function is identical to the incomplete beta + * integral function + * + *
Gamma.incompleteBeta(a, b, x)
.
+ *
+ * The complemented function is {@link betacdfComplemented}, specified by
+ *
+ * 1 - P(1-x) = Gamma.incompleteBeta( b, a, x )
;
+ *
+ */
+ static public double betacdf(double x, double a, double b) {
+ return Gamma.incompleteBeta( a, b, x );
+ }
+ /**
+ * Returns the area under the right hand tail (from x
to
+ * infinity) of the beta density function.
+ *
+ * This function is identical to the incomplete beta
+ * integral function Gamma.incompleteBeta(b, a, x)
.
+ */
+ static public double betacdfComplemented(double x, double a, double b ) {
+ return Gamma.incompleteBeta( b, a, x );
+ }
+
+ /**
+ * Returns the inverse of the beta cumulative distribution function.
+ * That is, given p
, the function finds x
such that
+ * + * Gamma.incompleteBeta( a, b, x ) = beta(x, a, b) = p; + *+ * + * This function is identical to the incomplete beta inverse + * function
Gamma.incompleteBetaInverse(aa, bb, yy0)
.
+ *
+ * @param a the alpha parameter of the beta distribution.
+ * @param b the beta parameter of the beta distribution.
+ * @param p the value for which to solve for the corresponding x
in the
+ * incomplete Beta integral.
+ * @return the value x
such that beta( a, b, x ) = p
.
+ */
+ static public double betainv( double p, double a, double b) {
+ return Gamma.incompleteBetaInverse( a, b, p);
+ }
+ /**
+ * Returns the sum of the terms 0
through k
of the Binomial
+ * probability density.
+ * + * + * Sum_(j=0 to k) [ (n!)/(j!*(n-j)! ] * [ p^j * (1-p)^(n-j) ] + * + *+ * The terms are not summed directly; instead the incomplete + * beta integral is employed, according to the formula + *
+ * y = binomcdf( p, k, n) = Gamma.incompleteBeta( n-k, k+1, 1-p )
.
+ *
+ * All arguments must be positive,
+ * @param k end term.
+ * @param n the number of trials.
+ * @param p the probability of success (must be in (0.0,1.0)
).
+ */
+ static public double binomcdf(double p, int k, int n) {
+ if( (p < 0.0) || (p > 1.0) ) throw new IllegalArgumentException();
+ if( (k < 0) || (n < k) ) throw new IllegalArgumentException();
+
+ if( k == n ) return( 1.0 );
+ if( k == 0 ) return Math.pow( 1.0-p, n-k );
+
+ return Gamma.incompleteBeta( n-k, k+1, 1.0 - p );
+ }
+ /**
+ * Returns the sum of the terms k+1
through n
of the Binomial
+ * probability density.
+ *
+ * + * Sum_(j=k+1 to n) [ (n!)/(j!*(n-j)! ] * [ p^j * (1-p)^(n-j) ] + * + *+ * The terms are not summed directly; instead the incomplete + * beta integral is employed, according to the formula + *
+ * y = binomcdfComplemented( p, k, n ) = Gamma.incompleteBeta( k+1, n-k, p )
.
+ *
+ * All arguments must be positive,
+ * @param k (start-1) term.
+ * @param n the number of trials.
+ * @param p the probability of success (must be in (0.0,1.0)
).
+ */
+ static public double binomcdfComplemented( double p , int k, int n) {
+ if( (p < 0.0) || (p > 1.0) ) throw new IllegalArgumentException();
+ if( (k < 0) || (n < k) ) throw new IllegalArgumentException();
+
+ if( k == n ) return( 0.0 );
+ if( k == 0 ) return 1.0 - Math.pow( 1.0-p, n-k );
+
+ return Gamma.incompleteBeta( k+1, n-k, p );
+ }
+
+ /**
+ * Finds the event probability p such that the sum of the
+ * terms 0 through k of the Binomial probability density
+ * is equal to the given cumulative probability y. That is,
+ *
+ * binomcdf( p, k, n ) = y; + *+ * This is accomplished using the inverse beta integral + * function and the relation + *
+ * 1 - p = Gamma.incompleteBetaInverse( n-k, k+1, y ). + *
+ * All arguments must be positive. + * @param k end term. + * @param n the number of trials. + * @param y the value to solve for such thatbinomcdf( p, k, n ) = y
.
+ */
+ static public double binominv( double y, int k, int n){
+ double dk, dn, p;
+
+ if( (y < 0.0) || (y > 1.0) ) throw new IllegalArgumentException("Test statistic not within possible range.");
+ if( (k < 0) || (n < k) ) throw new IllegalArgumentException();
+
+ dn = n - k;
+ if( k == 0 )
+ {
+ if( y > 0.8 )
+ p = -Math.expm1( Math.log1p(y-1.0) / dn );
+ else
+ p = 1.0 - Math.pow( y, 1.0/dn );
+ }
+ else
+ {
+ dk = k + 1;
+ p = Gamma.incompleteBeta( dn, dk, 0.5 );
+ if( p > 0.5 )
+ p = Gamma.incompleteBetaInverse( dk, dn, 1.0-y );
+ else
+ p = 1.0 - Gamma.incompleteBetaInverse( dn, dk, y );
+ }
+ return( p );
+ }
+
+ /**
+ * Returns the area under the left hand tail (from 0 to x
)
+ * of the Chi square probability density function with
+ * v
degrees of freedom.
+ * + * + * P( x | v ) = 1/ [ 2^(v/2) * Γ(v/2) ] * Integral_(from 0 to x) [ t^(v/2-1) * e^(-t/2) ] dt + * + *+ * where
x
is the Chi-square variable.
+ * + * The incomplete gamma integral is used, according to the + * formula + *
+ * y = chi2cdf( x, v ) = Gamma.incompleteGamma( v/2.0, x/2.0 )
.
+ *
+ * The arguments must both be positive.
+ *
+ * @param v degrees of freedom.
+ * @param x integration end point.
+ */
+ static public double chi2cdf(double x, double v) throws ArithmeticException {
+ if( x < 0.0 || v < 1.0 ) return 0.0;
+
+ //if(v==2) return 1-Math.exp(x/2.0); // special case.
+
+ return Gamma.incompleteGamma( v/2.0, x/2.0 );
+ }
+ /**
+ * Returns the area under the right hand tail (from x
to
+ * infinity) of the Chi square probability density function
+ * with v
degrees of freedom.
+ *
+ * + * P( x | v ) = 1/ [ 2^(v/2) * Γ(v/2) ] * Integral_(from x to infinity) [ t^(v/2-1) * e^(-t/2) ] dt + * + *+ * where
x
is the Chi-square variable.
+ *
+ * The incomplete gamma integral is used, according to the
+ * formula
+ *
+ * y = chi2cdfComplemented( x, v ) = incompleteGammaComplement( v/2.0, x/2.0 )
.
+ *
+ * The arguments must both be positive.
+ *
+ * @param v degrees of freedom.
+ * @param x integration start point.
+ */
+ static public double chi2cdfComplemented(double x, double v) throws ArithmeticException {
+ if( x < 0.0 || v < 1.0 ) return 0.0;
+ return Gamma.incompleteGammaComplement( v/2.0, x/2.0 );
+ }
+ /**
+ * Returns the error function of the normal distribution.
+ * The integral is
+ * + * + * erf(x) = 2/sqrt(PI) * Integral_(0 to x) [ e^(-t^2) ] dt + * + *+ * Implementation: + * For
0 <= |x| < 1, erf(x) = x * P4(x^2)/Q5(x^2)
; otherwise
+ * erf(x) = 1 - erfc(x)
.
+ *
+ *
+ * @param x the integral end point.
+ */
+ static public double erf(double x) throws ArithmeticException {
+ double y, z;
+ final double T[] = {
+ 9.60497373987051638749E0,
+ 9.00260197203842689217E1,
+ 2.23200534594684319226E3,
+ 7.00332514112805075473E3,
+ 5.55923013010394962768E4
+ };
+ final double U[] = {
+ 1.00000000000000000000E0,
+ 3.35617141647503099647E1,
+ 5.21357949780152679795E2,
+ 4.59432382970980127987E3,
+ 2.26290000613890934246E4,
+ 4.92673942608635921086E4
+ };
+
+ if( Math.abs(x) > 1.0 ) return( 1.0 - erfComplemented(x) );
+ z = x * x;
+ y = x * Polynomial.polyval( z, T) / Polynomial.polyval( z, U);
+ return y;
+ }
+ /**
+ * Returns the complementary Error function of the normal distribution; formerly named erfc
.
+ *
+ * + * erfc(x) = 2/sqrt(PI) * Integral_(x to inf) [ e^(-t^2) ] dt + * = 1 - 2/sqrt(PI) * Integral_(0 to x) [ e^(-t^2) ] dt + * = 1 - erf(x). + * + *+ * Implementation: + * For small x,
erfc(x) = 1 - erf(x)
; otherwise rational
+ * approximations are computed.
+ *
+ * @param a corresponds to x
above. An internal check is performed to see if
+ * a
is < 0 or otherwise. If a < 0 --> x = -a.
Else, x = a
.
+ */
+ static public double erfComplemented(double a) throws ArithmeticException {
+ double x,y,z,p,q;
+
+ double P[] = {
+ 2.46196981473530512524E-10,
+ 5.64189564831068821977E-1,
+ 7.46321056442269912687E0,
+ 4.86371970985681366614E1,
+ 1.96520832956077098242E2,
+ 5.26445194995477358631E2,
+ 9.34528527171957607540E2,
+ 1.02755188689515710272E3,
+ 5.57535335369399327526E2
+ };
+ double Q[] = {
+ 1.00000000000000000000E0,
+ 1.32281951154744992508E1,
+ 8.67072140885989742329E1,
+ 3.54937778887819891062E2,
+ 9.75708501743205489753E2,
+ 1.82390916687909736289E3,
+ 2.24633760818710981792E3,
+ 1.65666309194161350182E3,
+ 5.57535340817727675546E2
+ };
+
+ double R[] = {
+ 5.64189583547755073984E-1,
+ 1.27536670759978104416E0,
+ 5.01905042251180477414E0,
+ 6.16021097993053585195E0,
+ 7.40974269950448939160E0,
+ 2.97886665372100240670E0
+ };
+ double S[] = {
+ 1.00000000000000000000E0,
+ 2.26052863220117276590E0,
+ 9.39603524938001434673E0,
+ 1.20489539808096656605E1,
+ 1.70814450747565897222E1,
+ 9.60896809063285878198E0,
+ 3.36907645100081516050E0
+ };
+
+ if( a < 0.0 ) x = -a;
+ else x = a;
+
+ if( x < 1.0 ) return 1.0 - erf(a);
+
+ z = -a * a;
+
+ if( z < -MAXLOG ) {
+ if( a < 0 ) return( 2.0 );
+ else return( 0.0 );
+ }
+
+ z = Math.exp(z);
+
+ if( x < 8.0 ) {
+ p = Polynomial.polyval( x, P );
+ q = Polynomial.polyval( x, Q );
+ } else {
+ p = Polynomial.polyval( x, R);
+ q = Polynomial.polyval( x, S );
+ }
+
+ y = (z * p)/q;
+
+ if( a < 0 ) y = 2.0 - y;
+
+ if( y == 0.0 ) {
+ if( a < 0 ) return 2.0;
+ else return( 0.0 );
+ }
+
+ return y;
+ }
+ /**
+ * Returns the area from zero to x
under the F density
+ * function (also known as Snedcor's density or the
+ * variance ratio density); formerly named fdtr
.
+ * Corresponds to the left hand tail of the F-distribution.
+ *
+ * The incomplete beta integral is used, according to the formula
+ *
+ * + * P(x) = Gamma.incompleteBeta( df1/2, df2/2, (df1*x/(df2 + df1*x) ). + * + *+ * + * @param df1 ( greater than or equal to 1 ) + * @param df2 ( greater than or equal to 1 ) + * @param x the integration end point (greater than or equal to 0). + */ + public static double fcdf( double x, int df1, int df2 ){ + double a, b, w; + + if( (df1 < 1) || (df2 < 1) || x < 0.0 ) throw new IllegalArgumentException("F-distribution domain error."); + a = df1; + b = df2; + w = a * x; + w = w / (b + w); + return Gamma.incompleteBeta(0.5*a, 0.5*b, w); + } + + /** + * Returns the area from x to infinity under the F density + * function (also known as Snedcor's density or the + * variance ratio density); formerly named
fdtrc
.
+ * Corresponds to the right hand tail of the F-distribution ( and
+ * to the values presented in F-distribution tables).
+ *
+ * The incomplete beta integral is used, according to the
+ * relation
+ *+ * + * P(x) = Gamma.incompleteBeta( df2/2, df1/2, (df2/(df2 + df1*x) ) + * + *+ * + * @param df1 df1 ( greater than or equal to 1 ) + * @param df2 df2 ( greater than or equal to 1 ) + * @param x the left integration point. Corresponds to the F value in look-up tables. + * @return The area from
x
to inf. under the F density function.
+ */
+ public static double fcdfComplemented(double x, int df1, int df2){
+
+ double a, b, w;
+
+ if( (df1 < 1) || (df2 < 1) || (x < 0.0) ) throw new IllegalArgumentException("Complementary F-distribution domain error.");
+
+ a = df1;
+ b = df2;
+ w = b / (b + a * x); // df1 / (df1 + df2*x)
+ return Gamma.incompleteBeta( 0.5*b, 0.5*a, w );
+ }
+
+ /**
+ * Finds the F density argument x such that the integral
+ * from 0 to x
of the F density is equal to p; formerly named fdtri
.
+ * That is, it solves for the value of x
, given p
such that
+ * + * fcdf(x,df1,df2) - p = 0. + *+ * + * This is accomplished using the inverse beta integral + * function and the relations + *
+ * z = incompleteBetaInverse( df2/2, df1/2, p ), + * x = 1 - df2 * (1-z) / (df1 z), + *+ * if
p > Gamma.incompleteBeta( 0.5*df1, 0.5*df2, 0.5 );
, or p < 0.001
and,
+ *+ * z = incompleteBetaInverse( df1/2, df2/2, p ), + * x = 1 - df2 * z / (df1 (1-z)), + *+ * otherwise. + * The significance level for a one-sided test (or α in look-up tables) is then
1-x
+ *
+ * @param df1 degrees of freedom of the first dataset
+ * @param df2 degrees of freedom of the second dataset
+ * @param p the fcdf value for which to solve for x
+ * @return the point x such that fcdf(x,df1,df2) = p.
+ * @throws IllegalArgumentException if df1 or df2 are less than 1, or p is not in (0,1].
+ */
+ public static double finv( double p, int df1, int df2){
+ double a, b, w, x;
+
+ if( (df1 < 1) || (df2 < 1) || (p <= 0.0) || (p > 1.0) )
+ throw new IllegalArgumentException("Domain error in fInverse");
+ a = df1;
+ b = df2;
+ /* Compute probability for x = 0.5. */
+ w = Gamma.incompleteBeta( 0.5*b, 0.5*a, 0.5 );
+ /* If that is greater than y, then the solution is in w < .5.
+ Otherwise, solve at 1-y to remove cancellation in (b - b*w). */
+ if( w > p || p < 0.001)
+ {
+ /* FComplemented: w = b / (b + a * x); --> x = b(1/w- 1)/a = b*(1-w)/(a*w) */
+ w = Gamma.incompleteBetaInverse( 0.5*b, 0.5*a, p );
+ x = (b - b*w)/(a*w);
+ }
+ else
+ {
+ /* F: w = a*x/ (a*x+b) --> solve for x = b*w/(a*(1-w))
+ */
+ w = Gamma.incompleteBetaInverse( 0.5*a, 0.5*b, 1.0-p );// incbi( 0.5*a, 0.5*b, 1.0-p );
+ x = b*w/(a*(1.0-w));
+ }
+ return x;
+ }
+
+
+ /**
+ * Returns the integral from zero to x
of the gamma probability
+ * density function.
+ * + * + * y = a^b / Γ(b) * Integral_(0 to x) [ t^(b-1) e^(-a*t) ]dt + * + *+ * The incomplete gamma integral is used, according to the + * relation + * + *
y = Gamma.incompleteGamma( b, a*x )
.
+ *
+ * @param a the paramater a (alpha) of the gamma distribution.
+ * @param b the paramater b (beta, lambda) of the gamma distribution.
+ * @param x integration end point.
+ * @return y the integral from zero to x
of the gamma pdf.
+ */
+ static public double gammacdf( double x, double a, double b ) {
+ if( x < 0.0 ) return 0.0;
+ return Gamma.incompleteGamma(b, a*x);
+ }
+ /**
+ * Returns the integral from x
to infinity of the gamma
+ * probability density function:
+ * + * + * y = a^b / Γ(b) * Integral_(from x to infinity) [t^(b-1) * e^(-a*t) ] dt + * + *+ * The incomplete gamma integral is used, according to the + * relation + *
+ * y = Gamma.incompleteGammaComplement( b, a*x ).
+ *
+ * @param a the paramater a (alpha) of the gamma distribution.
+ * @param b the paramater b (beta, lambda) of the gamma distribution.
+ * @param x integration start point.
+ */
+ static public double gammacdfComplemented( double x, double a, double b ) {
+ if( x < 0.0 ) return 0.0;
+ return Gamma.incompleteGammaComplement(b, a*x);
+ }
+ /**
+ * Returns the sum of the terms 0
through k
of the Negative Binomial Distribution.
+ *
+ * + * Sum_(from j=0 to k) [ (n+j-1)! / ( j! * (n-1)! ) ] * [ p^n* (1-p)^j ] + * + *+ * In a sequence of Bernoulli trials, this is the probability + * that
k
or fewer failures precede the n
-th success.
+ * + * The terms are not computed individually; instead the incomplete + * beta integral is employed, according to the formula + *
+ * y = nbinomcdf( p, k, n ) = Gamma.incompleteBeta( n, k+1, p )
.
+ *
+ * All arguments must be positive,
+ * @param k end term.
+ * @param n the number of trials.
+ * @param p the probability of success (must be in (0.0,1.0)
).
+ */
+ static public double nbinomcdf(double p, int k, int n) {
+ if( (p < 0.0) || (p > 1.0) ) throw new IllegalArgumentException();
+ if(k < 0) return 0.0;
+
+ return Gamma.incompleteBeta( n, k+1, p );
+ }
+ /**
+ * Returns the sum of the terms k+1
to infinity of the Negative
+ * Binomial distribution.
+ *
+ * + * Sum_(from j=k+1 to inf) [ (n+j-1)! / ( j! * (n-1)! ) ] * [ p^n* (1-p)^j ] + * + *+ * The terms are not computed individually; instead the incomplete + * beta integral is employed, according to the formula + *
+ * y = nbinomcdfComplemented( p, k, n ) = Gamma.incompleteBeta( k+1, n, 1-p ).
+ *
+ * All arguments must be positive,
+ * @param k (start-1) term.
+ * @param n the number of trials.
+ * @param p the probability of success (must be in (0.0,1.0)
).
+ */
+ static public double nbinomcdfComplemented( double p, int k, int n ) {
+ if( (p < 0.0) || (p > 1.0) ) throw new IllegalArgumentException();
+ if(k < 0) return 0.0;
+
+ return Gamma.incompleteBeta( k+1, n, 1.0-p );
+ }
+ /**
+ * Returns the area under the Normal (Gaussian) probability density
+ * function, integrated from minus infinity to x
(assumes mean is zero, variance is one).
+ *
+ * + * normcdf(x) = 1/sqrt(2pi) * Integral_(-inf to x) [ e^(-t^2/2) ] dt + * = ( 1 + erf(z) ) / 2 + * = erfc(z) / 2 + * + *+ * where
z = x/sqrt(2)
.
+ * Computation is via the functions erf
and erfComplement
.
+ * + * Relation to alpha value (or confidence level): + *
alpha = 2*( 1-normcdf(x) )
.
+ * alpha = ( 1-normcdf(x) )
.
+ * x
in the equation above.
+ */
+ static public double normcdf( double a) throws ArithmeticException {
+ double x, y, z;
+
+ x = a * SQRTH;
+ z = Math.abs(x);
+
+ if( z < SQRTH ) y = 0.5 + 0.5 * erf(x);
+ else {
+ y = 0.5 * erfComplemented(z);
+ if( x > 0 ) y = 1.0 - y;
+ }
+
+ return y;
+ }
+ /**
+ * Returns the area under the Normal (Gaussian) probability density
+ * function, integrated from minus infinity to x
when the
+ * data has not been standardized (i.e. mean ≠0, variance ≠1).
+ * + * + * normcdf(x,mean,variance) = 1/sqrt(2pi*v) * Integral_(-inf to x) [ e^(-(t-mean)^2 / 2v) ] dt + * + *+ * where
v = variance
.
+ * Computation is via the functions erf
.
+ *
+ * @param mean the mean of the normal distribution.
+ * @param variance the variance of the normal distribution.
+ * @param x the integration limit.
+ */
+ static public double normcdf(double x, double mean, double variance) throws ArithmeticException {
+ if (x>0)
+ return 0.5 + 0.5*erf((x-mean)/Math.sqrt(2.0*variance));
+ else
+ return 0.5 - 0.5*erf((-(x-mean))/Math.sqrt(2.0*variance));
+ }
+ /**
+ * Returns the value, x
, for which the area under the
+ * Normal (Gaussian) probability density function (integrated from
+ * minus infinity to x
) is equal to the argument y
+ * (assumes mean is zero, variance is one); formerly named ndtri
.
+ * That is,
+ * + * normcdf(x) = y0. + *
+ *
+ * For small arguments 0 < y < exp(-2)
, the program computes
+ * z = sqrt( -2.0 * log(y) )
; then the approximation is
+ * x = z - log(z)/z - (1/z) P(1/z) / Q(1/z)
.
+ * There are two rational functions P/Q, one for 0 < y < exp(-32)
+ * and the other for y
up to exp(-2)
.
+ * For larger arguments,
+ * w = y - 0.5
, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2))
.
+ * Finally, this returns -infinity
for y0 = 0
+ * and +infinity for y0 = 1
.
+ *
+ * @param y0 the argument to the function
+ */
+ static public double norminv( double y0) throws ArithmeticException {
+ double x, y, z, y2, x0, x1;
+ int code;
+
+ final double s2pi = Math.sqrt(2.0*Math.PI);
+
+ if( y0 < 0.0 ) throw new IllegalArgumentException();
+ if( y0 > 1.0 ) throw new IllegalArgumentException();
+ if( y0==0.0 )return -Double.NEGATIVE_INFINITY;
+ if( y0==1.0 )return Double.POSITIVE_INFINITY;
+
+ code = 1;
+ y = y0;
+ if( y > (1.0 - 0.13533528323661269189) ) { /* 0.135... = exp(-2) */
+ y = 1.0 - y;
+ code = 0;
+ }
+
+ if( y > 0.13533528323661269189 ) {
+ y = y - 0.5;
+ y2 = y * y;
+ x = y + y * (y2 * Polynomial.polyval( y2, P0)/Polynomial.polyval( y2, Q0));
+ x = x * s2pi;
+ return(x);
+ }
+
+ x = Math.sqrt( -2.0 * Math.log(y) );
+ x0 = x - Math.log(x)/x;
+
+ z = 1.0/x;
+ if( x < 8.0 ) /* y > exp(-32) = 1.2664165549e-14 */
+ x1 = z * Polynomial.polyval( z, P1 )/Polynomial.polyval( z, Q1 );
+ else
+ x1 = z * Polynomial.polyval( z, P2 )/Polynomial.polyval( z, Q2 );
+ x = x0 - x1;
+ if( code != 0 )
+ x = -x;
+ return( x );
+ }
+ /**
+ * Returns the sum of the first k
terms of the Poisson distribution.
+ *
+ * + * Sum_(j=0 to k) [ e^(-m) * m^j /j! ] + * + *+ * The terms are not summed directly; instead the incomplete + * gamma integral is employed, according to the relation + *
+ * y = poissoncdf( mean, k m ) = Gamma.incompleteGammaComplement( k+1, mean )
.
+ *
+ * The arguments must both be positive.
+ *
+ * @param k number of terms.
+ * @param mean the mean of the poisson distribution.
+ */
+ static public double poissoncdf( double mean, int k) throws ArithmeticException {
+ if( mean < 0 ) throw new IllegalArgumentException();
+ if( k < 0 ) return 0.0;
+ return Gamma.incompleteGammaComplement((double)(k+1) ,mean);
+ }
+ /**
+ * Returns the sum of the terms k+1
to Infinity
of the Poisson distribution.
+ *
+ * + * Sum_(j=k+1 to inf.) [ e^(-m) * m^j /j! ] + * + *+ * The terms are not summed directly; instead the incomplete + * gamma integral is employed, according to the formula + *
+ * y = poissoncdfComplemented( k, m ) = Gamma.incompleteGamma( k+1, m )
.
+ *
+ * The arguments must both be positive.
+ *
+ * @param k (start-1) term.
+ * @param mean the mean of the poisson distribution.
+ */
+ static public double poissoncdfComplemented(int k, double mean) throws ArithmeticException {
+ if( mean < 0.0 ) throw new IllegalArgumentException();
+ if( k < -1 ) return 0.0;
+ return Gamma.incompleteGamma((double)(k+1),mean);
+ }
+ /**
+ * Returns the integral from minus infinity to t
of the Student-t
+ * distribution with k > 0
degrees of freedom.
+ *
+ * + * Γ((k+1)/2) / [ sqrt(k*pi) * Γ(k/2) ] * Integral_(-inf to t) [ ( 1+x^2/k )^( (-k+1)/2 ) ] dx + * + *+ * Relation to incomplete beta integral: + *
+ * tcdf(t,k) = 0.5 * Gamma.incompleteBeta( k/2, 1/2, z )
, when t < 0
,
+ * tcdf(t,k) = 1-0.5 * Gamma.incompleteBeta( k/2, 1/2, z )
, when t > 0
,
+ *
z = k/(k + t^2)
.
+ * Relation to alpha value (or confidence level):
+ * alpha = 2*( 1-tcdf(t,k) )
.
+ * alpha = ( 1-tcdf(t,k) )
.
+ * t
, for which the area under the
+ * Student-t probability density function (integrated from
+ * minus infinity to t
) is equal to p.
+ * The function uses the tcdf function to determine the return
+ * value iteratively.
+ *
+ * @param p the fcdf value for which to solve for x
+ * @param k the degrees of freedom
+ */
+ public static double tinv(double p, double k) {
+ if(p<0 || p>1) throw new IllegalArgumentException(p + "is not a valid value for p");
+
+ if(p==.5) return 0;
+ if(p==0.0) return tcdf(1e-8,k);
+ if(p==1.0) return tcdf(1-1e-8,k);
+
+
+ /* if p is in the left region, correct it. */
+ double cumProb = p; // Cumulative probability = 1-alpha/2
+ int multiplier = 1;
+ // Solution lies in the left half of the student-T curve ( t < 0)
+ // so we solve for the equivalent t-value in the right half, and then
+ // multiply that by -1.
+ if(cumProb < .5){
+ cumProb = 1-cumProb;
+ multiplier = -1;
+ }
+
+ double f1,f2,f3;
+ double x1,x2,x3;
+ double g,s12;
+
+ // make a first approximation using the norm-inv function.
+ x1 = norminv(cumProb);
+ // Return inverse of normal for large size (k > 30 is pretty sufficient usually).
+ if (k > 200) {
+ return x1;
+ }
+
+ /* Find a pair of x1,x2 that bracket zero to
+ guarantee asymptotic convergence later on */
+ f1 = tcdf(x1,k)-cumProb;
+ x2 = x1; f2 = f1;
+ do {
+ if (f1>0) {
+ x2 = x2/2; // decrease x2
+ } else {
+ x2 = x2+x1; // increase x2
+ }
+ f2 = tcdf(x2,k)-cumProb; //
+ } while (f1*f2>0);
+
+ // Find better approximation now that f1 and f2 bracket zero.
+ // Pegasus-method (also known as the false-position-method or regula-falsi-method)
+ do {
+ // Calculate slope of secant and t value for which it is 0.
+ s12 = (f2-f1)/(x2-x1);
+ x3 = x2 - f2/s12;
+
+ // Calculate function value at x3
+ f3 = tcdf(x3,k)-cumProb;
+ if (Math.abs(f3)<1e-8) { // This criteria needs to be very tight!
+ // We found a perfect value -> return
+ return multiplier*x3;
+ }
+
+ if (f3*f2<0) {
+ x1=x2; f1=f2;
+ x2=x3; f2=f3;
+ } else {
+ g = f2/(f2+f3);
+ f1=g*f1;
+ x2=x3; f2=f3;
+ }
+ } while(Math.abs(x2-x1)>0.001);
+
+ if (Math.abs(f2)<=Math.abs(f1)) {
+ return multiplier*x2;
+ } else {
+ return multiplier*x1;
+ }
+ }
+}
diff --git a/drawing/papaya/src/papaya/QR.java b/drawing/papaya/src/papaya/QR.java
new file mode 100644
index 0000000..8fc33db
--- /dev/null
+++ b/drawing/papaya/src/papaya/QR.java
@@ -0,0 +1,296 @@
+/**
+ * papaya: A collection of utilities for Statistics and Matrix-related manipulations
+ * http://adilapapaya.com/papayastatistics/, 1.1.0
+ * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014
+ *
+ *
+ * Copyright (C) 2014 Adila Faruk http://adilapapaya.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+package papaya;
+
+/** QR Decomposition.
++ For an m-by-n matrix A with m ≥ n, the QR decomposition is an m-by-n + orthogonal matrix Q and an n-by-n upper triangular matrix R so that + A = Q*R. +
+ The QR decompostion always exists, even if the matrix does not have + full rank, so the constructor will never fail. The primary use of the + QR decomposition is in the least squares solution of nonsquare systems + of simultaneous linear equations. This will fail if isFullRank() + returns false. +
+Shamelessly copied (and modified) from the +JAMA Java +Matrix package. To make things compatible with how most users use Processing, the +class take in float matrices. However, to preserve the acccuracy of the computations, the algorithm +first casts the input into a double array, prior to doing anything. All methods also return doubles; Use +{@link Cast#doubleToFloat(double[][])} if you want/need to cast everything back to floats for +further (non-high-accuracy-dependant) processing (pun intended). + */ + + + + +public class QR { + /* ------------------------ + Class variables + * ------------------------ */ + + /** Array for internal storage of decomposition. + @serial internal array storage. + */ + private double[][] QR; + + /** Row and column dimensions. + @serial column dimension. + @serial row dimension. + */ + private int m, n; + + /** Array for internal storage of diagonal of R. + @serial diagonal of R. + */ + private double[] Rdiag; + + /* ------------------------ + Constructor + * ------------------------ */ + + /** QR Decomposition, computed by Householder reflections. + * @param A Rectangular matrix + */ + + public QR (float[][] A) { + // Initialize. + QR = Cast.floatToDouble(A); + m = A.length; + n = A[0].length; + Rdiag = new double[n]; + + // Main loop. + for (int k = 0; k < n; k++) { + // Compute 2-norm of k-th column without under/overflow. + double nrm = 0; + for (int i = k; i < m; i++) { + nrm = SVD.hypot(nrm,QR[i][k]); + } + + if (nrm != 0.0) { + // Form k-th Householder vector. + if (QR[k][k] < 0) { + nrm = -nrm; + } + for (int i = k; i < m; i++) { + QR[i][k] /= nrm; + } + QR[k][k] += 1.0; + + // Apply transformation to remaining columns. + for (int j = k+1; j < n; j++) { + double s = 0.0; + for (int i = k; i < m; i++) { + s += QR[i][k]*QR[i][j]; + } + s = -s/QR[k][k]; + for (int i = k; i < m; i++) { + QR[i][j] += s*QR[i][k]; + } + } + } + Rdiag[k] = -nrm; + } + } + + /* ------------------------ + Public Methods + * ------------------------ */ + + /** Is the matrix full rank? + @return true if R, and hence A, has full rank. + */ + + public boolean isFullRank () { + for (int j = 0; j < n; j++) { + if (Rdiag[j] == 0) + return false; + } + return true; + } + + /** Return the Householder vectors + @return Lower trapezoidal matrix whose columns define the reflections + */ + + public double[][] getH () { + double[][] H = new double[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (i >= j) { + H[i][j] = QR[i][j]; + } else { + H[i][j] = 0.0; + } + } + } + return H; + } + + /** Return the upper triangular factor + @return R + */ + + public double[][] getR () { + double[][] R = new double[n][n]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i < j) { + R[i][j] = QR[i][j]; + } else if (i == j) { + R[i][j] = Rdiag[i]; + } else { + R[i][j] = 0.0; + } + } + } + return R; + } + + /** Generate and return the (economy-sized) orthogonal factor + @return Q + */ + + public double[][] getQ () { + double[][] Q = new double[m][n]; + for (int k = n-1; k >= 0; k--) { + for (int i = 0; i < m; i++) { + Q[i][k] = 0.0; + } + Q[k][k] = 1.0; + for (int j = k; j < n; j++) { + if (QR[k][k] != 0) { + double s = 0.0; + for (int i = k; i < m; i++) { + s += QR[i][k]*Q[i][j]; + } + s = -s/QR[k][k]; + for (int i = k; i < m; i++) { + Q[i][j] += s*QR[i][k]; + } + } + } + } + return Q; + } + + /** Least squares solution of A*X = b + @param b An array with the same length as the number of rows in A. + @return x that minimizes the two norm of Q*R*x-b. + @exception IllegalArgumentException Matrix row dimensions must agree. + @exception RuntimeException Matrix is rank deficient. + */ + + public double[] solve (float[] b) { + if (b.length != m) { + throw new IllegalArgumentException("Matrix row dimensions must agree."); + } + if (!this.isFullRank()) { + throw new RuntimeException("Matrix is rank deficient."); + } + + // Copy right hand side + int nx = 1; //B[0].length; + double[] X = Cast.floatToDouble(b); + + // Compute Y = transpose(Q)*B + for (int k = 0; k < n; k++) { + double s = 0.0; + for (int i = k; i < m; i++) { + s += QR[i][k]*X[i]; + } + s = -s/QR[k][k]; + for (int i = k; i < m; i++) { + X[i] += s*QR[i][k]; + } + + } + //Mat.print(Cast.doubleToFloat(X),4); + // Solve R*x = y; + for (int k = n-1; k >= 0; k--) { + X[k] /= Rdiag[k]; + + for (int i = 0; i < k; i++) { + X[i] -= X[k]*QR[i][k]; + + } + } + return X; + + } + + /** Least squares solution of A*X = B + @param B A Matrix with as many rows as A and any number of columns. + @return X that minimizes the two norm of Q*R*X-B. + @exception IllegalArgumentException Matrix row dimensions must agree. + @exception RuntimeException Matrix is rank deficient. + */ + + public double[][] solve (float[][] B) { + if (B.length != m) { + throw new IllegalArgumentException("Matrix row dimensions must agree."); + } + if (!this.isFullRank()) { + throw new RuntimeException("Matrix is rank deficient."); + } + + // Copy right hand side + int nx = B[0].length; + double[][] X = Cast.floatToDouble(B); + + // Compute Y = transpose(Q)*B + for (int k = 0; k < n; k++) { + for (int j = 0; j < nx; j++) { + double s = 0.0; + for (int i = k; i < m; i++) { + s += QR[i][k]*X[i][j]; + } + s = -s/QR[k][k]; + for (int i = k; i < m; i++) { + X[i][j] += s*QR[i][k]; + } + } + } + // Solve R*X = Y; + for (int k = n-1; k >= 0; k--) { + for (int j = 0; j < nx; j++) { + X[k][j] /= Rdiag[k]; + } + for (int i = 0; i < k; i++) { + for (int j = 0; j < nx; j++) { + X[i][j] -= X[k][j]*QR[i][k]; + } + } + } + return X; + } +} + + + + + diff --git a/drawing/papaya/src/papaya/Rank.java b/drawing/papaya/src/papaya/Rank.java new file mode 100644 index 0000000..0628635 --- /dev/null +++ b/drawing/papaya/src/papaya/Rank.java @@ -0,0 +1,618 @@ +/** + * papaya: A collection of utilities for Statistics and Matrix-related manipulations + * http://adilapapaya.com/papayastatistics/, 1.1.0 + * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014 + * + * + * Copyright (C) 2014 Adila Faruk http://adilapapaya.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA + */ +package papaya; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.io.*; +import processing.core.*; + +/** + *
Ranking based on the natural ordering on floats for a sequence of data that may also + * contain NaNs.
+ *When present, NaNs are treated according to the configured NaNStrategy constants and ties + * are handled using the configured tiesStrategy constants as follows: + * + *
Strategies for handling NaN values in rank transformations. + *
Float.NEGATIVE_INFINITY
.Float.POSITIVE_INFINITY
Strategies for handling tied values in rank transformations: + *
Examples: + *
+ * Input data: (20, 17, 30, 42.3, 17, 50, Float.NaN, Float.NEGATIVE_INFINITY, 17) + * | ||
---|---|---|
NaNStrategy | TiesStrategy | + *rank(data) |
+ *
0 (default = NaNs removed) | + *0 (default = ties averaged) | + *(5, 3, 6, 7, 3, 8, 1, 3) |
0 (default = NaNs removed) | + *1 (MINIMUM) | + *(5, 2, 6, 7, 2, 8, 1, 2) |
1 (MINIMAL) | + *0 (default = ties averaged) | + *(6, 4, 7, 8, 4, 9, 1.5, 1.5, 4) |
1 (MINIMAL) | + *2 (MAXIMUM) | + *(6, 5, 7, 8, 5, 9, 2, 2, 5) |
2 (MAXIMAL) | + *2 (MAXIMUM)/td> + * | (5, 4, 6, 7, 4, 8, 9, 1, 4) |
nanStrategy
and ties
+ * resolved using tiesStrategy
.
+ *
+ * Input values that specify which strategy to use for handling tied values in the + * rank transformations: + *
Input values that specify which strategy to use for handling NaN values in the + * rank transformations: + *
Float.NEGATIVE_INFINITY
.Float.POSITIVE_INFINITY
nanStrategy
and ties
+ * resolved using tiesStrategy
.
+ *
+ * Input values that specify which strategy to use for handling tied values in the + * rank transformations: + *
Input values that specify which strategy to use for handling NaN values in the + * rank transformations: + *
Float.NEGATIVE_INFINITY
.Float.POSITIVE_INFINITY
tiesStrategy
.
+ * Input values that specify which strategy to use for handling NaN values in the + * rank transformations: + *
tiesStrategy
.
+ * Input values that specify which strategy to use for handling NaN values in the + * rank transformations: + *
ranks.
+ *
+ * @param ranks array to be searched for NaNs
+ * @return true iff ranks contains one or more NaNs
+ */
+ private static boolean containsNaNs(IntFloatPair[] ranks) {
+ for (int i = 0; i < ranks.length; i++) {
+ if (Float.isNaN(ranks[i].getValue())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Resolve a sequence of ties, using the configured TiesStrategy.
+ * The input ranks
array is expected to take the same value
+ * for all indices in tiesTrace
. The common value is recoded
+ * according to the tiesStrategy. For example, if ranks = <5,8,2,6,2,7,1,2>,
+ * tiesTrace = <2,4,7> and tiesStrategy is MINIMUM, ranks will be unchanged.
+ * The same array and trace with tiesStrategy AVERAGE will come out
+ * <5,8,3,6,3,7,1,3>.
+ *
+ * @param ranks array of ranks
+ * @param tiesTrace list of indices where ranks
is constant
+ * -- that is, for any i and j in TiesTrace, ranks[i] == ranks[j]
+ *
+ */
+ private static void resolveTie(float[] ranks, Listdata[i] = value
for each i in tiesTrace.
+ *
+ * @param data array to modify
+ * @param tiesTrace list of index values to set
+ * @param value value to set
+ */
+ private static void fill(float[] data, Listranks[i] = Float.NaN
for each i in nanPositions.
+ *
+ * @param ranks array to modify
+ * @param nanPositions list of index values to set to Float.NaN
+ */
+ private static void restoreNaNs(float[] ranks, Listranks
is NaN.
+ *
+ * @param ranks array to search for NaNs
+ * @return list of indexes i such that ranks[i] = NaN
+ */
+ private static ListFloat.compare(value, other.value)
+ */
+ public int compareTo(IntFloatPair other) {
+ return Float.compare(value, other.value);
+ }
+
+ /**
+ * Returns the value of the pair.
+ * @return value
+ */
+ public float getValue() {
+ return value;
+ }
+
+ /**
+ * Returns the original position of the pair.
+ * @return position
+ */
+ public int getPosition() {
+ return position;
+ }
+ }
+}
+
+//public class Rank {
+// private int[] rank;//= new int[numProducts];
+// private int[] idx;// = new int[numProducts];
+//
+// public Rank(float[] sortedArray) {
+// int size = sortedArray.length;
+// rankTemp = new int[size];
+// int i=0;
+// while(im x n
matrix A
with m > n
,
+the singular value decomposition is an m-by-n
orthogonal matrix
+U
, an n-by-n
diagonal matrix S
, and
+an n-by-n
orthogonal matrix V
so that A = U*S*V'
.
+
+The singular values, sigma[k] = S[k][k]
, are ordered so that
+sigma[0] > sigma[1] > ... > sigma[n-1]
.
+
+The singular value decompostion always exists, so the constructor will +never fail. The matrix condition number and the effective numerical +rank can be computed from this decomposition. +
+Shamelessly copied (and modified) from the
+JAMA Java
+Matrix package. To make things compatible with how most users use Processing, the
+class take in float matrices. However, to preserve the acccuracy of the computations, the algorithm
+first casts the input into a double array, prior to doing anything. All methods also return doubles; Use
+{@link Cast#doubleToFloat(double[][])} if you want/need to cast everything back to floats for
+further (non-high-accuracy-dependant) processing (pun intended).
+ */
+
+public class SVD {
+
+ /* ------------------------
+ Class variables
+ * ------------------------ */
+
+ /** Arrays for internal storage of U and V.
+ @serial internal storage of U.
+ @serial internal storage of V.
+ */
+ private double[][] U, V;
+
+ /** Array for internal storage of singular values.
+ @serial internal storage of singular values.
+ */
+ private double[] s;
+
+ /** Row and column dimensions.
+ @serial row dimension.
+ @serial column dimension.
+ */
+ private int m, n;
+
+ /* ------------------------
+ Constructor
+ * ------------------------ */
+
+ /** Construct the singular value decomposition. At the end of this call,
+ * U, V, and S have been computed.
+ @param AA Rectangular matrix
+ @return
+ */
+
+ public SVD (float[][] AA) {
+
+ // Derived from LINPACK code.
+ // Initialize.
+ double[][] A = Cast.floatToDouble(AA);
+ m = AA.length;
+ n = AA[0].length;
+
+ /* Apparently the failing cases are only a proper subset of (m = -1; k--) {
+ if (k == -1) {
+ break;
+ }
+ if (Math.abs(e[k]) <=
+ tiny + eps*(Math.abs(s[k]) + Math.abs(s[k+1]))) {
+ e[k] = 0.0;
+ break;
+ }
+ }
+ if (k == p-2) {
+ kase = 4;
+ } else {
+ int ks;
+ for (ks = p-1; ks >= k; ks--) {
+ if (ks == k) {
+ break;
+ }
+ double t = (ks != p ? Math.abs(e[ks]) : 0.) +
+ (ks != k+1 ? Math.abs(e[ks-1]) : 0.);
+ if (Math.abs(s[ks]) <= tiny + eps*t) {
+ s[ks] = 0.0;
+ break;
+ }
+ }
+ if (ks == k) {
+ kase = 3;
+ } else if (ks == p-1) {
+ kase = 1;
+ } else {
+ kase = 2;
+ k = ks;
+ }
+ }
+ k++;
+
+ // Perform the task indicated by kase.
+
+ switch (kase) {
+
+ // Deflate negligible s(p).
+
+ case 1: {
+ double f = e[p-2];
+ e[p-2] = 0.0;
+ for (int j = p-2; j >= k; j--) {
+ double t = hypot(s[j],f);
+ double cs = s[j]/t;
+ double sn = f/t;
+ s[j] = t;
+ if (j != k) {
+ f = -sn*e[j-1];
+ e[j-1] = cs*e[j-1];
+ }
+ if (wantv) {
+ for (int i = 0; i < n; i++) {
+ t = cs*V[i][j] + sn*V[i][p-1];
+ V[i][p-1] = -sn*V[i][j] + cs*V[i][p-1];
+ V[i][j] = t;
+ }
+ }
+ }
+ }
+ break;
+
+ // Split at negligible s(k).
+
+ case 2: {
+ double f = e[k-1];
+ e[k-1] = 0.0;
+ for (int j = k; j < p; j++) {
+ double t = hypot(s[j],f);
+ double cs = s[j]/t;
+ double sn = f/t;
+ s[j] = t;
+ f = -sn*e[j];
+ e[j] = cs*e[j];
+ if (wantu) {
+ for (int i = 0; i < m; i++) {
+ t = cs*U[i][j] + sn*U[i][k-1];
+ U[i][k-1] = -sn*U[i][j] + cs*U[i][k-1];
+ U[i][j] = t;
+ }
+ }
+ }
+ }
+ break;
+
+ // Perform one qr step.
+
+ case 3: {
+
+ // Calculate the shift.
+
+ double scale = Math.max(Math.max(Math.max(Math.max(
+ Math.abs(s[p-1]),Math.abs(s[p-2])),Math.abs(e[p-2])),
+ Math.abs(s[k])),Math.abs(e[k]));
+ double sp = s[p-1]/scale;
+ double spm1 = s[p-2]/scale;
+ double epm1 = e[p-2]/scale;
+ double sk = s[k]/scale;
+ double ek = e[k]/scale;
+ double b = ((spm1 + sp)*(spm1 - sp) + epm1*epm1)/2.0;
+ double c = (sp*epm1)*(sp*epm1);
+ double shift = 0.0;
+ if ((b != 0.0) | (c != 0.0)) {
+ shift = Math.sqrt(b*b + c);
+ if (b < 0.0) {
+ shift = -shift;
+ }
+ shift = c/(b + shift);
+ }
+ double f = (sk + sp)*(sk - sp) + shift;
+ double g = sk*ek;
+
+ // Chase zeros.
+
+ for (int j = k; j < p-1; j++) {
+ double t = hypot(f,g);
+ double cs = f/t;
+ double sn = g/t;
+ if (j != k) {
+ e[j-1] = t;
+ }
+ f = cs*s[j] + sn*e[j];
+ e[j] = cs*e[j] - sn*s[j];
+ g = sn*s[j+1];
+ s[j+1] = cs*s[j+1];
+ if (wantv) {
+ for (int i = 0; i < n; i++) {
+ t = cs*V[i][j] + sn*V[i][j+1];
+ V[i][j+1] = -sn*V[i][j] + cs*V[i][j+1];
+ V[i][j] = t;
+ }
+ }
+ t = hypot(f,g);
+ cs = f/t;
+ sn = g/t;
+ s[j] = t;
+ f = cs*e[j] + sn*s[j+1];
+ s[j+1] = -sn*e[j] + cs*s[j+1];
+ g = sn*e[j+1];
+ e[j+1] = cs*e[j+1];
+ if (wantu && (j < m-1)) {
+ for (int i = 0; i < m; i++) {
+ t = cs*U[i][j] + sn*U[i][j+1];
+ U[i][j+1] = -sn*U[i][j] + cs*U[i][j+1];
+ U[i][j] = t;
+ }
+ }
+ }
+ e[p-2] = f;
+ iter = iter + 1;
+ }
+ break;
+
+ // Convergence.
+
+ case 4: {
+
+ // Make the singular values positive.
+
+ if (s[k] <= 0.0) {
+ s[k] = (s[k] < 0.0 ? -s[k] : 0.0);
+ if (wantv) {
+ for (int i = 0; i <= pp; i++) {
+ V[i][k] = -V[i][k];
+ }
+ }
+ }
+
+ // Order the singular values.
+
+ while (k < pp) {
+ if (s[k] >= s[k+1]) {
+ break;
+ }
+ double t = s[k];
+ s[k] = s[k+1];
+ s[k+1] = t;
+ if (wantv && (k < n-1)) {
+ for (int i = 0; i < n; i++) {
+ t = V[i][k+1]; V[i][k+1] = V[i][k]; V[i][k] = t;
+ }
+ }
+ if (wantu && (k < m-1)) {
+ for (int i = 0; i < m; i++) {
+ t = U[i][k+1]; U[i][k+1] = U[i][k]; U[i][k] = t;
+ }
+ }
+ k++;
+ }
+ iter = 0;
+ p--;
+ }
+ break;
+ }
+ }
+ }
+
+ /* ------------------------
+ Public Methods
+ * ------------------------ */
+
+ /** Return the left singular vectors
+ @return U
+ */
+
+ public double[][] getU () {
+ return U;
+ }
+
+ /** Return the right singular vectors
+ @return V
+ */
+
+ public double[][] getV () {
+ return V;
+ }
+
+ /** Return the one-dimensional array of singular values
+ @return diagonal of S.
+ */
+
+ public double[] getSingularValues () {
+ return s;
+ }
+
+ /** Return the diagonal matrix of singular values
+ @return S
+ */
+
+ public double[][] getS () {
+ double[][] S = new double[n][n];
+ for (int i = 0; i < n; i++) {
+ S[i][i] = (double)this.s[i];
+ }
+ return S;
+ }
+
+ /** Two norm
+ * @return max(S)
+ */
+ public double norm2 () {
+ return s[0];
+ }
+
+ /** Two norm condition number
+ @return max(S)/min(S)
+ */
+ public double cond () {
+ return s[0]/s[Math.min(m,n)-1];
+ }
+
+ /** Effective numerical matrix rank
+ @return Number of nonnegligible singular values.
+ */
+
+ public int rank () {
+ double eps = Math.pow(2.0,-52.0);
+ double tol = Math.max(m,n)*s[0]*eps;
+ int r = 0;
+ for (int i = 0; i < s.length; i++) {
+ if (s[i] > tol) {
+ r++;
+ }
+ }
+ return r;
+ }
+
+ /** sqrt(a^2 + b^2) without under/overflow. **/
+ protected static double hypot(double a, double b) {
+ double r;
+ if (a*a > b*b) {
+ r = b/a;
+ r = Math.abs(a)*Math.sqrt(1+r*r);
+ } else if (b != 0) {
+ r = a/b;
+ r = Math.abs(b)*Math.sqrt(1+r*r);
+ } else {
+ r = 0.0;
+ }
+ return r;
+ }
+
+}
+
+
+
+
+
+
+
+
diff --git a/drawing/papaya/src/papaya/ScatterPlot.java b/drawing/papaya/src/papaya/ScatterPlot.java
new file mode 100644
index 0000000..ca9d365
--- /dev/null
+++ b/drawing/papaya/src/papaya/ScatterPlot.java
@@ -0,0 +1,205 @@
+/**
+ * papaya: A collection of utilities for Statistics and Matrix-related manipulations
+ * http://adilapapaya.com/papayastatistics/, 1.1.0
+ * Created by Adila Faruk, http://adilapapaya.com, May 2012, Last Updated April 2014
+ *
+ *
+ * Copyright (C) 2014 Adila Faruk http://adilapapaya.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307 USA
+ */
+package papaya;
+import java.util.Arrays;
+import java.io.*;
+import java.lang.Object;
+import java.lang.String;
+
+import processing.core.*;
+
+/**
+ * A simple class to plot x vs y data as a
+ * scatter plot. Default is to draw the points as ellipses,
+ * but you can also use rectangles, and connect the points with
+ * lines.
+ * @author Adila Faruk
+ */
+public class ScatterPlot extends Visuals{
+ public boolean asConnectedLines = false;
+ public boolean asRectangles = false;
+ public boolean asEllipses = true;
+ public boolean[] XYLabels = {true,true};
+ public boolean[] XYTicks = {true,true};
+
+ /**
+ * Specifies whether to draw the bounding rectangle around the plot.
+ */
+ public boolean drawRectangle = true;
+ /**
+ * Specifies whether to draw the axes
+ */
+ public boolean drawAxes = true;
+
+ protected PApplet myParent;
+
+ /**
+ * Specifies the space on the left between the plot area, and the
+ * bounding rectangle. That is, the left-most point of
+ * the rectangle is given by plotLeft-leftBound.
+ */
+ public float leftBound = 40;
+ /**
+ * Specifies the space on the right betweent the plot area, and the
+ * bounding rectangle. That is, the right-most point of
+ * the rectangle is given by plotLeft+plotWidth+rightBound.
+ */
+ public float rightBound=20;
+ /**
+ * Specifies the space on the top, between the plot area and the bounding
+ * rectangle. That is, the top-most point of the rectangle is given by
+ * plotTop - topBound.
+ */
+ public float topBound = 20;
+ /**
+ * Specifies the space on the bottom, between the plot area and the bounding
+ * rectangle. That is, the bottom-most point of the rectangle is given by
+ * plotTop + plotHeight + bottomBound.
+ */
+ public float bottomBound=20;
+ //CENTER = 3; BOTTOM = 102; TOP = 101; BASELINE = 0; LEFT = 37; RIGHT = 39;
+
+ protected float minX;
+ protected float minY;
+ protected float maxX;
+ protected float maxY; // plot min/max
+ protected float minXDat;
+ protected float minYDat;
+ protected float maxXDat;
+ protected float maxYDat; // data min/max
+ boolean dataExtremesHaveBeenSet=false;
+
+ public ScatterPlot(PApplet _theParent, float _plotLeft, float _plotTop, float _plotWidth, float _plotHeight){
+ super(_theParent,_plotLeft,_plotTop,_plotWidth,_plotHeight);
+ myParent = _theParent;
+ bgColor = super.bgColor;
+ }
+
+ /** Set the minimum/maximum values on the x and y axis to nice numbers.
+ * e.g. 60 instead of 63.183628, 90 instead of 87.1, etc.
+ * It'll try do it automatically for you, if you don't set it here,
+ * but no guarantees on the result being a nice number. For example, it might
+ * go from 61 to 89 instead of 60 to 90.
+ * @param _minXDat the minimum x value to use. Set to more than the x data minimum.
+ * @param _maxXDat the maximum x value to use. Set to more than the x data maximum.
+ * @param _minYDat the minimum y value to use. Set to more than the y data minimum.
+ * @param _maxYDat the maximum y value to use. Set to more than the y data maximum.
+ */
+ public void setDataExtremes(float _minXDat, float _maxXDat, float _minYDat, float _maxYDat){
+ minXDat = _minXDat; maxXDat = _maxXDat;
+ minYDat = _minYDat; maxYDat = _maxYDat;
+ dataExtremesHaveBeenSet = true;
+ }
+
+ /**
+ * Draws a rectangle around the plot.
+ *
+ *
+ * The algorithm used to obtain these indices is a modified version of the
+ * tuned quicksort proposed by Jon L. Bentley and M. Douglas McIlroy's "Engineering a
+ * Sort Function", Software-Practice and Experience, Vol. 23(11)
+ * P. 1249-1265 (November 1993) (program 7 of the paper; currently used in the Arrays.sort() method
+ * to sort arrays).
+ * It is a modified version in the sense that the original array remains unsorted. Instead,
+ * a copy of the original array is used in the sorting routine, and the array of
+ * indices [0,1,2,...N-1] sorted concurrently with the array being sorted.
+ *
+ *
+ * @author Adila Faruk
+ *
+ */
+public final class Sorting {
+
+ /**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+ protected Sorting(){}
+
+ private static final int SMALL = 7; // params used in original paper
+ private static final int MEDIUM = 40; // params used in original paper
+
+ /**
+ * Gets the array of indices that can be used to sort the array in ascending or
+ * descending order.
+ *
+ *
+ * The algorithm used to obtain these indices is a modified version of the
+ * tuned quicksort proposed by Jon L. Bentley and M. Douglas McIlroy's "Engineering a
+ * Sort Function", Software-Practice and Experience, Vol. 23(11)
+ * P. 1249-1265 (November 1993) (program 7 of the paper; currently used in the Arrays.sort() method
+ * to sort arrays).
+ * It is a modified version in the sense that the original array remains unsorted. Instead,
+ * a copy of the original array is used in the sorting routine, and the array of
+ * indices [0,1,2,...N-1] sorted concurrently with the array being sorted.
+ *
+ * @param a the input array
+ * @param isAscending true for data sorted in increasing order.
+ * @return integar array of indices (with length=data.length) that can be used
+ * to sort the data from low to high or vice-versa..
+ * * @throws IllegalArgumentException if
+ * The algorithm used to obtain these indices is a modified version of the
+ * tuned quicksort proposed by Jon L. Bentley and M. Douglas McIlroy's "Engineering a
+ * Sort Function", Software-Practice and Experience, Vol. 23(11)
+ * P. 1249-1265 (November 1993) (program 7 of the paper; currently used in the Arrays.sort() method
+ * to sort arrays).
+ * It is a modified version in the sense that the original array remains unsorted. Instead,
+ * a copy of the original array is used in the sorting routine, and the array of
+ * indices [fromIndex,fromIndex+1, ... , toIndex-1] sorted concurrently with the array being sorted.
+ * @param a the array to be sorted.
+ * @param fromIndex the index of the first element (inclusive) to be
+ * sorted.
+ * @param toIndex the index of the last element (exclusive) to be sorted.
+ * @param isAscending true for data sorted in increasing order.
+ * @throws IllegalArgumentException if
+ * The algorithm used to obtain these indices is a modified version of the
+ * tuned quicksort proposed by Jon L. Bentley and M. Douglas McIlroy's "Engineering a
+ * Sort Function", Software-Practice and Experience, Vol. 23(11)
+ * P. 1249-1265 (November 1993) (program 7 of the paper; currently used in the Arrays.sort() method
+ * to sort arrays).
+ * It is a modified version in the sense that two arrays are sorted according to the first
+ * array. At the end of the method, the section of both arrays going from
+ *
+ *
+ * The implementor must also ensure that the relation is transitive:
+ * ((compare(x, y)>0) && (compare(y, z)>0)) implies
+ * compare(x, z)>0.
+ *
+ * Finally, the implementer must ensure that compare(x, y)==0
+ * implies that sgn(compare(x, z))==sgn(compare(y, z)) for all
+ * z.
+ *
+ */
+ private static float compare(float value1, float value2, boolean isAscending) {
+ if (isAscending) return value1 - value2;
+ return value2 - value1;
+ }
+ private static double compare(double value1, double value2, boolean isAscending) {
+ if (isAscending) return value1 - value2;
+ return value2 - value1;
+ }
+ private static int compare(int value1, int value2, boolean isAscending) {
+ if (isAscending) return value1 - value2;
+ return value2 - value1;
+ }
+ /** Swaps x[a] with x[b]. */
+ private static void swap(float x[], int a, int b) {
+ float t = x[a]; x[a] = x[b]; x[b] = t;
+ }
+ /** Swaps x[a] with x[b]. */
+ private static void swap(double x[], int a, int b) {
+ double t = x[a]; x[a] = x[b]; x[b] = t;
+ }
+ /** Swaps x[a] with x[b]. */
+ private static void swap(int x[], int a, int b) {
+ int t = x[a]; x[a] = x[b]; x[b] = t;
+ }
+
+ /** Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].*/
+ private static void vecswap(float x[], int a, int b, int n) {
+ for (int i=0; i This class is overkill if:
+ * Example:
+ *
+ * Visuals contains some useful functions for the (elementary!) visual representation
+ * of your data (e.g. setting up your font, writing labels, mapping
+ * arrays to the plot right-left-top-bottom parameters, xTicks, yTicks,legends,
+ * etc). Nothing very fancy like writing x-labels at an angle or computing your
+ * tax returns for you though so don't get your hopes up. Sorry.
+ *
+ *
+ * @param _theParent the Processing papplet. Just "this" in your sketch.
+ * @param _plotLeft the plot left
+ * @param _plotTop the plot top (in Processing coordinates!)
+ * @param _plotWidth the plot width
+ * @param _plotHeight the plot height.
+ * The bottom of the plot area is then given by
+ * Note: No internal checks are done to make sure that the min/max values are in the
+ * data range.
+ *
+ * Note: No internal checks are done to make sure that the min/max values are in the
+ * data range.
+ *
+ *
+ */
+ public void drawRectangle(){
+ drawRect(plotLeft-leftBound,plotTop-topBound,plotWidth+leftBound+rightBound,plotHeight+topBound+bottomBound,bgColor);
+ }
+
+ /**
+ * Parent function to plot scatter plot of the data.
+ * @param _xDat x Data array
+ * @param _yDat y Data array
+ * @param _sWeight stroke weight
+ * @param _sColor stroke color
+ */
+ public void drawScatterPlot(float[] _xDat, float[] _yDat, float _sWeight, int _sColor){
+ if(drawRectangle){
+ drawRect(plotLeft-leftBound,plotTop-topBound,plotWidth+leftBound+rightBound,plotHeight+topBound+bottomBound,bgColor);
+ }
+ if(drawAxes){
+ drawAxes(50,.5f);
+ }
+
+ // Check if the data extemes have been set.
+ if(!dataExtremesHaveBeenSet){
+ throw new IllegalStateException("Set the data extremes first.");
+ }
+ // map the data.
+ float[] xDatMapped = Mat.map(_xDat,minXDat,maxXDat,plotLeft,plotLeft+plotWidth);
+ float[] yDatMapped = Mat.map(_yDat,minYDat,maxYDat,plotTop+plotHeight,plotTop);
+
+
+ if(asConnectedLines){
+ drawConnectedWithLines(xDatMapped,yDatMapped,_sWeight,_sColor);
+ }
+ if(asRectangles){
+ drawAsRectangles(xDatMapped,yDatMapped,_sColor,10,6);
+ }
+ if(asEllipses){
+ drawAsEllipses(xDatMapped,yDatMapped,_sColor,6);
+ }
+ }
+
+ protected void drawScatterPlot(float[] _xDat, float[] _yDat){
+ drawScatterPlot(_xDat,_yDat,STROKEWEIGHT[0],GRAY[0]);
+ }
+
+ /**
+ * connects the dots
+ */
+ protected void drawConnectedWithLines(float[] xDatMapped, float[] yDatMapped, float _sWeight, int _sColor){
+
+ myParent.noFill();
+ myParent.stroke(_sColor);
+ myParent.strokeWeight(_sWeight);
+
+ myParent.beginShape();
+ for(int i=0; i
+ * X={3, 2, 1}, Y={3.0, 1.0, 2.0}, Z={6.0, 7.0, 8.0}
. The output should
+ * be
+ *
.
+ * To do this, we can use the indices corresponding to the elements of
+ * X={1, 2, 3}, Y={2.0, 1.0, 3.0}, Z={8.0, 7.0, 6.0}X
sorted
+ * in ascending order:
+ * X={3,2,1}
so indicesForSorting = {2,1,0}
+ * (X[2]
holds the smallest value of X
,
+ * X[1]
holds the next smallest value of X
, and X[0]
+ * holds the largest value of X
).
+ * The indicesForSorting
can then be used to arrange Y
and Z
+ * in the same order.
+ *
+ * fromIndex > toIndex
+ * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
or
+ * toIndex > a.length
+ *
+ *
+ */
+ public static int[] indices(float[] a, boolean isAscending) {
+ return indices(a,0,a.length,isAscending);
+ }
+ public static int[] indices(double[] a, boolean isAscending) {
+ return indices(a,0,a.length,isAscending);
+ }
+ public static int[] indices(int[] a, boolean isAscending) {
+ return indices(a,0,a.length,isAscending);
+ }
+ /**
+ * Gets the array of indices that can be used to sort the section of the
+ * input array going from fromIndex
(inclusive) to toIndex
(exclusive)
+ * in ascending or descending order.
+ *
+ * fromIndex > toIndex
+ * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
or
+ * toIndex > a.length
+ */
+ public static int[] indices(float[] a, int fromIndex, int toIndex, boolean isAscending) {
+ rangeCheck(a.length, fromIndex, toIndex);
+ int lenToSort = toIndex-fromIndex;
+ //int[] indicesToSort = Mat.linspace(fromIndex, toIndex-1);
+ int[] indicesToSort = Mat.linspace(0,a.length-1);
+ float[] x = Arrays.copyOf(a,a.length);
+ quickSort1(x, indicesToSort, fromIndex, lenToSort, isAscending);
+ return indicesToSort;
+ }
+ public static int[] indices(double[] a, int fromIndex, int toIndex, boolean isAscending) {
+ rangeCheck(a.length, fromIndex, toIndex);
+ int lenToSort = toIndex-fromIndex;
+ //int[] indicesToSort = Mat.linspace(fromIndex, toIndex-1);
+ int[] indicesToSort = Mat.linspace(0,a.length-1);
+ double[] x = Arrays.copyOf(a,a.length);
+ quickSort1(x, indicesToSort, fromIndex, lenToSort, isAscending);
+ return indicesToSort;
+ }
+ public static int[] indices(int[] a, int fromIndex, int toIndex, boolean isAscending) {
+ rangeCheck(a.length, fromIndex, toIndex);
+ int lenToSort = toIndex-fromIndex;
+ //int[] indicesToSort = Mat.linspace(fromIndex, toIndex-1);
+ int[] indicesToSort = Mat.linspace(0,a.length-1);
+ int[] x = Arrays.copyOf(a,a.length);
+ quickSort1(x, indicesToSort, fromIndex, lenToSort, isAscending);
+ return indicesToSort;
+ }
+
+ /**
+ * Sorts the input x
and y
arrays according to
+ * x
and going from fromIndex
(inclusive) to toIndex
(exclusive)
+ * in ascending or descending order.
+ *
+ * fromIndex
(inclusive) to toIndex
(exclusive) are sorted according
+ * to the first array's order. Use a copy of both arrays if you want to preserve the natural order of
+ * the original arrays.
+ * @param x the array to sort.
+ * @param y the second array, to sort according to x
.
+ * @param fromIndex the index of the first element (inclusive) to be
+ * sorted.
+ * @param toIndex the index of the last element (exclusive) to be sorted.
+ * @param isAscending true for data sorted in increasing order.
+ * @throws IllegalArgumentException if fromIndex > toIndex
+ * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
or
+ * toIndex > a.length
+ * @see Arrays#sort(float[])
+ */
+ public static void twoArrays(float[] x, float[] y, int fromIndex, int toIndex, boolean isAscending) {
+ rangeCheck(x.length, fromIndex, toIndex);
+ int lenToSort = toIndex-fromIndex;
+ quickSort2(x, y, fromIndex, lenToSort, isAscending);
+ }
+ public static void twoArrays(double[] x, double[] y, int fromIndex, int toIndex, boolean isAscending) {
+ rangeCheck(x.length, fromIndex, toIndex);
+ int lenToSort = toIndex-fromIndex;
+ quickSort2(x, y, fromIndex, lenToSort, isAscending);
+ }
+ public static void twoArrays(int[] x, int[] y, int fromIndex, int toIndex, boolean isAscending) {
+ rangeCheck(x.length, fromIndex, toIndex);
+ int lenToSort = toIndex-fromIndex;
+ quickSort2(x, y, fromIndex, lenToSort, isAscending);
+ }
+
+ /* Returns the index of the median of the three indexed floats */
+ private static int med3(float x[], int a, int b, int c) {
+ // isAscending/Descending doesn't make a difference since
+ // we're getting the median
+ float ab = compare(x[a],x[b],true);
+ float ac = compare(x[a],x[c],true);
+ float bc = compare(x[b],x[c],true);
+ return (ab<0 ?
+ (bc<0 ? b : ac<0 ? c : a) :
+ (bc>0 ? b : ac>0 ? c : a));
+ }
+ /* Returns the index of the median of the three indexed floats */
+ private static int med3(double x[], int a, int b, int c) {
+ // isAscending/Descending doesn't make a difference since
+ // we're getting the median
+ double ab = compare(x[a],x[b],true);
+ double ac = compare(x[a],x[c],true);
+ double bc = compare(x[b],x[c],true);
+ return (ab<0 ?
+ (bc<0 ? b : ac<0 ? c : a) :
+ (bc>0 ? b : ac>0 ? c : a));
+ }
+ /* Returns the index of the median of the three indexed floats */
+ private static int med3(int x[], int a, int b, int c) {
+ // isAscending/Descending doesn't make a difference since
+ // we're getting the median
+ int ab = compare(x[a],x[b],true);
+ int ac = compare(x[a],x[c],true);
+ int bc = compare(x[b],x[c],true);
+ return (ab<0 ?
+ (bc<0 ? b : ac<0 ? c : a) :
+ (bc>0 ? b : ac>0 ? c : a));
+ }
+
+ /* compare(a,b) returns (a-b) if isAscending. else returns b-a*/
+ private static void quickSort1(float[] x, int[] indices, int off, int len, boolean isAscending) {
+ //isAscendingnsertion sort on smallest arrays. (Checked, and works).
+ if (len < SMALL) {
+ for (int i=off; isgn(compare(x, y)) ==
+ * -sgn(compare(y, x))
for all x and y. (This
+ * implies that compare(x, y) must throw an exception if and only
+ * if compare(y, x) throws an exception.)
+ * you want to store only the unique elements. Use a Java
+ * Set instead.
+ *
+ * you don't want to store everything in a class. Use the
+ * {@link Descriptive#frequencies} method instead.
+ */
+public class Unique {
+
+ /**
+ * ArrayList containing the unique values
+ */
+ public ArrayList
+ * - The input data array is unsorted.
+ *
+ *
+ * - You can choose to store the indices of each unique value as well by setting the @param storeIndices
+ * to true
.
+ *
+ *
+ * - It (currently) only works for float arrays.
+ *
+ * data = (8,5,6,7,8,5,5) --> values = (5,6,7,8), frequencies = (3,1,1,2),
+ * idx[0] = {1,5,6}, idx[1] = {2}, idx[2] = {3}, idx[3] = {0,4}
+ * true
if you want to store the indices,
+ * else set to false
+ * It:
+ *
+ * 1. Contains some useful functions for the visual representation
+ * of your data (e.g. setting up your font, writing labels, mapping
+ * arrays to the plot right-left-top-bottom parameters, xTicks, yTicks,
+ * etc.) that should help you reduce the amount of code you end up
+ * writing.
+ *
+ * 2. Is extended by the available "Plotting" classes used to quickly give a
+ * visual snapshot of your data.
+ *
_plotTop + _plotHeight
+ * while the right is _plotLeft + _plotWidth
.mappedData[i] = plotLeft + (data[i] - min) / (max-min) * plotWidth
.
+ * This is not done directly though, but rather, the
+ * {@link Mat#map(float[] data, float low1, float high1, float low2, float high2)}
+ * function is used.
+ * @param data the x data to map.
+ * @return the mapped data.
+ */
+ public float[] mapXData(float[] data){
+ float min = Descriptive.min(data);
+ float max = Descriptive.max(data);
+ return Mat.map( data,min,max,plotLeft,plotLeft + plotWidth);
+ }
+ /**
+ * Maps the x-data from the range minXDat to maxXDat to
+ * the plotLeft and plotRight. That is,
+ * mappedData[i] = plotLeft + (data[i] - minXDat) / (maxXDat-minXDat) * plotWidth
.
+ * This is not done directly though, but rather, the
+ * {@link Mat#map(float[] data, float low1, float high1, float low2, float high2)}
+ * function is used.
+ * mappedData[i] = plotBottom + (data[i] - min) / (max-min) * (-plotHeight)
.
+ * (The minus sign above accounts for the difference in Processing's coordinate system
+ * and what we associate with Top/Bottom).
+ * The computation above is not done directly though, but rather, the
+ * {@link Mat#map(float[] data, float low1, float high1, float low2, float high2)}
+ * function is used.
+ * @param data the y data to map.
+ * @return the mapped data.
+ */
+ public float[] mapYData(float[] data){
+ float min = Descriptive.min(data);
+ float max = Descriptive.max(data);
+ return Mat.map( data, min, max, plotTop + plotHeight, plotTop);
+ }
+
+ /**
+ * Maps the y-data from the range minYDat to maxYDat to
+ * the plotBottom and plotTop. That is,
+ * mappedData[i] = plotBottom + (data[i] - minYDat) / (maxYDat-minYDat) * (-plotHeight)
.
+ * (The minus sign above accounts for the difference in Processing's coordinate system
+ * and what we associate with Top/Bottom).
+ * The computation above is not done directly though, but rather, the
+ * {@link Mat#map(float[] data, float low1, float high1, float low2, float high2)}
+ * function is used.
+ *