Skip to content

create LinearRegression #62

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/com/stuypulse/stuylib/math/Regression/LinearRegression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.stuypulse.stuylib.math.Regression;

import com.stuypulse.stuylib.math.Vector2D;

// sum of x
public class LinearRegression {

public Vector2D[] points;

public LinearRegression(Vector2D... points){
this.points = points;
}

private Vector2D equation(){
double sumX = 0;
double sumY = 0;
double sumXX = 0;
double sumXY = 0;

for(Vector2D point : points) {
sumX += point.x;
sumY += point.y;
sumXX += Math.pow(point.x, 2);
sumXY += point.x * point.y;

}

double intercept = (sumY * sumXX - sumX * sumXY) / (sumXX - Math.pow(sumX, 2));
double slope = (points.length * sumXY - sumX * sumY) / (points.length * sumXX - Math.pow(sumX,2));

return new Vector2D(slope, intercept);
}

public void addRefPoint(double x, double y){

}

public double predictedValue(double x){
return equation().x * x + equation().y;
}

}
38 changes: 26 additions & 12 deletions src/com/stuypulse/stuylib/util/plot/Playground.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.stuypulse.stuylib.util.plot;

import com.stuypulse.stuylib.math.Vector2D;
import com.stuypulse.stuylib.math.Regression.LinearRegression;
import com.stuypulse.stuylib.math.interpolation.*;
import com.stuypulse.stuylib.streams.*;
import com.stuypulse.stuylib.streams.angles.AFuser;
Expand Down Expand Up @@ -34,11 +35,11 @@ public interface Constants {
int WIDTH = 800;
int HEIGHT = 600;

double MIN_X = -1.0;
double MAX_X = 1.0;
double MIN_X = 0.0;
double MAX_X = 10.0;

double MIN_Y = -1.0;
double MAX_Y = 1.0;
double MIN_Y = 0.0;
double MAX_Y = 10.0;

Settings SETTINGS =
new Settings()
Expand Down Expand Up @@ -69,6 +70,11 @@ public static Series make(String id, BStream series) {
}

public static void main(String[] args) throws InterruptedException {





Plot plot = new Plot(Constants.SETTINGS);

VStream m = VStream.create(() -> plot.getMouse().sub(new Vector2D(0.5, 0.5)).mul(2));
Expand All @@ -95,14 +101,22 @@ public static void main(String[] args) throws InterruptedException {
VStream delay = VStream.create(() -> delay_angle.get().getVector().mul(1.1));
VStream afuser = VStream.create(() -> afuser_angle.get().getVector().mul(1.05));

plot.addSeries(Constants.make("Angle", mouse))
.addSeries(Constants.make("Jerk", jerk))
.addSeries(Constants.make("Rate", rate))
.addSeries(Constants.make("LPF", lpf))
.addSeries(Constants.make("HPF", hpf))
.addSeries(Constants.make("afuser", afuser))
.addSeries(Constants.make("delayed", delay))
.addSeries(Constants.make("mouse", m));
Vector2D[] refPoints = {new Vector2D(1,2), new Vector2D(2,5), new Vector2D(6, 10)};
LinearRegression LinearRegression = new LinearRegression(refPoints);
plot.addSeries(new FuncSeries(
new Config("linear regression", 1000),
new Domain(0, 10),
x -> LinearRegression.predictedValue(x)
));

// plot.addSeries(Constants.make("Angle", mouse))
// .addSeries(Constants.make("Jerk", jerk))
// .addSeries(Constants.make("Rate", rate))
// .addSeries(Constants.make("LPF", lpf))
// .addSeries(Constants.make("HPF", hpf))
// .addSeries(Constants.make("afuser", afuser))
// .addSeries(Constants.make("delayed", delay))
// .addSeries(Constants.make("mouse", m));
//
// .addSeries(Constants.make("y=x", x -> x))
// .addSeries(
Expand Down