Ejemplo n.º 1
0
def plot_Clusters_HMM_time(Xdata, Ks_params):
    # this function takes the list of trials and computes the likelihoods
    # of every sample of every trial belonging to a cluster, sums them up and
    # then plots them

    Ntrials = len(Xdata)
    Nclasses = len(Ks_params)

    total_llresp = []
    for K_p_i in range(Nclasses):
        total_llresp.append([0])
    for trial_i in range(Ntrials):
        for K_p_i in range(Nclasses):  # For every cluster type
            #                print Xdata[trial_i].shape
            A = Ks_params[K_p_i][1]
            B = Ks_params[K_p_i][2]
            pi = Ks_params[K_p_i][0]
            alpha = HMMlf.get_alfa_matrix_log(A, B, pi, [Xdata[trial_i]])
            beta = HMMlf.get_beta_matrix_log(A, B, pi, [Xdata[trial_i]])
            ll_resp = HMMlf.get_gamma_matrix_log(alpha, beta)[0]
            #                print ll_resp[0].shape
            ll_resp = np.exp(ll_resp).T

            #    for nsample
            total_llresp[K_p_i] += ll_resp
#                total_llresp[K_p_i] = HMMlf.sum_logs([total_llresp[K_p_i],ll_resp])
    for K_p_i in range(Nclasses):
        total_llresp[K_p_i] = total_llresp[K_p_i] / Ntrials


#    total_llresp[0] = np.log (total_llresp[0])
#    total_llresp[1] = np.log (total_llresp[1])
# Now we have the sum of log responsabilities !!
# Product for chains !!

#    gl.plot([],total_llresp[0])
#    gl.plot([],total_llresp[1])

    for K_p_i in range(Nclasses):
        pi = Ks_params[K_p_i][0]
        NK = pi.size
        labels = []
        for k in range(NK):
            labels.append("Cluster " + str(k + 1))
        gl.plot_filled(
            [],
            total_llresp[K_p_i],
            labels=["Responsability HMM", "Time index", "Responsability"],
            legend=labels)
Ejemplo n.º 2
0
def plot_Clusters_HMM_time(Xdata,Ks_params):
    # this function takes the list of trials and computes the likelihoods
    # of every sample of every trial belonging to a cluster, sums them up and
    # then plots them

    Ntrials = len(Xdata)
    Nclasses = len (Ks_params)
    
    total_llresp = []
    for K_p_i in range(Nclasses): 
        total_llresp.append([0])
    for trial_i in range(Ntrials): 
            for K_p_i in range(Nclasses):   # For every cluster type
#                print Xdata[trial_i].shape
                A = Ks_params[K_p_i][1]
                B = Ks_params[K_p_i][2]
                pi = Ks_params[K_p_i][0]
                alpha = HMMlf.get_alfa_matrix_log(A,B,pi, [Xdata[trial_i]])
                beta = HMMlf.get_beta_matrix_log(A,B,pi, [Xdata[trial_i]])
                ll_resp =  HMMlf.get_gamma_matrix_log(alpha, beta)[0]
#                print ll_resp[0].shape
                ll_resp = np.exp(ll_resp).T
                
#    for nsample
                total_llresp[K_p_i] += ll_resp
#                total_llresp[K_p_i] = HMMlf.sum_logs([total_llresp[K_p_i],ll_resp])
    for K_p_i in range(Nclasses): 
        total_llresp[K_p_i] = total_llresp[K_p_i]/Ntrials

    
#    total_llresp[0] = np.log (total_llresp[0])
#    total_llresp[1] = np.log (total_llresp[1])
    # Now we have the sum of log responsabilities !!
    # Product for chains !! 
    
    
