Archive for May, 2014

Math on the web 1

May 23, 2014 Leave a comment

The idea just came out of the blue. I like mathematics and I am using twitter. I am amazed that there are so many mathematicians using twitter. I am soo fortunate that there are so many mathematics articles around the web that are very useful, interesting and opening our minds. So, why not save and keep those interesting articles (according to me) .  These articles are mathematics articles that I read in the past few months.

1. @MrHonner: Evolutionary game theory in the classroom by @drvinceknight #mathchat #math

@MrHonner is a twitter account owned by a mathematician Patrick Honner. I followed him and at some stage his tweet opened my mind to learn something new in mathematical teaching

2. @MrHonner: “It’s not how big your class is, it’s what you do with it” Interesting take on class size #edchat

Again, this was a tweet from @MrHonner that brought me an article about the class size vs the quality of teaching. Since we are all teacher, I am sure that this article will be of interest.

3. @ISACalculus: Daylight Savings is mathematically illogical. Dilation not translation. Thanks @MrHonner for clearing this up.

This is a light article yet interesting about Daylight Saving. I am now living in Indonesia, a very nice tropical region which does not have Daylight Saving. I once lived in Australia where Daylight Saving occurs every November – March (the other way around from Europe). I find it Daylight Saving is very interesting and I like this article very much.

4. @standupmaths: You need some maths inequalities? This guy has ≥ you’ll ever need:

Very useful article for mathematics students who wants to compete in mathematics olympiad. But let me give a credit to @standupmaths first. This account belongs to Matt Parker who is (of course) a mathematician. And he is also a stand-up comedian. If I get the chance and the universe allows me, I would invite him to my university. Anyway, the title of the article explains a lot. It would give us a summary of inequalities you have ever (or never) heard. So, grab it before the link is dead.

5. I am so sorry that I do not put who is the one tweeted this link cause I have forgotten. However, the link will bring you to an article about fractal. It is a very nice introduction to fractal and you’ll love it the way the author presented the article. Not only did he provide figures and illustrations but also he provided animations and simulations on how to make a fractal shape. 

6. Again I have forgotten who was the one who tweeted/retweeted this link. This is my last article about mathematics on the web. I think it is quite pretty to end this post with this article cause this article is about how math can give you the best job (in terms of salary). The article said in the beginning: “Another day, another reason to get better at math,” which for me, is true.

As a conclusion, I’ll let you read the articles. I am pretty sure I am going to write math on the web 2 next month or after that.

Categories: Teachings

Random Numbers in Matlab

May 23, 2014 3 comments

In the past two years, I have been supervising bachelor degree students for their final projects. Unfortunately, most of the projects were related to time series, forecasting, stochastics process, financial mathematics and many topics related to application to statistics in finance. To be honest, this is not my strong suit. As a result, we struggled when reading and using many statistical techniques especially the one in time series. With this notes, I hope to understand and to keep remember those techniques. Moreover, as I am teaching Time Series Analysis this semester, I believe this post will be very useful for my students.

In this post, I concentrate on how to generate random numbers with certain distributions in Matlab and its application in my time series lecture. I know that we can easily google on how to do these, but as always, it would be very nice to have this ready in my blog. When I forget to do this, I don’t have to google it again and save myself a few minutes.

Generating random numbers and introduction to subplot, histogram, state commands in matlab

To generate random numbers in Matlab with certain distributions, I  need to type

x1 = normrnd(mean,std,[m,n]); %normal distribution
x2 = binornd(N,p,[m,n]); %binomial distribution
x3 = exprnd(lambda,[m,n]); %exponential distribution
x4 = gamrnd(a,b,[m,n]); %Gamma distribution
x5 = rand(m,n); %uniform distribution
x6 = poissrnd(lambda,[m,n]); %Poisson distribution

The commands above returns an m x n matrix containing pseudorandom values drawn from normal, binomial, exponential, gamma, uniform and Poisson distributions respectively. Some usefull commands related to random number generator in Matlab are

