Published
Edited
Dec 2, 2021
5 forks
Importers
Insert cell
Insert cell
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]
}
}
Insert cell
X = [1,4,6,10]
Insert cell
Y = [3,2,7,9]
Insert cell
regression = new Regression(X,Y,.01,10000)
Insert cell
regression.fit()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more