This repository has been archived on 2023-11-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
MINDLE/LinearRegression/linearRegression.cpp

97 lines
1.9 KiB
C++

/* @authors: Yann & Sam' */
#include <iostream>
#include <random>
#include "../Modules/Graphics/graphics.hpp"
using namespace std;
#define FIRSTALPHA 0.01
#define NBEXAMPLES 1000
#define NBITERATIONS 1000
#define INTERVALWIDTH 20
void getRandomCoordinates(long double[], long double[], const long double);
long double getGradient(const long double[], const long double[], const long double);
int main(int argc, char const *argv[])
{
(void)argc;
(void)argv;
/* We set the coefficient here */
const long double LEADINGCOEFFICIENT = 2.0;
long double x[NBEXAMPLES];
long double y[NBEXAMPLES];
/* Coordinates generation */
getRandomCoordinates(x, y, LEADINGCOEFFICIENT);
long double theta(0.0);
long double alpha(FIRSTALPHA);
long double gradOld(getGradient(x, y, theta));
long double grad;
/* Training ! */
int i;
for(i = 0; i < NBITERATIONS; i++)
{
theta -= alpha * gradOld;
grad = getGradient(x, y, theta);
if(grad != gradOld)
{
alpha = -alpha * gradOld / (grad - gradOld);
}
if(grad == 0.0)
{
i++;
break;
}
gradOld = grad;
}
cout << "Theta: " << theta << endl;
cout << "| Done in " << i << " iterations. |" << endl;
sfmlDisplay("Linear Regression (C++)", Matrix<long double, 1, NBEXAMPLES>(x), Matrix<long double, 1, NBEXAMPLES>(y), Matrix<long double, 1, 1>(theta));
return 0;
}
void getRandomCoordinates(long double x[], long double y[], const long double leadingCoefficient)
{
random_device rd;
mt19937 mt(rd());
uniform_real_distribution<double> random_bias(-INTERVALWIDTH, INTERVALWIDTH);
for(int i(0); i < NBEXAMPLES; i++)
{
x[i] = i;
y[i] = (i * leadingCoefficient) + random_bias(mt);
}
}
long double getGradient(const long double x[], const long double y[], const long double theta)
{
long double value = 0.0;
for(int i(0); i < NBEXAMPLES; i++)
{
value += (theta * x[i] - y[i]) * x[i];
}
return value / NBEXAMPLES;
}