I'm James and I write code. Sometimes I blog.

Month

January 2011

5 posts

My O'Reilly Wish List

O’Reilly is currently holding a $500 giveaway. My entry form is a blog about the books, ebooks, and videos that I would purchase with $500. Beside every title is the dollar value of the product rounded up to the nearest dollar. After rounding, my wishlist totals $499. I think I hit the mark pretty close.

Paperback Books:

The Definitive ANTLR Reference - 37

Language Implementation Patterns - 35

Manga Guide to Calculus - 20

Hacking: The Art of Exploitation, Second Edition - 50

Ebooks:

Beautiful Data - 36

Beautiful Security - 32

Beautiful Architecture - 36

Beautiful Code - 36

jQuery Cookbook - 28

Data Analysis with Open Source Tools - 32

Statistics in a Nutshell - 35

Hadoop - 39

Bioinformatics Programming Using Python - 48

Videos:

Great R: Level 1 - 35

Jan 26, 20111 note
3D Visualization of an Adjacency Matrix in Matlab

An adjacency matrix is a data model to represent an undirected graph. Most adjacency matrices are two dimensional, which means you can use the built-in Matlab function “gplot” to visualize it. This function “gplot3” allows you to visualize adjacency matrices in 3D.

I created the above demonstration using Matlab’s built in Bucky Ball creator.

» [B XY] = bucky;
» gplot3(B, XY);

Here’s the code!

Jan 21, 20111 note
#matlab
A Slinky in Matlab

I made this because I need to learn about 3D graphics, but I’m too lazy to learn Blender. This was made with 1 line of Matlab.

>> x = linspace(0, 200*pi, 10000); y = sin(x/100)+sin(x); z = cos(x); plot3(x, y, z)
Jan 5, 20112 notes
#matlab
Comparing Stock Volatility Using R

In my last post, I wrote a quick Python script to download 1 years worth of a stock’s history into a CSV file for given a list of stocks. Today, we are going to take those stocks an compare the volatility between each stock’s daily price change.

Measuring volatility is a simple calculation of the standard deviation of the percentage of the stock’s price change over a given year. The higher the standard deviation of percent change means higher volatility (and conversely, lower standard deviation means lower volatility).

This program takes a list of ticker symbols, opens up their corresponding CSV files (see yesterday’s post for more information), computes the standard deviation based on percent change, sorts that list, and prints.

In the results, you can see that I’m tracking 26 stocks. The three stocks with the lowest volatility are Proctor & Gamble (0.85%), Coca-Cola (0.99%) and Kraft (1.08%). The three stocks with the highest volatility are Micron Technology (3.24%), Netflix (3.44%), and Crosstex Energy Inc (3.62%).

Disclaimer: I’m not a broker and I haven’t been investing for very long. This information should not be used as investment advice, but it should be used as a tutorial on how to pull information out of a CSV file in the R language. How you interpret this data is up to you.

Here’s the data.

R version 2.7.1 (2008-06-23)
Copyright (C) 2008 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

> source('stockSD.R')
          sdList
PG   0.008494452
KO   0.009928666
KFT  0.010838839
PFE  0.013281879
MSFT 0.013841909
YUM  0.014198831
SAP  0.014843072
FTR  0.015125052
ORCL 0.015317628
INTC 0.015906689
CPNO 0.016704188
AAPL 0.016787813
EMR  0.016905197
GE   0.017227925
GOOG 0.017538399
BIG  0.018467030
YHOO 0.018651226
GS   0.019989476
AMZN 0.020597942
BAC  0.023268540
C    0.023951356
F    0.023993128
RIMM 0.024404948
MU   0.032412592
NFLX 0.034474349
XTXI 0.036222946

Here’s the code.

symbols <- c("BAC", "YHOO", "RIMM", "MU", "MSFT", "GOOG", "PFE", "INTC", "C", "GE", "SAP", "FTR", "BIG", "ORCL", "AMZN", "XTXI", "GS", "KO", "F", "PG", "EMR", "AAPL", "KFT", "NFLX", "CPNO", "YUM")

sdList <- 1:length(symbols)

for (s in 1:length(symbols)) {
    infile <- paste(symbols[s], ".csv", sep="")
    df <- read.csv(infile)
    percent_change <- ((df$Close - c(0, df$Close[1:length(df$Close)-1]))/ c(0, df$Close[1:length(df$Close)-1]) )[-1]
    sdList[s] <- sd(percent_change)
}

# Create a frame for our standard devation list
sdDF <- data.frame(symbols, sdList)

# Order the standard devation list by standard devations in ascending order
sorted.sdDF <- sdDF[order(sdDF$sdList), ]

# Apply names to the standard devation list
rownames(sorted.sdDF) <- sorted.sdDF$symbols

# Take out the names 
sorted.sdDF <- sorted.sdDF[-1]

print(sorted.sdDF)
Jan 2, 2011
#r
Downloading Stock Market Data via Yahoo using Python

"""

In my spare time, I'm an investor. I figure I should
should use my powers in math and computer science and
help build for myself a better future.

This program will download the past year's stock
history for a list of ticker symbols defined by the
'symbols' list variable. Each stock's data will be
downloaded to your computer into a CSV file, which
can easily be viewed with Microsoft Excel.

Tomorrow's post will be a program on how to read the
output files using the popular statistical computing
software R.

"""

import urllib
from datetime import date

symbols = [ "BAC", "YHOO", "RIMM", "MU", "MSFT", "GOOG", "PFE", "INTC", "C", "GE", "SAP", "YUM", "CLRT", "ORCL", "AMZN", "XTXI", "GS", "KO", "F", "PG", "EMR", "AAPL", "KFT", "NFLX", "CPNO", "BIG", "FTR"]

def download(url, outfile):
    webFile = urllib.urlopen(url)
    localFile = open(outfile, 'w')
    localFile.write(webFile.read())
    webFile.close()
    localFile.close()

def get1YearPriceHistory(symbol):
    today = date.today()
    url = "http://ichart.finance.yahoo.com/table.csv?s=%s&a=%02d&b=%02d&c=%04d&d=%02d&e=%02d&f=%04d&g=d&ignore=.csv" % \
           (symbol, today.month - 1, today.day, today.year - 1, today.month - 1, today.day, today.year)
    outfile = symbol + ".csv"
    download(url, outfile)

if __name__ == '__main__':
    for s in symbols:
        get1YearPriceHistory(s)
Jan 1, 2011
#python
Next page →
2012 2013
  • January 2
  • February
  • March
  • April
  • May
  • June
  • July
  • August
  • September
  • October
  • November
  • December
2011 2012 2013
  • January
  • February
  • March 1
  • April
  • May
  • June 1
  • July
  • August 1
  • September 1
  • October
  • November
  • December
2010 2011 2012
  • January 5
  • February 1
  • March 2
  • April 2
  • May 1
  • June 1
  • July
  • August
  • September
  • October 1
  • November
  • December 2
2010 2011
  • January
  • February
  • March
  • April
  • May
  • June
  • July
  • August
  • September 4
  • October 10
  • November 10
  • December 3