Skip to content

Instantly share code, notes, and snippets.

@diegoolipa
Created September 20, 2023 12:38
Show Gist options
  • Save diegoolipa/3e983cf0c67d78ef480ab5b51837d396 to your computer and use it in GitHub Desktop.
Save diegoolipa/3e983cf0c67d78ef480ab5b51837d396 to your computer and use it in GitHub Desktop.
Algoritmo en JAVA REGRESION LINELA SIMPLE
import org.apache.commons.math3.analysis.function.Exp;
import org.apache.commons.math3.fitting.WeightedObservedPoints;
import org.apache.commons.math3.fitting.WeightedObservedPoint;
import org.apache.commons.math3.stat.regression.SimpleRegression; // Cambiar la importación
public class RegresionLinealSimple {
public static void main(String[] args) {
// Datos de entrada
double[] years = {2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023};
double[] saldo = {751, 795, 852, 953, 971, 1002, 1287, 1396, 1399, 1502, 1596, 2000, 2842, 3052, 3520};
// Crear un objeto SimpleRegression
SimpleRegression regression = new SimpleRegression();
// Agregar los datos al objeto SimpleRegression
for (int i = 0; i < years.length; i++) {
regression.addData(years[i], saldo[i]);
}
// Obtener los coeficientes de la regresión
double b0 = regression.getIntercept();
double b1 = regression.getSlope();
// Imprimir los coeficientes
System.out.println("Coeficiente de la pendiente (B1): " + b1);
System.out.println("Coeficiente de la ordenada al origen (B0): " + b0);
// Predecir el saldo para un año determinado (por ejemplo, 2024)
double yearToPredict = 2025;
double predictedSaldo = b0 + b1 * yearToPredict;
System.out.println("Predicción de saldo para el año " + yearToPredict + ": " + predictedSaldo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment