Created
January 25, 2024 09:02
-
-
Save gusdelact/6d09711bcffb819d924811ad7f163362 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use nalgebra::{DMatrix, DVector}; | |
fn main() { | |
// Datos de ejemplo | |
let x_data = vec![1.0, 2.0, 3.0, 4.0, 5.0]; | |
let y_data = vec![2.0, 4.0, 6.0, 8.0, 10.0]; | |
// Crear una matriz de diseño X | |
let mut x_matrix = DMatrix::from_fn(x_data.len(), 2, |i, j| if j == 0 { 1.0 } else { x_data[i] }); | |
println!("{}",x_matrix); | |
// Crear un vector y | |
let y_vector = DVector::from_vec(y_data); | |
println!("{}",y_vector); | |
// Calcular la transpuesta de X | |
let x_transpose = x_matrix.transpose(); | |
println!("{}",x_transpose); | |
// Calcular los coeficientes (θ) usando la fórmula: θ = (X^T * X)^-1 * X^T * y | |
let coefficients = (x_transpose.clone() * x_matrix).try_inverse().unwrap() * x_transpose * y_vector; | |
// Imprimir los coeficientes | |
println!("Coeficientes: {:?}", coefficients); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment