Monday, May 23, 2016

C# Colours

Introduction

I must admit I got a basic understanding of Gradient Brushes and hurriedly started coding. But, I didn't really get a good understanding of Gradients until I started playing with this utility. At that point, I realized that I needed to go back and do some further research on the subject. So, I've been researching, upgrading, researching,...well you get the idea. And, since I was upgrading the Gradiator application, I thought I would relate what I learned while this research/upgrade process has been going on.
In the latest upgrade, I have:
  • Changed the UI, hopefully to make it a tad easier to use
  • Added a brush manager with the ability to load/save your favorite brushes
  • Added a color selector with the ability to load/save color palettes
  • Added a status bar with the ability to modify the position and size, view the current shape type, and cycle through shapes
I'm not going to go knee deep into explaining the graphics, GDI+, or even gradients; instead, I'm going to give a brief overview of each of the important concepts and then, I'll move on and and get into how to create the gradients that you see/have seen in this article. A lot of them were easy, some I stumbled on, and some I really had to scratch my head and just try different things, but overall, I've managed to create some pretty nice graphics and you can too. The first thing we need to do is get a basic understanding of the concepts, so this first section is devoted to that.
  • Concepts
    • Introduction to Gradients
    • Linear Gradient Brush
    • PathGradientBrush
    • Blending
    • Color Blending
    • Alpha Blending
  • Using Gradiator
  • An Example
  • References and Links
I need to add a disclaimer here and state that for some applications, GDI+ may be slow because at this point, it doesn't take advantage of the hardware acceleration features built in to many of the graphics cards. Hopefully, in the future, this will be solved. But until that time, there are things we can do to make sure we're getting the most from GDI+.
  1. Use double buffering.
  2. Only redraw areas that need to be redrawn.
  3. Avoid drawing in controls such as Panel or PictureBox, instead use the parent control's client area.
  4. If you can use DrawImageUnscaled instead of DrawImage, the auto scaling is nice, but eats resources.
I'm sure there are more tips on performance, but these are the ones I've personally found to be helpful.
As I've used/developed this application, I have added functionality that I've found useful and time saving. There is a lot more that could be done with this application, and if you have any suggestions or want to add to it, please feel free to. I have provided default and web color palettes, and a brush palette with some interesting brushes. If you come up with any brushes you find interesting, I would be interested in seeing them. Barney says "share" :)
As always, I hope this article is instructive and the application useful. If you learn as much from it as I did writing it, then we are both rich.

Introduction to Gradients

My interpretation of a gradient is the progression from one color to another at a defined angle. Webster's definition: "ascending or descending with a uniform slope".
The two types of gradients that will be covered here are the Linear and Radial gradient brushes.
Screenshot - Liear_Normal.png
Figure 1. Simple linear gradient
The gradient in Figure 1 is the simplest type of linear gradient. It "starts" with the color white and progresses to the color red at an angle of 0 degrees. The code used to draw the above gradient is:
private void label1_Paint(object sender, PaintEventArgs e) 
{ 
    LinearGradientBrush lgb = 
      new LinearGradientBrush(label1.ClientRectangle, clr1, clr2, 0f, true);

    e.Graphics.FillRectangle(lgb, label1.ClientRectangle);

    lgb.Dispose();
}
Screenshot - Path_Simple.png
Figure 2. Simple path gradient
The gradient in Figure 2 is the simplest type of radial gradient. It also starts at one color and progresses to another, but the axis begins at a center point with a center color, and progresses outward to the end color, also white and red as in the above figure. The code to draw the gradient in Figure 2 is:
private void label1_Paint(object sender, PaintEventArgs e)
{
    GraphicsPath gp = new GraphicsPath();
    gp.AddEllipse(label1.ClientRectangle);

    PathGradientBrush pgb = new PathGradientBrush(gp);

    pgb.CenterPoint = new PointF(label1.ClientRectangle.Width / 2, 
                                 label1.ClientRectangle.Height / 2);
    pgb.CenterColor = Color.White;
    pgb.SurroundColors = new Color[] { Color.Red };

    e.Graphics.FillPath(pgb, gp);

    pgb.Dispose();
    gp.Dispose();
}

Linear Gradient Brushes

Now that we've got the basics out of the way, we can find out what else we can do with gradients. Two interesting methods that can be used to alter the way in which gradients are drawn are: SetSigmaShapeBelland SetTriangularShape.
Screenshot - Linear_Bell.png
Figure 3. Using the SetSigmaShapeBell property with a linear gradient brush
Screenshot - Linear_Triangular.png
Figure 4. Using the SetBlendTriangularShape with a linear gradient brush
Figure 3 and 4 show the result of using the SetSigmaShapeBell and SetBlendTriangularShape methods with the middle of the label as the focus point and a drop off rate of 50%. As you can see from the figures, the bell shape method distributes the center color over a broader range than its triangular counterpart.
The constructors for both brushes have parameters that allow one to determine the focal point and the rate of fall off from that point. The example code below shows how to use a linear gradient brush to draw the gradient of Figure 4:
private void label1_Paint(object sender, PaintEventArgs e) 
{ 
    LinearGradientBrush lgb = 
      new LinearGradientBrush(label1.ClientRectangle, clr1, clr2, 0f, true);
    lgb.SetBlendTriangularShape(.5f, 1.0f);
    e.Graphics.FillRectangle(lgb, label1.ClientRectangle);

    lgb.Dispose();
}
We've seen this code before, but with the addition of the highlighted text. The first parameter defines the focal point and the second the drop off scale. In this case, I used a drop off rate of 100% for a dramatic/visual effect.
You can also use an additional property that controls the overall brightness and ratio of red to green to blue, the gamma correction property. For a detailed discussion of gamma correction, refer to this link.

Path Gradient Brushes

The path gradient brush employs the same methods as the linear brush, and behaves similarly, but the outcome looks different because the path gradient brush works radially. One thing worth mentioning here is that when setting the focus, the focus starts at the outer edge (from 0) and works towards the center (to 1.0). To set the center point, use the path gradient's center point property.
Screenshot - Path_Trangular.png
Figure 5. Path gradient brush using the SetTriangularBlendShape method
In Figure 5, on the left, we are using the SetBlendTriangularShape method and setting the focal point to 50%, and again the focal scale is set to 100%, and on the right, the focal point has been shifted to the upper left corner. Using the SetBlendTriangularShape and the SetSigmaShapeBell methods in conjunction with theFocusScales, which is similar to the focus parameter for both methods, defines the scaling of the gradient. Note: oddly enough, the FocusScales work just the opposite of there equivalents in that they start at the center (0) and work out (1).
The code used to produce the left hand drawing in Figure 5 is listed below:
private void label1_Paint(object sender, PaintEventArgs e)
{
    GraphicsPath gp = new GraphicsPath();
    gp.AddEllipse(label1.ClientRectangle);

    PathGradientBrush pgb = new PathGradientBrush(gp);

    pgb.CenterPoint = new PointF(label1.ClientRectangle.Width / 2, 
                                 label1.ClientRectangle.Height / 2);
    pgb.CenterColor = Color.White;
    pgb.SurroundColors = new Color[] { Color.Red };
    pgb.SetBlendTriangularShape(.5f, 1.0f);
    pgb.FocusScales = new PointF(0f, 0f);

    e.Graphics.FillPath(pgb, gp);

    pgb.Dispose();
    gp.Dispose();
}
As an example of using the SetBlendTriangularShape method with the FocusScales property, we will use the left drawing in Figure 5. If we use the above code but change the FocusScales property to (.5f, .5f), the focal point will be 75% out from the center of the shape.

Blending

With blending, we can define a custom gradient and therefore take control over how the gradient is rendered. Blending allows us to override the normal blending behavior and set points along the path where we define alternate drop off rates. We still go from a start color to an end color, but as you can see in the figure below, the drop off rates at the various positions allow much greater control over the rendering process.
Screenshot - Blend.png
Figure 6. Blending using LinearGradientBrush (left) and PathGradientBrush (right)
A couple of terms are in order here:
  • Position - A point along the gradient path
  • Factor - Drop off rate at the defined position
In Figure 6 above, I have numbered the positions, and combined with the listing below, you will get a better understanding of how blending works. This code is used to produce the right hand drawing in Figure 6.
private void label1_Paint(object sender, PaintEventArgs e)
{
    GraphicsPath gp = new GraphicsPath();
    gp.AddEllipse(label1.ClientRectangle);    

    PathGradientBrush pgb = new PathGradientBrush(gp);
    pgb.FocusScales = new PointF(0f, 0f);

    Blend blnd = new Blend();
    blnd.Positions = new float[] { 0f, .25f, .5f, .75f, 1f };
    blnd.Factors = new float[] { 1f, 0f, 1f, 0f, 1f };
    pgb.Blend = blnd;
    e.Graphics.FillPath(pgb, gp);

    pgb.Dispose();
    gp.Dispose();
}
Both gradients were drawn using the same values, and from what we've learned so far, the outcome is predictable. The only difference being the PathGradientBrush, the positions radiate outward from the center point, and at the positions noted, you can see the transitions made.
Note: There are three things that are very important here:
  1. The number of positions and factors must be the same.
  2. The first position must be 0 and the last 1.
  3. The number of positions, factors, and colors should not exceed the number of points in the graphics point list. If it does, I turn off color blending, or GDI+ throws an exception. (Working on this.)
If either of these conditions is not met, GDI+ will throw an exception.

Color Blending

Color blending works much the same as regular blending except that at each position, instead of a drop off value, we can use a color. An interesting observation can be made at this point, if we move point 2 to where the dotted line is, this will move the end position for red over to where the arrow is and effectively make point 3 useless, which is pretty much the way we would expect it to behave. But, if either of the two objects are semitransparent, then the color in the resultant union is a blend of the two colors, and will in itself be an alpha blend. Alpha blending will be covered in the next section.
Screenshot - Color_Blend.png
Figure 7. Color Blend using the LinearGradientBrush
Figure 7 shows an example of a shape that employs the color blend features of the linear gradient brush. As you can see, the color at each position is a different color of the rainbow at evenly spaced positions along a gradient path of 0 degrees. The snippet below was used to draw Figure 7.
private void label1_Paint(object sender, PaintEventArgs e)
{
    GraphicsPath gp = new GraphicsPath();
    gp.AddEllipse(label1.ClientRectangle);

    PathGradientBrush pgb = new PathGradientBrush(gp);

    pgb.CenterPoint = new PointF(label1.ClientRectangle.Width / 2, 
                                 label1.ClientRectangle.Height / 2);
    pgb.CenterColor = Color.White;
    pgb.SurroundColors = new Color[] { Color.Red, 
                                       Color.Orange, 
                                       Color.Yellow, 
                                       Color.Green, 
                                       Color.Blue, 
                                       Color.Indigo, 
                                       Color.Violet };
    pgb.SetBlendTriangularShape(.5f, 1.0f);
    pgb.FocusScales = new PointF(0f, 0f);

    e.Graphics.FillPath(pgb, gp);

    pgb.Dispose();
    gp.Dispose();
} 

Alpha Blending

While I was searching for a good explanation for alpha blending, I ran across the following from an article "Alpha Blending Tutorial" written by Toshihiro Horie, and it pretty much says it all.
What is alpha blending? It's a way of mixing the colors of two images together to form a final image. An example of naturally occurring alpha blending is a rainbow over a waterfall. If you think of the rainbow by itself as one image, and the background waterfall as another, then the final image can be formed by alpha blending the two together. Pixel artists and programmers sometimes call alpha blending "translucency"-- it is the same thing. In addition, when the blending factor changes dynamically over time, alpha blending is called "cross fade" or "dissolve".
To add alpha blending to your drawings, merely adjust the first argument in the Color.FromArgb method call. This is the alpha channel parameter and can have a value from 0 - 255, 0 being completely transparent and 255 being opaque. Things get interesting when you start using alpha blending. It adds another dimension to the problem. The reason for this is that where the shapes overlap, it forms a blend of the two colors and the opacity level of each. I can akin this to an artist when he mixes colors on his palette to form a new color. In the same way, you can adjust the levels of each...add a little more white, a little less black, etc..

Using Gradiator

I have used this application as I've been developing it, and have used it as a kind of a doodle pad. I get an idea and scratch my head for a while, then I start doodling and go smoke a cig, think a little more and then save it, try it, then go back and tweak it until I get what I want. The problem is, the more I use Gradiator, my skills get better, and I run across something that looks better, and I just can't seem to finalize the dang thing. The About dialog is a perfect example since I want it to reflect what can be done with gradients. Check it out, there are only four objects drawn on the form, and two are text, the other two are the background and the button.

Overview

