This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double[] solvesystem(double L[][], double D[], double XTY[]){ | |
//back substitution with L | |
double p[] = new double[XTY.length] ; | |
for (int j = 0; j < inputs; j++){ | |
p[j] = XTY[j] ; | |
for (int i = 0; i < j; i++){ | |
p[j] -= L[j][i] * p[i]; | |
} | |
} | |
//Multiply by inverse of D matrix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double[][] L = new double[inputs][ inputs]; | |
double D[] = new double[inputs] ; | |
//for each column j | |
for (int j = 0; j < inputs; j++){ | |
D[j] = XTX[j][j];//calculate Dj | |
for (int k = 0; k < j; k++){ | |
D[j] -= L[j][k] * L[j][k] * D[k]; | |
} | |
//calculate jth column of L | |
L[j][j] = 1 ; // don't really need to save this but its a 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Calculate the probability that the given input is in the positive class | |
public double probability(double in[]){ | |
double relativepositive=0,relativenegative=0; | |
for(int j=0; j<in.length; j++){ | |
relativepositive += (in[j]-posmean[j])*(in[j]-posmean[j]) / posvariance[j] ; | |
relativenegative += (in[j]-negmean[j])*(in[j]-negmean[j]) / negvariance[j] ; | |
} | |
relativepositive = positives*Math.exp(0.5*relativepositive) ; | |
relativenegative = negatives*Math.exp(0.5*relativenegative) ; | |
return relativepositive / (relativepositive + relativenegative) ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//constructs a naive Bayes binary classifier | |
public NaiveBayes(double in[][], boolean out[]){ | |
int inputs = in[0].length ; | |
//initialize sums and sums of squares for each class | |
double[] poss = new double[inputs], poss2 = new double[inputs]; | |
double[] negs = new double[inputs], negs2 = new double[inputs]; | |
//calculate amount of each class, sums, and sums of squares | |
for(int k=0;k<in.length;k++){//for each data point | |
if(out[k]){ | |
positives++;//keep track of total positives |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double ProbabilityOfInputIfPositive(double in[]){ | |
double prob = 1/Math.sqrt(2 * Math.PI) ; | |
for(int j=0; j<in.length;j++){ | |
prob*= Math.exp(- (in[j]-posmean[j])*(in[j]-posmean[j]) / (2*posvariance[j]) ) ; | |
} | |
return prob ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This is an efficient implementation of a fixed size cache using Java generics. | |
* It uses a least recently used eviction policy implemented via a combination hashtable/linked list data structure. | |
* Written by Alrecenk October 2015 because I felt like playing with generics. | |
*/ | |
import java.util.HashMap; | |
import java.util.Random; | |
public class Cache<KeyType, RecordType>{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//scales an image, maintaining aspect ratio, to fit within the desired width and height | |
//averages color over squares weighted by overlap area to get "perfect" results when scaling down | |
//does not interpolate and is not designed for scaling up, only down (hence thumbnail) | |
public static BufferedImage makethumbnail(BufferedImage img, double desiredwidth, double desiredheight){ | |
//System.out.println("Original Image size: " + img.getWidth(null)+ " x "+img.getHeight(null)); | |
if(img ==null || img.getWidth(null) <1 || img.getHeight(null) <1){ | |
return null; // something wrong with image | |
}else{ | |
byte image[][][] = convertimage(img) ; // convert to byte array, first index is x, then y, then {r,g,b} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//returns the hessian (gradient of gradient) of error with respect to weights | |
//for a logistic regression with weights w on the given input and output | |
//output should be in the form 0 for negative, 1 for positive | |
public double[][] hessian(double w[]){ | |
heval++;//keep track of how many times this has been called | |
double h[][] = new double[w.length][] ; | |
//second derivative matrices are always symmetric so we only need triangular portion | |
for(int j=0;j<h.length;j++){ | |
h[j] = new double [j+1] ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//starting from w0 searches for a weight vector using gradient descent | |
//and Wolfe condition line-search until the gradient magnitude is below tolerance | |
//or a maximum number of iterations is reached. | |
public double[] gradientDescent(double w0[], double tolerance, int maxiter){ | |
double w[] = w0 ; | |
double gradient[] = gradient(w0) ; | |
int iteration = 0 ; | |
while(Math.sqrt(dot(gradient,gradient)) > tolerance && iteration < maxiter){ | |
iteration++ ; | |
//calculate step-size in direction of negative gradient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Performs a binary search to satisfy the Wolfe conditions | |
//returns alpha where next x =should be x0 + alpha*d | |
//guarantees convergence as long as search direction is bounded away from being orthogonal with gradient | |
//x0 is starting point, d is search direction, alpha is starting step size, maxit is max iterations | |
//c1 and c2 are the constants of the Wolfe conditions (0.1 and 0.9 can work) | |
public static double stepSize(OptimizationProblem problem, double[] x0, double[] d, double alpha, int maxit, double c1, double c2){ | |
//get error and gradient at starting point | |
double fx0 = problem.error(x0); | |
double gx0 = dot(problem.gradient(x0), d); |