Struct rustml::regression::Hypothesis [] [src]

pub struct Hypothesis {
    // some fields omitted
}

Hypothesis for linear regression.

Methods

impl Hypothesis

fn random(n: usize) -> Hypothesis

Creates a hypothesis with parameters initialized with random values in the interval [0,1).

The parameter n denotes the number of parameters of the hypothesis.

Example

use rustml::regression::*;

let h = Hypothesis::random(5);
assert_eq!(h.params().len(), 5);
assert!(h.params().iter().all(|&x| x < 1.0));

fn from_params(params: &[f64]) -> Hypothesis

Creates a new hypothesis with the given parameters.

Example

use rustml::regression::*;

let h = Hypothesis::from_params(&[0.1, 0.2, 0.3]);
assert_eq!(h.params().len(), 3);
assert_eq!(h.params(), vec![0.1, 0.2, 0.3]);

fn params(&self) -> Vec<f64>

Returns the parameters for this hypothesis.

Example

use rustml::regression::*;

let h = Hypothesis::from_params(&[0.1, 0.2, 0.3]);
assert_eq!(h.params(), vec![0.1, 0.2, 0.3]);

fn eval(&self, x: &Matrix<f64>) -> Vec<f64>

Computes the hypothesis for the given design matrix using the underlying BLAS implementation for high performance.

The following equation is used to compute the hypothesis: $h_\theta (X) = X\theta$ where $\theta$ is the row vector of the parameters. The result is a vector $y$ where the component $y_i$ is the result of the hypothesis evaluated for the example at row $i$ of the matrix, i.e. $y_i = h_\theta(x^{(i)}) = \theta^Tx^{(i)}$

Example

use rustml::*;
use rustml::regression::*;

let x = mat![
    1.0, 2.0, 3.0;
    1.0, 5.0, 4.0
];
let h = Hypothesis::from_params(&[0.1, 0.2, 0.3]);
assert_eq!(
    h.eval(&x), 
    vec![0.1 + 0.4 + 0.9, 0.1 + 1.0 + 1.2]
);

fn error(&self, x: &Matrix<f64>, y: &[f64]) -> f64

Computes the error of the hypothesis for the examples in the design matrix x and the target values y.

$$\frac{1}{m}(X\theta-y)^T(X\theta-y)$$

fn derivatives(&self, x: &Matrix<f64>, y: &[f64]) -> Vec<f64>