Saturday, March 23, 2013

Backtracking

Though over the past year, the indices I've worked with seem to all have a dominant period of 128 days, this period may not hold true for a longer period of time. After all, markets as a whole also undergo trends. Keeping this in mind, let's take a look at what happens when I analyze the 5 year historical price data of the Nasdaq Composite index...

As before, here's the graph of its prices, this time spanning a greater length of time:



And the graph of its frequency content:


But instead of getting a period of 128 days, the period of this data is actually closer to 341 days. Hmmm...



Friday, March 22, 2013

I'm Back!

Hello everyone, Lu's back! It's been a while. After a refreshing week indulging in sleep and food, I'm ready to return to work. Hopefully, for those of you who had them, your spring break was enjoyable, too.

To refresh all our minds, allow me to briefly summarize what I've blogged about in the last few weeks. After acquainting myself thoroughly with the Matlab software by using it to solve problems such as how to create magic matrices, I've acquired historical data for several stock indices, and again using Matlab, have determined that based on the last year, these indices' prices all varied with a period of about 128 days.

Now, while many people use these indices to help them make investment decisions and track the condition of the stock market, the ways in which such indices are derived are not necessarily common knowledge.

Let's take a look at how one such index, the Dow Jones Industrial Average, is calculated.

The Dow Jones Industrial Average is calculated from the prices of 30 of the largest and most held public companies in the United States. When the index was first created, the stock prices were simply added up and divided by the number of stocks included in the index. Today, the method is a little more complicated.

Today, the DJIA incorporates what's known as the Dow divisor, a constantly changing number that accounts for events like stock splits. To calculate the DJIA, one simply adds up the prices of the 30 companies and divides by the Dow divisor, thus getting a number reflective of how large corporations are performing in the stock market. Furthermore, to find how much changes in a specific stock affects the index, one can divide the change in stock value by the Dow divisor, yielding the percent change in the index that stock is responsible for.

Pretty cool, no?


Saturday, March 9, 2013

Twice Is A Coincidence, But What About Three Times?

Happy weekend, everyone. Are you excited for Spring Break next week? I know I am. I'm looking forward to a long, relaxing week with plenty of time for sleeping...even though I do a lot of that anyway nowadays. :) That's right, next week, I won't be updating this blog. But don't worry, I'll be back the following week!

Ahem. Ok, back to the point of this post. Yesterday, I showed that the price data for Nasdaq over the past year has a dominant period of about 128 days. That led me to wonder what the periods of other indices' prices were. So, over the course of the week, I analyzed two other indices: the Dow Jones Industrial Average, and the S&P 500, in the same manner.

First the Dow Jones, an index that measures how large, public U.S. companies have traded during a standard trading session. (Ticker Symbol: DJI) It is influenced not only by economic and corporate reports, but by domestic and foreign policy issues. Its components trade on both the NYSE Euronext and the NASDAQ OMX stock markets.

Here's the graph of its past year prices:


And from this data, I can find its period to be 128 days, just like Nasdaq's. Um, alright.

Moving on, I do the same for the S&P 500, which measures the market capitalizations of 500 leading companies publicly traded in U.S. stock markets. It covers about 75% of U.S. equities and is considered by many to be the best representation of the welfare of the U.S. economy. 

Its graph:


And its period turns out to be...128 days again! 

Hmm, so the Nasdaq Composite, DJ Industrial Average, and S&P 500 all have the same period? Coincidence? I think not!




Friday, March 8, 2013

Closer Approximation

Hello everyone!

To recap, last week I plotted the prices of the Nasdaq index against time and attempted to determine the period of these prices, which I approximated to 118 days. But after all the effort of plotting, detrending, and graphing dominant frequencies, it seemed silly to find just an approximation of the period. I decided to go ahead and find the (more or less) exact period for Nasdaq.

As you may recall, this is the graph of the Nasdaq's frequency content:


Now, instead of zooming in and finding an approximate value for the frequency, I'll include a bit of code in my m-file that finds the exact dominant frequency and period. Here it is:

And when I run the file in Matlab, in addition to the graphs, I get:

xmax =
    0.0078
dompdnasdaq =
   128


This means that the period of Nasdaq is actually much closer to 128 days than my previous guess of 118 days according to the data I used. It'd be interesting to see if this period remains consistent over a longer period of time, like 5 or 10 years...or what the period of other stock indices are...

Well, until next time! See you soon!

Saturday, March 2, 2013

Wavy Shenanigans

Hello, it's me again. In the last post, I mentioned that the plot of Nasdaq prices against time appeared rather wave-like.

Here's that graph again, just to recap. :)


Looking at the graph, it appears that the Nasdaq prices crest and dip at regular intervals and thus possess  frequency and period like a wave. In fact, just by eyeballing it, I can guess that Nasdaq prices peak about every 100 days.

