Sunday, 7 February 2016

Simple Time Series Pattern Recognition Source Code

If you've read the posts about Bayesian Network, you are now ready to write your first pattern recognition system based on a Bayesian Network. Since I'm working with Java in these days, I prepared a simple Java version, let's see how it works. You can find the source code in the following page:

Simple Time Series Pattern Recognition Source Code

Terminology and setters
The main class which does the job is "SimpleTimeSeries". The terminology is that we have some raw time and value which are those you need the system learns their pattern. We also have model time and value; these are the parameters our Bayesian Network uses them as the nodes.

There are also some setter methods which set the crucial parameters of the system like maximum raw time (maxRawTime) which is the largest possible time value. For example, if your single period of data has 720 sample, you need to set it to 719 because we assume raw time for a single period starts from 0 and ends to 719. There is another setter for time series raw value (maxRawValue), if your series value goes up to 5000, set it to 5000.



The network's model parameters (maxMoldeTime, maxModelValue) also have their maximum values. The defaults are 23 for time and 9 for value, which means the time axis has divided to 24 and value axis has divided to 10 units.

Training methods
There ar two methods for training the network, here they are:

public SimpleTimeSeries train(int rawTimePoint, double rawValue) {...}
public SimpleTimeSeries train(double[] rawValuesOfAPeriod) {...}

The first one trains only a single sample data, and you need to pass it both raw time index and raw value while the second one teaches a whole period to our Bayesian Network, so you only need to pass it an array of raw values, the length of the array should be maxRawTime+1.

Note that since this is just a simple application to get the idea, we implemented the network with a two-dimensional array named "scores" to keep the scores of the network's edges.

Testing data
There is only one method to test the data, the name is "getProbability", it simply returns the probability of how much it is possible to have the given raw pair in the trained network. 

public double getProbability(int rawTime, double rawValue)

There is also another method which you can call it, and it returns the all available nodes in the network for that raw time, it is "getAllPossibleValues()".

Running the test
Look at the following picture we have trained the network with three waves with a shift in time.

Training data we feed to our Bayesian Network

Now if you check the "Test" class, you'll see that in the three for loop we have sent three sinusoidal waves with small time shift to our the network. And after that, we have tested some points, if a point matches with our model its corresponding probability should be more than 0 otherwise it does not comply the learned pattern and its probability will be zero. Now you can send your test data continuously and check if the given data belongs to the learned pattern or not.

Yes it learns 
In case of having any problem with the code or any doubt on if it works at all, just contact me by sending a message. But if you found it satisfactory, try changing the "myTimeSeriesFunction" and see how it learns the new behavior of the function.


No comments:

Post a comment