science: maths and stats dr andy evans. mathematics the classic texts for scientific computing are...

29
Science: Maths and Stats Dr Andy Evans

Upload: lance-varco

Post on 16-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Science: Maths and Stats

Dr Andy Evans

Page 2: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Mathematics

The classic texts for scientific computing are the Numerical Recipes books.

Java code available to buy:http://www.nr.com/aboutJava.html

Page 3: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Numerical Recipes

For java there is also Hang T. Lau (2003) A Numerical Library in Java for Scientists and Engineers

Colthttp://acs.lbl.gov/software/colt/JSciencehttp://www.jscience.org/JAMA http://math.nist.gov/javanumerics/jama/

Page 4: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 5: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Mathematics

StatisticsGraphs and NetworksText and Language

Page 6: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Statistics

R (GNU):http://www.r-project.org/

Developed as a free version on the stats language S, combined with a functional programming language.

Page 7: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 8: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Programming languagesThe 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?

Page 9: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 10: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 11: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

RGui

Page 12: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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() : list packageslibrary(packageName) : load package for uselibrary(help = packageName) : what’s in a packagedetach("package:packageName") : unload

Page 13: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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)

Page 14: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 15: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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")

Orsource("c:/scripts/commands.r")

The scripts are just text files of commands.

Page 16: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Quick tipsSimplest data structure is the vector of datax <- 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.

Page 17: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Other data structures

Matrices or more generally arraysFactors (handle categorical data).Vectors or lists (latter can be recursive)Data frames – tables of dataFunctions (store code)

Each data element is assigned a mode, or data type: logical, numeric, complex, character or raw.

Page 18: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 19: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Help

Best start is “Introduction to R”:http://cran.r-project.org/doc/manuals/R-intro.html

?solve : help for solve functionhelp.start() : start the HTML help??solve : search help for solve?help : info other help systems

Page 20: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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/

Page 21: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

Non-package addons

R-Forge:http://r-forge.r-project.org/

GUIs, bridges to other languages, etc.

Page 22: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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)

Page 23: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 24: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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).

Page 25: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 26: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 27: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 28: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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.

Page 29: Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to

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