Using a Matlab function called "fft," which produces a one dimensional fast Fourier transform, I can determine if my guess is correct. Using this function, I can determine the dominant frequencies of the data, and thus, its periods. But before I employ the fft function, I need to "detrend" my data by subtracting its average from every point, thus centering my new graph at y=0. 

Here's a comparison of what the data looks like before and after detrending:


Now, we can use fft:


And get this graph:


After zooming in, it becomes apparent that the dominant frequency in the Nasdaq data is about 0.0085, which means its period (1/frequency), is about 118 days. This means that on average, the price of Nasdaq dips and peaks every 118 days. How exciting!

It's fascinating how periodic these prices are. I wonder why this is the case...

Well, that's all for now. See you next time!

Thursday, February 28, 2013

I've Been Plotting...

...to plot some plots! In Matlab!

Hello again. As alluded to in last week's post, I've been researching the stock market and its indices this week. In hopes of analyzing trends in index prices that might aid me in predicting their future, I've downloaded historical data for indices such as the Dow Jones Industrial Average and the Nasdaq Composite.

Also as promised, I've reunited with Matlab. Beyond programming and matrices, Matlab also has significant graphing and graph analysis capabilities. By converting the data I'd downloaded into text files, I was able to upload them into Matlab, where they could be graphed. Read ahead, and I'll show you how using Nasdaq Composite data as an example. :)

Before that, I'd like to start with a little background about the Nasdaq Composite. One of the most highly followed stock market indices in the United States, the Nasdaq Composite functions as an indicator of the performance of technology and growth companies. It includes common stocks and similar securities that appear on the NASDAQ stock market, amounting to over 3000 components. Its ticker symbol is IXIC.

Right then. Here's how I loaded its data into Matlab:


>> cd nasdaq
>> ls

.           ..          NASDAQ.txt

>> data=load('nasdaq.txt');

The historical data for the Nasdaq Composite appears in Matlab as a 248x1 matrix. I can then graph this data against time, t, with single day intervals, like so:


>> t=(1:248);
>> plot(t,data)
>> grid

and get the following figure:



Looking at the graph, it appears that Nasdaq Composite prices vary in a somewhat wavelike manner. Therefore, it might be analyzed as such. More about that in the next post!


Saturday, February 23, 2013

Darling, Let's Not See Each Other For A While


As you've probably noticed, I've spent the last several weeks working quite extensively with Matlab. In fact, I've been spending so much time with Matlab that I think the flames of our once passionate love have simmered and died away! Really, it's probably a good idea for Matlab and me to take a break from each other.

Don't get me wrong; we had a great run. I have learned many useful things and have had great fun playing around in Matlab, as evidenced by the previous post. It's just that I want to take a small break from the programming and mathematics to touch upon other subjects relevant to this project. And fear not! Matlab and I will surely be reunited. After all, I will be using him a lot more in the (near!) future. This separation is merely temporary. 

So what have I been doing during this brief hiatus? Well, I thought it would be a good idea to learn more about the data I will be analyzing. In other words, I’ve been reading a lot about the stock market and how it works. Though this knowledge may not be directly relevant to the analysis I’ll be doing, since that will mainly involve spotting and modeling trends, I do think it is worthwhile to have. Why not try to understand what the things I want to analyze actually are?

All right, that’s all I have for today.

See ya next week!

Thursday, February 21, 2013

Making Magic, Part Two

Hey! Lu here. Ready to pick up where we left off last week.

After long and grueling hours laboring alongside my father, I was able to devise a new program that can generate a 3x3 magic matrix in Matlab. Alright! Yay! This working program differs a bit from the failed one last week in approach, but more on that later. The real good news is that after the last several weeks, I'm now comfortable working in Matlab and have familiarized myself with the basic commands and structures I need to program in it. Why is this good news? Weelll, now I'm ready to move into the next part of my project! In the near future, I anticipate actually working with stock market prices. Then I'll be one step closer to creating my final model. So hold on tight everyone. The ride's only beginning. Whoo hoo!

Anyway, back to business. (Haha, see what I did there? Not funny? Ok.) As previously mentioned, this week's program doesn't start quite like last week's attempt. Instead of searching for any 3 non-repeating row vectors adding to 15 and generating a matrix, this program first lists the set of all combinations of 3 numbers between 1 and 9 that add to 15. Like so:


















From that bit of code, I get the following:


mat =

     1     5     9
     1     6     8
     2     4     9
     2     5     8
     2     6     7
     3     4     8
     3     5     7
     4     5     6

Then, the program finds how many times each digit appears in the set of combinations:


















generating:


rept =

     2     3     2     3     4     3     2     3     2

Now, you may be wondering why it's important to find out how many times each digit appears in the set of combinations adding to 15. Take a look at the magic matrix again:



Notice how the middle number must add in 4 ways in combination with other numbers to 15? Up, down, and twice diagonally. Now look at the corner numbers. They all have to add to 15 in 3 ways, right? By finding out how many times a digit appears in the set of combinations summing to 15, we can figure out which numbers have to go where in the magic matrix!

...Which is exactly what this program does:


















and I get this matix:


mym =

     2     9     4
     7     5     3
     6     1     8


Now just to check. Do the rows, columns, and diagonals all add to fifteen? Yep, they sure do.

Tadaa!

Saturday, February 16, 2013

Making Magic, Part One

Hello all!

You might be wondering why I'm back so soon. No, it has nothing to do with the fact that I'm supposed to blog twice a week...Nope! Nothing at all. It's because I have some interesting developments to share! Read ahead to find out because I know you're simply dying to know. ;)

As I promised in the last post, I've been working on making the 3x3 magic square from scratch in Matlab. Matlab has this cool feature that allows you to write and save your own functions and programs as ".m" files that you can then run within the Matlab command window. So, to make my magic square algorithm, I'm writing these ".m" files.Guiding me with this process is my dear old dad, the resident Matlab expert. Here's what we've got so far.

In a file I've meaningfully dubbed "akfasld.m," the following is found:

















I won't bore you with explanations of every line, but this is essentially a program that lists conditions for building a 3x3 matrix, "mat." Supposedly, akfasld should make a 3x3 magic matrix. Let's see what happens when I run it.

I get the following: mat =

     1     5     9
     2     6     7
     3     8     4

Hmm...
The rows all add up to 15 like they're supposed to, but what about the columns and diagonals? Oh snap. They don't add to 15 at all!

It appears I've managed to determine the row numbers that make the sum of each row 15, but I've neglected the orders in which the rows and the numbers within them should appear. Now I have to somehow shuffle the rows and their numbers around so that sums of the columns and diagonals equal the rows.

Err...I'll save that for next week.

Until then, have a nice weekend!

Friday, February 15, 2013

A Little Bit of Magic

Hi everyone! It's Lu again, back with another update.

Today, I'm going to talk a little about a piece of mathematical "magic."
Ladies and gentlemen, let me introduce...the 3x3 magic square.

Magic square of order 3.
Take a close look. What do each of the rows add up to? What about the columns? And the diagonals? They all add to 15! How magical.

An n-by-n magic square is defined as a matrix containing the integers from 1 to n^2 where each row, column, and the two diagonals all have the same sum. This sum, called the magic constant (M), is determined solely by n by this formula: M = \frac{n(n^2+1)}{2}. For the 3x3 square it is 15.

In Matlab, the command "magic(n)" generates a magic matrix when "n" is a scalar larger than or equal to 3.

For example,


>> X=magic(3)

X =

     8     1     6
     3     5     7
     4     9     2

To verify that X is indeed a magic square, I check the sums of its rows, columns, and diagonal:

>> sum(X)

ans =

    15    15    15