In a nutshell, what we are doing here is creating brushes to render our shapes. When we first create a shape, the default brush assigned to it is a solid brush with a white background and a border. You change the properties of the brush using the Properties editor located in the tab control to the right of the workspace area. I couldn't put all the controls I needed in one area and have room for a decent workspace, and I didn't want a bunch of floaters, so I compromised and divided the functionality as I saw it, so ended up with three tabs, and they are:
  • General Properties - This tab contains the common properties for the brush selected
  • Brushes - Brush management
  • Blend - Blend property editor

The Shape Editor

I don't think a lot needs to be said about the shape editor itself, it's just a basic graphics editor that allows you to create, move, and resize a few basic shapes.
To create a shape: Click on the shape desired, move mouse into the canvas area, press the left mouse button at the place you want to start the shape, and drag out until you get to the end point, and release the mouse button.
To select a shape: Click anywhere within the shape boundary. Something I did a little different when selecting the shapes, that is bothersome in some cases, but a really nice feature for most editing. In my selection logic, if you have two objects and one sits on top of the other, I select the smaller of the two; if they are the same, I pick the last one drawn (top of z-order), but if the top shape is not transparent, then you will not see the shape underneath, unless you bring it to the top, by selecting it and pressing the right mouse button and choosing the "bring to top" option. I did this because when I'm working with two objects, it allows me to see what I'm doing when I'm positioning the shape in the background. Once you start using Gradiator, you will appreciate this option.
To move a shape: Press and hold the left mouse button anywhere within the shape boundary, and move it to the new location.
To resize an object: Press and hold the left mouse button within the resizing rectangles, and drag to new shape. You can only resize in the positive direction right now. I'm still working on this problem.

Brush Manager

The brush manager is the repository for brushes that you've created and want to use in the future. You can:
  • Create a new brush palette.
  • Load a palette - On startup, a default palette is loaded called "default palette.xml" in the startup directory.
  • Import a palette, appending it to the current palette.
  • Save the current palette to file.
To use brushes from the palette, use the shape editor to create shape primitives, and if not already selected, select it, then click on any one of the thumbnail images from the brush manager's list to associate it with the primitive. These primitives, either opaque or semitransparent, may then be overlaid one on top of another to produce composite images. Now, to save a brush once you have it, just select it and right click, choose the "Save Brush" option, and it will be added to the brush manager's list. Be sure to save before you exit the application.

Blend Editor

Here is where you can really get artistic and create some nice images. I'm by no means an artist, but after spending a short time working with blends, it became easier to visualize what I wanted to do and how to achieve the images shown in this article (even a blind squirrel gets a nut occasionally).
The only real advice I can give you here is just play with it!
To use the editor, select an object, go to the Blend tab, and select either Blend or the Color Blend style by using the check box to turn blending on or off then selecting the type of blend desired, regular blending being the default. Blending is only available when using the linear gradient or path gradient brushes, for obvious reasons. The type of blending determines which parameters are meaningful, for:
  • LinearGradientBrush - Position and Factor
  • PathGradientBrush - Color and Position

Color Selector

For color selection, I decided to devise a way to have a selector active. To do this, I put a color swatch next to each color property. In some cases, this is kind of a pain, but overall, it turned out to be a good solution. To select a color, go to the color selector and pick a color, then for the property you wish to modify, left click on the swatch. To transfer the color from the property swatch to the primary color in the color selector, right click on the Properties swatch. On startup, a default color palette "color palette.xml" is loaded from the startup directory.

An Example

When I first started, I had a little trick I would do if I got confused. I would choose a color/position combo in the area that I was confused about and dramatically change the color. This would give me a general idea what the blend covered and the color distribution if opacity was involved. The point here is if you don't understand the way I'm explaining, or I'm explaining it wrong, go into the gradient, create a shape, and associate this brush with it, and change the color or just play with it. I tried thin borders, thick borders, and a lot of different things before I got to this. Shading around the edge took a little head scratching (I told ya I ain't no artiste').
Screenshot - example.png
So, here was what I was thinking when I created this:
  • Picture frame-ish so I can overlay something in this area or blend it to suit
  • A nice clean transition between the client area and the border
  • Shading for the border, hopefully adding a little 3D look to it
Well, let's get started. While I'm putting a Saliva CD and getting a quick smoke, you can start up Gradiator or not (smoke em if ya got em), and then we'll begin.
Ok, I'm back.
So, let's start at the center and wind our way out:
  • Start position (0): Let's start with a light/powder blue.
  • 82: We want to end this transition abruptly, so we gradate to a steel blue/gray, just a tad darker so it isn't plain.
  • 85: We want a dark shaded area make it appear sunken. (85 - 82 = 3) So we have a 3 pixel shaded area.
  • 86: After the darker shaded area, we want a thin light area, back to the steel gray, to represent that it's raised, and to start the edge blend.
  • 92: At this point, we've gradated from the steel color and we want to darken it, fade the colors out. If we bypassed this step, the transition would be linear and would look like a ramp instead of having the curved edge look as in the above example.
