Using Java inside Matlab
Matlab is pretty good, but it doesn't do everything, and you might not always have the toolbox that contains the function you want. There might even be an excellent open-source software library that can't be included in Matlab, but can be used within Matlab.
Matlab has excellent support for using Java with minimal effort. A repository of numerical libraries can be found at http://math.nist.gov/javanumerics/. In this example we install the commons-math Java library, and show an example of using it.
Here are a few things the commons libary can do:
Computing means, variances and other summary statistics for a list of numbers Fitting a line to a set of data points using linear regression Finding a smooth curve that passes through a collection of points (interpolation) Fitting a parametric model to a set of measurements using least-squares methods Solving equations involving real-valued functions (i.e. root-finding) Solving systems of linear equations Solving Ordinary Differential Equations Minimizing multi-dimensional functions Generating random numbers with more restrictions (e.g distribution, range) than what is possible using the JDK Generating random samples and/or datasets that are "like" the data in an input file Performing statistical significance tests Miscellaneous mathematical functions such as factorials, binomial coefficients and "special functions" (e.g. gamma, beta functions)
Many of them duplicate features in Matlab, but they may implement different algorithms, or more convenient syntax.
clear all; clc; close all
These two commands only need to be run once, so comment them out after you run them.
We have to add the jar file to the javaclasspath so we can use it.
Note that the regress command is part of the Statistics toolbox, which is great if you have it! We show here how to do it, so we also have something to compare the Java result to.
x = [1 2 3 4 5]'; y = [3 5 7 14 11]'; [b bint]= regress(y,[x.^0 x])
b = 0.5000 2.5000 bint = -7.5615 8.5615 0.0694 4.9306
The first thing we do is import the Java library. the * at the end indicates that all classes should be imported.
The SimpleRegression class has several methods that we can call later to perform analysis
regression = SimpleRegression(); methods(regression)
Methods for class org.apache.commons.math.stat.regression.SimpleRegression: SimpleRegression getSlope addData getSlopeConfidenceInterval clear getSlopeStdErr equals getSumSquaredErrors getClass getTotalSumSquares getIntercept hashCode getInterceptStdErr notify getMeanSquareError notifyAll getN predict getR toString getRSquare wait getRegressionSumSquares getSignificance
m = regression.getSlope() mstd = regression.getSlopeStdErr() mint = regression.getSlopeConfidenceInterval(); % 95% by default [m - mint m + mint] % this is the 95% interval
m = 2.5000 mstd = 0.7638 ans = 0.0694 4.9306
Note the confidence interval is greater than +- 2*sigma. This is because of the student-t table multiplier on the standard error due to the small number
note there is not getInterceptConfidenceInterval method for some reason!
b = regression.getIntercept() bstd = regression.getInterceptStdErr() sprintf('R^2 = %1.3f', regression.getRSquare())
b = 0.5000 bstd = 2.5331 ans = R^2 = 0.781
It takes more lines of code to get the Java version of linear regression, but you have finer control over what data you get, and what order you get it in. You still have to read the Java documentation to learn the command syntax, and debugging can be somewhat more difficult than straight Matlab. However, the integration of Java into Matlab opens some interesting possibilities to extend Matlab's functionality.
% categories: Miscellaneous, data analysis