Changeset 12


Ignore:
Timestamp:
07/17/15 17:29:05 (9 years ago)
Author:
tim
Message:

.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/graph

    r11 r12  
    105105    pass
    106106
    107 print('tsval diff mean: %f' % numpy.mean(differences(db, 'long', 'tsval')))
    108 print('tsval null diff mean: %f' % numpy.mean(null_differences(db, 'long', 'tsval')))
    109 print('tsval diff weighted mean: %f' % tsvalwmean(db.subseries('train','long')+db.subseries('test','long')))
    110 print('tsval null diff weighted mean: %f' % tsvalwmean(db.subseries('train_null','long')))
     107#print('tsval diff mean: %f' % numpy.mean(differences(db, 'long', 'tsval')))
     108#print('tsval null diff mean: %f' % numpy.mean(null_differences(db, 'long', 'tsval')))
     109#print('tsval diff weighted mean: %f' % tsvalwmean(db.subseries('train','long')+db.subseries('test','long')))
     110#print('tsval null diff weighted mean: %f' % tsvalwmean(db.subseries('train_null','long')))
     111
     112
     113def getTCPTSPrecision():
     114    cursor = db.conn.cursor()
     115    query="""SELECT tcpts_mean FROM meta;"""
     116    cursor.execute(query)
     117    row = cursor.fetchone()
     118    if row:
     119        return row[0]
     120    return None
     121
     122
     123def tsFilteredHistogram():
     124    tcpts_precision = getTCPTSPrecision()
     125   
     126    num_bins = 500
     127    all = db.subseries('train','long')+db.subseries('test','long')
     128    diffs     = [s['unusual_packet']-s['other_packet'] for s in all]
     129    ts0_diffs = [s['unusual_packet']-s['other_packet'] for s in all if s['unusual_tsval']-s['other_tsval'] == 0]
     130    ts1_diffs = [s['unusual_packet']-s['other_packet'] for s in all if abs(s['unusual_tsval']-s['other_tsval']) > 0]
     131    ts2_diffs = [s['unusual_packet']-s['other_packet'] for s in all if abs(round((s['unusual_tsval']-s['other_tsval'])/tcpts_precision)) <= 1.0]
     132
     133    ts_mode = statistics.mode([s['unusual_tsval'] for s in all]+[s['other_tsval'] for s in all])
     134    ts_diff_mode = statistics.mode([s['unusual_tsval']-s['other_tsval'] for s in all])
     135    ts_common_mode = [s['unusual_packet']-s['other_packet'] for s in all if s['unusual_tsval']<=ts_mode and s['other_tsval']<=ts_mode]
     136    ts_common_diff_mode = [s['unusual_packet']-s['other_packet'] for s in all if s['unusual_tsval']-s['other_tsval']==ts_diff_mode]
     137
     138    print('packet_rtt diff quadsummary: %f' % quadsummary(diffs))
     139    print('packet_rtt tsval diff=0 quadsummary: %f' % quadsummary(ts0_diffs))
     140    print('packet_rtt tsval diff>0 quadsummary: %f' % quadsummary(ts1_diffs))
     141    print('packet_rtt tsval diff<=1 quadsummary: %f' % quadsummary(ts2_diffs))
     142    print('packet_rtt tsval mode quadsummary: %f' % quadsummary(ts_common_mode))
     143    print(len(diffs), len(ts0_diffs)+len(ts1_diffs))
     144    diffs.sort()
     145    cut_off_low = diffs[int(len(diffs)*0.005)]
     146    cut_off_high = diffs[int(len(diffs)*0.995)]
     147
     148    plt.clf()
     149    # the histogram of the data
     150    n, bins, patches = plt.hist(diffs, num_bins, normed=0, color='black', histtype='step', alpha=0.8,
     151                                range=(cut_off_low,cut_off_high), label='all')
     152    n, bins, patches = plt.hist(ts0_diffs, num_bins, normed=0, color='blue', histtype='step', alpha=0.8,
     153                                range=(cut_off_low,cut_off_high), label='tsval diff=0')
     154    n, bins, patches = plt.hist(ts1_diffs, num_bins, normed=0, color='red', histtype='step', alpha=0.8,
     155                                range=(cut_off_low,cut_off_high), label='tsval diff>0')
     156    n, bins, patches = plt.hist(ts2_diffs, num_bins, normed=0, color='orange', histtype='step', alpha=0.8,
     157                                range=(cut_off_low,cut_off_high), label='tsval diff<=1')
     158    #n, bins, patches = plt.hist(ts_common_mode, num_bins, normed=0, color='green', histtype='step', alpha=0.8,
     159    #                            range=(cut_off_low,cut_off_high), label='tsval common mode')
     160    n, bins, patches = plt.hist(ts_common_diff_mode, num_bins, normed=0, color='green', histtype='step', alpha=0.8,
     161                                range=(cut_off_low,cut_off_high), label='tsval common diff mode')
     162    plt.xlabel('RTT Difference')
     163    plt.ylabel('Probability')
     164    plt.title(r'Histogram - distribution of differences by tsval')
     165
     166    # Tweak spacing to prevent clipping of ylabel
     167    plt.subplots_adjust(left=0.15)
     168    plt.legend()
     169    plt.show()
     170    #plt.savefig('paper/graphs/dists-vs-dist-of-diffs2.svg')
     171
     172tsFilteredHistogram()
     173sys.exit(0)
     174
     175
     176from pykalman import KalmanFilter
     177#kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]])
     178kf = KalmanFilter(transition_matrices = [[1, 0], [0, 1]], n_dim_obs=2, observation_matrices = [[1.0, 0], [0, 1.0]])
     179
     180#delta = 1e-5
     181#trans_cov = delta / (1 - delta) * np.eye(2)
     182
     183#kf = KalmanFilter(n_dim_obs=2, n_dim_state=2,
     184#                  initial_state_mean=np.zeros(2),
     185#                  initial_state_covariance=np.ones((2, 2)),
     186#                  transition_matrices=np.eye(2),
     187#                  observation_matrices=obs_mat,
     188#                  observation_covariance=1.0,
     189#                  transition_covariance=trans_cov)
     190
     191
     192#measurements = numpy.asarray([[1,0], [0,0], [0,1]])  # 3 observations
     193measurements = numpy.asarray([(s['unusual_packet'],s['other_packet']) for s in (db.subseries('train','long')+db.subseries('test','long'))])
     194kf = kf.em(measurements, n_iter=5)
     195#(filtered_state_means, filtered_state_covariances) = kf.filter(measurements)
     196#print("packet_rtt pykalman:", filtered_state_means[-1][0]-filtered_state_means[-1][1])
     197#print("packet_rtt pykalman:", filtered_state_means[-1])
     198
     199(smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurements)
     200#up = numpy.mean([m[0] for m in smoothed_state_means])
     201#op = numpy.mean([m[1] for m in smoothed_state_means])
     202print("packet_rtt pykalman:", smoothed_state_means[-1], smoothed_state_means[-1][0]-smoothed_state_means[-1][1])
     203#print("packet_rtt pykalman:", up, op, up-op)
    111204
    112205
Note: See TracChangeset for help on using the changeset viewer.