Stitching the pieces together

While the sine function has so far given us the most amount of control, there are properties of all those functions that would be nice to incorporate into our stream of price data. What we will aim for is to randomly include data samples from the sine, polynomial, and oscillating stochastic functions.

To do this, we'll need a way to mark the start and end of each cycle or phase of a graph. This at least applies to the polynomial and sine functions, and not to the oscillating stochastic one. In order to find the phase of the first two functions, we'll need to determine the x intercepts of each one. There's a mathematical way of determining both. However, we'll use a different algorithmic approach that employs Clojure's data traversal mechanisms. We can also randomize phase entry points and the sample length taken from each phase.

Since we're using these math functions to mimic the behavior of real stock graphs, we'll want to randomize a few other inputs to each graph.

We can randomize the vertical dilation of both the sine and polynomial graphs. For the sine wave, this is done by adjusting the value of the amplitude (or a) of y = a sin (b(x − Pi/2)) + d. A larger amplitude increases the vertical dilation. For the polynomial, the vertical (and, in some cases, horizontal) dilation are controlled by the a variable. However, in this case, larger values result in smaller curves. For example, (0.5 * x^3) + (b * x^2) - (c * x) yields a larger curve than (2 * x^3) + (b * x^2) - (c * x).

We randomize the horizontal dilation of both graphs. The sine wave's horizontal dilation is controlled by the b variable in y = a sin (b(x − Pi/2)) + d. The larger the absolute value of b, the tighter each cycle is, that is, 2 yields tighter cycles than 1, and -2 yields tighter cycles than -1. The polynomial wave's horizontal (and, in some cases, vertical) dilation is controlled by its b variable in (0.5 * x^3) + (b * x^2) - (c * x).

You may remember how taking y values at an increment of 1 of each whole number gave a very coarse view of a curve. When we zoomed into the steps at each 1/10th increment, we saw a much more granular view of this curve. Accordingly, we can randomize the granularity (or zoom level) of points on both curves.

Finally, we need a way to randomly generate either of our three graph functions with a given set of inputs. Clojure lets us randomly select integers, so we can take a random integer within a range, determine if it's within a subrange, then sample (that is, generate) a graph from it. However, math also gives us useful tools. In this case, a probability distribution can help us evenly distribute graph generation according to inputs for a given probability curve. There are a number of probability (or continuous) distributions available (normal, exponential, beta, gamma, and so on). A graphic from Apache Commons math library gives an overview of some probability distributions, which can be seen at https://en.wikipedia.org/wiki/Probability_distribution:

Stitching the pieces together

Source: http://commons.apache.org/proper/commons-math/images/userguide/real_distribution_examples.png

We're going to use a beta distribution for our function. Try out an online beta distribution calculator at http://keisan.casio.com/exec/system/1180573226. Plug in the initial values of a=2 and b=3 and your result. The idea is that if we take a sample of 100 items, the likelihood that a value will be under 0.33 is determined by the shape of the curve. Therefore, there is a likelihood that a sample will be between 0.34 and 0.66 or above 0.67. We're going to use these very metrics to make online decisions of which price graphs get generated. By online, I mean we do not have to hardcode our sections between 0 and 1 [for example, 0.66 and 0.86 (0.20); the remainder will be (0.14)]. We can evenly divide an area between 0 and 1, and if the hump of our probability curve is skewed to the left-hand side of the graph, there's a greater probability that sampled values will fall under this area of the graph.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.205.99