Random Walk Stochastic process example
This will be yet another Stochastic process example when it comes to working out some basic and maybe sometime not so basic examples of such a process in statistics, this time on a random walk which is an easy typical getting started type example.
The process of making this kind of random process is simple enough, it will typically include a module what will be used to create a grid, another to create a collection of objects in the grid, and a walk method that will be used to get delta values for an object. However maybe the main method to write about when it comes to this kind of project, and the central topic of the post would be the walk method. When it comes to having an object at a given point in a grid there is taking the next step in one of several directions, there is taking a completely random direction, and then there is taking the same direction over and over again.
1 - Working out a walk method
The first step here is to work out a simple walk method. This is a method where each time I call it, the method will return a set od delta values for x and y that will move an object in a grid in one of up to 8 possible directions. The default walk method will be built in and return a random number in the set of numbers of [0,2,4,6] which will mean right, down, left, and up. When calling the walk method I can give and object that is the object that is to move, along with another walk method, and a custom set of directions other than the built in default.
|
|
2 - A canvas example of this
Now that I have a walk method worked out I am going to want to create some additional modules, and scripts around it to make use of it. I could make some kind of command line example of it using nodejs, but I generally like to make canvas examples first and foremost with this sort of thing.
This example will contain a grid, and an object pool, I will then be hacking over the walk method a little to get it to work better with these other modules. The end result should then be a half way decent canvas example of this walk module, and I can then start getting creative when it comes to making walk methods that make use of Math.random as well as many other expressions that are also stochastic rather than deterministic in nature.
2.1 - The Utils module for this canvas example
When I make a canvas example, or any kind of major javaScript project really, I like to have a general utility library. This is a place where I will place methods that I might use across more than one module in a project, and also methods that I might result across projects also.
|
|
2.2 - A Grid module
In this random process project I will want to have something that holds the state of a grid. This is the kind of module that I keep remaking over and over again, but never seem to get just right. So once Again I made this grid module from the ground up just for this example.
|
|
2.3 - The pool module
I am going to want to at least start something that will serve as a object pool module for this example. I wrote a post on this topic in general with my canvas example post on object pools where I got into this topic in detail. The general idea is to create a collection of objects just once, and then just reuse those object over and over again rather than creating and removing objects as needed. For this example I am just going to want at least one, but as many as a few objects for the sake of resting out my walk method.
|
|
2.4 - The Walk module now
Now to hack over the walk module a little to add some features that I might want with this canvas example.
|
|
2.5 - The draw module to render to a canvae element
This is a canvas example so I will want a module that will be used to render data in a main state object, to the canvas so I know what is going on. For now I have a draw background method, along with methods to draw the current state of the grid, and the pool of display objects that will be over the grid that are subject to called of the walk method in the min javaScript file of this example.
|
|
2.6 - The Main.js javaScript file
So then I have a utility library, a grid library, an object pool library, the walk method of course,and a draw module to render things to a canvas. Now I just need a little more javaScript that will make use of all of this to create a main state object, and update that state in a main app loop method that will use the walk method, and draw current status of the pool and grid to the canvas element.
|
|
3 - Conclusion
This turned out to be a nice little example of a random process, and so far I all ready have the basic idea that I wanted working. There are a lot of additional features that I would like to add when it comes to additional walk methods that are also random, and also maybe not so random actually.
This was a fun little project, and there is still a lot more that could be done when it comes to making the canvas example that I made a little more involved. However I have so many other projects that are in need of some attention from me, and this is just one of so many others. Still what I worked out here should maybe turn into a canvas example of mine, I have made a few projects that make use of a grid and they have objects in them that move around on top of or as part of that grid. So this sort of thing can carry over into other projects.