Time_series_prediction

Last updated: August 8th, 20202020-08-08Project preview
In [ ]:
!pip install networkx
!pip install linkpred
In [6]:
from itertools import combinations
import networkx as nx
import linkpred

def visibility_graph(series):

    g = nx.Graph()
    
    # convert list of magnitudes into list of tuples that hold the index
    tseries = []
    n = 0
    for magnitude in series:
        tseries.append( (n, magnitude ) )
        n += 1

    # contiguous time points always have visibility
    for n in range(0,len(tseries)-1):
        (ta, ya) = tseries[n]
        (tb, yb) = tseries[n+1]
        g.add_node(ta, mag=ya)
        g.add_node(tb, mag=yb)
        #g.add_edge(ta, tb)

    for a,b in combinations(tseries, 2):
        # two points, maybe connect
        (ta, ya) = a
        (tb, yb) = b

        connect = True
        
        # let's see all other points in the series
        for tc, yc in tseries:
            # other points, not a or b
            if tc > ta and tc != ta and tc < tb and tc != tb:
                # does c obstruct?
                if yc >= yb + (ya - yb) * ( (tb - tc) / (tb - ta) ):
                    connect = False
                    
        if connect and tb-ta > 1:
            g.add_edge(ta, tb)
            
    nx.write_pajek(g, "test.net")
    G = linkpred.read_network("test.net")
    simrank = linkpred.predictors.SimRank(G, excluded=G.edges())
    simrank_results = simrank.predict(c=0.5)
    #top = simrank_results.top()
    top = simrank_results
    print(top)

    for sim_node, sim_value in top.items():
        if int(max(sim_node)) == int(max(g.nodes())):
            break

    dx = int(max(sim_node)) - int(min(sim_node))
    dy = series[int(max(sim_node))] - series[int(min(sim_node))]
    print(dy/dx, max(sim_node), series[int(max(sim_node))])
    pred_init = dy/dx + series[int(max(sim_node))]
            
    return pred_init
In [ ]:
series = [20, 40, 48, 70, 40, 60, 40, 100, 40, 80]

init_pred = visibility_graph( series )
init_pred
In [9]:
series = [10, 20, 30, 40, 50, 60, 70, 80, 10, 80]

init_pred = visibility_graph( series )
init_pred
Scoresheet(<class 'float'>, {Pair('1', '0'): 0.05555539330131974, Pair('2', '0'): 0.05555539330131974, Pair('3', '0'): 0.05555539330131974, Pair('4', '0'): 0.05555539330131974, Pair('5', '0'): 0.05555539330131974, Pair('6', '0'): 0.05555539330131974, Pair('7', '0'): 0.09256967961123633, Pair('8', '0'): 0.05555539330131974, Pair('9', '0'): 0.09256967961123633, Pair('2', '1'): 0.05555539330131974, Pair('3', '1'): 0.05555539330131974, Pair('4', '1'): 0.05555539330131974, Pair('5', '1'): 0.05555539330131974, Pair('6', '1'): 0.05555539330131974, Pair('7', '1'): 0.09256967961123633, Pair('8', '1'): 0.05555539330131974, Pair('9', '1'): 0.09256967961123633, Pair('3', '2'): 0.05555539330131974, Pair('4', '2'): 0.05555539330131974, Pair('5', '2'): 0.05555539330131974, Pair('6', '2'): 0.05555539330131974, Pair('7', '2'): 0.09256967961123633, Pair('8', '2'): 0.05555539330131974, Pair('9', '2'): 0.09256967961123633, Pair('4', '3'): 0.05555539330131974, Pair('5', '3'): 0.05555539330131974, Pair('6', '3'): 0.05555539330131974, Pair('7', '3'): 0.09256967961123633, Pair('8', '3'): 0.05555539330131974, Pair('9', '3'): 0.09256967961123633, Pair('5', '4'): 0.05555539330131974, Pair('6', '4'): 0.05555539330131974, Pair('7', '4'): 0.09256967961123633, Pair('8', '4'): 0.05555539330131974, Pair('9', '4'): 0.09256967961123633, Pair('6', '5'): 0.05555539330131974, Pair('7', '5'): 0.09256967961123633, Pair('8', '5'): 0.05555539330131974, Pair('9', '5'): 0.09256967961123633, Pair('7', '6'): 0.09256967961123633, Pair('8', '6'): 0.05555539330131974, Pair('9', '6'): 0.09256967961123633, Pair('8', '7'): 0.09256967961123631, Pair('9', '8'): 0.09256967961123633})
7.777777777777778 9 80
Out[9]:
87.77777777777777
Notebooks AI
Notebooks AI Profile20060