class Regression{
constructor(X,Y,rate,epochs){
this.X = X
this.Y = Y
this.rate = rate
this.epochs = epochs
}
mse(w,b){
const Yhat =this.X.map(x => w*x+b)
const differences = this.X.map((x,i)=>this.Y[i]-Yhat[i])
const squares = differences.map(d => d*d)
const mse = d3.mean(squares)
return mse
}
msePartialw(w,b){
const summand = this.X.map((x,i) => (this.Y[i]-w*this.X[i]-b)*this.X[i])
return -2*d3.sum(summand)/this.X.length
}
msePartialb(w,b){
const summand = this.X.map((x,i) => (this.Y[i]-w*this.X[i]-b))
return -2*d3.sum(summand)/this.X.length
}
fit(){
const coordinates = []
let w = 0
let b = 0
for(let i = 0; i <= this.epochs; i = i + 1){
w = w - this.rate*this.msePartialw(w,b)
b = b - this.rate*this.msePartialb(w,b)
}
return [w,b]
}
}