HelloNMDev

Hello NM Dev

This tutorial walks you through a simple first example on using the NM Dev library. You will need to have a NM Dev project to follow the examples here. For details on how to create or download a NM Dev project, please read the Setup Guide.

We will create some matrices and vectors and do some simple linear algebra. Java is an object-oriented language. So, other than the primitive types, like int and double, all variables are objects, namely, instances of some object definitions called classes. For example, to have a matrix instance, a matrix variable, or simply a matrix, we create an instance of the Matrix class.

Java matrix
Matrix A1 = new DenseMatrix(
  new double[][]{ // create a matrix
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
});

We can print out the Java matrix to check.

System.out.println(A1);

In NetBeans the short cut for System.out.println is to type “sout” (without the “”) and then hit the “Tab” key.

To fix the imports, hit “Ctrl+Shift+I”. There are multiple choices. Choose com.numericalmethod.suanshu.matrix.doubles.Matrix.

fix_all_imports_matrix

Hit “F6” to run.

Vola! You will see this output.

[,1] [,2] [,3]
[1,] 1.000000, 2.000000, 3.000000,
[2,] 4.000000, 5.000000, 6.000000,
[3,] 7.000000, 8.000000, 9.000000,

Congratulations! You have just successfully created your first matrix using the NM Dev library. We are now ready to do more.

Let’s check whether this matrix has a full rank. We do

int rank1 = MatrixMeasure.rank(A1); // compute A1's rank
System.out.println(rank1); // rank1 is 2 implies a deficient matrix

The MatrixMeasure.rank method computes the numerical rank. For more information, see Compute the numerical rank of a matrix.

Hit “F6” to run.

We see that the rank of A1 is 2 so A1 is a rank deficient matrix. We can make it a full rank matrix by changing one entry.

A1.set(3, 3, 10); // change A1[3, 3] to 10

Note that NM Dev counts from 1!

Let’s try again.

rank1 = MatrixMeasure.rank(A1); // compute modified A1's rank
System.out.println(String.format("rank for the modified A1 = %d", rank1));

OK. A1 is now a full rank matrix. We can now find its inverse.

Matrix A2 = new Inverse(A1); // compute A1 inverse
System.out.println(A2);

Hit “Ctrl+Shift+I” to fix the imports. Hit “F6” to run. Here is the output.

3x3

[,1] [,2] [,3]
[1,]  -0.666667, -1.333333, 1.000000,
[2,]  -0.666667, 3.666667, -2.000000,
[3,]  1.000000, -2.000000, 1.000000,

It looks good. To make sure, we multiply them together.

