Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linear Regression #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "nushell-liniar-regression"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
94 changes: 94 additions & 0 deletions crates/nu-command/src/math/nushell-liniar-regression/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Matrix operation
The structures the store variables in lines and columns:
```rust
pub struct MatrixMN {
pub values: Vec<Vec<f64>>,
}
```

## Creating a matrix
```rust
let values: Vec<f64> = vec![1.0, 5.0];
let mat: MatrixMN = MatrixMN::create_matrix(&values, 1, 2); // a vector of f64, nr of lines, nr of columns
```

## Accessing an element
```rust
mat.values[i][j]
```
- element on the line `i - 1` and `j - 1` column
- indexing starts from `0`


# Linear Regression

```rust
let nm1: String = String::from("X-var");
let nm2: String = String::from("Y-var");
let val1: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0];
let val2: Vec<f64> = vec![14.5, 140.1, 201.3, 220.5];

let dt: DataSet = DataSet::new(nm1.clone(), nm2.clone(), val1.clone(), val2.clone());

match dt.compute_linear_regression() {
Ok(line) => {
// d : y = a * x + b ; a = slope; b = intercept
println!("d : y = {} * x + {}", line.slope, line.intercept);
},
Err(xbar) => {
// d : x = constant
println!("d : x = {}", xbar.x);
}
}

```


# Explaining The Algorithm
What is linear regression?
The best line that can go through a set of points P1(x1, y1), P2(x2, y2) ... P3(x3, y3)

`d : y = a * x + b (x = ?, y = ?)` = the equation of the line

We are to find the `slope (x)` and the `intercept (y)` of this line.

Assuming that all points are on the same line,
we will obtain the following system of equations:
- a * x1 + b = y1
- a * x2 + b = y2
- a * x3 + b = y3
- .....
- a * xn + b = yn


Therefore, we can define the following matrices
```
| 1 x1 | | y1 |
| 1 x2 | | y2 |
A = | .... | X = | b | B = | .. |
| 1 xn | | a | | yn |
```


Now, the matrix `X` is the answear of the equations,
which are incompatible, therefore they are part of an indeterminate system `A * X = B`.

## How to approximate the matrix X?
```
A * X = B
At * | A * X = B
(At*A)^-1 | At * A * X = At * B
[(At * A)^(-1) * (At * A)] * X = (At * A)^(-1) * At * B
In * X = (At * A)^(-1) * At * B
X = (At * A)^(-1) * At * B
```


Where:
- `In`:
1. identity matrix with n lines and n columns
2. has 1 on the diagonal and 0 in rest
- `At`:
1. the transposed matrix
2. columns of the initial matrix become rows of the transpose
3. rows of the initial matrix become columns of the transpose
Loading