>> sum(X')'

ans =

    15
    15
    15

>> sum(diag(X))

ans =

    15

Tada! We have a magic square of order 3!

But how are such magic squares constructed? 
In the spirit of learning the in-and-outs of Matlab, I've been attempting to write an original program that constructs the 3x3 magic square. 

Be back with details on that soon!

Saturday, February 9, 2013

Programming Is What's Playing on TV, Right?

...Wait, you mean computer programming? Oh...



Well, I'll be the first to admit I didn't know a thing about programming. But, as it's necessary to know the basics of programming to get to know Matlab and ultimately use it to create predictive models, I decided to poke my head out of my shell of ignorance and learn a little about programming. 

So what is programming?

It's the process of scheduling shows on television.

Just kidding. We're talking about computer programming. 

Computer programming is the process of designing source codes (instructions) that tell a computer what to do. The source code is commonly written in some programming language. There are several types of programming languages. Matlab is an example of a high-level language. This means that programming in Matlab features a lot more abstraction from the machine language of the computer. High-level languages such as Matlab are easier to learn and use as they use more natural language elements and automate processes coded in low-level languages.

Here's an example of basic programming in Matlab:



As you can see, the Matlab language is not so far off from our own. Here, let me break it down, starting from the first line. 

"Assume x is a random 1x1 matrix between 0 and 1.
If x is greater than 5,
let y equal 1.
Or if x is less than 5,
let y equal 2.
Or if x equals 5,
let y equal 3.
That's all!

Pick an x value.
x equals 0.6068
So y equals 2."

Not too hard to understand, right?

Now compare that with an example of low-level programming language:

8B542408 83FA0077 06B80000 0000C383
FA027706 B8010000 00C353BB 01000000
B9010000 008D0419 83FA0376 078BD98B
C84AEBF1 5BC3


 .....What is that ugly mess? Boy, I'm sure glad I don't have to deal with that!

See you next time!

Thursday, February 7, 2013

A Lunch Date with Mr. Matlab

Hello all! It's been a little while. Here's an update on what I've been up to as of late.

Today I had a chicken wrap for lunch with -- you guessed it -- Matlab. Though the food was delicious, I admit the conversation was a little lacking. You see, Matlab is not a suave gentleman but a interactive computations and graphics program. Matlab's name stands for "matrix laboratory," and so, unsurprisingly, is especially proficient at matrix computations and manipulations. I've been spending the last few days exploring the basics of Matlab and familiarizing myself with its operations. I've discovered Matlab is somewhat like a glorified calculator. Both are used to solve mathematical problems and can be programmed to do specific things.

True to its name, Matlab's basic data element is the matrix array. Essentially, it computes everything as if it were in or part of a matrix. Addtionally, it solves problems numerically, not symbolically, which differentiates it from other programs such as Maple or Mathematica. Understanding this, I can begin to see the unique ways of using Matlab to solve problems.

For example, suppose I wish to graph the function y=f(x). If I set x to be a vector with linearly spaced entries, it can be regarded as a sort of one-dimensional grid that y can be graphed on. To do this, I simply set the array "x," then create "y" as another array whose values are the function of x's values.
To illustrate:
If I type the following into the command window,


>> x = (0:.05:2*pi);
>> y = cos(x);
>> plot(x,y)

I get the graph,


Pretty cool, right?


"Hold up, Lu!" you might be thinking. " What happened to all that whole spiel about predicting the future?" But don't worry. I haven't forgotten about about my hopes of predicting the stock market. Rather, before I attempt any such forecasts, I must first understand the program I'll be using to make predictive models: Matlab. Patience, everyone. One step at a time.

Tuesday, January 22, 2013

The Mysterious First Post a.k.a. Welcome to My Blog

Predictions, prophesies, prognostications. Augury, forecast, divination. Using anything from crystal balls to scientific models, humans have been trying forever to predict the future. More often than not, they're unsuccessful, (although recent advancements in science and technology have garnered some promising results.) 

But that won't stop me from giving it a shot! Ladies and gentlemen, join me as I seek the secret of predicting the future. Follow me as I prepare to boldly go where no man few seventeen-year-old-Asian-American-teenagers have gone before: on a quest for an elusive model that will foretell changes in stock market index prices.




Have I piqued your interest? A little bit? Haha, good. Now, let's get down to the nitty gritty as I tell you a bit more about my project.



Research Question:
What is the relationship between financial market indices, and how can their current price trends be used to make a mathematical model that can predict future prices?

Evidence to Assist:
  1. Both the basics of stock markets, indices, and the Matlab program can be researched easily on the Internet. For example, letslearnfinance.com offers a variety of articles that explain basic financial concepts, and the “Matlab Tutorial and Learning Resources” page on mathworks.com will allow me to familiarize myself with Matlab.
  2. In high-frequency trading, many firms, such as Renaissance Technologies, use computerized complex algorithms to make trading decisions before humans can analyze the data themselves. However, such tools are not available and are too complex for the average person.
  3. The Matlab program has been used previously in relation to financial and stock markets. For example, Sebastian Musielak of Worcester Polytechnic Institute used Matlab and various mathematical techniques to evaluate different techniques for predicting the short term changes in stock prices for his college thesis.


Argument:
I hope to: 1) gain a greater understanding of the relationships between different stock market indices through Internet research, and 2) use Matlab and proper orthogonal decomposition to create an autoregressive moving-average model that will predict future prices. This will hopefully show that accessible mathematical software can be used by a home investor to make more accurate predictions in financial markets.

Bias:
As a potential finance major in college, I’ve always been interested in investment theory. Since my linear algebra and vector calculus class has been dealing extensively with matrices, I feel like now is an opportune time to use the basics I’ve learned in that class along with new techniques I will research to learn more about financial markets as well as possibly produce something of practical use. Though I know that the real predictive models companies use to determine their investment strategies are infinitely more complex than anything I can produce at the moment, I want to familiarize myself with the possibilities of predictive modeling, mathematical programs and models, and financial markets.

Counterargument:
It is possible I will be unable to devise a model that can predict the future prices of stock market indices with reasonable accuracy in the time I have.

Response:
Should this occur, I will instead research why my model could not work. Or, rather than attempting to predict future prices of indices, I could research what drives these indices up and down. Perhaps this research would help me determine why I could not devise a suitable model.        
    


....Phew. Did you get through all of that? If you managed to read everything above in one go, I'm pretty impressed.

Anyhow, thanks for stopping by. Hope you'll visit again.

Until next time! :)

~Lu