#    gl.plot([],total_llresp[0])
#    gl.plot([],total_llresp[1])

    
    for K_p_i in range(Nclasses): 
        pi = Ks_params[K_p_i][0]
        NK = pi.size
        labels = []
        for k in range(NK):
            labels.append("Cluster " + str(k +1))
        gl.plot_filled([],total_llresp[K_p_i], labels = ["Responsability HMM", "Time index","Responsability"], legend = labels)
Ejemplo n.º 3
0
        """
        gl.init_figure()
        nf = 0

    AxesStyle = "Normal - No xaxis - Ny:3"
    for i in range(days_plot):
        chain = Xdata[i]
        r = myEM.get_responsibilities([chain],myDManager, theta_list[-1],model_theta_list[-1])
        if (myEM.clusters_relation == "independent"):
           resp = r
        elif(myEM.clusters_relation == "MarkovChain1"):
            resp = r[0]
        if (i == days_plot -1):
            AxesStyle = "Normal - Ny:3"
        ax1 = gl.plot_filled(Xdata_dates[i],resp , nf = nf, fill_mode = "stacked", legend = legend, 
                             sharex = ax1, sharey = ax1, AxesStyle = AxesStyle, labels = [labels_title,"","D(%i)"%i],
                         dataTransform  = dataTransform, step_mode = "yes")
        
        gl.set_fontSizes(ax = ax1, title = 20, xlabel = 20, ylabel = 10, 
                  legend = 20, xticks = 10, yticks = 10)
#        ax1 = gl.step(Xdata_dates[i],resp , nf = nf, fill= 1, legend = legend, 
#                             sharex = ax1, sharey = ax1, AxesStyle = "Normal", labels = [labels_title,"","%i"%i],
#                             dataTransform  = dataTransform, alpha = 0.5)
        
        gl.colorIndex = 0
        labels_title = ""

    gl.subplots_adjust(left=.09, bottom=.20, right=.90, top=.95, wspace=.2, hspace=0.001)
    image_name = "EM_1Symbol_timeAnalysis"+ clusters_relation+ "_"+str(periods[0])+ '.png'
    
    gl.savefig(folder_images + image_name, 
Ejemplo n.º 4
0
    ##### but fed directly, we need to still make such a transformation for this to work?

    gl.barchart(dates,
                dataHLOC,
                ax=ax1,
                color=color1,
                labels=[title, "", r"Price ($\$$)"],
                legend=["HLOC Price"])

    BBs = np.concatenate([BB1[:, [0]], BB1[:, [1]]], axis=1)
    gl.plot(dates, SMA1, color=colorBB1, legend=["0.95 BB(%i)" % nBB1])
    gl.plot(dates, BBs, color=colorBB1, ls="--")
    gl.plot_filled(
        dates,
        BBs,
        color=colorBB1,
        alpha=0.1,
        AxesStyle="Normal - No xaxis",
    )

    gl.plot(dates, BBs, color=colorBB1, ls="--")
    gl.plot_filled(
        dates,
        BBs,
        color=colorBB1,
        alpha=0.1,
        AxesStyle="Normal - No xaxis",
    )

    ##
    ax2 = gl.subplot2grid((5, 1), (4, 0), rowspan=1, colspan=1, sharex=ax1)
Ejemplo n.º 5
0
    SMA2_ATR = timeData.EMA(n=nCha2)

    ## Plotting just the ATR !!
    # TODO: Divide 3-1
    gl.set_subplots(3, 1)
    gl.plot(dates,
            price,
            nf=1,
            labels=["Volatility indicator Chainkin", "", "Price"],
            legend=["Price"])

    gl.plot(dates, SMA1_ATR, nf=0, color="k", legend=["Chainkin(%i)" % nATR1])
    gl.plot_filled(dates,
                   np.concatenate(
                       [SMA1_ATR + 2 * EMA_Range1, SMA1_ATR - 2 * EMA_Range1],
                       axis=1),
                   alpha=0.5,
                   nf=0,
                   color="k")

    gl.plot(dates, SMA2_ATR, nf=0, color="y", legend=["Chainkin(%i)" % nATR2])

    gl.plot_filled(dates,
                   np.concatenate(
                       [SMA2_ATR + 2 * EMA_Range2, SMA2_ATR - 2 * EMA_Range2],
                       axis=1),
                   alpha=0.5,
                   nf=0,
                   color="y")

    gl.plot(dates,
Ejemplo n.º 6
0
        #        r = myEM.get_alpha_responsibilities([chain],myDManager, theta_list[-1],model_theta_list[-1])
        if (myEM.clusters_relation == "independent"):
            resp = r
        elif (myEM.clusters_relation == "MarkovChain1"):
            resp = r[0]

        Nclusters = resp.shape[1]
        ax_ii = gl.subplot2grid((days_plot, Ndiv), (i, 0),
                                rowspan=1,
                                colspan=Ndiv - 1)
        ax1 = gl.plot_filled(Xdata_dates[i],
                             resp,
                             nf=0,
                             fill_mode="stacked",
                             legend=legend,
                             sharex=ax1,
                             sharey=ax1,
                             AxesStyle="Normal - No xaxis - No yaxis",
                             labels=[labels_title, "",
                                     "%i" % i],
                             dataTransform=dataTransform,
                             step_mode="yes")

        gl.colorIndex = 0
        labels_title = ""
        axes_l.append(ax_ii)

    gl.subplots_adjust(left=.09,
                       bottom=.20,
                       right=.90,
                       top=.95,
                       wspace=.2,
Ejemplo n.º 7
0
    ## Plotting t the BB !!
    ax1 = gl.subplot2grid((5,1), (0,0), rowspan=4, colspan=1)
#    gl.plot(dates, price, ax = ax1,  AxesStyle = "Normal - No xaxis",
#            labels = ["Volatility indicator BB", "", "Price"],
#            legend = ["Price"], color = "dark navy blue")
    title = "Bollinger Bands. " + str(symbols[0]) + "(" + ul.period_dic[timeData.period]+ ")" 
    gl.barchart(dates, dataHLOC, ax = ax1, color = color1,
            labels = [title, "", r"Price ($\$$)"],
            legend = ["HLOC Price"])
    
    BBs = np.concatenate([BB1[:,[0]],BB1[:,[1]]],axis = 1)
    gl.plot(dates, SMA1, color = colorBB1,
            legend = ["0.95 BB(%i)"%nBB1])
    gl.plot(dates, BBs, 
            color =  colorBB1, ls = "--")
    gl.plot_filled(dates, BBs, 
                    color =  colorBB1, alpha = 0.1,  AxesStyle = "Normal - No xaxis",)

    gl.plot(dates, BBs, 
                    color =  colorBB1, ls = "--")
    gl.plot_filled(dates, BBs,
                    color =  colorBB1, alpha = 0.1,  AxesStyle = "Normal - No xaxis",)
     
                   
    ## 
    ax2 = gl.subplot2grid((5,1), (4,0), rowspan=1, colspan=1, sharex = ax1)
    gl.plot(dates, BB1[:,0] - BB1[:,1] ,ax = ax2, color =  colorBB1,
            labels = ["","","STD"],  AxesStyle = "Normal - Ny:5",
            legend = ["BBstd(%i)"%nBB1], fill = 1, alpha = 0.5)
            
            
    gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.20, hspace=0)
Ejemplo n.º 8
0
    for i in range(Npeople_plot):
        if (myEM.clusters_relation == "independent"):
            resp = r[N * i:N * (i + 1), :]
        elif (myEM.clusters_relation == "MarkovChain1"):
            resp = r[i]

        Nclusters = resp.shape[1]
        ax_ii = gl.subplot2grid((Npeople_plot, Ndiv), (i, 0),
                                rowspan=1,
                                colspan=Ndiv - 1)
        ax1 = gl.plot_filled(time,
                             resp,
                             nf=0,
                             fill_mode="stacked",
                             legend=legend,
                             sharex=ax1,
                             sharey=ax1,
                             AxesStyle="Normal - No yaxis",
                             labels=[labels_title, "",
                                     "%i" % i])
        gl.colorIndex = 0
        labels_title = ""

    ax_i = gl.subplot2grid((Npeople_plot, Ndiv), (0, Ndiv - 1),
                           rowspan=Npeople_plot / 2,
                           colspan=1)
    for i in range(1, K + 1):
        gl.scatter(0,
                   i,
                   legend=[" K = %i" % (i)],
                   lw=28,
Ejemplo n.º 9
0
    Ndiv = 4
    
    axes_l = []
    for i in range(days_plot):
        chain = Xdata[i]
        r = myEM.get_responsibilities([chain],myDManager, theta_list[-1],model_theta_list[-1])
#        r = myEM.get_alpha_responsibilities([chain],myDManager, theta_list[-1],model_theta_list[-1])
        if (myEM.clusters_relation == "independent"):
           resp = r
        elif(myEM.clusters_relation == "MarkovChain1"):
            resp = r[0]
    
        Nclusters = resp.shape[1]
        ax_ii = gl.subplot2grid((days_plot,Ndiv), (i,0), rowspan=1, colspan=Ndiv-1) 
        ax1 = gl.plot_filled(Xdata_dates[i],resp , nf = 0, fill_mode = "stacked", legend = legend, 
                             sharex = ax1, sharey = ax1, AxesStyle = "Normal - No xaxis - No yaxis", labels = [labels_title,"","%i"%i],
                             dataTransform  = dataTransform, step_mode = "yes")
        
        gl.colorIndex = 0
        labels_title = ""
        axes_l.append(ax_ii)
    
    gl.subplots_adjust(left=.09, bottom=.20, right=.90, top=.95, wspace=.2, hspace=0.001)
    ax_i = gl.subplot2grid((days_plot,Ndiv), (0,Ndiv-1), rowspan=int(days_plot/2), colspan=1) 
    for i in range(1,K+1):
        gl.scatter(0, i, legend = [" K = %i"%(i)], lw = 28, AxesStyle = "Normal - No xaxis - No yaxis" , loc = "center left")
        
    gl.subplots_adjust(left=.09, bottom=.10, right=.90, top=.95, wspace=.2, hspace=0.01)
    image_name = "EM_%iSymbol_timeAnalysis_%iclusters"%(2,K)+ clusters_relation+ "_"+str(periods[0])+ '.png'
#    
    gl.set_fontSizes(ax = axes_l, title = 20, xlabel = 20, ylabel = 20, 
Ejemplo n.º 10
0
    for i in range(days_plot):
        chain = Xdata[i]
        r = myEM.get_responsibilities([chain], myDManager, theta_list[-1],
                                      model_theta_list[-1])
        if (myEM.clusters_relation == "independent"):
            resp = r
        elif (myEM.clusters_relation == "MarkovChain1"):
            resp = r[0]
        if (i == days_plot - 1):
            AxesStyle = "Normal - Ny:3"
        ax1 = gl.plot_filled(Xdata_dates[i],
                             resp,
                             nf=nf,
                             fill_mode="stacked",
                             legend=legend,
                             sharex=ax1,
                             sharey=ax1,
                             AxesStyle=AxesStyle,
                             labels=[labels_title, "",
                                     "D(%i)" % i],
                             dataTransform=dataTransform,
                             step_mode="yes")

        gl.set_fontSizes(ax=ax1,
                         title=20,
                         xlabel=20,
                         ylabel=10,
                         legend=20,
                         xticks=10,
                         yticks=10)
        #        ax1 = gl.step(Xdata_dates[i],resp , nf = nf, fill= 1, legend = legend,
        #                             sharex = ax1, sharey = ax1, AxesStyle = "Normal", labels = [labels_title,"","%i"%i],