A couple of observations can be made:
  • If we change the first two colors and make them transparent, then it will be a true frame since the client area is transparent!
  • Pay attention to perspective. Look at the border size on the sides as compared to the top and bottom. If it were square, this wouldn't be a problem. Just something to keep in mind.
Well, that's about it...Good luck!

 Source : http://www.codeproject.com/KB/GDI-plus/Gradiator.aspx?msg=2732284

Wednesday, March 30, 2016

How to batch resize photos

Installing ImageMagick

For Windows users:

  • Install the appropriate binaries from here
  • During installation make sure the box is checked for Add application directory to your system path

For Linux (Ubuntu/Debian) users:

  • ImageMagick is included in the software repository so you just need to run the following commands to install it.
sudo apt-get update
sudo apt-get install imagemagick

Resizing the photos

Next open up a command prompt/terminal and navigate to where the photos are that you want to resize. There you’ll want to create sub-folder to store the resized photos. Then run the following command replacing output with the name of the sub-folder you created.
mogrify -format jpg -verbose -auto-orient -resize 2048x2048 -quality 100% -path 'output' *.JPG
Once that’s done just go into the sub-folder and your photos should all be resized and ready to go.

Download

More Details

Wednesday, September 23, 2015

EXCEL Advance Match Function

To check value is avalable
=MATCH(A3,A57:A592,0)

To Get row number of matching value
=MATCH(A3,A57:A592,0)+ROW(A57:A592)-1


Get value of the Cell
=INDIRECT("B"&G5) // B is column and G5 contains row index

Thursday, June 18, 2015

Google Sites

1. Contact Form

Can use JotForm for contact form that send you email from your web site
Ex: OEM contact page http://www.jotform.com/?formID=51298047806461

2. SEO - Search Engine Operator

You can use https://www.xml-sitemaps.com/ to build  site-map for you Google site. Once you build the site map, upload it to your web site using  "Attachment " under "Mange Your Site"

3. Google Web Master

  • Go to "Crawl" -> "SiteMaps" to ubmit the site-map you uploaded.
  • Then "Fetch as Google" and Submit.
4. Adding Sub Menu Tabs/Links

Go to "Edit Site Layout" -> "Edit Horizontal Navigation", And use Arrows to make sub menu/tabs


Saturday, May 2, 2015

MRTG Network Monitor

What it does

You have a router, you want to know what it does all day long? Then MRTG is for you. It will monitor SNMP network devices and draw pretty pictures showing how much traffic has passed through each interface.
Routers are only the beginning. MRTG is being used to graph all sorts of network devices as well as everything else from weather data to vending machines.
MRTG is written in perl and works on Unix/Linux as well as Windows and even Netware systems. MRTG is free software licensed under the Gnu GPL.
Sample Graph

Friday, December 5, 2014

Using R to Test Pairs of Securities for Cointegration

Paul Teetor
August 2009

Ernie Chan's book, Quantitative Trading, explains why cointegrated pairs of securities are useful for constructing mean-reverting trades.  It also explains how to test pairs of securities for cointegration.  Ernie uses Matlab, but some readers may want to use R, the software for statistical computing and graphics.  This note explains how to perform the cointegration test using R.

Let's assume you have the history of daily prices for two stocks, GLD and GDX. You want to know if the prices are cointegrated. Let's also assume the data are captured in two files, GLD.csv andGDX.csv, which are in comma-separated value (CSV) format with seven columns: date, open, high, low, close, volume, and adjusted close.

Two notes before we begin.

    This is not a tutorial on R.  (Sorry.)  You can learn about R from its official web site, from An Introduction to R, from one of the tutorials, or from one of the available books.
    This is also not a tutorial on cointegration.  For that I recommend Ernie Chan's book; or, for the more mathematically inclined, the book by Bernhard Pfaff, Analysis of Integrated and Cointegrated Time Series with R.  Both books are excellent.

Data Representation

