Making predictions about the future can be fun.
If you have some data to work with it can be fun
to fit some curves to the data and then use those
curves to create a few points into the future.
J has some built in utilities that can be of use to make
predictions.
So be requiring statfns to be able to use the lsfit verb.
In order to look at the results it can be good to require plot
as well.
require'plot statfns'
In order to make it easy to call the verb I create one that
takes both left and right arguments and call it spa.
spa=: 4 : 0
starts the editor to get in new lines to a verb with arguments
on both sides.
The right argument will be containing the data for the verb.
I take the number of numbers coming in and create x1 to contain
a list of x values for those data values.
x1=. i.#y
I prepare the right argument for lsfit and create dat
dat=. x1,:y
The left agument of spa will give me the number of points I
want to extend the curves and give me predictions and create
a new list x2 to hold all the x1 values and add extra numbers
at the end
x2=.x1,({:x1)+1+i.x
I am now ready to make predicions and call lsfit and store the
results in y1 to y5
y1=. (1 lsfit dat) p. x2
I want to create a plot and use the pd commands
pd 'new'
And then put some text on the plot and use symbol for
the original data
pd 'title Spa i framtidina'
pd 'backcolor lightgray'
pd 'type symbol'
It is now time to create the original plot
pd x1;y
Then use line for the curves
pd 'type line'
pd x2;y1,y2,y3,y4,:y5
And the I want to lok at what the plot looks like
pd 'show'
)
A right paranthesis at the end will en the verb and it is ready
to use
3 spa 1 2 3 4 2 5 2 11
Calls spa and asks for 3 points prediction and gives the list
1 2 3 4 2 5 2 11 as data
Hmmmmm.....
This plot is a bit crowded with all these curves at the same
time.
I use this verb and take copy and create a new verb spall
and add an extra parameter that allows me to get the curves
one at a time.
I will call spall with the same right argument but I add extra
argument to x on the left.
I use x3 to replace the original x and create a new x4 to choose
the plots.
x3=.>{.x
x4=.0+{.>}.x
I then use x4 in a new select and case section
select. x4
case. 1 do. pd x2;y1
So if x4 is one I get a plot with only the results from
1 lsfit and so on.
Calling spall like
(3;1) spall 1 2 3 4 2 5 2 11 55 22 11
will select that case
The other cases come similarly
(3;2) spall 1 2 3 4 2 5 2 11 55 22 11
(3;3) spall 1 2 3 4 2 5 2 11 55 22 11
(3;4) spall 1 2 3 4 2 5 2 11 55 22 11
(3;5) spall 1 2 3 4 2 5 2 11 55 22 11
(3;6) spall 1 2 3 4 2 5 2 11 55 22 11
If I want more points predicted
(13;1) spall 1 2 3 4 2 5 2 11 55 22 11
Would give me 13 points instead 3 in the earlier examples.
----------------------- all the script follows here
require'plot statfns'
spa=: 4 : 0
x1=. i.#y
dat=. x1,:y
x2=.x1,({:x1)+1+i.x
y1=. (1 lsfit dat) p. x2
y2=. (2 lsfit dat) p. x2
y3=. (3 lsfit dat) p. x2
y4=. (4 lsfit dat) p. x2
y5=. (5 lsfit dat) p. x2
pd 'new'
pd 'title Spa i framtidina'
pd 'backcolor lightgray'
pd 'type symbol'
pd x1;y
pd 'type line'
pd x2;y1,y2,y3,y4,:y5
pd 'show'
)
3 spa 1 2 3 4 2 5 2 11
spall=: 4 : 0
x3=.>{.x
x4=.0+{.>}.x
x1=. i.#y
dat=. x1,:y
x2=.x1,({:x1)+1+i.x3
y1=. (1 lsfit dat) p. x2
y2=. (2 lsfit dat) p. x2
y3=. (3 lsfit dat) p. x2
y4=. (4 lsfit dat) p. x2
y5=. (5 lsfit dat) p. x2
pd 'new'
pd 'title Spa i framtidina'
pd 'backcolor lightgray'
pd 'type symbol'
pd x1;y
pd 'type line'
select. x4
case. 1 do. pd x2;y1
case. 2 do. pd x2;y2
case. 3 do. pd x2;y3
case. 4 do. pd x2;y4
case. 5 do. pd x2;y5
case. 6 do. pd x2;y1,y2,y3,y4,:y5
end.
pd 'show'
)
(3;1) spall 1 2 3 4 2 5 2 11 55 22 11
(3;2) spall 1 2 3 4 2 5 2 11 55 22 11
(3;3) spall 1 2 3 4 2 5 2 11 55 22 11
(3;4) spall 1 2 3 4 2 5 2 11 55 22 11
(3;5) spall 1 2 3 4 2 5 2 11 55 22 11
(3;6) spall 1 2 3 4 2 5 2 11 55 22 11
(13;1) spall 1 2 3 4 2 5 2 11 55 22 11