Sunday, December 31, 2017

Random Movement On A Line

12/31/2017 - For New Years Eve I thought of a problem.

Lets say I took a point on a line. Then used a random number generator that picked a number between -3 and 3, and moved the point by that many unit. Run this option 1000 times and see where the point ends up.

Target: Model simple change.

Note: It may say blue line, but I changed them to Yellow due to the website background. Blue may actually be yellow.

Having it available, I decided to use the program "R Studio" for this problem.
First function to use is:

runif(n=1, min=-3, max=3)

The "runfi" is the statement? Function? Not sure what to call it. The stuff is parenthesis tells the function what to do. "n" is the number of times the function should run. In this case one time. The "min" and the "max" set the random number to come from between those two values. For example:


> runif(n=1, min=-3, max=3)
[1] -2.560463


Now I will try it 10 times:


> runif(n=10, min=-3, max=3)
 [1] -2.4503095  1.0310952  2.5430256
 [4]  2.9415187  2.4478618  2.9400206
 [7]  0.8676038  1.8867387  2.7229856
[10] -1.4708780

The program puts the random numbers into columns on its own. To put this into a single column, I have to set a matrix. First I have to make the list of random numbers. Then I will call the matrix NYE for New Years Eve.

> data =runif(n=10, min=-3, max=3)
> NYE=matrix(data, nrow=10)
> NYE
            [,1]
 [1,] -0.6741637
 [2,]  1.3053726
 [3,] -2.3259956
 [4,] -2.2537471
 [5,]  2.2671777
 [6,] -0.9219094
 [7,]  0.1321093
 [8,] -2.7951761
 [9,] -1.7896060
[10,]  2.0454334


The first blue line created 10 random numbers between -3 and 3. The second blue line put the 10 numbers into a table with the numbers in a single column. The third blue list requests to display that table.

Within R Studio, NYE is also shown as an object. I can double click this object and it will bring up a table.


Now in table form, I can copy the table and place it into excel.



I will try to explain the picture. The first two columns I took from R by copy and paste. The F column I started with zero, then for F2, I entered "=F1+B2" then I dragged down the cell down. This takes the original number in the F column, then adds the change from the B column to get a result. The next cell in the F column then adds the change from the next cell in the B column and so on ....

I then displayed a graph of the change. I'll do it again:


> data =runif(n=10, min=-3, max=3); NYE=matrix(data, nrow=10); NYE
            [,1]
 [1,]  0.9088863
 [2,] -0.4418274
 [3,]  0.4265573
 [4,]  1.9331647
 [5,]  1.2779913
 [6,] -0.5924471
 [7,] -2.0950772
 [8,]  0.5375038
 [9,] -2.1327807
[10,] -2.2383148

This is


I can just keep repeating, getting different changes, and a different graph each time. This gives us a simple model of change.

I'm not the first person to come up with such an idea. I think this may be called "Geometric Brownian Motion" in text books. I'd have to read more on the idea.

I would like to find out how to do this all in R, as I know the program should be able to do this function. This would cut out a lot of my excel copy and pasts, make the model easier, more efficient for testing.

I thought of this concept thinking about plant growth. For plant growth, change would not be negative, rather keep going up if everything is "normal." Normal being a condition where everything is the same. If a bug ate the plant, that would be a negative. If I added fertilizer, that could make the plant grow more. The bug and fertilizer example are not normal conditions.

Now I make some modifications:

data =runif(n=100, min=0, max=3); NYE=matrix(data, nrow=10); NYE
I changed the n to 100, to give me 100 random numbers. I changed the min to 0 so that I could have no negative numbers. I changed the nrow to 10 so that I would have 10 rows to make a 10 by 10 table. I tried to circle the areas of change in the image.



I was then able to copy the table to paste into excel.



In the excel table, I labeled the different imaginary plants P#, 1 through 10. The first table is the change, or random growth from 1 to 3. The second table is the additive growth for each time point. All things normal, the plants grew. Yet at the end, some plants have randomly grown more than others. Again, I would like to see this all done in the R program, but I wasn't willing to find out how to make it happen at this time. Note this is theory I guess, it is not actual measurements.

If you look at a corn field, or any field of a planted crop, many of the plants have the same height and size. If we were to measure them, some might be a little different. This randomness is sort of the same concept.

For this equation:

data =runif(n=100, min=0, max=3); NYE=matrix(data, nrow=10); NYE
You need to input the n, min, max. Then make sure the nrow matches the number of seeds you planted... that last part kind of complicated and hard to explain, but for why I mention it, doesn't matter.

Lets say you planted 100 corn seeds, this is your n. The variation in change would be your min and max.

To get the variation of change, you have to be clever. Perhaps you could go to a corn field and measure 100 plants that were planted 100 days ago. You find all plants are within 20 cm height of each other, the shortest is 100 cm and the tallest is 120 cm. Seems a plant grows, on average, from 1 to 1.2 cm per day in order to reach 100 or 120 cm in height after 100 days. So for the min you would have 1 and the max 1.2.


data =runif(n=10000, min=1, max=1.2); NYE=matrix(data, nrow=100); NYE

I ran the above code to try my 100 corn plants.



I zoomed to show after 100 days, my random growth corn plants ended up at 108.488 to 111.069. This is within about 3 cm from each other, based on my model. I was hoping to see some nearer to 100 or 120, but this does make sense. I just didn't have any plant continuously, randomly grow 1.2 cm every day. What I am looking at is kind of the bell curve I would guess.





The bell curve seems has 53 plants 110 or more, so I guess it was a good year for the corn. Hard to tell or explain what has happened.

From this I see the model still needs work. I think the concept of "Drift" is somewhere. Also my variation estimate didn't work out like I had expected.

To obtain better estimates for the model, I would have to try other values and compare to an actual. Then use it to see if I can estimate or predict a future crop. Here I have looked into a model, yet it needs to be better. I hope this opened the eyes to the potential power such a model can show. This isn't genius or new work, but for me without formal training on the concept, I have come to some conclusions and issues that others have probably accounted for in more complex models. Stuff like this can be used to predict crops, judge how much profit to be made, finances, stocks, and who knows what else. This is probably all elementary to mathematics degree graduates.

I went off topic. Back to a point moving on a line.

data =runif(n=1000, min=-3, max=3); NYE=matrix(data, nrow=1000)


I made 1000 random numbers between -3 and 3 for changes of the point. After 1000 of such random changes, starting at 0:



Above is the R program had my point on a line end at -79.4605.

I also tried this with Excel. Randbetween, using a similar method. Here are 2 results: 70 and 87.



Excel chose whole numbers. R would use fractions. The graphs look like lightning, lightning has some random properties in the code of nature perhaps.

I'd like to learn more about all this and how to get R to just spit out a number for me with less steps. That may be for another time.

Happy New Year!