subplot(2,2,1) %top left figure
hist(x1) %histogram of normal random numbers
subplot(2,2,2) %top right figure
hist(x2) %histogram of binomial random numbers
subplot(2,2,3) %bottom left figure
hist(x3) %histogram of exponential random numbers
subplot(2,2,4) %bottom right figure
hist(x4) %histogram of Gamma random numbers

The above commands return histograms of random numbers generated by the previous commands.  The output of those commands is the following figure.


Another useful command regarding random number generators is the ‘state’ option. This is very usefull if we want to repeat our computation that involves random number. The following code will help us understand how to use the state command.

theta = -0.8;
phi = 0.9;
mu = 0;
sigma = 0.1;
%-----generating random numbers-------------------------
e = normrnd(mu,sigma,[100 1]);
%-----generating MA(1) and AR(1) process----------------
y(1) = 0;
z(1) = 1;
for i = 2:length(e)
y(i) = e(i) - theta*e(i-1);
z(i) = phi*z(i-1) + e(i);
%-----plotting MA(1) and AR(1) process----------------

Using the above command, especially when we write the ‘ state’ option in line 8, it allows us to repeat our computation later. We can show the result of our computation tomorrow exactly the same as our computation we conduct today, even though our code involves a random number.  The result of the above command is plotted in the following figure.


Plotting the auto correlation function and introduction to bar command

In time series analysis, when we have a time series, it is common to plot the sample auto correlation function (ACF) to be compared to time series models we have in the textbooks. The thing is today, our students now rely heavily on statistical softwares to plot the ACF so that they forget how to plot it in the beginning.  Recall that the sample ACF of the observed series Y_1, Y_2, \dots, Y_n is defined as

(1)——–\displaystyle{r_k = \frac{\sum_{t=k+1}^{n}(Y_t - \bar{Y})(Y_{t-k}-\bar{Y})}{\sum_{t=1}^{n}(Y_t - \bar{Y})^2}}

The following code shows a MATLAB code that will plot the ACF of both time series generated by the code above.

sumY = 0;
sumZ = 0;
for i = 1:length(y)
sumY = sumY + y(i);
sumZ = sumZ + z(i);
ybar = sumY/length(y);
zbar = sumZ/length(z);
sum2Y = 0;
sum2Z = 0;
for i = 1:length(y)
sum2Y = sum2Y + (y(i)-ybar)^2;
sum2Z = sum2Z + (z(i)-zbar)^2;
for k = 1:length(y)
sum3Y = 0;
sum3Z = 0;
for t = k+1 : length(y)
sum3Y = sum3Y + (y(t)-ybar)*(y(t-k)-ybar);
sum3Z = sum3Z + (z(t)-zbar)*(z(t-k)-zbar);
ry(k) = sum3Y/sum2Y;
rz(k) = sum3Z/sum2Z;

The last two commands above will plot the ACF of time series y and z in the bar style as it is always for the ACF, as shown in the following figure.




It doesn’t show, really, the supposed to be acf of MA(1) (above) and AR(1) (below) process. Perhaps this is I have not learned  the sample acf measurement (1).

How to export/import data between Matlab and Excell

Finally, we are going to write a code in Matlab on how to export or import data to/from Excell in Matlab. Again, this is to save myself a few minutes rather than googling it around the internet.

%--------------write data to excell-----------------------
y = transpose(y);
z = transpose(z);
filename = 'ma_ar_data.xlsx';
%-------------read data from excell----------------------
filename = 'ma_ar_data.xlsx';
sheet = 1;
xlRange = 'A1:A100';
dataY = xlsread(filename, sheet, xlRange);

The transpose command above puts the variable y generated before to be a column vector.


In this post, I have created various codes so that I am able to remember

  • generating random numbers
  • using subplot
  • using histogram and bar
  • exporting/importing data to/from excell
  • plotting the ACF