Matrix A3 = A1.multiply(A2); // A3 = A1 %*% A2
/** In numerical computing, we are usually satisfied to compare equality up to a precision.*/
final double precision = 1e-14; // A3 and the 3x3 identity matrix are equal up to 14 decimal points
if (AreMatrices.equal(A3, new DenseMatrix(3, 3).ONE(), precision)) { // compare A3 to the 3x3 identity matrix
System.out.println("A2 is indeed A1's inverse");

Hit “Ctrl+Shift+I” to fix the imports. Hit “F6” to run.

OK. It works.

Let’s try a vector. We do

Vector y = new DenseVector(new double[]{ // create a vector
20, 10, 4
});

Hit “Ctrl+Shift+I” to fix the imports. There are multiple choices. Choose “com.numericalmethod.suanshu.vector.doubles.Vector”.

fix_all_imports_vector2

We solve a linear equation of this form.

Ax = y

We need to set up a linear equation solver.

LinearSystemSolver solver = new LinearSystemSolver(A1); // set up the linear equation solver
Vector x = solver.solve(y); // solve A1 %*% x = y
System.out.println(x);

Hit “Ctrl+Shift+I” to fix the imports. There are multiple choices. Choose com.numericalmethod.suanshu.matrix.doubles.linearsystem.LinearSystemSolver.

x is a vector

[-22.666667, 15.333333, 4.000000]

The source code can be browsed in our svn repository here.

Tutorials

The NM Dev Tutorials

The NM Dev tutorials are practical guides for those who would like to create mathematics applications using the NM Dev library. They include lessons with many complete and working examples. Groups of related lessons are organized into “trails”.

For the most up-to-date information on using the NM Dev library, please consult the Javadoc. Please check out the NM Forum, the place to discuss the NM Dev library, mathematics and programming. You are also encouraged to post your questions there to get help beyond documents.

Before you start these lessons, please make sure you have set up the development environment properly. You can find the details in the Setup Guide.

Basic Trails

Specialized Trails

 

Time Series Analysis

  • various moving average filters
  • univariate/multivariate time series analysis
    • ARIMA modeling, simulation, fitting, prediction
    • GARCH modeling, simulation, fitting, prediction
    • Auto-Covariance computation

Linear Regression

  • OLS regression
  • logistic regression
  • GLM regression
    • family: Binomial, Gamma, Gaussian, Inverse Gaussian, Poisson
    • quasi-family: Binomial, Gamma, Gaussian, Inverse Gaussian, Poisson
  • GLM model selection

Hypothesis Testing

  • Kolmogorov-Smirnov
  • D’Agostino
  • Jarque-Bera
  • Lilliefors
  • Shapiro-Wilk
  • One-way ANOVA
  • T
  • Kruskal-Wallis
  • Siegel-Tukey
  • Van der Waerden
  • Wilcoxon rank sum
  • Wilcoxon signed rank
  • Breusch-Pagan
  • Glejser
  • Harvey-Godfrey
  • White
  • ADF
  • Bartlett
  • Brown-Forsythe
  • F
  • Levene

STATS

This is a collection of advanced statistics algorithms.

  • cointegration
  • covariance selection
  • factor analysis
  • hidden Markov model (Rabiner)
  • hidden Markov model (mixture models: Beta, Binomial, Exponential, Gamma, Log-Normal, Normal, Poisson)
  • hypothesis testing
  • Kalman filter
  • LASSO (Least Absolute Shrinkage and Selection Operator)
  • linear regression
  • principal component analysis
  • stochastic process simulation
  • time series analysis

OPTIM

This is a collection of the advanced optimization algorithms.

  • linear programming (LP)
  • quadratic programming (QP)
  • second order conic programming (SOCP)
  • semi-definite programming (SDP)
  • sequential quadratic programming (SQP)
  • mixed linear integer programming (MIP)
  • genetic algorithms (GA)
  • differential evolution optimization (DE)

OPDE

This is a collection of numerical algorithms to solve ordinary and partial differential equation problems.

  • Ordinary Differential Equation (ODE) solvers for initial value problem (IVP):
    • Euler’s method
    • Runge Kutta
      • 1st order Runge Kutta
      • 2nd order Runge Kutta
      • 3rd order Runge Kutta
      • 4th order Runge Kutta
      • 5th order Runge Kutta
      • 6th order Runge Kutta
      • 7th order Runge Kutta
      • 8th order Runge Kutta
      • 10th order Runge Kutta
    • Runge-Kutta-Fehlberg (RKF45) (adaptive step-size control)
    • Adams-Bashforth-Moulton predictor-corrector multi-step method
      • 1st order
      • 2nd order
      • 3rd order
      • 4th order
      • 5th order
    • solvers based on Richardson extrapolation
      • Burlisch-Stoer extrapolation
      • semi-implicit extrapolation (suitable for stiff systems)
    • first order system of ODEs
    • conversion from high order ODE to first order ODE system
  • Partial Differential Equation (PDE) solvers
    • finite difference methods:
      • elliptic problem:
        • iterative central difference method (for Poisson’s equation)
      • 1D hyperbolic problem:
        • explicit central difference method (for 1D wave equation)
      • 2D hyperbolic problem:
        • explicit central difference method (for 2D wave equation)
      • 1D parabolic problem:
        • Crank-Nicolson method (for 1D heat or diffusion equation)
      • 2D parabolic problem:
        • alternating direction implicit (ADI) method (for 2D heat or diffusion equation)

Packages

NM Dev is a large collection of numerical algorithms covering many topics in mathematics. The following is a summary of our packages. For a complete listing, please refer to the Javadoc.

  • BASIC: basic numerical analysis algorithms
  • OPTIM: advanced optimization algorithms
  • STATS: advanced statistics algorithms
  • O/PDE: ordinary and partial differential equations
  • DSP: digital signal processing
  • EVT: extreme value theory
  • many more……

All our code is well tested. We have thousands of test cases that run daily.

Disclaimer: For each subject/topic heading above, there may or may not be features you expected. You will have to download the trial version to experiment with the library to decide whether it is suitable for your purpose, or whether the features you are looking for are included in the library or a particular version of the library. For example, we have listed “OLS regression” in the feature list, but not all possible mathematics, subjects, or topics related to OLS regression are found in the NM Dev library.

 

EVT

This is a collection of algorithms for Extreme Value Theory.

  • univariate distributions:
    • Gumbel
    • Frechet
    • Weibull
    • Generalized EV (GEVD)
    • Generalized Pareto (GPD)
    • maxima
    • minima
    • order statistics
  • bivariate distributions:
    • asymmetric logic
    • asymmetric mixed
    • asymmetric negative logistic
    • bilogistic
    • Coles-Tawn
    • Husler-Reiss
    • logistic
    • negative bilogistic
    • negative logistic
  • return level and return period
  • extremal index estimation
    • Ferro-Seegers algorithm
  • cluster analysis
  • extreme value markov chain
  • extreme value time series, i.e., MARMA(p,q) model
  • fitting GEVD by maximum likelihood estimation
  • fitting GPD by Peaks-over-Threshold (POT) method
  • fitting Gumbel type by Average Conditional Exceedance Rate (ACER) method