You could perform this work using vectors or data frames to represent your time series data, but that would be tedious. I use zoo objects for representation of time series, and I strongly recommend either the zoo package or the xts package.  xts is a super-set of zoo with an extremely fast implementation and many other features. Here, I'll assume you are using zoo.

Once your data are captured in a zoo object, say t, it behaves like a data frame. One zoo object can contain several columns, where each column is a different time series and each row is a set of (simultaneous) observations of those series.  The object provides an additional attribute, index(t), which is the vector of dates, one date per observation.  The first and last dates are available using start(t) and end(t), respectively.
Loading the Data

Loading the CSV files into a zoo object requires a few simple steps.

    Read the two files into two data frames.
    Convert the dates from strings into Date objects.
    Convert the two data frames into two zoo objects.
    Take the intersection of the two zoo objects.  That will create one zoo object with the observations common to both datasets.

Here is the R code.

library(zoo)            # Load the zoo package

# Read the CSV files into data frames
#
gld <- read.csv("GLD.csv", stringsAsFactors=F)
gdx <- read.csv("GDX.csv", stringsAsFactors=F)

# The first column contains dates.  The as.Date
# function can convert strings into Date objects.
#
gld_dates <- as.Date(gld[,1])
gdx_dates <- as.Date(gdx[,1])

# The seventh column contains the adjusted close.
# We use the zoo function to create zoo objects from that data.
# The function takes two arguments: a vector of data and
# a vector of dates.
#
gld <- zoo(gld[,7], gld_dates)
gdx <- zoo(gdx[,7], gdx_dates)

# The merge function can combine two zoo objects,
# computing either their intersection (all=FALSE)
# or union (all=TRUE).
#
t.zoo <- merge(gld, gdx, all=FALSE)

# At this point, t.zoo is a zoo object with two columns: gld and gdx.
# Most statistical functions expect a data frame for input,
# so we create a data frame here.
#
t <- as.data.frame(t.zoo)

# Tell the user what dates are spanned by the data.
#
cat("Date range is", format(start(t.zoo)), "to", format(end(t.zoo)), "\n")

The as.Date function assumes your date strings are formatted like yyyy-mm-dd, which is the ISO standard format. If your strings use the common US format of mm/dd/yy, include a formatspecification.

gld_dates <- as.Date(gld[,1], format="%m/%d/%y")

gdx_dates <- as.Date(gdx[,1], format="%m/%d/%y")

Constructing the Spread

In Ernie's book, he first tests for cointegration, then he constructs the spread. In R, we do it the other way around: First we construct the spread, then we test the spread for a unit root.  It the spread has a root inside the unit circle, the underlying securities are cointegrated.  (See Bernhard Pfaff's book for a discusion of unit roots and their significance.)

The spread is defined this way.

S = y - (ß × x)

where ß is the hedge ratio, calculated using ordinary least squares (OLS).  Rearranging terms, we want to find the value of ß which best fits this equation.

 y = (-ß) × x

This is a simple linear equation with no y intercept.  In R, the lm function can fit a linear model such as this.

# The lm function builds linear regression models using OLS.
# We build the linear model, m, forcing a zero intercept,
# then we extract the model's first regression coefficient.
#
m <- lm(gld ~ gdx + 0, data=t)
beta <- coef(m)[1]

cat("Assumed hedge ratio is", beta, "\n")

# Now compute the spread
#
sprd <- t$gld - beta*t$gdx

The first argument to lm is a formula, which specifies the linear model.  The formula gld ~ gdx + 0 says the model is

GLDi = ß×GDXi + ei

(If we omit "+ 0" from the formula, R would fit a y intercept, too.)
Testing for Cointegration

The Augmented Dickey-Fuller test is a basic statistical test for a unit root, and several R packages implement that test.  Here, we will use the adf.test function which is implemented in thetseries package.  The function returns an object which contains the test results. In particular, it contains the p-value that we want.

library(tseries)            # Load the tseries package

# Setting alternative="stationary" chooses the appropriate test.
# Setting k=0 forces a basic (not augmented) test.  See the
# documentation for its full meaning.
#
ht <- adf.test(sprd, alternative="stationary", k=0)
cat("ADF p-value is", ht$p-value, "\n")

Setting alternative="stationary" is important.

    To a statistician, it specifies a null hypothesis that the spread is non-stationary or explosive.
    For everyone else, it means that if the p-value is small, the spread is mean-reverting. By "small," I mean less than 0.10 or less than 0.05, depending how fussy you are.  (Smaller is better.)

