## Archive for September, 2012

Pop quiz: What does the following line of MATLAB code do?

rand('state',10)

If you said* ‘It changes the seed of the random number generator to 10’* you get half a point.

*‘Only half a point!?’* I hear you say accusingly *‘but it says so in my book [for example, 1-3], why not a full point?’*

You only get a full point if you’d said something like *‘It changes the seed of the random number generator to 10 and it also changes the random number generator from the high quality, default Mersenne Twister generator to a lower quality legacy random number generator.‘*

OK, how about this one?

rand('seed',10)

This behaves in a very similar manner– it changes both the seed and the type of the underlying generator. However, the random number generator it switches to this time is an even older one that was introduced as far back as MATLAB version 4. It is not very good at all by modern standards!

**A closer look**

Open up a fresh copy of a recent version of MATLAB and ask it about the random number generator it’s using

>> RandStream.getGlobalStream ans = mt19937ar random stream (current global stream) Seed: 0 NormalTransform: Ziggurat

mt1993ar refers to a particular variant of the Mersenne Twister algorithm— an industry strength random number generator that’s used in many software packages and simulations. It’s been the default generator in MATLAB since 2007a. Change the seed using the modern (since 2011a), recommended syntax and ask again:

>> rng(10) >> RandStream.getGlobalStream ans = mt19937ar random stream (current global stream) Seed: 10 NormalTransform: Ziggurat

This is behaving exactly as you’d expect, you ask it to change the seed and it changes the seed…nothing more, nothing less. Now, let’s use the older syntax

>> rand('state',10) >> RandStream.getGlobalStream ans = legacy random stream (current global stream) RAND algorithm: V5 (Subtract-with-Borrow), RANDN algorithm: V5 (Ziggurat)

The random number generator has completely changed! We are no longer using the Mersenne Twister algorithm, we are now using a ‘subtract with borrow’ [see reference 4 for implementation details] generator which has been shown to have several undesirable issues [5-7].

Let’s do it again but this time using the even older ‘seed’ version:

>> rand('seed',10) >> RandStream.getGlobalStream ans = legacy random stream (current global stream) RAND algorithm: V4 (Congruential), RANDN algorithm: V5 (Ziggurat)

Now, this random number generator is ancient by computing standards. It also has a relatively tiny period of only 2 billion or so. For details see [4]

**Why this matters**

Now, all of this is well documented so you may wonder why I am making such a big issue out of it. Here are my reasons

- I often get sent MATLAB code for the purposes of code-review and optimisation. I see the old seeding syntax a LOT and the program’s authors are often blissfully unaware of the consequnces.
- The old syntax looks like all it should do is change the seed. It doesn’t! Before 2007a, however, it did!
- The old syntax is written in dozens of books because it was once the default, correct syntax to use.
- Many users don’t read the relevent section of the MATLAB documentation because they have no idea that there is a potential issue. They read a book or tutorial..it says to use rand(‘state’,10) so they do.
- MATLAB doesn’t use the old generators by default any more because they are not very good [4-7]!
- Using these old generators may adversely affect the quality of your simulation.

**The bottom line**

Don’t do either of these to change the seed of the default generator to 10:

rand('state',10) rand('seed',10)

Do this instead:

rng(10)

Only if you completely understand and accept the consequences of the older syntax should you use it.

**References**

1. ‘MATLAB – A practical introduction to programming and problem solving’, 2009,Stormy Attaway

2. MATLAB Guide (Second Edition), 2005, Desmond Higham and Nicholas Higham

3. Essential MATLAB for Engineers and Scientists (Fourth Edition), 2009, Hahn and Valentine

4. Numerical Computing with MATLAB, 2004, Cleve Moler (available online)

5. Why does the random number generator in MATLAB fail a particular test of randomness? The Mathworks, retreived 26th September 2012

6. A strong nonrandom pattern in Matlab default random number generator, 2006, Petr Savicky, retreived 26th September 2012

7. Learning Random Numbers: A Matlab Anomaly, 2008, Petr Savicky and Marko Robnik-Šikonja, Applied Artificial Intelligence, Vol22 issue 3, pp 254-265

**Other posts on random numbers in MATLAB**

- Parallel Random Numbers in MATLAB #1 – An introduction to random numbers and seeding in MATLAB
- Probability of overlapping subsequences (How likely is it to get overlapping random sequences using two different seeds for the Mersenne Twister algorithm)

