Skip to content

Instantly share code, notes, and snippets.

@sarath-soman
Created July 21, 2018 17:30
Show Gist options
  • Save sarath-soman/2b03c82365baf597036fe58eb796fe8e to your computer and use it in GitHub Desktop.
Save sarath-soman/2b03c82365baf597036fe58eb796fe8e to your computer and use it in GitHub Desktop.
package kjug;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
/**
* Implementation of Linear Regression learning algorithm
* @author Sarath Soman
*/
public class LinearRegression {
public List<Double> train(List<List<Double>> X, List<Double> y, double learningRate) {
if(X.size() != y.size()) {
throw new RuntimeException("IDV - DV size mismatch");
}
for(int i = 0; i < X.size(); i++) {
List<Double> xi = new ArrayList<>(X.get(i));
xi.add(1.0);
X.set(i, xi);
}
int noOfIdv = X.get(0).size();
List<Double> thetas = IntStream.range(0, noOfIdv)
.mapToObj(theta -> 0.0)
.collect(Collectors.toList());
List<Double> thetasTemp = new ArrayList<>(thetas);
for(int i = 0; i < 100000; i++) {
for (int j = 0; j < noOfIdv; j++) {
double tempTheta = thetasTemp.get(j) - learningRate * differentiateCostFunction(thetasTemp, X, y, j);
thetas.set(j, tempTheta);
}
thetasTemp = new ArrayList<>(thetas);
}
return thetas;
}
private double differentiateCostFunction(List<Double> thetas, List<List<Double>> X, List<Double> y, int index) {
int m = X.size();
return IntStream.range(0, m)
.mapToDouble(i -> (computeHTheta(thetas, X, i) - y.get(i)) * X.get(i).get(index))
.sum() / m;
}
private double computeHTheta(List<Double> thetas, List<List<Double>> X, int index) {
int noOfIdv = X.get(0).size();
return IntStream.range(0, noOfIdv)
.mapToDouble(i -> thetas.get(i) * X.get(index).get(i))
.sum();
}
public static void main(String[] args) {
LinearRegression regression = new LinearRegression();
//y = x + 2
List<List<Double>> X = Arrays.asList(Arrays.asList(1.0), Arrays.asList(2.0), Arrays.asList(3.0),
Arrays.asList(4.0), Arrays.asList(5.0), Arrays.asList(6.0),
Arrays.asList(7.0), Arrays.asList(8.0), Arrays.asList(9.0),
Arrays.asList(10.0), Arrays.asList(11.0));
List<Double> y = Arrays.asList(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0);
List<Double> thetas = regression.train(X, y, 0.001);
thetas.stream()
.forEach(System.out::println);
}
public static class CostCalculator {
public double estimateCost(List<Double> thetas, List<List<Double>> X, List<Double> y) {
LinearRegression regression = new LinearRegression();
int m = X.size();
return IntStream.range(0, m)
.mapToDouble(i -> (regression.computeHTheta(thetas, X, i) - y.get(i)))
.map(x -> Math.pow(x, 2))
.sum() / (2 * m);
}
public static void main(String[] args) {
CostCalculator costCalculator = new CostCalculator();
List<List<Double>> X = Arrays.asList(Arrays.asList(1.0), Arrays.asList(2.0), Arrays.asList(3.0),
Arrays.asList(4.0), Arrays.asList(5.0), Arrays.asList(6.0),
Arrays.asList(7.0), Arrays.asList(8.0), Arrays.asList(9.0),
Arrays.asList(10.0), Arrays.asList(11.0));
List<Double> y = Arrays.asList(3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0);
DoubleStream.of(-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8)
.map(theta -> costCalculator.estimateCost(Arrays.asList(theta, 2.0), X, y))
.forEach(System.out::println);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment