## My first try creating a bash script

I am a ubuntu user, but sometimes I also use windows, mainly because my computer in the office is using windows. So, I use both ubuntu and windows. Even, my laptop has both windows and ubuntu. I tried to get rid of windows once, and installed wine to use windows application in my ubuntu. But it runs very slowly and it’s killing me and in the end, I came back to reinstall windows. I mainly use ubuntu for my research. The softwares I use for my research are auto, python, dstool, latex, xfig which run smoothly in ubuntu, even though python and latex can also be installed and run smoothly on windows machine. On the other hand, I use windows to do some regular activities such as browsing the internet, watching movies, checking my email, creating an office documents etc. All of which can be done in ubuntu as well. But there are activities that I must use windows, I sometimes need to use matlab and sometimes I like to play a game that only runs in windows. These two things are the main reason I still come back to use windows. Recently, I learn python so I am now trying to less use matlab.

That is just a background and my main point here is about bash scripting. After a few years using ubuntu, I have not created any bash script. Today, finally I learn to create one script. I created a script to automate my boring routine. When I write a paper, I need some illustrations. I mostly use xfig to create some mathematical images and to be able to use in the figure I need to convert it to an eps file. The produced eps file will be then converted to a pdf file as it is perfectly compatible to my pdflatex command. But before that, I need to crop the resulted pdf file in order to remove white space around the image. Suppose the name of my xfig file is spam.fig. I then write a series of command.

figtex2eps spam.fig ps2pdf spam.eps pdfcrop spam.pdf

I want to write a script that do all the above automatically. Thus I created the following script.

#!/bin/bash # Converts a .fig-file (xfig file) to a .eps-file by using a built-in function figtex2eps # and then convert it to a .pdf file by using a built-in function ps2pdf # and finally convert it to a cropped pdf file by using a built-in function pdfcrop # # ivanky saputra https://ivanky.wordpress.com # # credit to : # $ /home/abel/c/berland/etc/cvsrepository/figtex2eps/prog/figtex2eps $ # $ ps2pdf in ubuntu $ # $ pdfcrop in ubuntu $ echo "We are going to change the following $1.fig to a cropped pdf file" function quit { exit } if [$1 == ""]; then echo "no files given"; quit else echo "Processing $1.fig............"; figtex2eps $1.fig ps2pdf $1.eps pdfcrop $1.pdf echo "Done"; fi

As someone has said that it is better to share your code and data openly as we as human are idiots and will make mistakes, please give me any suggestion to improve mine.

## Random Numbers in Matlab

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.

`clc;`

clear;

theta = -0.8;

phi = 0.9;

mu = 0;

sigma = 0.1;

%-----generating random numbers-------------------------

normrnd('state',100);

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

end

%-----plotting MA(1) and AR(1) process----------------

subplot(2,1,1)

plot(y,'r*-')

subplot(2,1,2)

plot(z,'b*-')

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 is defined as

(1)——–

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

end

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;

end

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

end

ry(k) = sum3Y/sum2Y;

rz(k) = sum3Z/sum2Z;

end

subplot(2,1,1);

bar(ry);

subplot(2,1,2);

bar(rz);

The last two commands above will plot the ACF of time series and 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';

xlswrite(filename,y,1,'A1');

xlswrite(filename,z,1,'B1');

%-------------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 generated before to be a column vector.

Conclusion

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

## Your says