import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%0A%20%20%20%20return%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Note%2014%3A%20Distribution%20of%20the%20Minimum%2C%20Order%20Statistics%2C%20and%20Extreme%20Values%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B13%20Unbiased%20vs%20Consistent%5D(13_unbiased_vs_consistent.py)%20%7C%20Next%20Note%3A%20%5B15%20Mutual%20Information%5D(15_mutual_information.py)%20%26rarr%3B%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Ba%5D%20Why%20do%20you%20need%20to%20know%20these%20concepts%3F%0A%0A%20%20%20%20Standard%20introductory%20statistics%20focuses%20on%20sample%20averages%20and%20sums%20(governed%20by%20the%20Central%20Limit%20Theorem).%20In%20engineering%2C%20finance%2C%20machine%20learning%2C%20and%20computer%20systems%2C%20however%2C%20critical%20outcomes%20are%20frequently%20dictated%20not%20by%20averages%2C%20but%20by%20the%20**minimum**%20or%20**maximum**%20across%20a%20sample%3A%0A%0A%20%20%20%201.%20**Reliability%20and%20Series%20System%20Architectures**%3A%20In%20distributed%20systems%2C%20microservice%20DAG%20pipelines%2C%20and%20hardware%20component%20arrays%2C%20a%20series%20system%20functions%20only%20as%20long%20as%20its%20weakest%20link%20survives.%20The%20system%20failure%20time%20is%20the%20minimum%20of%20component%20failure%20times%3A%20%24T_%7B%5Ctext%7Bsystem%7D%7D%20%3D%20%5Cmin(T_1%2C%20%5Cdots%2C%20T_n)%24.%0A%20%20%20%202.%20**Extreme%20Value%20Theory%20(EVT)%20and%20Tail%20Risk**%3A%20In%20quantitative%20risk%20management%2C%20financial%20engineering%2C%20and%20flood%20prediction%2C%20the%20focus%20is%20on%20extreme%20tail%20events%20(such%20as%20maximum%20portfolio%20drawdown%20or%20minimum%20liquidity%20reserves).%20The%20Fisher-Tippett-Gnedenko%20theorem%20shows%20that%20sample%20extremes%20converge%20to%20generalized%20extreme%20value%20(GEV)%20distributions.%0A%20%20%20%203.%20**First-Hitting%20Times%20in%20Optimization**%3A%20In%20stochastic%20gradient%20descent%2C%20simulated%20annealing%2C%20and%20hyperparameter%20sweeps%20across%20%24n%24%20parallel%20worker%20threads%2C%20the%20time%20until%20the%20first%20worker%20reaches%20a%20target%20loss%20is%20governed%20by%20the%20distribution%20of%20the%20minimum.%0A%20%20%20%204.%20**Nearest%20Neighbor%20Distances%20in%20Vector%20Databases**%3A%20In%20%24k%24-NN%20classifiers%2C%20recommendation%20systems%2C%20and%20vector%20databases%20(HNSW%2C%20ScaNN%2C%20FAISS)%2C%20the%20distance%20from%20a%20query%20vector%20%24%5Cmathbf%7Bq%7D%24%20to%20the%20nearest%20database%20item%20is%20%24R_%7B(1)%7D%20%3D%20%5Cmin_%7Bi%7D%20%5C%7C%5Cmathbf%7Bq%7D%20-%20%5Cmathbf%7Bx%7D_i%5C%7C_2%24.%20Understanding%20the%20distribution%20of%20the%20minimum%20explains%20the%20geometric%20curse%20of%20dimensionality%20in%20high-dimensional%20vector%20search.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bb%5D%20Concept%20explanation%20with%20their%20role%20in%20ML%2FAI%2FStats%3F%0A%0A%20%20%20%20%23%23%23%20Definition%20of%20Order%20Statistics%0A%0A%20%20%20%20Let%20%24X_1%2C%20X_2%2C%20%5Cdots%2C%20X_n%24%20be%20independent%20and%20identically%20distributed%20(i.i.d.)%20continuous%20random%20variables%20with%20common%20cumulative%20distribution%20function%20%24F_X(x)%24%20and%20probability%20density%20function%20%24f_X(x)%24.%0A%0A%20%20%20%20Sorting%20the%20sample%20in%20ascending%20order%20yields%20the%20**order%20statistics**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20X_%7B(1)%7D%20%5Cleq%20X_%7B(2)%7D%20%5Cleq%20%5Cdots%20%5Cleq%20X_%7B(n)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24X_%7B(1)%7D%20%3D%20%5Cmin(X_1%2C%20%5Cdots%2C%20X_n)%24%20is%20the%20sample%20minimum%2C%20and%20%24X_%7B(n)%7D%20%3D%20%5Cmax(X_1%2C%20%5Cdots%2C%20X_n)%24%20is%20the%20sample%20maximum.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Derivation%20of%20the%20Cumulative%20Distribution%20Function%20(CDF)%20of%20the%20Minimum%0A%0A%20%20%20%20To%20find%20the%20distribution%20of%20%24X_%7B(1)%7D%20%3D%20%5Cmin(X_1%2C%20%5Cdots%2C%20X_n)%24%2C%20we%20analyze%20the%20survival%20probability%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20F_%7BX_%7B(1)%7D%7D(x)%20%3D%20P(X_%7B(1)%7D%20%5Cleq%20x)%20%3D%201%20-%20P(X_%7B(1)%7D%20%3E%20x)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20minimum%20of%20a%20set%20of%20values%20is%20strictly%20greater%20than%20%24x%24%20if%20and%20only%20if%20**every%20individual%20sample**%20%24X_i%24%20is%20strictly%20greater%20than%20%24x%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20P(X_%7B(1)%7D%20%3E%20x)%20%3D%20P(X_1%20%3E%20x%2C%20X_2%20%3E%20x%2C%20%5Cdots%2C%20X_n%20%3E%20x)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Because%20the%20random%20variables%20are%20independent%20and%20identically%20distributed%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20P(X_1%20%3E%20x%2C%20%5Cdots%2C%20X_n%20%3E%20x)%20%3D%20%5Cprod_%7Bi%3D1%7D%5En%20P(X_i%20%3E%20x)%20%3D%20%5Cleft(1%20-%20F_X(x)%5Cright)%5En%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Substituting%20this%20back%20yields%20the%20exact%20**CDF%20of%20the%20minimum**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20F_%7BX_%7B(1)%7D%7D(x)%20%3D%201%20-%20%5Cleft(1%20-%20F_X(x)%5Cright)%5En%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Derivation%20of%20the%20Probability%20Density%20Function%20(PDF)%20of%20the%20Minimum%0A%0A%20%20%20%20Differentiating%20the%20CDF%20with%20respect%20to%20%24x%24%20using%20the%20chain%20rule%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f_%7BX_%7B(1)%7D%7D(x)%20%3D%20%5Cfrac%7Bd%7D%7Bdx%7D%20F_%7BX_%7B(1)%7D%7D(x)%20%3D%20%5Cfrac%7Bd%7D%7Bdx%7D%20%5Cleft%5B%201%20-%20(1%20-%20F_X(x))%5En%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f_%7BX_%7B(1)%7D%7D(x)%20%3D%20n%20%5Cleft(1%20-%20F_X(x)%5Cright)%5E%7Bn-1%7D%20f_X(x)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Intuition%20Behind%20the%20Formula%0A%20%20%20%20To%20have%20the%20minimum%20fall%20in%20an%20infinitesimal%20interval%20%24%5Bx%2C%20x%20%2B%20dx%5D%24%3A%0A%20%20%20%20*%20One%20of%20the%20%24n%24%20items%20must%20land%20in%20%24%5Bx%2C%20x%20%2B%20dx%5D%24%20(%24n%20%5Ccdot%20f_X(x)%20%5C%2C%20dx%24%20ways).%0A%20%20%20%20*%20All%20remaining%20%24n%20-%201%24%20items%20must%20be%20strictly%20greater%20than%20%24x%24%20(probability%20%24(1%20-%20F_X(x))%5E%7Bn-1%7D%24).%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Special%20Case%201%3A%20Exponential%20Distribution%20(Memoryless%20Series%20Systems)%0A%0A%20%20%20%20Let%20%24X_i%20%5Csim%20%5Ctext%7BExp%7D(%5Clambda)%24%20with%20CDF%20%24F_X(x)%20%3D%201%20-%20e%5E%7B-%5Clambda%20x%7D%24%20and%20PDF%20%24f_X(x)%20%3D%20%5Clambda%20e%5E%7B-%5Clambda%20x%7D%24%20for%20%24x%20%5Cgeq%200%24.%0A%0A%20%20%20%20Computing%20the%20survival%20function%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%201%20-%20F_X(x)%20%3D%20e%5E%7B-%5Clambda%20x%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Substituting%20into%20the%20minimum%20CDF%20formula%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20F_%7BX_%7B(1)%7D%7D(x)%20%3D%201%20-%20%5Cleft(e%5E%7B-%5Clambda%20x%7D%5Cright)%5En%20%3D%201%20-%20e%5E%7B-n%20%5Clambda%20x%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Differentiating%20to%20obtain%20the%20density%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f_%7BX_%7B(1)%7D%7D(x)%20%3D%20n%20%5Clambda%20e%5E%7B-n%20%5Clambda%20x%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Remarkable%20Mathematical%20Result%0A%20%20%20%20The%20minimum%20of%20%24n%24%20independent%20Exponential%20variables%20with%20rate%20%24%5Clambda%24%20is%20itself%20an%20Exponential%20variable%20with%20scaled%20rate%20parameter%20%24n%5Clambda%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20X_%7B(1)%7D%20%5Csim%20%5Ctext%7BExp%7D(n%5Clambda)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20expected%20value%20scales%20inversely%20with%20%24n%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbb%7BE%7D%5BX_%7B(1)%7D%5D%20%3D%20%5Cfrac%7B1%7D%7Bn%5Clambda%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%7D%20%5Cmathbb%7BE%7D%5BX%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20If%20a%20server%20has%20Mean%20Time%20to%20Failure%20(MTTF)%20of%20%24100%24%20hours%2C%20a%20cluster%20of%20%24n%20%3D%2010%24%20servers%20running%20in%20series%20has%20an%20MTTF%20of%20only%20%24%5Cfrac%7B100%7D%7B10%7D%20%3D%2010%24%20hours.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Special%20Case%202%3A%20Uniform%20Distribution%20%24%5Ctext%7BUniform%7D(0%2C%201)%24%0A%0A%20%20%20%20Let%20%24X_i%20%5Csim%20%5Ctext%7BUniform%7D(0%2C%201)%24%20where%20%24F_X(x)%20%3D%20x%24%20and%20%24f_X(x)%20%3D%201%24%20for%20%24x%20%5Cin%20%5B0%2C%201%5D%24.%0A%0A%20%20%20%20%24%24%0A%20%20%20%20F_%7BX_%7B(1)%7D%7D(x)%20%3D%201%20-%20(1%20-%20x)%5En%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f_%7BX_%7B(1)%7D%7D(x)%20%3D%20n%20(1%20-%20x)%5E%7Bn-1%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20corresponds%20to%20a%20%24%5Ctext%7BBeta%7D(1%2C%20n)%24%20distribution.%20The%20expected%20minimum%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbb%7BE%7D%5BX_%7B(1)%7D%5D%20%3D%20%5Cint_0%5E1%20x%20%5Ccdot%20n%20(1%20-%20x)%5E%7Bn-1%7D%20%5C%2C%20dx%20%3D%20%5Cfrac%7B1%7D%7Bn%20%2B%201%7D%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bc%5D%20Interactive%20Visualizations%3A%20Density%20Compression%20and%20Empirical%20Verification%0A%0A%20%20%20%20The%20interactive%20subplots%20below%20demonstrate%20how%20the%20distribution%20of%20the%20minimum%20behaves%3A%0A%20%20%20%20*%20**Left%20Panel**%3A%20Theoretical%20PDF%20compression%20for%20%24X_%7B(1)%7D%20%5Csim%20%5Ctext%7BExp%7D(n%5Clambda)%24%20with%20base%20rate%20%24%5Clambda%20%3D%202.0%24%20across%20increasing%20sample%20sizes%20%24n%20%5Cin%20%5C%7B1%2C%202%2C%205%2C%2010%2C%2025%5C%7D%24.%20As%20%24n%24%20increases%2C%20probability%20mass%20is%20compressed%20toward%20the%20origin%2C%20and%20the%20initial%20density%20%24f_%7B(1)%7D(0)%20%3D%20n%5Clambda%24%20scales%20linearly.%0A%20%20%20%20*%20**Right%20Panel**%3A%20Empirical%20validation%20of%20%2450%2C000%24%20simulated%20minimums%20(%24n%20%3D%205%24%2C%20%24%5Clambda%20%3D%202.0%24)%20compared%20against%20the%20exact%20analytical%20distribution%20%24%5Ctext%7BExp%7D(10.0)%24%2C%20demonstrating%20exact%20correspondence%20between%20finite-sample%20simulation%20and%20theoretical%20derivation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(go%2C%20make_subplots%2C%20np)%3A%0A%20%20%20%20lambda_param%20%3D%202.0%0A%20%20%20%20x_axis_range%20%3D%20np.linspace(0.001%2C%201.6%2C%20250)%0A%0A%20%20%20%20%23%20Sample%20sizes%20to%20compare%0A%20%20%20%20batch_sizes%20%3D%20%5B1%2C%202%2C%205%2C%2010%2C%2025%5D%0A%20%20%20%20color_palette%20%3D%20%5B%22%232563eb%22%2C%20%22%2306b6d4%22%2C%20%22%2316a34a%22%2C%20%22%23ea580c%22%2C%20%22%23dc2626%22%5D%0A%0A%20%20%20%20fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22PDF%20Compression%20of%20the%20Minimum%20(%CE%BB%20%3D%202.0)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Empirical%20Histogram%20vs%20Analytical%20PDF%20(n%20%3D%205%2C%20Rate%20%3D%2010.0)%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Theoretical%20Curves%20for%20varying%20n%0A%20%20%20%20for%20idx%2C%20n_size%20in%20enumerate(batch_sizes)%3A%0A%20%20%20%20%20%20%20%20effective_rate%20%3D%20n_size%20*%20lambda_param%0A%20%20%20%20%20%20%20%20pdf_curve%20%3D%20effective_rate%20*%20np.exp(-effective_rate%20*%20x_axis_range)%0A%20%20%20%20%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx_axis_range%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dpdf_curve%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3Dcolor_palette%5Bidx%5D%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22n%3D%7Bn_size%7D%20(Rate%3D%7Beffective_rate%3A.0f%7D%2C%20E%3D%7B1.0%20%2F%20effective_rate%3A.2f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%23%20Right%3A%20Monte%20Carlo%20Simulation%20for%20n%20%3D%205%0A%20%20%20%20rng_mc%20%3D%20np.random.default_rng(42)%0A%20%20%20%20n_fixed_demo%20%3D%205%0A%20%20%20%20n_draws%20%3D%2050000%0A%20%20%20%20raw_exponential_matrix%20%3D%20rng_mc.exponential(scale%3D1.0%20%2F%20lambda_param%2C%20size%3D(n_draws%2C%20n_fixed_demo))%0A%20%20%20%20simulated_minimums%20%3D%20np.min(raw_exponential_matrix%2C%20axis%3D1)%0A%0A%20%20%20%20effective_rate_demo%20%3D%20n_fixed_demo%20*%20lambda_param%0A%20%20%20%20analytical_pdf_demo%20%3D%20effective_rate_demo%20*%20np.exp(-effective_rate_demo%20*%20x_axis_range)%0A%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Histogram(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dsimulated_minimums%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20histnorm%3D%22probability%20density%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20nbinsx%3D65%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%232563eb%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20opacity%3D0.60%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Simulated%20Minimum%20(50k%20Trials)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx_axis_range%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Danalytical_pdf_demo%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23dc2626%22%2C%20width%3D3.0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Analytical%20Exp(Rate%3D%7Beffective_rate_demo%3A.0f%7D)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D520%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D60%2C%20b%3D40)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22Value%20(x)%22%2C%20range%3D%5B0.0%2C%201.6%5D%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22Probability%20Density%22%2C%20range%3D%5B0%2C%2052%5D%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20xaxis2%3Ddict(title%3D%22Value%20(x)%22%2C%20range%3D%5B0.0%2C%200.8%5D%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20yaxis2%3Ddict(title%3D%22Density%22%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.26%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(fig%2C)%0A%0A%0A%40app.cell%0Adef%20_(fig%2C%20mo)%3A%0A%20%20%20%20mo.ui.plotly(fig)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bd%5D%20Code%20Examples%0A%0A%20%20%20%20%23%23%23%20Example%201%3A%20Theoretical%20vs%20Empirical%20Verification%20of%20Order%20Statistics%0A%0A%20%20%20%20In%20this%20example%2C%20we%20draw%20%24M%20%3D%2050%2C000%24%20independent%20samples%20across%20batch%20sizes%20%24n%20%5Cin%20%5C%7B1%2C%202%2C%205%2C%2010%2C%2025%2C%2050%5C%7D%24%20for%20both%20Exponential%20(%24%5Clambda%20%3D%202.0%24)%20and%20Uniform%20(%24%5B0%2C%201%5D%24)%20random%20variables.%0A%0A%20%20%20%20For%20each%20batch%20size%20%24n%24%2C%20we%20evaluate%3A%0A%20%20%20%201.%20**Exponential%20Minimum**%3A%20Empirical%20Mean%20vs%20Theoretical%20%24%5Cmathbb%7BE%7D%5BX_%7B(1)%7D%5D%20%3D%20%5Cfrac%7B1%7D%7Bn%5Clambda%7D%24%0A%20%20%20%202.%20**Uniform%20Minimum**%3A%20Empirical%20Mean%20vs%20Theoretical%20%24%5Cmathbb%7BE%7D%5BX_%7B(1)%7D%5D%20%3D%20%5Cfrac%7B1%7D%7Bn%20%2B%201%7D%24%0A%20%20%20%203.%20**Relative%20Percentage%20Error**%3A%20Demonstrating%20convergence%20within%20%24%3C%201%5C%25%24%20across%20all%20sample%20sizes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20rng_ex1%20%3D%20np.random.default_rng(101)%0A%20%20%20%20lambda_val%20%3D%202.0%0A%20%20%20%20num_experiments%20%3D%2050000%0A%20%20%20%20sample_sizes_tested%20%3D%20%5B1%2C%202%2C%205%2C%2010%2C%2025%2C%2050%5D%0A%0A%20%20%20%20order_stats_records%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20n_count%20in%20sample_sizes_tested%3A%0A%20%20%20%20%20%20%20%20%23%20Exponential%20draws%0A%20%20%20%20%20%20%20%20exp_matrix%20%3D%20rng_ex1.exponential(scale%3D1.0%20%2F%20lambda_val%2C%20size%3D(num_experiments%2C%20n_count))%0A%20%20%20%20%20%20%20%20exp_mins%20%3D%20np.min(exp_matrix%2C%20axis%3D1)%0A%20%20%20%20%20%20%20%20mean_exp_empirical%20%3D%20float(np.mean(exp_mins))%0A%20%20%20%20%20%20%20%20mean_exp_theoretical%20%3D%201.0%20%2F%20(n_count%20*%20lambda_val)%0A%20%20%20%20%20%20%20%20exp_rel_err%20%3D%20abs(mean_exp_empirical%20-%20mean_exp_theoretical)%20%2F%20mean_exp_theoretical%20*%20100.0%0A%0A%20%20%20%20%20%20%20%20%23%20Uniform%20draws%0A%20%20%20%20%20%20%20%20unif_matrix%20%3D%20rng_ex1.uniform(0.0%2C%201.0%2C%20size%3D(num_experiments%2C%20n_count))%0A%20%20%20%20%20%20%20%20unif_mins%20%3D%20np.min(unif_matrix%2C%20axis%3D1)%0A%20%20%20%20%20%20%20%20mean_unif_empirical%20%3D%20float(np.mean(unif_mins))%0A%20%20%20%20%20%20%20%20mean_unif_theoretical%20%3D%201.0%20%2F%20(n_count%20%2B%201.0)%0A%20%20%20%20%20%20%20%20unif_rel_err%20%3D%20abs(mean_unif_empirical%20-%20mean_unif_theoretical)%20%2F%20mean_unif_theoretical%20*%20100.0%0A%0A%20%20%20%20%20%20%20%20order_stats_records.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Sample%20Size%20(n)%22%3A%20n_count%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Empirical%20E%5BExp%20Min%5D%22%3A%20f%22%7Bmean_exp_empirical%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Theoretical%20E%5B1%2Fn%CE%BB%5D%22%3A%20f%22%7Bmean_exp_theoretical%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Exp%20Relative%20Error%22%3A%20f%22%7Bexp_rel_err%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Empirical%20E%5BUnif%20Min%5D%22%3A%20f%22%7Bmean_unif_empirical%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Theoretical%20E%5B1%2F(n%2B1)%5D%22%3A%20f%22%7Bmean_unif_theoretical%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Unif%20Relative%20Error%22%3A%20f%22%7Bunif_rel_err%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(order_stats_records%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20order_stats_records%2C%20pd)%3A%0A%20%20%20%20df_order%20%3D%20pd.DataFrame(order_stats_records)%0A%20%20%20%20mo.ui.table(df_order)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Example%202%3A%20Distributed%20Cloud%20System%20Reliability%20(Series%20vs%20Parallel%20Architectures)%0A%0A%20%20%20%20In%20distributed%20systems%2C%20microservices%20can%20be%20wired%20in%20two%20canonical%20configurations%3A%0A%20%20%20%20*%20**Series%20Configuration%20(All%20Must%20Succeed)**%3A%20System%20fails%20at%20%24T_%7B%5Ctext%7Bseries%7D%7D%20%3D%20%5Cmin(T_1%2C%20%5Cdots%2C%20T_n)%24.%0A%20%20%20%20*%20**Parallel%20Quorum%20Configuration%20(At%20Least%20One%20Succeeds)**%3A%20System%20fails%20at%20%24T_%7B%5Ctext%7Bparallel%7D%7D%20%3D%20%5Cmax(T_1%2C%20%5Cdots%2C%20T_n)%24.%0A%0A%20%20%20%20Below%2C%20we%20simulate%20a%20cluster%20of%20%24n%20%3D%206%24%20nodes%20where%20each%20component's%20lifetime%20follows%20an%20Exponential%20distribution%20with%20base%20MTTF%20%24%3D%20100.0%24%20hours%20(%24%5Clambda%20%3D%200.01%24).%0A%0A%20%20%20%20We%20compare%20the%20Mean%20Time%20to%20Failure%20(MTTF)%20and%20the%20probability%20of%20survival%20beyond%20%24t%20%3D%2020.0%24%20hours%20for%20both%20architectures.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20rng_sys%20%3D%20np.random.default_rng(202)%0A%20%20%20%20mttf_component%20%3D%20100.0%0A%20%20%20%20lambda_comp%20%3D%201.0%20%2F%20mttf_component%0A%20%20%20%20cluster_nodes%20%3D%206%0A%20%20%20%20sim_runs%20%3D%2050000%0A%20%20%20%20target_time_hours%20%3D%2020.0%0A%0A%20%20%20%20lifetimes_matrix%20%3D%20rng_sys.exponential(scale%3Dmttf_component%2C%20size%3D(sim_runs%2C%20cluster_nodes))%0A%0A%20%20%20%20%23%20Series%3A%20Minimum%0A%20%20%20%20series_lifetimes%20%3D%20np.min(lifetimes_matrix%2C%20axis%3D1)%0A%20%20%20%20mttf_series_emp%20%3D%20float(np.mean(series_lifetimes))%0A%20%20%20%20mttf_series_theo%20%3D%20mttf_component%20%2F%20cluster_nodes%0A%20%20%20%20survival_series_emp%20%3D%20float(np.mean(series_lifetimes%20%3E%20target_time_hours))%20*%20100.0%0A%20%20%20%20survival_series_theo%20%3D%20np.exp(-cluster_nodes%20*%20lambda_comp%20*%20target_time_hours)%20*%20100.0%0A%0A%20%20%20%20%23%20Parallel%3A%20Maximum%0A%20%20%20%20parallel_lifetimes%20%3D%20np.max(lifetimes_matrix%2C%20axis%3D1)%0A%20%20%20%20mttf_parallel_emp%20%3D%20float(np.mean(parallel_lifetimes))%0A%20%20%20%20%23%20Theoretical%20harmonic%20sum%20for%20parallel%20MTTF%3A%20(1%2Flambda)%20*%20sum(1%2Fi%20for%20i%20in%201..n)%0A%20%20%20%20mttf_parallel_theo%20%3D%20mttf_component%20*%20np.sum(1.0%20%2F%20np.arange(1%2C%20cluster_nodes%20%2B%201))%0A%20%20%20%20survival_parallel_emp%20%3D%20float(np.mean(parallel_lifetimes%20%3E%20target_time_hours))%20*%20100.0%0A%20%20%20%20survival_parallel_theo%20%3D%20(1.0%20-%20(1.0%20-%20np.exp(-lambda_comp%20*%20target_time_hours))%20**%20cluster_nodes)%20*%20100.0%0A%0A%20%20%20%20reliability_summary_records%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Architecture%22%3A%20f%22Single%20Component%20(n%3D1)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Governing%20Statistic%22%3A%20%22T%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Empirical%20MTTF%20(hrs)%22%3A%20f%22%7Bfloat(np.mean(lifetimes_matrix%5B%3A%2C%200%5D))%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Theoretical%20MTTF%20(hrs)%22%3A%20f%22%7Bmttf_component%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Survival%20P(T%20%3E%2020h)%20Emp%22%3A%20f%22%7Bfloat(np.mean(lifetimes_matrix%5B%3A%2C%200%5D%20%3E%20target_time_hours))%20*%20100.0%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Survival%20P(T%20%3E%2020h)%20Theo%22%3A%20f%22%7Bnp.exp(-lambda_comp%20*%20target_time_hours)%20*%20100.0%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Architecture%22%3A%20f%22Series%20Cluster%20(n%3D%7Bcluster_nodes%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Governing%20Statistic%22%3A%20%22T_(1)%20%3D%20min(T%E2%82%81..T%E2%82%99)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Empirical%20MTTF%20(hrs)%22%3A%20f%22%7Bmttf_series_emp%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Theoretical%20MTTF%20(hrs)%22%3A%20f%22%7Bmttf_series_theo%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Survival%20P(T%20%3E%2020h)%20Emp%22%3A%20f%22%7Bsurvival_series_emp%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Survival%20P(T%20%3E%2020h)%20Theo%22%3A%20f%22%7Bsurvival_series_theo%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Architecture%22%3A%20f%22Parallel%20Redundancy%20(n%3D%7Bcluster_nodes%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Governing%20Statistic%22%3A%20%22T_(n)%20%3D%20max(T%E2%82%81..T%E2%82%99)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Empirical%20MTTF%20(hrs)%22%3A%20f%22%7Bmttf_parallel_emp%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Theoretical%20MTTF%20(hrs)%22%3A%20f%22%7Bmttf_parallel_theo%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Survival%20P(T%20%3E%2020h)%20Emp%22%3A%20f%22%7Bsurvival_parallel_emp%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Survival%20P(T%20%3E%2020h)%20Theo%22%3A%20f%22%7Bsurvival_parallel_theo%3A.2f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20(reliability_summary_records%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20pd%2C%20reliability_summary_records)%3A%0A%20%20%20%20df_reliability%20%3D%20pd.DataFrame(reliability_summary_records)%0A%20%20%20%20mo.ui.table(df_reliability)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
59ef35e3da13e801ef9a1eda76e42a07