We can interpret the ADF test results for the user.

# The ht object contains the p-value from the ADF test.
# The p-value is the probability that the spread is NOT
# mean-reverting.  Hence, a small p-value means it is very
# improbable that the spread is NOT mean-reverting
# (got that?).
#
if (ht$p.value < 0.05) {
    cat("The spread is likely mean-reverting.\n")
} else {
    cat("The spread is not mean-reverting.\n")
}

One word of caution:  The adf.test function essentially detrends your data before testing for stationarity.  If your data contains a strong trend, you might be very surprised to learn it is "mean reverting" when it is obvously moving upward or downward.  If this is a problem for you, consider the  fUnitRoots package which contains the adfTest function (note the spelling!).  That function lets you analyze either with or without the trend assumption.
Putting It All Together

OK, I've talked a lot, but it's really not much code. Here it is with some fat removed.

library(zoo)
library(tseries)

gld <- read.csv("GLD.csv", stringsAsFactors=F)
gdx <- read.csv("GDX.csv", stringsAsFactors=F)

gld <- zoo(gld[,7], as.Date(gld[,1]))
gdx <- zoo(gdx[,7], as.Date(gdx[,1]))

t.zoo <- merge(gld, gdx, all=FALSE)
t <- as.data.frame(t.zoo)

cat("Date range is", format(start(t.zoo)), "to", format(end(t.zoo)), "\n")
m <- lm(gld ~ gdx + 0, data=t)
beta <- coef(m)[1]

cat("Assumed hedge ratio is", beta, "\n")

sprd <- t$gld - beta*t$gdxht <- adf.test(sprd, alternative="stationary", k=0)

cat("ADF p-value is", ht$p.value, "\n")
if (ht$p.value < 0.05) {
    cat("The spread is likely mean-reverting\n")
} else {
    cat("The spread is not mean-reverting.\n")
}

When I ran this code on recent data, I got this output.

Date range is 2006-05-23 to 2009-08-10
Assumed hedge ratio is 1.928096
ADF p-value is 0.2609909
The spread is not mean-reverting.
Postscripts

I used many R functions, but did not try to explain all their nuances.  If you have any questions (e.g., "Why did you set stringsAsFactors=FALSE for read.csv?"), use the help facility to learn more about the function:

> ?read.csv

These notes assume your data is captured inside CSV files.  I did that just to mirror the example in Ernie Chan's book.  R can read its data from many places.  To learn more about R's input/output in general, start with the R Data Import/Export manual.  To learn more about loading financial data in particular, visit the web site for the quantmod package, an excellent source of tools for anyone working with financial data.

R's input/output is so flexible, in fact, that you can read data directly from web sites, such as the Yahoo! Finance web site.  Just use a URL instead of a file name, like this.

gld <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=GLD&ignore=.csv", stringsAsFactors=F)
gdx <- read.csv("http://ichart.finance.yahoo.com/table.csv?s=GDX&ignore=.csv", stringsAsFactors=F)

This eliminates the extra step of saving the data to an intermediate file.

I used the adf.test function, above, to test for stationarity.  The ADF test is implemented in several other packages, too, such as the fUnitRoots and urca packages.  Bernhard Pfaff's book contains an entire chapter on tests for unit roots, including tests beyond the original ADF test.

Finally, high-powered statisticians will be offended by my statement that "the p-value [of the ADF test] is the probability that the spread is not mean-reverting." Just for the record, here is the strict interpretation:  "The p-value is the probability that we could have observed the spread data, given the assumption that the spread is not mean-reverting."

Tuesday, November 18, 2014

Scheduler to reboot Windows PC

    1. Press WinKey+R > type taskschd.msc and press Enter (it will launch Task Scheduler)
    2. Left click from the right pane on the Task Scheduler Library > open Action from the top menu >New Folder... > name it MyTasks > click OK
    3. Left click on the MyTasks > choose Action from menu > Create Basic Task... (it will open task wizard)
    4. You can enter the Name , for example "Restart", and press Next
    5. In Trigger section you can specify when you want to run your task, for example:
      • Select Daily , and press Next
      • Now you can specify the day, the hour and set to recur every day, after that press Next
    6. In Action section you can choose what you want to run, for example restart your system, to do this:
      • choose Start a program , and press Next
      • in the Program/script field type shutdown /r , and press Next
    7. And that's all, you can press Finish