Search code examples
phpmathrandomgraphcurve

Generating a random curve


This may be more of a mathmatical problem rather than a direct programming issue.

I'm trying to create a curve, that simulates a market's interest in your product (I'm making a simulation game) in my script.

There are some criterias that has to be met

  • The curve has to be generated at random
  • The curve has 16 steps on the x axis where the curve is able to increase or decrease for each step
  • The curve has 100 steps on the y axis
  • The curve can start anywhere on the y axis but has to start at step 0 of x axis
  • The increments and decrements should be realistic, meaning they should increase or decrease slightly every step though wtih a smaller chance for larger / smaller increments/decrements to occur.

Consider the following image as a demonstration as to what I'm trying to achieveenter image description here

My original thought was that for each step I would increase or decrease it with a random number, the higher the current step would be the higher the chance of the curve falling would be, but there are some errors with that idea, like drastic change to the step.

I'm coding this is PHP, but the theory should apply to any programming language, any ideas as to what would be the best way to generate a random graph as shown above?

In the end, I only need the actual numbers for the graph so I can store the data. I was thinking a multi dimensional array, so that I could store multiple graphs in one array like so: (Disclaimer, the blow example, is not based on the graph)

$graph = array('Americas' => array(0, 15, 20, 33, 34, 38, 47, 52, 60, 65, 73, 23, 18, 12, 16, 18, 22));

Solution

  • What you probably want and what is routinely used in the description of markets is the geometric Brownian motion. In abstract terms it is defined by a stochastic differential equation

    dX(t)=X(t)*[ r*dt+s*dB(t) ]
    

    where r is the drift or average growth, s is the variance or volatility and dB(t) is a random variable following a normal distribution N(0,dt). One can discretize this for t=t0+k*Δt as

    X(t+Δt)=X(t)*[ 1+r*Δt±s*√(Δt) ]
    

    where the sign is chosen randomly with probability 0.5 in each direction. This works well for very small values of Δt. For larger steps use

    X(t+Δt)=X(t)*[ 1+r*Δt+s*z(t)*√(Δt) ]
    

    where the random variable z(t) follows a standard normal distribution.

    For more complicated methods see "Financial computations without agonizing pain"[1]

    [1]http://cs.uwaterloo.ca/~paforsyt/agon.pdf