fsml_lin Module

Module for linear algebra procedures. Uses LAPACK routines (through stdlib).


Uses

  • module~~fsml_lin~~UsesGraph module~fsml_lin fsml_lin module~fsml_con fsml_con module~fsml_lin->module~fsml_con module~fsml_err fsml_err module~fsml_lin->module~fsml_err module~fsml_ini fsml_ini module~fsml_lin->module~fsml_ini module~fsml_sts fsml_sts module~fsml_lin->module~fsml_sts module~fsml_utl fsml_utl module~fsml_lin->module~fsml_utl module~fsml_con->module~fsml_ini module~fsml_err->module~fsml_con module~fsml_err->module~fsml_ini module~fsml_err->module~fsml_utl ieee_arithmetic ieee_arithmetic module~fsml_ini->ieee_arithmetic iso_fortran_env iso_fortran_env module~fsml_ini->iso_fortran_env stdlib_linalg stdlib_linalg module~fsml_ini->stdlib_linalg module~fsml_sts->module~fsml_con module~fsml_sts->module~fsml_err module~fsml_sts->module~fsml_ini module~fsml_sts->module~fsml_utl module~fsml_utl->module~fsml_con module~fsml_utl->module~fsml_ini

Used by

  • module~~fsml_lin~~UsedByGraph module~fsml_lin fsml_lin module~fsml fsml module~fsml->module~fsml_lin module~fsml_nlp fsml_nlp module~fsml->module~fsml_nlp module~fsml_nlp->module~fsml_lin

Functions

public impure function f_lin_mahalanobis(x, y, cov) result(dist)

Impure wrapper function for f_lin_mahalanobis_core.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(:)

input vector 1

real(kind=wp), intent(in) :: y(:)

input vector 2

real(kind=wp), intent(in), optional :: cov(:,:)

covariance matrix

Return Value real(kind=wp)

Mahalanobis distance

public pure function f_lin_mahalanobis_core(x, y, cov) result(dist)

Compute Mahalanobis distance between vectors x and y using covariance matrix cov if provided; otherwise estimate covariance from the two-sample dataset formed by x and y. NOTE: check if cov matrix is positive definite.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(:)

input vector 1 (features)

real(kind=wp), intent(in) :: y(:)

input vector 2 (features)

real(kind=wp), intent(in), optional :: cov(:,:)

covariance matrix (m,m)

Return Value real(kind=wp)

Mahalanobis distance


Subroutines

public subroutine s_lin_eof(x, nd, nv, pc, eof, ew, opt, wt, r2, eof_scaled)

Empirical Orthogonal Function (EOF) analysis

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(nd,nv)

input data

integer(kind=i4), intent(in) :: nd

number of rows

integer(kind=i4), intent(in) :: nv

number of columns

real(kind=wp), intent(out) :: pc(nd,nv)

principal components

real(kind=wp), intent(out) :: eof(nv,nv)

EOFs/eigenvectors (unweighted)

real(kind=wp), intent(out) :: ew(nv)

eigenvalues

integer(kind=i4), intent(in), optional :: opt

0 = covariance, 1 = correlation

real(kind=wp), intent(in), optional :: wt(nv)

optional weights (default = 1.0/n)

real(kind=wp), intent(out), optional :: r2(nv)

explained variance (fraction)

real(kind=wp), intent(out), optional :: eof_scaled(nv,nv)

EOFs/eigenvectors scaled for plotting

public subroutine s_lin_lda_2c(x, nd, nv, nc, sa, g, score, mh)

2-class multivariate Linear Discriminant Analysis (LDA)

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(nd,nv,nc)

input data (nd samples × nv variables × nc classes)

integer(kind=i4), intent(in) :: nd

number of datapoints per class

integer(kind=i4), intent(in) :: nv

number of variables

integer(kind=i4), intent(in) :: nc

number of classes (must be 2)

real(kind=wp), intent(out) :: sa(nv)

standardised discriminant coefficients

real(kind=wp), intent(out) :: g

discriminant criterion

real(kind=wp), intent(out) :: score

classification score

real(kind=wp), intent(out), optional :: mh

Mahalanobis distance

public subroutine s_lin_ols(x, y, nd, nv, b0, b, r2, y_hat, se, cov_b)

Multiple Linear Ordinary Least Squares (OLS) Regression with intercept. NOTE: OLS could be wrapper for ridge (with lambda = 0 or presence checks if mande an optional argument). However, it would increase computation slightly and make code less readable. OLS is often used in teaching and therefore, an easily readable standalone is kept.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(nd,nv)

predictor data matrix (no intercept column)

real(kind=wp), intent(in) :: y(nd)

response vector

integer(kind=i4), intent(in) :: nd

number of datapoints

integer(kind=i4), intent(in) :: nv

number of predictors (excluding intercept)

real(kind=wp), intent(out) :: b0

intercept coefficient

real(kind=wp), intent(out) :: b(nv)

predictor coefficients

real(kind=wp), intent(out) :: r2

coefficient of determination R²

real(kind=wp), intent(out), optional :: y_hat(nd)

predicted y values

real(kind=wp), intent(out), optional :: se(nv)

standard errors of predictor coefficients

real(kind=wp), intent(out), optional :: cov_b(nv,nv)

covariance matrix of predictor coefficients

public subroutine s_lin_pca(x, nd, nv, pc, ev, ew, r2)

Principal Component Analysis (PCA). It is a special (simplified) case of EOF analysis offered as a separate procedure for clarity/familiarity. It calls s_lin_eof with equal weights.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(nd,nv)

input data

integer(kind=i4), intent(in) :: nd

number of rows

integer(kind=i4), intent(in) :: nv

number of columns

real(kind=wp), intent(out) :: pc(nd,nv)

principal components

real(kind=wp), intent(out) :: ev(nv,nv)

eigenvectors (unweighted)

real(kind=wp), intent(out) :: ew(nv)

eigenvalues

real(kind=wp), intent(out), optional :: r2(nv)

explained variance (fraction)

public subroutine s_lin_ridge(x, y, nd, nv, lambda, b0, b, r2, y_hat, se, cov_b)

Multiple Linear Ridge Regression (λ >= 0) with intercept.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(nd,nv)

predictor data matrix (no intercept column)

real(kind=wp), intent(in) :: y(nd)

response vector

integer(kind=i4), intent(in) :: nd

number of datapoints

integer(kind=i4), intent(in) :: nv

number of predictors (excluding intercept)

real(kind=wp), intent(in) :: lambda

ridge penalty parameter (≥ 0, non-optional)

real(kind=wp), intent(out) :: b0

intercept coefficient

real(kind=wp), intent(out) :: b(nv)

predictor coefficients

real(kind=wp), intent(out) :: r2

coefficient of determination R²

real(kind=wp), intent(out), optional :: y_hat(nd)

predicted y values

real(kind=wp), intent(out), optional :: se(nv)

standard errors of predictor coefficients

real(kind=wp), intent(out), optional :: cov_b(nv,nv)

covariance matrix of predictor coefficients