Maths and Stats
Download
Report
Transcript Maths and Stats
Science: Maths and Stats
Dr Andy Evans
Mathematics
The classic texts for scientific computing are the Numerical
Recipes books.
Java code available to buy:
http://www.nr.com/aboutJava.html
Numerical Recipes
For java there is also
Hang T. Lau (2003) A Numerical Library in Java for Scientists
and Engineers
Colt
http://acs.lbl.gov/software/colt/
JScience
http://www.jscience.org/
JAMA
http://math.nist.gov/javanumerics/jama/
Maple
Commercial mathematics application (commercial licence
$2,845).
Does, for example, algebraic manipulation, calculus, etc.
Outputs processes as C, C#, Java, Fortran, Visual Basic, and
MATLAB code.
C and Java APIs for program connection.
Mathematics
Statistics
Graphs and Networks
Text and Language
Statistics
R (GNU):
http://www.r-project.org/
Developed as a free version on the stats language S,
combined with a functional programming language.
Programming languages
We’ve dealt with Imperative Programming languages:
commands about what to do to change the state of the program
(i.e. its collected variables).
These are usually also Procedural, in that the program is divided
into procedures to change states.
Most Procedural languages are now Object Orientated.
Programming languages
The other branch of languages allow Declarative Programming:
concentrates on describing what a program should do, not how, and
avoiding state changes.
Clearest examples are Functional Programming: everything is
described as a reference to another function:
a = x + 10;
x = y + 2;
Run program for the argument y = 12
Also Logical Programming: same kind of thing but based on finding
logical proofs/derivations.
Things that fall into the category mortal includes humans.
Socrates is human.
Run program to find if Socrates is mortal?
Declarative languages
Examples: Lisp; Prolog; (bits of SQL)
Beloved of academics, but weren’t used much in the real world,
until recently (except SQL).
Advantage is that they avoid unlimited internal and external
state changes, therefore much easier to check and predict.
Prolog useful for language processing.
A version of Lisp, Scheme, inspired elements of R.
R
Language and a series of packages.
Written in C/C++/Fortran but Java can be used.
Functional language but with procedural and OOP elements.
Uses scalars, matrices, vectors, and lists.
Can replace the GUI with a variety of alternatives.
Powerful and increasingly stats software of choice, but steep
learning curve and massive range of add-on packages.
RGui
Packages
Lots come with it.
Comprehensive R Archive Network (CRAN):
http://cran.r-project.org/web/packages/available_packages_by_name.html
Packages → Set CRAN Mirror…
Packages → Install package(s)…
library()
library(packageName)
library(help = packageName)
detach("package:packageName")
: list packages
: load package for use
: what’s in a package
: unload
Example
data1 <- read.csv(“m:\\r-projects\\data.tab", header = TRUE)
attach(data1)
plot(Age, Desperation, main="Age vs. Desperation")
lineeq <- lm(Desperation ~ Age, data=data1)
x <- seq(min(Age), max(Age), by=10.0)
newData <- data.frame(Age = x)
predictions <- predict(lineeq, newdata = newData)
lines(Age, predictions)
detach(data1)
rm (data1, lineeq, newData, predictions, x)
Working with R
R uses ‘Workspace’ directories.
Good practice to work in a new directory for each project
(File → Change Dir…)
Dataset names etc. must have a letter before any numbers.
R constructs data objects, that can be seen with objects()
and removed with rm(objectName).
If you save the workspace, it saves these objects in an .RData
file.
Working with R
Commands can be separated by new lines or enclosed thus:
{command;command;command}
If you fail to close a command, you’ll see “+”.
You can load scripts of commands. Note that on Windows you
just have to be careful to adjust all filepaths, thus:
source("c:\\scripts\\commands.r")
Or
source("c:/scripts/commands.r")
The scripts are just text files of commands.
Quick tips
Simplest data structure is the vector of data
x <- c(10.4, 5.6, 3.1, 6.4, 21.7)
attach() makes data available by column name (cp.
detach(name)).
Vector elements can be searched and selected using indices or
expressions in [], e.g.:
y <- x[!is.na(x)] where na is “Not available”
In operations using 2 vectors, the shortest gets cycled through as
much as is needed.
Other data structures
Matrices or more generally arrays
Factors (handle categorical data).
Vectors or lists (latter can be recursive)
Data frames – tables of data
Functions (store code)
Each data element is assigned a mode, or data type: logical,
numeric, complex, character or raw.
Quick tips
$ can be used to look inside objects, e.g. myData$column1
Operators: +, -, *, / and ^ (i.e. raise to the power)
Functions include: log, exp, sin, cos, tan, sqrt, max, min, length,
sum, mean, var (variance), sort.
Help
Best start is “Introduction to R”:
http://cran.r-project.org/doc/manuals/R-intro.html
?solve
help.start()
??solve
?help
: help for solve function
: start the HTML help
: search help for solve
: info other help systems
R-Spatial
Large number of packages dealing with spatial analysis,
including:
Mapping (incl. GoogleMap/Chart, and KML production)
Point pattern and cluster analysis.
Geographically Weighted Regression.
Network mathematics.
Kriging and other interpolation.
Excellent starting point is James Cheshire’s (CASA) :
http://spatialanalysis.co.uk/r/
Non-package addons
R-Forge:
http://r-forge.r-project.org/
GUIs, bridges to other languages, etc.
Programming R
Has its own flow control:
if ( condition ) {
statement 1
} else {
statement 2
}
for (i in 1:3) print(i)
Note that this is actually a “for-each” loop - “:” just
generates a list of numbers, so you can also do this:
x <- c("Hello","World")
for (i in x) print(i)
Programming with R
Various options, but best is rJava:
http://cran.r-project.org/web/packages/rJava/index.html
Two parts:
rJava itself : lets R use Java objects.
JRI (Java/R Interface) : lets Java use R.
JRI
Start by setting up an Rengine object.
Can run it with or without an R prompt GUI.
Send in standard R commands using Rengine’s eval(String)
method.
Can also assign () various values to a symbol
re.assign(“x”, “10.0,20.0,30.0”);
Methods for dealing with GUI elements (see also the iPlot and
JavaGD packages).
Getting data back
Two mechanisms:
Push:
Get back an object containing the information R would
have output to the console (and a bit more).
Callback:
Java provides methods which R calls when different tasks
done.
Push
Get back a REXP object:
Contains R output and other information.
rexp.toString() : shows content.
Can filter out information with:
rexp.asDoubleArray()
rexp.asStringArray()
etc.
Callback
Add an object to handle events:
rengine.addMainLoopCallbacks(RMainLoopCallbacks)
Largely set up to manage user interface interaction.
RMainLoopCallbacks contains methods called at key moments,
for example:
rReadConsole()
Called while R is waiting for user input.
Floating point numbers
Be aware that floating point numbers are rounded.
For example, in R, floating point numbers are rounded to
(typically) 53 binary digits accuracy.
This means numbers may differ depending on the algorithm
sequence used to generate them.
There is no guarantee that even simple floating point numbers
will be accurate at large decimal places, even if they don’t
appear to use them.
Floating point numbers
David Goldberg (1991), “What Every Computer Scientist Should
Know About Floating-Point Arithmetic”, ACM Computing
Surveys, 23/1, 5–48
http://www.validlab.com/goldberg/paper.pdf
http://floating-point-gui.de/
Hacker's Delight by Henry S. Warren Jr
Randall Hyde’s “Write Great Code” series.
http://ta.twi.tudelft.nl/
users/vuik/wi211/disasters.html