[6] | 1 | #!/usr/bin/env python3 |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | import os |
---|
| 5 | import time |
---|
| 6 | import random |
---|
| 7 | import tempfile |
---|
| 8 | import argparse |
---|
| 9 | import socket |
---|
| 10 | import json |
---|
| 11 | |
---|
[10] | 12 | import numpy |
---|
[6] | 13 | import matplotlib.mlab as mlab |
---|
| 14 | import matplotlib.pyplot as plt |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | VERSION = "{DEVELOPMENT}" |
---|
| 18 | if VERSION == "{DEVELOPMENT}": |
---|
| 19 | script_dir = '.' |
---|
| 20 | try: |
---|
| 21 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
---|
| 22 | except: |
---|
| 23 | try: |
---|
| 24 | script_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
---|
| 25 | except: |
---|
| 26 | pass |
---|
| 27 | sys.path.append("%s/../lib" % script_dir) |
---|
| 28 | |
---|
| 29 | from nanownlib import * |
---|
| 30 | from nanownlib.stats import * |
---|
| 31 | import nanownlib.storage |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | parser = argparse.ArgumentParser( |
---|
| 35 | description="") |
---|
| 36 | parser.add_argument('db_file', default=None, |
---|
| 37 | help='') |
---|
| 38 | options = parser.parse_args() |
---|
| 39 | db = nanownlib.storage.db(options.db_file) |
---|
| 40 | |
---|
| 41 | |
---|
[11] | 42 | def differences(db, unusual_case, rtt_type='packet'): |
---|
| 43 | ret_val = [s['unusual_'+rtt_type]-s['other_'+rtt_type] for s in db.subseries('train', unusual_case)] |
---|
| 44 | ret_val += [s['unusual_'+rtt_type]-s['other_'+rtt_type] for s in db.subseries('test', unusual_case)] |
---|
| 45 | return ret_val |
---|
[6] | 46 | |
---|
[11] | 47 | def null_differences(db, unusual_case, rtt_type='packet'): |
---|
| 48 | ret_val = [s['unusual_'+rtt_type]-s['other_'+rtt_type] for s in db.subseries('train_null', unusual_case)] |
---|
| 49 | return ret_val |
---|
[6] | 50 | |
---|
[11] | 51 | |
---|
[6] | 52 | def timeSeries(db, probe_type, unusual_case): |
---|
| 53 | cursor = db.conn.cursor() |
---|
| 54 | query=""" |
---|
| 55 | SELECT time_of_day,packet_rtt AS uc,(SELECT avg(packet_rtt) FROM probes,analysis |
---|
| 56 | WHERE analysis.probe_id=probes.id AND probes.test_case!=:unusual_case AND probes.type=:probe_type AND sample=u.sample) AS oc |
---|
| 57 | FROM (SELECT time_of_day,probes.sample,packet_rtt FROM probes,analysis |
---|
| 58 | WHERE analysis.probe_id=probes.id AND probes.test_case =:unusual_case AND probes.type=:probe_type) u |
---|
| 59 | """ |
---|
| 60 | |
---|
| 61 | params = {"probe_type":probe_type,"unusual_case":unusual_case} |
---|
| 62 | cursor.execute(query, params) |
---|
| 63 | for row in cursor: |
---|
| 64 | yield {'time_of_day':row['time_of_day'],unusual_case:row['uc'],'other_cases':row['oc']} |
---|
| 65 | #samples,derived,null_derived = parse_data(input1) |
---|
| 66 | |
---|
| 67 | #trust = trustValues(derived, sum) |
---|
| 68 | #weights = linearWeights(derived, trust, 0.25) |
---|
| 69 | #print('(test): %f' % weightedMean(derived,weights)) |
---|
| 70 | |
---|
| 71 | diffs = list(differences(db, 'long')) |
---|
| 72 | reported_diffs = list(differences(db, 'long', 'reported')) |
---|
| 73 | #shorts = [s['packet_rtt'] for s in samples.values() if s['test_case']=='short'] |
---|
| 74 | #longs = [s['packet_rtt'] for s in samples.values() if s['test_case']=='long'] |
---|
| 75 | |
---|
| 76 | short_overtime = [(sample['time_of_day'],sample['short']) for sample in timeSeries(db,'train','short')] |
---|
| 77 | long_overtime = [(sample['time_of_day'],sample['long']) for sample in timeSeries(db,'train','long')] |
---|
| 78 | diff_overtime = [(sample['time_of_day'],sample['long']-sample['other_cases']) for sample in timeSeries(db,'train','long')] |
---|
| 79 | short_overtime.sort() |
---|
| 80 | long_overtime.sort() |
---|
| 81 | diff_overtime.sort() |
---|
| 82 | |
---|
[13] | 83 | print('packet_rtt diff mean: %f' % statistics.mean(diffs)) |
---|
[6] | 84 | print('packet_rtt diff median: %f' % statistics.median(diffs)) |
---|
[10] | 85 | print('packet_rtt diff midhinge: %f' % midsummary(diffs)) |
---|
[6] | 86 | print('packet_rtt diff trimean: %f' % trimean(diffs)) |
---|
[10] | 87 | print('packet_rtt diff quadsummary: %f' % quadsummary(diffs)) |
---|
| 88 | print('packet_rtt diff ubersummary: %f' % ubersummary(diffs)) |
---|
[13] | 89 | print('packet_rtt diff septasummary: %f' % septasummary(diffs)) |
---|
[6] | 90 | print('packet_rtt diff MAD: %f' % mad(diffs)) |
---|
[11] | 91 | try: |
---|
| 92 | print('reported diff trimean: %f' % trimean(reported_diffs)) |
---|
| 93 | print('reported diff quadsummary: %f' % quadsummary(reported_diffs)) |
---|
| 94 | print('reported diff ubersummary: %f' % ubersummary(reported_diffs)) |
---|
[13] | 95 | print('reported diff septasummary: %f' % septasummary(reported_diffs)) |
---|
[11] | 96 | print('reported diff MAD: %f' % mad(reported_diffs)) |
---|
[6] | 97 | |
---|
[13] | 98 | #import cProfile |
---|
| 99 | #start = time.time() |
---|
| 100 | #kresults = kfilter({},diffs) |
---|
[11] | 101 | #print('packet_rtt diff kfilter: ', numpy.mean(kresults['est']), kresults['var']) |
---|
[13] | 102 | #print('packet_rtt diff kfilter: ', kresults['est'][-1], kresults['var'][-1]) |
---|
| 103 | #kresults = kfilter({},reported_diffs) |
---|
[11] | 104 | #print('reported diff kfilter: ', numpy.mean(kresults['est']), kresults['var'][-1]) |
---|
[13] | 105 | #print('reported diff kfilter: ', kresults['est'][-1], kresults['var'][-1]) |
---|
| 106 | #print("kfilter time: %f" % (time.time()-start)) |
---|
[11] | 107 | except: |
---|
| 108 | pass |
---|
[6] | 109 | |
---|
[12] | 110 | #print('tsval diff mean: %f' % numpy.mean(differences(db, 'long', 'tsval'))) |
---|
| 111 | #print('tsval null diff mean: %f' % numpy.mean(null_differences(db, 'long', 'tsval'))) |
---|
| 112 | #print('tsval diff weighted mean: %f' % tsvalwmean(db.subseries('train','long')+db.subseries('test','long'))) |
---|
| 113 | #print('tsval null diff weighted mean: %f' % tsvalwmean(db.subseries('train_null','long'))) |
---|
[10] | 114 | |
---|
[11] | 115 | |
---|
[13] | 116 | |
---|
| 117 | |
---|
| 118 | def testKalman4D(params=None): |
---|
| 119 | from pykalman import KalmanFilter |
---|
| 120 | train = db.subseries('train','long', offset=0) |
---|
| 121 | test = db.subseries('test','long', offset=0) |
---|
| 122 | null = db.subseries('train_null','long', offset=0) |
---|
| 123 | measurements = numpy.asarray([(s['unusual_packet'],s['other_packet'],s['unusual_tsval'],s['other_tsval']) for s in (train+test)]) |
---|
| 124 | null_measurements = numpy.asarray([(s['unusual_packet'],s['other_packet'],s['unusual_tsval'],s['other_tsval']) for s in null]) |
---|
| 125 | |
---|
| 126 | if params == None: |
---|
| 127 | kf = KalmanFilter(n_dim_obs=4, n_dim_state=4, |
---|
| 128 | initial_state_mean=[quadsummary([s['unusual_packet'] for s in train]), |
---|
| 129 | quadsummary([s['other_packet'] for s in train]), |
---|
| 130 | numpy.mean([s['unusual_tsval'] for s in train]), |
---|
| 131 | numpy.mean([s['other_tsval'] for s in train])]) |
---|
| 132 | kf = KalmanFilter(n_dim_obs=4, n_dim_state=4) |
---|
| 133 | |
---|
| 134 | start=time.time() |
---|
| 135 | kf = kf.em(measurements[0:len(train)]+null_measurements[0:50000], n_iter=10, |
---|
| 136 | em_vars=('transition_matrices', |
---|
| 137 | 'observation_matrices', |
---|
| 138 | 'transition_offsets', |
---|
| 139 | 'observation_offsets', |
---|
| 140 | 'transition_covariance', |
---|
| 141 | 'observation_covariance', |
---|
| 142 | 'initial_state_mean', |
---|
| 143 | 'initial_state_covariance')) |
---|
| 144 | params = {'transition_matrices': kf.transition_matrices.tolist(), |
---|
| 145 | 'observation_matrices': kf.observation_matrices.tolist(), |
---|
| 146 | 'transition_offsets': kf.transition_offsets.tolist(), |
---|
| 147 | 'observation_offsets': kf.observation_offsets.tolist(), |
---|
| 148 | 'transition_covariance': kf.transition_covariance.tolist(), |
---|
| 149 | 'observation_covariance': kf.observation_covariance.tolist(), |
---|
| 150 | 'initial_state_mean': kf.initial_state_mean.tolist(), |
---|
| 151 | 'initial_state_covariance': kf.initial_state_covariance.tolist()} |
---|
| 152 | print("Learned Params:\n") |
---|
| 153 | import pprint |
---|
| 154 | pprint.pprint(params) |
---|
| 155 | print("pykalman em time: %f" % (time.time()-start)) |
---|
| 156 | |
---|
| 157 | #kf = KalmanFilter(n_dim_obs=2, n_dim_state=2, **params) |
---|
| 158 | |
---|
| 159 | num_obs=5000 |
---|
| 160 | for offset in range(50000,100000+num_obs,num_obs): |
---|
| 161 | start=time.time() |
---|
| 162 | m = measurements[offset:offset+num_obs] |
---|
| 163 | #params['initial_state_mean']=[quadsummary([s[0] for s in m]), |
---|
| 164 | # quadsummary([s[1] for s in m]), |
---|
| 165 | # numpy.mean([s[2] for s in m]), |
---|
| 166 | # numpy.mean([s[3] for s in m])] |
---|
| 167 | kf = KalmanFilter(n_dim_obs=4, n_dim_state=4, **params) |
---|
| 168 | (smoothed_state_means, smoothed_state_covariances) = kf.smooth(m) |
---|
| 169 | #print("pykalman smooth time: %f" % (time.time()-start)) |
---|
| 170 | up = numpy.mean([m[0] for m in smoothed_state_means]) |
---|
| 171 | op = numpy.mean([m[1] for m in smoothed_state_means]) |
---|
| 172 | #print("packet_rtt pykalman final:", smoothed_state_means[-1][0]-smoothed_state_means[-1][1]) |
---|
| 173 | print("packet_rtt pykalman mean:", up-op) |
---|
| 174 | print("packet_rtt mean:", numpy.mean([s[0]-s[1] for s in m])) |
---|
| 175 | #up = numpy.mean([m[2] for m in smoothed_state_means]) |
---|
| 176 | #op = numpy.mean([m[3] for m in smoothed_state_means]) |
---|
| 177 | #print("tsval_rtt pykalman final:", smoothed_state_means[-1][2]-smoothed_state_means[-1][3]) |
---|
| 178 | #print("tsval_rtt pykalman mean:", up-op) |
---|
| 179 | #print("tsval_rtt mean:", numpy.mean([s[2]-s[3] for s in m])) |
---|
| 180 | |
---|
| 181 | for offset in range(0,len(null_measurements)+num_obs,num_obs): |
---|
| 182 | start=time.time() |
---|
| 183 | m = null_measurements[offset:offset+num_obs] |
---|
| 184 | #params['initial_state_mean']=[quadsummary([s[0] for s in m]), |
---|
| 185 | # quadsummary([s[1] for s in m]), |
---|
| 186 | # numpy.mean([s[2] for s in m]), |
---|
| 187 | # numpy.mean([s[3] for s in m])] |
---|
| 188 | kf = KalmanFilter(n_dim_obs=4, n_dim_state=4, **params) |
---|
| 189 | (smoothed_state_means, smoothed_state_covariances) = kf.smooth(m) |
---|
| 190 | up = numpy.mean([m[0] for m in smoothed_state_means]) |
---|
| 191 | op = numpy.mean([m[1] for m in smoothed_state_means]) |
---|
| 192 | #print("null packet_rtt pykalman final:", smoothed_state_means[-1][0]-smoothed_state_means[-1][1]) |
---|
| 193 | print("null packet_rtt pykalman mean:", up-op) |
---|
| 194 | print("null packet_rtt mean:", numpy.mean([s[0]-s[1] for s in m])) |
---|
| 195 | #up = numpy.mean([m[2] for m in smoothed_state_means]) |
---|
| 196 | #op = numpy.mean([m[3] for m in smoothed_state_means]) |
---|
| 197 | #print("null tsval_rtt pykalman final:", smoothed_state_means[-1][2]-smoothed_state_means[-1][3]) |
---|
| 198 | #print("null tsval_rtt pykalman mean:", up-op) |
---|
| 199 | #print("null tsval_rtt mean:", numpy.mean([s[2]-s[3] for s in m])) |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | echo_vm_5k={'initial_state_covariance': [[33599047.5, |
---|
| 204 | -18251285.25, |
---|
| 205 | 3242535690.59375, |
---|
| 206 | -8560730487.84375], |
---|
| 207 | [-18251285.25, |
---|
| 208 | 9914252.3125, |
---|
| 209 | -1761372688.59375, |
---|
| 210 | 4650260880.1875], |
---|
| 211 | [3242535690.59375, |
---|
| 212 | -1761372688.59375, |
---|
| 213 | 312926663745.03125, |
---|
| 214 | -826168494791.7188], |
---|
| 215 | [-8560730487.84375, |
---|
| 216 | 4650260880.1875, |
---|
| 217 | -826168494791.7188, |
---|
| 218 | 2181195982530.4688]], |
---|
| 219 | 'initial_state_mean': [12939012.5625, |
---|
| 220 | 12934563.71875, |
---|
| 221 | 13134751.608, |
---|
| 222 | 13138990.9985], |
---|
| 223 | 'observation_covariance': [[11960180434.411114, |
---|
| 224 | 4760272534.795976, |
---|
| 225 | 8797551081.431936, |
---|
| 226 | 6908794128.927051], |
---|
| 227 | [4760272534.795962, |
---|
| 228 | 12383598172.428213, |
---|
| 229 | 5470747537.2599745, |
---|
| 230 | 11252625555.297853], |
---|
| 231 | [8797551081.431955, |
---|
| 232 | 5470747537.2601185, |
---|
| 233 | 1466222848395.7058, |
---|
| 234 | 72565713883.12643], |
---|
| 235 | [6908794128.927095, |
---|
| 236 | 11252625555.297981, |
---|
| 237 | 72565713883.12654, |
---|
| 238 | 1519760903943.507]], |
---|
| 239 | 'observation_matrices': [[1.4255288693095167, |
---|
| 240 | -0.4254638445329988, |
---|
| 241 | 0.0003406844036817347, |
---|
| 242 | -0.0005475021956726778], |
---|
| 243 | [-0.46467270827589857, |
---|
| 244 | 1.4654311778340343, |
---|
| 245 | -0.0003321330280128265, |
---|
| 246 | -0.0002853945703691352], |
---|
| 247 | [-0.2644570970067974, |
---|
| 248 | -0.33955835481495455, |
---|
| 249 | 1.7494161615202275, |
---|
| 250 | -0.15394117603733548], |
---|
| 251 | [-0.3419097544041847, |
---|
| 252 | -0.23992883666045373, |
---|
| 253 | -0.15587790880447727, |
---|
| 254 | 1.7292393175137022]], |
---|
| 255 | 'observation_offsets': [165.2279084503762, |
---|
| 256 | 157.76807691937614, |
---|
| 257 | 168.4235495099334, |
---|
| 258 | 225.33433430227353], |
---|
| 259 | 'transition_covariance': [[2515479496.145993, |
---|
| 260 | -401423541.70620924, |
---|
| 261 | 1409951418.1627903, |
---|
| 262 | 255932902.74454522], |
---|
| 263 | [-401423541.706214, |
---|
| 264 | 2744353887.676857, |
---|
| 265 | 1162316.2019491254, |
---|
| 266 | 1857251491.3987627], |
---|
| 267 | [1409951418.1628358, |
---|
| 268 | 1162316.2020361447, |
---|
| 269 | 543279068599.8229, |
---|
| 270 | -39399311190.5746], |
---|
| 271 | [255932902.74459982, |
---|
| 272 | 1857251491.398838, |
---|
| 273 | -39399311190.574585, |
---|
| 274 | 537826124257.5266]], |
---|
| 275 | 'transition_matrices': [[0.52163952865412, |
---|
| 276 | 0.47872618354122665, |
---|
| 277 | -0.0004322286766109684, |
---|
| 278 | 0.00017293351811531466], |
---|
| 279 | [0.5167436693545113, |
---|
| 280 | 0.48319044922845933, |
---|
| 281 | 7.765428142114672e-05, |
---|
| 282 | -0.00021518950285326355], |
---|
| 283 | [0.2091705950622469, |
---|
| 284 | 0.41051399729482796, |
---|
| 285 | 0.19341113299389256, |
---|
| 286 | 0.19562916616052917], |
---|
| 287 | [0.368592004009912, |
---|
| 288 | 0.22263632461118732, |
---|
| 289 | 0.20756792378812872, |
---|
| 290 | 0.20977025833570906]], |
---|
| 291 | 'transition_offsets': [592.5708159274, |
---|
| 292 | 583.3804671015271, |
---|
| 293 | 414.4187239098291, |
---|
| 294 | 562.166786712371]} |
---|
| 295 | |
---|
| 296 | echo_vm_5k={'initial_state_covariance': [[0.375, 0.0, 0.0, 0.0], |
---|
| 297 | [0.0, 0.375, 0.0, 0.0], |
---|
| 298 | [0.0, 0.0, 0.375, 0.0], |
---|
| 299 | [0.0, 0.0, 0.0, 0.375]], |
---|
| 300 | 'initial_state_mean': [15997944.198361743, |
---|
| 301 | 16029825.435899183, |
---|
| 302 | 17093077.26228404, |
---|
| 303 | 17524263.088803563], |
---|
| 304 | 'observation_covariance': [[36572556646.179054, |
---|
| 305 | 21816054953.37006, |
---|
| 306 | 31144379008.310543, |
---|
| 307 | 19651005729.823025], |
---|
| 308 | [21816054953.372543, |
---|
| 309 | 440428106325.20325, |
---|
| 310 | 41103447776.740585, |
---|
| 311 | 427146570672.51227], |
---|
| 312 | [31144379008.31037, |
---|
| 313 | 41103447776.74027, |
---|
| 314 | 3280009435458.6953, |
---|
| 315 | 458734528073.65686], |
---|
| 316 | [19651005729.82234, |
---|
| 317 | 427146570672.5109, |
---|
| 318 | 458734528073.6557, |
---|
| 319 | 3769493190697.773]], |
---|
| 320 | 'observation_matrices': [[1.0248853427592337, |
---|
| 321 | -0.031198859962501047, |
---|
| 322 | 0.001613706836380402, |
---|
| 323 | 0.004720209443291878], |
---|
| 324 | [-0.8604422900368718, |
---|
| 325 | 1.8583369609057172, |
---|
| 326 | -0.0022646214457040514, |
---|
| 327 | 0.004437933935378169], |
---|
| 328 | [-0.5814771409524866, |
---|
| 329 | 0.22228184387142846, |
---|
| 330 | 1.6259599749174072, |
---|
| 331 | -0.271594798325566], |
---|
| 332 | [-0.5862601003257453, |
---|
| 333 | 0.2598285939005791, |
---|
| 334 | -0.28286590143513024, |
---|
| 335 | 1.604087079832425]], |
---|
| 336 | 'observation_offsets': [1979.4518332096984, |
---|
| 337 | 1889.3380163762793, |
---|
| 338 | 2132.9112026744906, |
---|
| 339 | 1750.7759421584785], |
---|
| 340 | 'transition_covariance': [[6176492087.271547, |
---|
| 341 | 762254719.4171592, |
---|
| 342 | 4584288694.652873, |
---|
| 343 | 3044796192.4357214], |
---|
| 344 | [762254719.4185101, |
---|
| 345 | 173302376079.4761, |
---|
| 346 | 5261303152.757347, |
---|
| 347 | 167562483383.9925], |
---|
| 348 | [4584288694.651718, |
---|
| 349 | 5261303152.755746, |
---|
| 350 | 1056156956874.4131, |
---|
| 351 | -115859156952.07962], |
---|
| 352 | [3044796192.434162, |
---|
| 353 | 167562483383.9901, |
---|
| 354 | -115859156952.08018, |
---|
| 355 | 1225788436266.3086]], |
---|
| 356 | 'transition_matrices': [[0.9673912485796876, |
---|
| 357 | 0.03252962227543321, |
---|
| 358 | 0.0006756067792537124, |
---|
| 359 | -0.0006566638567164773], |
---|
| 360 | [0.9548761966068113, |
---|
| 361 | 0.03841774395880293, |
---|
| 362 | 0.00426067282319309, |
---|
| 363 | 0.002303362691861821], |
---|
| 364 | [0.6215040230859188, |
---|
| 365 | -0.2584476837756142, |
---|
| 366 | 0.3176491193420503, |
---|
| 367 | 0.3241682768126566], |
---|
| 368 | [0.6634028281470279, |
---|
| 369 | -0.33548335246018723, |
---|
| 370 | 0.3298144902195048, |
---|
| 371 | 0.3475836278392421]], |
---|
| 372 | 'transition_offsets': [1751.3049487348183, |
---|
| 373 | 1764.989515773476, |
---|
| 374 | 1986.8405778425586, |
---|
| 375 | 2232.830254345267]} |
---|
| 376 | #testKalman4D(echo_vm_5k) |
---|
| 377 | |
---|
| 378 | |
---|
| 379 | |
---|
| 380 | def testKalman(params=None): |
---|
| 381 | from pykalman import AdditiveUnscentedKalmanFilter,KalmanFilter |
---|
| 382 | train = db.subseries('train','long', offset=0) |
---|
| 383 | test = db.subseries('test','long', offset=0) |
---|
| 384 | measurements = numpy.asarray([(s['unusual_packet'],s['other_packet']) for s in (train+test)]) |
---|
| 385 | |
---|
| 386 | #kf = KalmanFilter(transition_matrices = [[1, 1], [0, 1]], observation_matrices = [[0.1, 0.5], [-0.3, 0.0]]) |
---|
| 387 | kf = KalmanFilter(n_dim_obs=2, n_dim_state=2, |
---|
| 388 | initial_state_mean=[quadsummary([s['unusual_packet'] for s in train]), |
---|
| 389 | quadsummary([s['other_packet'] for s in train])]) |
---|
| 390 | #kf = AdditiveUnscentedKalmanFilter(n_dim_obs=2, n_dim_state=2) |
---|
| 391 | |
---|
| 392 | if params == None: |
---|
| 393 | start=time.time() |
---|
| 394 | kf = kf.em(measurements[0:len(train)], n_iter=10, |
---|
| 395 | em_vars=('transition_matrices', |
---|
| 396 | 'observation_matrices', |
---|
| 397 | 'transition_offsets', |
---|
| 398 | 'observation_offsets', |
---|
| 399 | 'transition_covariance', |
---|
| 400 | 'observation_covariance', |
---|
| 401 | 'initial_state_covariance')) |
---|
| 402 | params = {'transition_matrices': kf.transition_matrices.tolist(), |
---|
| 403 | 'observation_matrices': kf.observation_matrices.tolist(), |
---|
| 404 | 'transition_offsets': kf.transition_offsets.tolist(), |
---|
| 405 | 'observation_offsets': kf.observation_offsets.tolist(), |
---|
| 406 | 'transition_covariance': kf.transition_covariance.tolist(), |
---|
| 407 | 'observation_covariance': kf.observation_covariance.tolist(), |
---|
| 408 | 'initial_state_mean': kf.initial_state_mean.tolist(), |
---|
| 409 | 'initial_state_covariance': kf.initial_state_covariance.tolist()} |
---|
| 410 | print("Learned Params:\n") |
---|
| 411 | import pprint |
---|
| 412 | pprint.pprint(params) |
---|
| 413 | print("pykalman em time: %f" % (time.time()-start)) |
---|
| 414 | |
---|
| 415 | #kf = KalmanFilter(n_dim_obs=2, n_dim_state=2, **params) |
---|
| 416 | |
---|
| 417 | num_obs=10000 |
---|
| 418 | for offset in range(50000,100000+num_obs,num_obs): |
---|
| 419 | start=time.time() |
---|
| 420 | kf = KalmanFilter(n_dim_obs=2, n_dim_state=2, **params) |
---|
| 421 | m = measurements[offset:offset+num_obs] |
---|
| 422 | (smoothed_state_means, smoothed_state_covariances) = kf.smooth(m) |
---|
| 423 | print("pykalman smooth time: %f" % (time.time()-start)) |
---|
| 424 | up = numpy.mean([m[0] for m in smoothed_state_means]) |
---|
| 425 | op = numpy.mean([m[1] for m in smoothed_state_means]) |
---|
| 426 | print("packet_rtt pykalman final:", smoothed_state_means[-1][0]-smoothed_state_means[-1][1]) |
---|
| 427 | print("packet_rtt pykalman mean:", up-op) |
---|
| 428 | print("packet_rtt mean:", numpy.mean([s[0]-s[1] for s in m])) |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | five_iter = {'observation_offsets': [-54.53185823, -55.25219184], |
---|
| 432 | 'observation_covariance': [[ 1.15059170e+10, 4.36743765e+09], |
---|
| 433 | [ 4.36743765e+09, 1.19410313e+10]], |
---|
| 434 | 'initial_state_mean': [ 12939012.5625 , 12934563.71875], |
---|
| 435 | 'transition_covariance': [[ 2.98594543e+09, 6.86355073e+07], |
---|
| 436 | [ 6.86355073e+07, 3.21368699e+09]], |
---|
| 437 | 'initial_state_covariance': [[ 2.36836696e+09, 1.63195635e+09], |
---|
| 438 | [ 1.63195635e+09, 1.12452233e+09]], |
---|
| 439 | 'transition_offsets': [ 343.69740217, 338.5042467 ], |
---|
| 440 | 'observation_matrices': [[ 1.42539895, -0.4255261 ], |
---|
| 441 | [-0.46280375, 1.46295189]], |
---|
| 442 | 'transition_matrices': [[ 0.56151623, 0.4385931 ], |
---|
| 443 | [ 0.47309189, 0.52673508]]} |
---|
| 444 | ten_iter = {'initial_state_covariance': [[229936928.28125, 41172601.0], |
---|
| 445 | [41172601.0, 7372383.46875]], |
---|
| 446 | 'initial_state_mean': [12939012.5625, 12934563.71875], |
---|
| 447 | 'observation_covariance': [[11958914107.88334, 4761048283.066559], |
---|
| 448 | [4761048283.066557, 12388186543.42032]], |
---|
| 449 | 'observation_matrices': [[1.4258395826727792, -0.42598392357467674], |
---|
| 450 | [-0.4647443890462455, 1.4648767294384015]], |
---|
| 451 | 'observation_offsets': [165.409715349344, 157.96206130876212], |
---|
| 452 | 'transition_covariance': [[2515594742.7187943, -401728959.41375697], |
---|
| 453 | [-401728959.41375697, 2743831805.402682]], |
---|
| 454 | 'transition_matrices': [[0.521306461057975, 0.47879632652984583], |
---|
| 455 | [0.5167881285851763, 0.483006520280469]], |
---|
| 456 | 'transition_offsets': [592.4419187566978, 583.2272403965366]} |
---|
| 457 | #testKalman(ten_iter) |
---|
| 458 | |
---|
| 459 | |
---|
[12] | 460 | def getTCPTSPrecision(): |
---|
| 461 | cursor = db.conn.cursor() |
---|
[13] | 462 | query="""SELECT tcpts_mean FROM meta""" |
---|
[12] | 463 | cursor.execute(query) |
---|
| 464 | row = cursor.fetchone() |
---|
| 465 | if row: |
---|
| 466 | return row[0] |
---|
| 467 | return None |
---|
| 468 | |
---|
| 469 | |
---|
| 470 | def tsFilteredHistogram(): |
---|
| 471 | tcpts_precision = getTCPTSPrecision() |
---|
| 472 | |
---|
| 473 | num_bins = 500 |
---|
| 474 | all = db.subseries('train','long')+db.subseries('test','long') |
---|
| 475 | diffs = [s['unusual_packet']-s['other_packet'] for s in all] |
---|
| 476 | ts0_diffs = [s['unusual_packet']-s['other_packet'] for s in all if s['unusual_tsval']-s['other_tsval'] == 0] |
---|
| 477 | ts1_diffs = [s['unusual_packet']-s['other_packet'] for s in all if abs(s['unusual_tsval']-s['other_tsval']) > 0] |
---|
| 478 | 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] |
---|
| 479 | |
---|
| 480 | ts_mode = statistics.mode([s['unusual_tsval'] for s in all]+[s['other_tsval'] for s in all]) |
---|
| 481 | ts_diff_mode = statistics.mode([s['unusual_tsval']-s['other_tsval'] for s in all]) |
---|
| 482 | 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] |
---|
| 483 | ts_common_diff_mode = [s['unusual_packet']-s['other_packet'] for s in all if s['unusual_tsval']-s['other_tsval']==ts_diff_mode] |
---|
| 484 | |
---|
| 485 | print('packet_rtt diff quadsummary: %f' % quadsummary(diffs)) |
---|
| 486 | print('packet_rtt tsval diff=0 quadsummary: %f' % quadsummary(ts0_diffs)) |
---|
| 487 | print('packet_rtt tsval diff>0 quadsummary: %f' % quadsummary(ts1_diffs)) |
---|
| 488 | print('packet_rtt tsval diff<=1 quadsummary: %f' % quadsummary(ts2_diffs)) |
---|
| 489 | print('packet_rtt tsval mode quadsummary: %f' % quadsummary(ts_common_mode)) |
---|
| 490 | print(len(diffs), len(ts0_diffs)+len(ts1_diffs)) |
---|
| 491 | diffs.sort() |
---|
| 492 | cut_off_low = diffs[int(len(diffs)*0.005)] |
---|
| 493 | cut_off_high = diffs[int(len(diffs)*0.995)] |
---|
| 494 | |
---|
| 495 | plt.clf() |
---|
| 496 | # the histogram of the data |
---|
| 497 | n, bins, patches = plt.hist(diffs, num_bins, normed=0, color='black', histtype='step', alpha=0.8, |
---|
| 498 | range=(cut_off_low,cut_off_high), label='all') |
---|
| 499 | n, bins, patches = plt.hist(ts0_diffs, num_bins, normed=0, color='blue', histtype='step', alpha=0.8, |
---|
| 500 | range=(cut_off_low,cut_off_high), label='tsval diff=0') |
---|
| 501 | n, bins, patches = plt.hist(ts1_diffs, num_bins, normed=0, color='red', histtype='step', alpha=0.8, |
---|
| 502 | range=(cut_off_low,cut_off_high), label='tsval diff>0') |
---|
| 503 | n, bins, patches = plt.hist(ts2_diffs, num_bins, normed=0, color='orange', histtype='step', alpha=0.8, |
---|
| 504 | range=(cut_off_low,cut_off_high), label='tsval diff<=1') |
---|
| 505 | #n, bins, patches = plt.hist(ts_common_mode, num_bins, normed=0, color='green', histtype='step', alpha=0.8, |
---|
| 506 | # range=(cut_off_low,cut_off_high), label='tsval common mode') |
---|
| 507 | n, bins, patches = plt.hist(ts_common_diff_mode, num_bins, normed=0, color='green', histtype='step', alpha=0.8, |
---|
| 508 | range=(cut_off_low,cut_off_high), label='tsval common diff mode') |
---|
| 509 | plt.xlabel('RTT Difference') |
---|
| 510 | plt.ylabel('Probability') |
---|
| 511 | plt.title(r'Histogram - distribution of differences by tsval') |
---|
| 512 | |
---|
| 513 | # Tweak spacing to prevent clipping of ylabel |
---|
| 514 | plt.subplots_adjust(left=0.15) |
---|
| 515 | plt.legend() |
---|
| 516 | plt.show() |
---|
| 517 | #plt.savefig('paper/graphs/dists-vs-dist-of-diffs2.svg') |
---|
| 518 | |
---|
[13] | 519 | #tsFilteredHistogram() |
---|
[12] | 520 | |
---|
| 521 | |
---|
| 522 | |
---|
| 523 | |
---|
| 524 | |
---|
[6] | 525 | #all_data = longs+shorts |
---|
| 526 | #all_data.sort() |
---|
| 527 | #cut_off_low = all_data[0] |
---|
| 528 | #cut_off_high = all_data[int(len(all_data)*0.997)] |
---|
| 529 | |
---|
| 530 | |
---|
[11] | 531 | def plotSingleProbe(probe_id=None): |
---|
| 532 | if probe_id == None: |
---|
| 533 | cursor = db.conn.cursor() |
---|
| 534 | query="""SELECT probe_id FROM analysis WHERE suspect='' ORDER BY probe_id DESC limit 1 OFFSET 10""" |
---|
| 535 | cursor.execute(query) |
---|
| 536 | probe_id = cursor.fetchone()[0] |
---|
| 537 | |
---|
| 538 | cursor = db.conn.cursor() |
---|
| 539 | query="""SELECT observed,payload_len FROM packets WHERE probe_id=? AND sent=1""" |
---|
| 540 | cursor.execute(query, (probe_id,)) |
---|
| 541 | pkts = cursor.fetchall() |
---|
| 542 | sent_payload = [row[0] for row in pkts if row[1] != 0] |
---|
| 543 | sent_other = [row[0] for row in pkts if row[1] == 0] |
---|
| 544 | |
---|
| 545 | query="""SELECT observed,payload_len FROM packets WHERE probe_id=? AND sent=0""" |
---|
| 546 | cursor.execute(query, (probe_id,)) |
---|
| 547 | pkts = cursor.fetchall() |
---|
| 548 | rcvd_payload = [row[0] for row in pkts if row[1] != 0] |
---|
| 549 | rcvd_other = [row[0] for row in pkts if row[1] == 0] |
---|
| 550 | |
---|
| 551 | #query="""SELECT reported,time_of_day FROM probes WHERE id=?""" |
---|
| 552 | #cursor.execute(query, (probe_id,)) |
---|
| 553 | #reported,tod = cursor.fetchone() |
---|
| 554 | #userspace_times = [sent_times[0]-reported/3.0, sent_times[0]+reported] |
---|
| 555 | |
---|
| 556 | print("single probe counts:",len(sent_payload),len(sent_other),len(rcvd_payload),len(rcvd_other)) |
---|
| 557 | plt.clf() |
---|
| 558 | plt.title("Single HTTP Request - Packet Times") |
---|
| 559 | sp = plt.eventplot(sent_payload, colors=('red',), lineoffsets=8, linewidths=2, alpha=0.6,label='sent') |
---|
| 560 | so = plt.eventplot(sent_other, colors=('red',), lineoffsets=6, linewidths=2, alpha=0.6,label='sent') |
---|
| 561 | rp = plt.eventplot(rcvd_payload, colors=('blue',), lineoffsets=4, linewidths=2, alpha=0.6,label='received') |
---|
| 562 | ro = plt.eventplot(rcvd_other, colors=('blue',), lineoffsets=2, linewidths=2, alpha=0.6,label='received') |
---|
| 563 | #plt.legend((s,r), ('sent','received')) |
---|
| 564 | #plt.savefig('../img/http-packet-times.svg') |
---|
| 565 | plt.show() |
---|
| 566 | |
---|
| 567 | #plotSingleProbe() |
---|
| 568 | |
---|
| 569 | |
---|
| 570 | def graphTestResults(): |
---|
| 571 | plt.clf() |
---|
| 572 | plt.title("Test Results") |
---|
| 573 | plt.xlabel('sample size') |
---|
| 574 | plt.ylabel('error rate') |
---|
| 575 | legend = [] |
---|
| 576 | colors = ['red','blue','green','purple','orange','black','brown'] |
---|
| 577 | color_id = 0 |
---|
| 578 | |
---|
| 579 | cursor = db.conn.cursor() |
---|
| 580 | query = """ |
---|
| 581 | SELECT classifier FROM classifier_results GROUP BY classifier ORDER BY classifier; |
---|
| 582 | """ |
---|
| 583 | cursor.execute(query) |
---|
| 584 | classifiers = [] |
---|
| 585 | for c in cursor: |
---|
| 586 | classifiers.append(c[0]) |
---|
| 587 | |
---|
[13] | 588 | max_obs = 0 |
---|
[11] | 589 | for classifier in classifiers: |
---|
| 590 | query=""" |
---|
| 591 | SELECT params FROM classifier_results |
---|
| 592 | WHERE trial_type='test' |
---|
| 593 | AND classifier=:classifier |
---|
| 594 | AND (false_positives+false_negatives)/2.0 < 5.0 |
---|
| 595 | ORDER BY num_observations,(false_positives+false_negatives) |
---|
| 596 | LIMIT 1 |
---|
| 597 | """ |
---|
| 598 | cursor.execute(query, {'classifier':classifier}) |
---|
| 599 | row = cursor.fetchone() |
---|
| 600 | if row == None: |
---|
| 601 | query=""" |
---|
| 602 | SELECT params FROM classifier_results |
---|
| 603 | WHERE trial_type='test' and classifier=:classifier |
---|
| 604 | ORDER BY (false_positives+false_negatives),num_observations |
---|
| 605 | LIMIT 1 |
---|
| 606 | """ |
---|
| 607 | cursor.execute(query, {'classifier':classifier}) |
---|
| 608 | row = cursor.fetchone() |
---|
| 609 | if row == None: |
---|
| 610 | sys.stderr.write("WARN: couldn't find test results for classifier '%s'.\n" % classifier) |
---|
| 611 | continue |
---|
| 612 | |
---|
| 613 | best_params = row[0] |
---|
| 614 | query=""" |
---|
| 615 | SELECT num_observations,(false_positives+false_negatives)/2.0 FROM classifier_results |
---|
| 616 | WHERE trial_type='test' |
---|
| 617 | AND classifier=:classifier |
---|
| 618 | AND params=:params |
---|
| 619 | ORDER BY num_observations |
---|
| 620 | """ |
---|
| 621 | cursor.execute(query, {'classifier':classifier,'params':best_params}) |
---|
| 622 | |
---|
| 623 | num_obs = [] |
---|
| 624 | performance = [] |
---|
| 625 | for row in cursor: |
---|
[13] | 626 | max_obs = max(max_obs, row[0]) |
---|
[11] | 627 | num_obs.append(row[0]) |
---|
| 628 | performance.append(row[1]) |
---|
| 629 | #print(num_obs,performance) |
---|
| 630 | path = plt.scatter(num_obs, performance, color=colors[color_id], s=4, alpha=0.8, linewidths=3.0) |
---|
| 631 | plt.plot(num_obs, performance, color=colors[color_id], alpha=0.8) |
---|
| 632 | legend.append((classifier,path)) |
---|
| 633 | color_id = (color_id+1) % len(colors) |
---|
| 634 | |
---|
| 635 | plt.legend([l[1] for l in legend], [l[0] for l in legend], scatterpoints=1, fontsize='xx-small') |
---|
[13] | 636 | plt.plot([0, max_obs], [5.0, 5.0], "k--") |
---|
[11] | 637 | plt.show() |
---|
| 638 | |
---|
| 639 | graphTestResults() |
---|
| 640 | |
---|
| 641 | sys.exit(0) |
---|
| 642 | |
---|
[6] | 643 | plt.clf() |
---|
| 644 | plt.title("Packet RTT over time") |
---|
| 645 | plt.xlabel('Time of Day') |
---|
| 646 | plt.ylabel('RTT') |
---|
| 647 | s = plt.scatter([t for t,rtt in short_overtime], [rtt for t,rtt in short_overtime], s=1, color='red', alpha=0.6) |
---|
| 648 | l = plt.scatter([t for t,rtt in long_overtime], [rtt for t,rtt in long_overtime], s=1, color='blue', alpha=0.6) |
---|
| 649 | d = plt.scatter([t for t,rtt in diff_overtime], [rtt for t,rtt in diff_overtime], s=1, color='purple', alpha=0.6) |
---|
| 650 | plt.legend((s,l,d), ('short','long','difference'), scatterpoints=1) |
---|
| 651 | #plt.savefig('paper/figures/comcast-powerboost1.png') |
---|
| 652 | plt.show() |
---|
| 653 | |
---|
[11] | 654 | |
---|
| 655 | |
---|
| 656 | plt.clf() |
---|
| 657 | plt.title("Simple HTTP Request") |
---|
| 658 | plt.xlabel('Time of Day') |
---|
| 659 | plt.ylabel('') |
---|
| 660 | s = plt.scatter(sent_times, [2]*len(sent_times), s=3, color='red', alpha=0.9) |
---|
| 661 | r = plt.scatter(rcvd_times, [1]*len(rcvd_times), s=3, color='blue', alpha=0.9) |
---|
| 662 | plt.legend((s,r), ('sent','received'), scatterpoints=1) |
---|
| 663 | plt.show() |
---|
| 664 | |
---|
| 665 | sys.exit(0) |
---|
[6] | 666 | short_overtime,long_overtime,diff_overtime = None,None,None |
---|
| 667 | |
---|
| 668 | |
---|
| 669 | num_bins = 300 |
---|
| 670 | reported_diffs.sort() |
---|
| 671 | cut_off_low = reported_diffs[int(len(diffs)*0.003)] |
---|
| 672 | cut_off_high = reported_diffs[int(len(diffs)*0.997)] |
---|
| 673 | |
---|
| 674 | plt.clf() |
---|
| 675 | # the histogram of the data |
---|
| 676 | n, bins, patches = plt.hist(reported_diffs, num_bins, normed=1, color='black', histtype='step', alpha=0.8, |
---|
| 677 | range=(cut_off_low,cut_off_high)) |
---|
| 678 | plt.xlabel('RTT Difference') |
---|
| 679 | plt.ylabel('Probability') |
---|
| 680 | plt.title(r'Histogram - distribution of differences') |
---|
| 681 | |
---|
| 682 | # Tweak spacing to prevent clipping of ylabel |
---|
| 683 | plt.subplots_adjust(left=0.15) |
---|
| 684 | #plt.legend() |
---|
| 685 | plt.show() |
---|
| 686 | #plt.savefig('paper/graphs/dists-vs-dist-of-diffs2.svg') |
---|
| 687 | |
---|
| 688 | |
---|
| 689 | |
---|
| 690 | |
---|
| 691 | num_bins = 300 |
---|
| 692 | diffs.sort() |
---|
| 693 | cut_off_low = diffs[int(len(diffs)*0.003)] |
---|
| 694 | cut_off_high = diffs[int(len(diffs)*0.997)] |
---|
| 695 | |
---|
| 696 | plt.clf() |
---|
| 697 | # the histogram of the data |
---|
| 698 | n, bins, patches = plt.hist(diffs, num_bins, normed=1, color='purple', histtype='step', alpha=0.8, |
---|
| 699 | range=(cut_off_low,cut_off_high)) |
---|
| 700 | plt.xlabel('RTT Difference') |
---|
| 701 | plt.ylabel('Probability') |
---|
| 702 | plt.title(r'Histogram - distribution of differences') |
---|
| 703 | |
---|
| 704 | # Tweak spacing to prevent clipping of ylabel |
---|
| 705 | plt.subplots_adjust(left=0.15) |
---|
| 706 | #plt.legend() |
---|
| 707 | plt.show() |
---|
| 708 | #plt.savefig('paper/graphs/dists-vs-dist-of-diffs2.svg') |
---|
| 709 | |
---|
| 710 | sys.exit(0) |
---|
| 711 | |
---|
| 712 | |
---|
| 713 | |
---|
| 714 | num_bins = 150 |
---|
| 715 | # the histogram of the data |
---|
| 716 | n, bins, patches = plt.hist((shorts,longs), num_bins, normed=1, label=['short', 'long'], color=['red','blue'], histtype='step', alpha=0.8, |
---|
| 717 | range=(cut_off_low,cut_off_high)) |
---|
| 718 | #n, bins, patches = plt.hist(shorts2+longs2, num_bins, normed=1, facecolor='blue', histtype='step', alpha=0.3) |
---|
| 719 | # add a 'best fit' line |
---|
| 720 | #y = mlab.normpdf(bins, mu, sigma) |
---|
| 721 | #plt.plot(bins, y, 'r--') |
---|
| 722 | plt.xlabel('packet_rtt') |
---|
| 723 | plt.ylabel('Probability') |
---|
| 724 | plt.title(r'Histogram - RTT short and long') |
---|
| 725 | |
---|
| 726 | # Tweak spacing to prevent clipping of ylabel |
---|
| 727 | plt.subplots_adjust(left=0.15) |
---|
| 728 | plt.legend() |
---|
| 729 | #plt.show() |
---|
| 730 | plt.savefig('paper/figures/comcast-powerboost2.svg') |
---|
| 731 | |
---|
| 732 | |
---|
| 733 | |
---|
| 734 | |
---|
| 735 | num_trials = 200 |
---|
| 736 | |
---|
| 737 | |
---|
| 738 | subsample_sizes = (50,150,300,500,700,1000,2000,3000,5000,7000,10000,15000,20000) |
---|
| 739 | estimator = functools.partial(boxTest, 0.07, 0.08) |
---|
| 740 | performance = [] |
---|
| 741 | for subsample_size in subsample_sizes: |
---|
| 742 | estimates = bootstrap(derived, subsample_size, num_trials, estimator) |
---|
| 743 | performance.append(100.0*len([e for e in estimates if e == 1])/num_trials) |
---|
| 744 | |
---|
| 745 | null_performance = [] |
---|
| 746 | for subsample_size in subsample_sizes: |
---|
| 747 | null_estimates = bootstrap(null_derived, subsample_size, num_trials, estimator) |
---|
| 748 | null_performance.append(100.0*len([e for e in null_estimates if e == 0])/num_trials) |
---|
| 749 | |
---|
| 750 | plt.clf() |
---|
| 751 | plt.title("boxTest bootstrap") |
---|
| 752 | plt.xlabel('sample size') |
---|
| 753 | plt.ylabel('performance') |
---|
| 754 | plt.scatter(subsample_sizes, performance, s=2, color='red', alpha=0.6) |
---|
| 755 | plt.scatter(subsample_sizes, null_performance, s=2, color='blue', alpha=0.6) |
---|
| 756 | plt.show() |
---|
| 757 | |
---|
| 758 | |
---|
| 759 | |
---|
| 760 | subsample_sizes = (50,150,300,400,500,700,1000,2000,3000,4000,5000,7000,10000) |
---|
| 761 | estimator = diffMedian |
---|
| 762 | performance = [] |
---|
| 763 | for subsample_size in subsample_sizes: |
---|
| 764 | estimates = bootstrap(derived, subsample_size, num_trials, estimator) |
---|
| 765 | performance.append(100.0*len([e for e in estimates if e > expected_mean*0.9 and e < expected_mean*1.1])/num_trials) |
---|
| 766 | |
---|
| 767 | plt.clf() |
---|
| 768 | plt.title("diff median bootstrap") |
---|
| 769 | plt.xlabel('sample size') |
---|
| 770 | plt.ylabel('performance') |
---|
| 771 | plt.scatter(subsample_sizes, performance, s=1, color='red', alpha=0.6) |
---|
| 772 | plt.show() |
---|
| 773 | |
---|
| 774 | |
---|
| 775 | |
---|
| 776 | |
---|
| 777 | subsample_sizes = (50,150,300,400,500,700,1000,2000,3000,4000,5000,7000,10000) |
---|
| 778 | weight_funcs = (linearWeights, prunedWeights) |
---|
| 779 | for wf in weight_funcs: |
---|
| 780 | estimator = functools.partial(estimateMean, hypotenuse, wf, 0.40) |
---|
| 781 | performance = [] |
---|
| 782 | for subsample_size in subsample_sizes: |
---|
| 783 | estimates = bootstrap(derived, subsample_size, num_trials, estimator) |
---|
| 784 | performance.append(100.0*len([e for e in estimates if e > expected_mean*0.9 and e < expected_mean*1.1])/num_trials) |
---|
| 785 | |
---|
| 786 | plt.clf() |
---|
| 787 | plt.title(repr(wf)) |
---|
| 788 | plt.xlabel('sample size') |
---|
| 789 | plt.ylabel('performance') |
---|
| 790 | plt.scatter(subsample_sizes, performance, s=1, color='red', alpha=0.6) |
---|
| 791 | plt.show() |
---|
| 792 | |
---|
| 793 | |
---|
| 794 | |
---|
| 795 | num_bins = 300 |
---|
| 796 | # the histogram of the data |
---|
| 797 | n, bins, patches = plt.hist((tsshorts,tslongs), num_bins, normed=1, label=['short', 'long'], color=['red','blue'], histtype='step', alpha=0.8) |
---|
| 798 | #n, bins, patches = plt.hist(shorts2+longs2, num_bins, normed=1, facecolor='blue', histtype='step', alpha=0.3) |
---|
| 799 | # add a 'best fit' line |
---|
| 800 | #y = mlab.normpdf(bins, mu, sigma) |
---|
| 801 | #plt.plot(bins, y, 'r--') |
---|
| 802 | plt.xlabel('packet_rtt') |
---|
| 803 | plt.ylabel('Probability') |
---|
| 804 | plt.title(r'Histogram - tsval_rtt short vs long') |
---|
| 805 | |
---|
| 806 | # Tweak spacing to prevent clipping of ylabel |
---|
| 807 | plt.subplots_adjust(left=0.15) |
---|
| 808 | plt.legend() |
---|
| 809 | plt.show() |
---|
| 810 | |
---|
| 811 | |
---|
| 812 | |
---|
| 813 | |
---|
| 814 | #### |
---|
| 815 | #trust_methods = [min,max,sum,difference,product] |
---|
| 816 | trust_methods = [sum,product,hypotenuse] |
---|
| 817 | colors = ['red','blue','green','purple','orange','black'] |
---|
| 818 | weight_methods = [prunedWeights, linearWeights] |
---|
| 819 | alphas = [i/100.0 for i in range(0,100,2)] |
---|
| 820 | |
---|
| 821 | |
---|
| 822 | |
---|
| 823 | |
---|
| 824 | plt.clf() |
---|
| 825 | plt.title(r'Trust Method Comparison - Linear') |
---|
| 826 | plt.xlabel('Alpha') |
---|
| 827 | plt.ylabel('Mean error') |
---|
| 828 | paths = [] |
---|
| 829 | for tm in trust_methods: |
---|
| 830 | trust = trustValues(derived, tm) |
---|
| 831 | series = [] |
---|
| 832 | for alpha in alphas: |
---|
| 833 | weights = linearWeights(derived, trust, alpha) |
---|
| 834 | series.append(weightedMean(derived, weights) - expected_mean) |
---|
| 835 | |
---|
| 836 | paths.append(plt.scatter(alphas, series, s=1, color=colors[len(paths)],alpha=0.6)) |
---|
| 837 | |
---|
| 838 | plt.legend(paths, [repr(tm) for tm in trust_methods], scatterpoints=1) |
---|
| 839 | plt.show() |
---|
| 840 | |
---|
| 841 | |
---|
| 842 | |
---|
| 843 | plt.clf() |
---|
| 844 | plt.title(r'Trust Method Comparison - Pruned') |
---|
| 845 | plt.xlabel('Alpha') |
---|
| 846 | plt.ylabel('Mean error') |
---|
| 847 | paths = [] |
---|
| 848 | for tm in trust_methods: |
---|
| 849 | trust = trustValues(derived, tm) |
---|
| 850 | series = [] |
---|
| 851 | for alpha in alphas: |
---|
| 852 | weights = prunedWeights(derived, trust, alpha) |
---|
| 853 | series.append(weightedMean(derived, weights) - expected_mean) |
---|
| 854 | |
---|
| 855 | paths.append(plt.scatter(alphas, series, s=1, color=colors[len(paths)],alpha=0.6)) |
---|
| 856 | |
---|
| 857 | plt.legend(paths, [repr(tm) for tm in trust_methods], scatterpoints=1) |
---|
| 858 | plt.show() |
---|
| 859 | |
---|
| 860 | |
---|
| 861 | sys.exit(0) |
---|
| 862 | |
---|
| 863 | plt.clf() |
---|
| 864 | plt.title(r'Trust Method Comparison - Inverted') |
---|
| 865 | plt.xlabel('Alpha') |
---|
| 866 | plt.ylabel('Mean error') |
---|
| 867 | paths = [] |
---|
| 868 | for tm in trust_methods: |
---|
| 869 | trust = trustValues(derived, tm) |
---|
| 870 | series = [] |
---|
| 871 | for alpha in alphas: |
---|
| 872 | weights = invertedWeights(derived, trust, alpha) |
---|
| 873 | series.append(weightedMean(derived, weights) - expected_mean) |
---|
| 874 | |
---|
| 875 | paths.append(plt.scatter(alphas, series, s=1, color=colors[len(paths)],alpha=0.6)) |
---|
| 876 | |
---|
| 877 | plt.legend(paths, [repr(tm) for tm in trust_methods], scatterpoints=1) |
---|
| 878 | plt.show() |
---|
| 879 | |
---|
| 880 | |
---|
| 881 | plt.clf() |
---|
| 882 | plt.title(r'Trust Method Comparison - Arctangent') |
---|
| 883 | plt.xlabel('Alpha') |
---|
| 884 | plt.ylabel('Mean error') |
---|
| 885 | paths = [] |
---|
| 886 | for tm in trust_methods: |
---|
| 887 | trust = trustValues(derived, tm) |
---|
| 888 | series = [] |
---|
| 889 | for alpha in alphas: |
---|
| 890 | weights = arctanWeights(derived, trust, alpha) |
---|
| 891 | series.append(weightedMean(derived, weights) - expected_mean) |
---|
| 892 | |
---|
| 893 | paths.append(plt.scatter(alphas, series, s=1, color=colors[len(paths)],alpha=0.6)) |
---|
| 894 | |
---|
| 895 | plt.legend(paths, [repr(tm) for tm in trust_methods], scatterpoints=1) |
---|
| 896 | plt.show() |
---|