One of my favourite investment news sites is The Motley Fool which frequently run articles such as 10 Shares Trading Near 52 week lows and 15 Shares Trading Near 52 week Highs. The idea behind such filtering is to seek out shares that have done particularly badly (or well) over the last year and then subject them to further analysis in order to find opportunities. Thanks to Mathematica’s FinancialData command, it is rather easy to generate these lists yourself whenever you like.

**15 Shares Trading Near 52 Week Highs**

The original article selected the 15 largest cap shares from the FTSE All Share Index that were trading within 3% of their 52 week high at the time of publication. Let’s see how to do that using Mathematica.

The following code returns the tickers of** all** shares from the FTSE All Share Index that are trading within 3% of their 52 week high.

percentage = 3; all52weekHighs = Select[FinancialData["^FTAS", "Members"], Abs[FinancialData[#, "FractionalChangeHigh52Week"]] < (percentage/100.) &];

The variable **all52weekHighs** contains a list of stock tickers (e.g. LLOY.L) that meet our criteria. The next thing to do is to find the market cap of each one:

all52WeekHighsWithCaps = Map[{#, FinancialData[#, "MarketCap"]} &, all52weekHighs];

This works fine for most shares. LloydsTSB for example returns {“LLOY.L”, 2.7746*10^10} at the time of writing but the MarketCap query fails for some tickers. For example, the Market Cap for HSL.L is not available and we get {“HSL.L”, Missing[“NotAvailable”]}. Let’s discard these by insisting that we only consider stocks that have a numeric market cap.

Goodall52WeekHighsWithCaps = Select[all52WeekHighsWithCaps, NumberQ[#[[2]]] &];

We sort the list according to MarketCap:

sorted = Sort[Goodall52WeekHighsWithCaps, #1[[2]] > #2[[2]] &];

Let’s prettify the list a little by iterating over all tickers and replacing the ticker with the associated stock name. Also, let’s divide the market cap by 1 million to make it more readable

finallist = Map[{FinancialData[#[[1]], "Name"], #[[2]]/1000000} &, sorted];

Now, you may be wondering why I haven’t been showing you the output of these commands. This is simply because even this final list is rather large at 118 entries at the time of writing

Length[finallist] 118

The original article only considered the top 15 sorted by Market Cap so let’s show those. Market Caps are given in millions.

top15 = finallist[[1;;15]]//Grid HSBC Holdings PLC 94159. National Grid 24432. Prudential PLC 21775. Centrica PLC 17193. Rolls Royce Group 16363. WPP Plc 10743. Experian PLC 10197. Old Mutual PLC 8400. Legal & General Group PLC 8036. Wolseley PLC 7955. Standard Life 6662. J Sainsbury plc 6401. Aggreko PLC 6391. Land Securities Group PLC 6180. British Land Co PLC 4859.

and we are done.

Welcome to the 90th edition of the Carnival of Mathematics and the first one hosted by me since I handed over the administrative reigns to the good people of aperiodical. The CoM is a great way to read about and promote mathematical blogging and has been running for over 5 years. Hosted on a different blog each month, it covers the entire mathematical spectrum from simple mucking around with numbers right up to cutting edge research.

Writers can submit their own posts for inclusion in a carnival if they like and anyone can submit any mathy post that they’ve found interesting– ideally, something written over the last month or so to keep it fresh.

If you want to keep up with the CoM, head over to its twitter feed or the dedicated page at aperiodical.

**Trivia**

Carnival tradition dictates that I post some trivia about this month’s edition number. Here’s what I came up with for 90:

- 90 is the only number that is the sum of its digits plus the squares of its digits (via NumberGossip)
- 90 is a Harshad number in base 10. It is also a Perrin Number, a pronic number, a unitary perfect number and a semi perfect number.
- 90 is the smallest number that has six representations as a sum of four positive squares (via Wolfram Alpha)

**Neat Stuff**

- Matthew Handy explains how researchers at MIT used geometric series to solve an economic problem in Zeno’s Facebook Page.
- Carnival regular, Pat Ballew tells us about a nice connection between numeric frieze patterns and triangulating polygons.
- Ethan Brown, a thirteen-year-old “Mathemagician” and the author of the blog coolmathstuff.com brings us Probability, The Number e, and Magic all in one

**Computation**

- The Numerical Algorithms Group (NAG) discusses Linear Quantile Regression, a new feature in the latest version of their software.
- Samir Kahn has been Modelling Flow Intertia in Three Couple Liquid Tanks using Maple.
- Cleve Moler, inventor of MATLAB, has written two posts on Conway’s Game of Life (part 1, part 2)

**Puzzles and Games
**

- Shecky R brings us Mind Wrenching – A self-referential logic puzzle that will give your brain cells a workout.
- Brent Yorgey has been visualizing winning strategies for “nim-like” games and says
*‘This is a post about visualizing winning strategies for certain games where players take turns removing counters from two piles. The games make for fun games to actually play, and analyzing them can get quite interesting!’*

**Funnies**

- The xkcd comic shows how pointless the phrase ‘We are the fastest growing [whatever]’ is
- Mark Dominus offers a koan on the Consistency of the Peano Axioms.

**Art and Mathematics
**

- Egan Chernoff sent in Alternative Base Representation saying
*‘I look forward to your critiques of my latest piece’* - Gianluigi Filippelli gives us Poincaré, Einstein and Picasso: children of time and says
*‘Following an article by Arthur I. Miller on the Guardian, I try to tell the possible connections between cubism, mathematics and relativity.’* - Gianluigi continues his cultural theme with a look at some mathematical poems recently published in Nature in The day of mathematical poetry.

**Books**

- Shiva Kintali has reviewed a couple of books – Matching Theory by Laszlo Lovasz and Elements of Automata Theory by Jacques Sakarovitch.

**Tricks and Tactics**

- John D Cook gives us Binomial Coefficient Trick

**Topology**

- Mark Dominus of The Universe of Discourse gives us three posts this month: A two parter on topology and set theory (Click here for part 1 and here for part 2).

**Teaching**

- Dan McQuillan gives us On Trigonometric Nostalgia and says
*‘This is a post about fostering a problem-solving mentality in a world where we do not even understand how our own tools work. It superimposes our nostalgia for the world we used to know with our innate curiosity, which still exists. Basic trigonometry is still fun and still relevant. Indeed, one can always ask questions and calculate!’* - Frederick Koh takes on the dot product in Understanding MATTERS (7) saying ‘T
*his dot product concept involving parallel vector planes is rather fundamental, yet a handful of my students are unable to figure out how things exactly work. Hence I have decided to pen this detailed explanation in the hope that it will benefit not just my charges, but other math learners as well.’* - Augustus Van Dusen has written the first in an upcoming series of posts that will prove properties of logarthmic and exponential functions. Augustus says
*‘This particular post will focus on the properties of logarithmic functions of real variables. Students in advanced placement calculus in high school and beginning college students who are not math majors are the intended audience.’*

**Comment**

- Peter Rowlett asks does mathematics have a culture of historical inaccuracy?
- Mike Thayer submitted his article Tired of playing defense saying
*‘These are my thoughts on the Andrew Hacker NY Times article from July, and the Roger Schank Washington Post article from earlier this week.It is my defense of teaching algebra…’* - Egan Chernoff submits Bill, you had me at “arithmetic” and asks
*‘how do other people feel about the phrase “let’s do the math.” ‘*

**Not the only game in town**

The Carnival of Mathematics isn’t the only mathematical blog carnival that’s doing the tour. There’s also the fantastic monthly Math Teachers at Play.

**End**

That’s it for the 90th Edition. Past editions written by me include 80, 76, 74 and 73 among others. For future editions keep an eye on @carnivalofmath and aperiodical.com

I’m hosting the 90th Carnival of Mathematics very soon. If you have written (or read) a mathematics blog article over the last month and want to give it more attention, why not make a submission? The deadline for submissions is 10th September.

Welcome to the August edition of A Month of Math Software– a regular series of articles where I share what’s shiny and new in the world of mathematical software. If you like what you see and want more, take a browse through the archives. If you have some news that you think should go into next month’s edition, contact me to tell me all about it so I can tell the world.

This edition includes lots of new releases, blog posts and news about mathematics on mobile devices…enjoy!

**Mobile Mathematics**

August was a very big month for mobile mathematical applications with the following releases

- Full LaTeX on iPad with on device compilation and .dvi viewer
- Maxima on Android – Maxima is a free computer algebra system with a long pedigree
- Octave on Android – Octave is basically a free version of MATLAB. The author of this package is Corbin Champion who also wrote Addi (a simplified MATLAB clone based on JMathLib). Corbin tried to get funding via kickstarter (see my article on this – here) to allow him to dedicate himself full time to this port but, sadly, was unsuccessful. Thankfully, Corbin has managed to do some development on the project anyway and has released this package as a starter for 10. Its lacking a lot of stuff but is a fantastic start!
- The Geogebra team have started a kickstarter project that aims to bring Geigebra to iPad. Why not head over there and pledge some support?

**General purpose mathematics**

- Maxima, a free computer algebra system (CAS) for Windows, Linux and Mac OSX, has been updated to version 5.28. Back in 2010, a guest writer wrote a Maxima tutorial here at WalkingRandomly – Maxima Tutorial – plotting direction fields for 1st order ODEs
- Mathics is a free, lightweight alternative to Mathematica and has recently been updated to version 0.5.
- Pari/GP is another free computer algebra system that was updated to 2.5.2 in August (The website says august but the changelog says June I’ve only just noticed the update so its going in August’s edition) — Pari focuses on number theory but can be used for many other kinds of computation.
- The free Euler Math Toolbox has been updated many times throughout August and is now at version 18.4. See the Changelog for what’s new.

**Do numerical computing using….Javascript!**

- The Numeric Javascript library has been updated to version 1.2.2. The main new feature is linear programming– the function is numeric.solveLP()

**Mathematical software libraries**

- The AMD Core Math Libray (ACML) has been updated to version 5.2.0.
- Version 2.4.6 of PLASMA (Parallel Linear Algebra for Scalable Multi-core Architectures) has been released. See what’s new at http://icl.cs.utk.edu/plasma/news/news.html?id=299
- A new minor version of ARPACK-NG (3.1.2) has been released. See http://forge.scilab.org/index.php/p/arpack-ng/source/tree/master/CHANGES for the newness. ARPACK is a collection of Fortran77 subroutines designed to solve large scale eigenvalue problems

**GPU Programming **

GPU stands for Graphical Processing Unit but these days you can get a GPU to do a lot more than just graphics. You could think of them as essentially massively parallel math co-processors that can make light work of certain operations.

- Jacket is a commercial GPU Processing add-on for MATLAB. In recent blog posts, the Jacket developers discuss SAR Image Formation Algorithms on the GPU and Option Pricing.
- CULA is a set of commercial GPU-accelerated linear algebra libraries. CULA-Dense is, as you might expect, for dense matrices and is now at version 15. CULA-Sparse is at version S3. I can’t find a what’s new document but the main change seems to be the addition of support for NVIDIA’s Kepler architecture. The CULA library can be called from C, C++, Fortran, MATLAB, and Python and is
**free for individual academic use**. - GPULib is a commercial software library enabling GPU-accelerated calculations for IDL. In a recent blog post, one of GPULib’s developers has been experimenting with OpenCL support.

**Statistics**

- R Commander, a basic GUI for the free R programming language, has been updated to version 1.9.x
- IBM’s SPSS has been updated to version 21. Some new features are discussed at http://www-01.ibm.com/software/analytics/spss/products/statistics/features.html
- VSN International have released version 15.1 of their bio-statistics package, Genstat. The list of new stuff is at http://www.vsni.co.uk/software/genstat/15th-edition-new-features

**Academic codes and applications**

- Version 3.0 of the SCIP Optimization Suite has been released. According to the website, ‘SCIP is currently one of the fastest non-commercial
*mixed integer programming (MIP) solvers*. It is also a framework for*constraint integer programming*and*branch-cut-and-price’.*Here are the all important Release Notes and Changelog. - Templates for First-Order Conic Solvers (TFOCS, pronounced
*tee-fox*) is a software package that provides a set of templates, or building blocks, that can be used to construct efficient, customized solvers for a variety of models. The latest version, 1.1a, was released back in February but I have only recently learned of it and so am including it in this month’s edition. A set of demos and wiki for this software is available. - Version 1.0 of Blaze has been released.
**Blaze**is an open-source, high-performance C++ math library for dense and sparse arithmetic. There is a getting started tutorial and a set of benchmarks.