"""Exact finite sampling calculations, not a production telemetry benchmark.""" import json,itertools,math SLUG="tracing-sampling-evidence" def run(): ps=[.001,.005,.01,.02,.05,.1,.2,.5,1.] n=20 detect=[1-(1-p)**n for p in ps] assert abs(detect[2]-.18209306240276923)<1e-12 # Exact finite Horvitz-Thompson expectation for three known inclusion probabilities. values=[0,1,1];pis=[.1,.5,.8];expect=0 for bits in itertools.product([0,1],repeat=3): prob=math.prod(p if b else 1-p for p,b in zip(pis,bits)) estimate=sum(v*b/p for v,b,p in zip(values,bits,pis)) expect+=prob*estimate assert abs(expect-2)<1e-12 rates=[.001,.005,.01,.02,.05,.1,.2] selected_error=[r/(r+.01*(1-r)) for r in rates] assert abs(.01/(.01+.01*.99)-.5025125628140703)<1e-12 # Counterexample: additive span durations exceed critical path. durations=[100,80,80] assert sum(durations)==260 and max(durations)==100 return {"slug":SLUG,"checks":[{"name":"Rare-event inclusion probability","passed":True,"cases":9,"detail":"Twenty failures at 1% head sampling give an 18.2093% chance of retaining at least one."},{"name":"Unequal probability estimator expectation","passed":True,"cases":8,"detail":"All 8 inclusion outcomes recover a total of 2 in expectation with known positive inclusion probabilities."},{"name":"Tail-selection bias and nested span double count","passed":True,"cases":2,"detail":"Retaining all failures and 1% of successes at 1% population incidence gives a ratio of expected retained failure to total counts of 50.2513%; nested span durations 100 + 80 + 80 do not imply 260 ms elapsed."}],"figures":[{"id":"detection","title":"A small sample may miss a short incident","x_label":"Independent trace inclusion probability","y_label":"Probability of seeing at least one of 20 failures","series":[{"name":"1-(1-p)^20","x":ps,"y":detect}],"caption":"An exact Bernoulli inclusion calculation conditional on twenty failure traces existing. It assumes all relevant instrumentation and exports succeed.","assumptions":["Independent unbiased head decisions","No additional sampling or export loss"],"kind":"Constructed analytical model; not a measured benchmark"},{"id":"selection","title":"Outcome-dependent retention changes the apparent error rate","x_label":"True population failure fraction","y_label":"Ratio of expected retained counts","series":[{"name":"All failures, 1% of successes retained","x":rates,"y":selected_error},{"name":"Unbiased population fraction","x":rates,"y":rates}],"caption":"The curve is a ratio of expected class counts in a large population, not the expectation of every finite-sample ratio. It illustrates selection bias.","assumptions":["Error retention probability 1; success retention probability 0.01","No capacity drops or missing classification"],"kind":"Constructed analytical model; not a measured benchmark"}],"result_summary":{"detection_probability_n20_p001":detect[2],"sampled_error_share_population_001":.01/(.01+.01*.99),"weighted_total_expectation":expect,"nested_span_sum_ms":260,"elapsed_ms":100}} if __name__=="__main__":print(json.dumps(run(),indent=2))