Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Science: Maths and Stats Dr Andy Evans. Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to."— Presentation transcript:

1 Science: Maths and Stats Dr Andy Evans

2 Mathematics The classic texts for scientific computing are the Numerical Recipes books. Java code available to buy:

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

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

5 Mathematics Statistics Graphs and Networks Text and Language

6 Statistics R (GNU): Developed as a free version on the stats language S, combined with a functional programming language.

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

8 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?

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

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

11 RGui

12 Packages Lots come with it. Comprehensive R Archive Network (CRAN): Packages → Set CRAN Mirror… Packages → Install package(s)… library() : list packages library(packageName) : load package for use library(help = packageName) : what’s in a package detach("package:packageName") : unload

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

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

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

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

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

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

19 Help Best start is “Introduction to R”: ?solve : help for solve function help.start() : start the HTML help ??solve : search help for solve ?help : info other help systems

20 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) :

21 Non-package addons R-Forge: GUIs, bridges to other languages, etc.

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

23 Programming with R Various options, but best is rJava: Two parts: rJava itself : lets R use Java objects. JRI (Java/R Interface) : lets Java use R.

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

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

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

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

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

29 Floating point numbers David Goldberg (1991), “What Every Computer Scientist Should Know About Floating-Point Arithmetic”, ACM Computing Surveys, 23/1, 5–48 Hacker's Delight by Henry S. Warren Jr Randall Hyde’s “Write Great Code” series. users/vuik/wi211/disasters.html


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

Similar presentations


Ads by Google