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%2017%3A%20Jensen's%20Inequality%2C%20Convexity%2C%20and%20Information%20Bounds%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B16%20Point-Biserial%20Correlation%5D(16_point_biserial.py)%20%7C%20Next%20Note%3A%20%5B18%20Cramer%20V%5D(18_cramer_v.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%20Jensen's%20inequality%20is%20arguably%20the%20most%20ubiquitous%20mathematical%20inequality%20in%20modern%20statistical%20learning%2C%20information%20theory%2C%20and%20generative%20artificial%20intelligence.%20It%20connects%20the%20geometric%20definition%20of%20function%20convexity%20directly%20to%20mathematical%20expectations.%0A%0A%20%20%20%20Key%20reasons%20why%20Jensen's%20inequality%20is%20essential%3A%0A%20%20%20%201.%20**The%20Engine%20of%20Variational%20Autoencoders%20(VAEs)%20and%20Diffusion%20Models**%3A%20In%20deep%20generative%20models%20with%20continuous%20latent%20variables%20%24%5Cmathbf%7Bz%7D%24%2C%20the%20true%20marginal%20log-likelihood%20%24%5Cln%20p(%5Cmathbf%7Bx%7D)%20%3D%20%5Cln%20%5Cint%20p(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7Bz%7D)%20d%5Cmathbf%7Bz%7D%24%20is%20computationally%20intractable.%20By%20applying%20Jensen's%20inequality%20to%20the%20concave%20logarithm%20function%2C%20we%20derive%20the%20**Evidence%20Lower%20Bound%20(ELBO)**%3A%0A%0A%20%20%20%20%24%24%5Cln%20p(%5Cmathbf%7Bx%7D)%20%5Cgeq%20%5Cmathbb%7BE%7D_%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%5Cleft%5B%20%5Cln%20%5Cfrac%7Bp(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7Bz%7D)%7D%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%20%5Cright%5D%20%3D%20%5Ctext%7BELBO%7D%24%24%0A%0A%20%20%20%20VAEs%20and%20latent%20diffusion%20models%20train%20by%20maximizing%20this%20Jensen%20lower%20bound.%0A%20%20%20%202.%20**Expectation-Maximization%20(EM)%20Algorithm**%3A%20In%20Gaussian%20Mixture%20Models%20(GMMs)%20and%20Hidden%20Markov%20Models%20(HMMs)%2C%20the%20E-step%20constructs%20a%20tight%20Jensen%20lower%20bound%20around%20the%20incomplete%20data%20log-likelihood%2C%20and%20the%20M-step%20maximizes%20it.%0A%20%20%20%203.%20**Information%20Theory%20and%20Non-Negativity%20of%20Relative%20Entropy**%3A%20Gibbs'%20inequality%20(%24D_%7B%5Ctext%7BKL%7D%7D(P%20%5C%7C%20Q)%20%5Cgeq%200%24)%2C%20which%20establishes%20that%20cross-entropy%20is%20always%20greater%20than%20or%20equal%20to%20true%20entropy%2C%20is%20proven%20in%20two%20steps%20via%20Jensen's%20inequality%20on%20%24f(t)%20%3D%20-%5Cln(t)%24.%0A%20%20%20%204.%20**The%20Arithmetic%20Mean%20-%20Geometric%20Mean%20(AM-GM)%20Inequality**%3A%20The%20fundamental%20inequality%20%24%5Cfrac%7B1%7D%7Bn%7D%20%5Csum%20x_i%20%5Cgeq%20(%5Cprod%20x_i)%5E%7B1%2Fn%7D%24%20is%20a%20direct%20corollary%20of%20Jensen's%20inequality%20applied%20to%20the%20concave%20logarithmic%20function.%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%20Convex%20and%20Concave%20Functions%0A%0A%20%20%20%20A%20real-valued%20function%20%24f%3A%20%5Cmathcal%7BC%7D%20%5Cto%20%5Cmathbb%7BR%7D%24%20defined%20on%20a%20convex%20set%20%24%5Cmathcal%7BC%7D%20%5Csubseteq%20%5Cmathbb%7BR%7D%5Ed%24%20is%20**convex**%20if%20for%20all%20%24%5Cmathbf%7Bx%7D_1%2C%20%5Cmathbf%7Bx%7D_2%20%5Cin%20%5Cmathcal%7BC%7D%24%20and%20all%20%24%5Calpha%20%5Cin%20%5B0%2C%201%5D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f(%5Calpha%20%5Cmathbf%7Bx%7D_1%20%2B%20(1%20-%20%5Calpha)%20%5Cmathbf%7Bx%7D_2)%20%5Cleq%20%5Calpha%20f(%5Cmathbf%7Bx%7D_1)%20%2B%20(1%20-%20%5Calpha)%20f(%5Cmathbf%7Bx%7D_2)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Geometric%20Meaning%0A%20%20%20%20The%20secant%20line%20segment%20(chord)%20connecting%20any%20two%20points%20%24(%5Cmathbf%7Bx%7D_1%2C%20f(%5Cmathbf%7Bx%7D_1))%24%20and%20%24(%5Cmathbf%7Bx%7D_2%2C%20f(%5Cmathbf%7Bx%7D_2))%24%20on%20the%20graph%20lies%20entirely%20above%20or%20on%20the%20function%20curve.%0A%0A%20%20%20%20*%20**Second-Order%20Condition**%3A%20If%20%24f%24%20is%20twice%20differentiable%2C%20%24f%24%20is%20convex%20if%20and%20only%20if%20its%20Hessian%20is%20positive%20semi-definite%20everywhere%3A%20%24%5Cnabla%5E2%20f(%5Cmathbf%7Bx%7D)%20%5Csucceq%200%24.%0A%20%20%20%20*%20**Concave%20Function**%3A%20A%20function%20%24g%24%20is%20concave%20if%20%24-g%24%20is%20convex%2C%20meaning%20the%20secant%20line%20segment%20lies%20below%20or%20on%20the%20graph.%20Examples%20include%20%24%5Cln(x)%24%20and%20%24%5Csqrt%7Bx%7D%24.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Statement%20of%20Jensen's%20Inequality%0A%0A%20%20%20%20%23%23%23%23%20Convex%20Function%20Case%0A%20%20%20%20Let%20%24f%24%20be%20a%20convex%20function%2C%20and%20let%20%24X%24%20be%20a%20random%20variable%20with%20finite%20expectation%20%24%5Cmathbb%7BE%7D%5BX%5D%20%3C%20%5Cinfty%24.%20Then%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f(%5Cmathbb%7BE%7D%5BX%5D)%20%5Cleq%20%5Cmathbb%7BE%7D%5Bf(X)%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20function%20evaluated%20at%20the%20mean%20is%20less%20than%20or%20equal%20to%20the%20expected%20value%20of%20the%20function.%0A%0A%20%20%20%20%23%23%23%23%20Concave%20Function%20Case%0A%20%20%20%20Let%20%24g%24%20be%20a%20concave%20function%20(such%20as%20%24%5Cln(x)%24).%20Reversing%20the%20inequality%20yields%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20g(%5Cmathbb%7BE%7D%5BX%5D)%20%5Cgeq%20%5Cmathbb%7BE%7D%5Bg(X)%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20The%20Jensen%20Gap%20and%20Second-Order%20Taylor%20Approximation%0A%0A%20%20%20%20The%20difference%20between%20the%20two%20sides%20of%20Jensen's%20inequality%20is%20the%20**Jensen%20gap**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5CDelta_J%20%3D%20%5Cmathbb%7BE%7D%5Bf(X)%5D%20-%20f(%5Cmathbb%7BE%7D%5BX%5D)%20%5Cgeq%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Performing%20a%20second-order%20Taylor%20expansion%20of%20%24f(X)%24%20around%20the%20mean%20%24%5Cmu%20%3D%20%5Cmathbb%7BE%7D%5BX%5D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f(X)%20%5Capprox%20f(%5Cmu)%20%2B%20f'(%5Cmu)(X%20-%20%5Cmu)%20%2B%20%5Cfrac%7B1%7D%7B2%7D%20f''(%5Cmu)(X%20-%20%5Cmu)%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Taking%20mathematical%20expectations%20on%20both%20sides%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbb%7BE%7D%5Bf(X)%5D%20%5Capprox%20f(%5Cmathbb%7BE%7D%5BX%5D)%20%2B%200%20%2B%20%5Cfrac%7B1%7D%7B2%7D%20f''(%5Cmathbb%7BE%7D%5BX%5D)%20%5Ctext%7BVar%7D(X)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Therefore%2C%20the%20Jensen%20gap%20is%20approximately%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5CDelta_J%20%5Capprox%20%5Cfrac%7B1%7D%7B2%7D%20f''(%5Cmathbb%7BE%7D%5BX%5D)%20%5Ctext%7BVar%7D(X)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20fundamental%20formula%20reveals%20that%20the%20magnitude%20of%20the%20Jensen%20gap%20is%20governed%20by%20two%20factors%3A%0A%20%20%20%201.%20The%20curvature%20of%20the%20function%20(%24f''(%5Cmu)%24).%0A%20%20%20%202.%20The%20variance%20of%20the%20random%20variable%20(%24%5Ctext%7BVar%7D(X)%24).%0A%20%20%20%20If%20either%20%24f%24%20is%20linear%20(%24f''%20%3D%200%24)%20or%20%24X%24%20is%20deterministic%20(%24%5Ctext%7BVar%7D(X)%20%3D%200%24)%2C%20the%20gap%20vanishes%20with%20exact%20equality.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Derivation%20of%20the%20Evidence%20Lower%20Bound%20(ELBO)%20in%20Variational%20Inference%0A%0A%20%20%20%20In%20generative%20modeling%2C%20let%20%24%5Cmathbf%7Bx%7D%24%20be%20observed%20data%20and%20%24%5Cmathbf%7Bz%7D%24%20be%20latent%20variables.%20We%20want%20to%20maximize%20the%20marginal%20log-likelihood%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cln%20p(%5Cmathbf%7Bx%7D)%20%3D%20%5Cln%20%5Cint%20p(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7Bz%7D)%20%5C%2C%20d%5Cmathbf%7Bz%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Introducing%20an%20arbitrary%20proposal%20distribution%20%24q(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%24%20over%20the%20latent%20space%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cln%20p(%5Cmathbf%7Bx%7D)%20%3D%20%5Cln%20%5Cint%20q(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%20%5Cfrac%7Bp(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7Bz%7D)%7D%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%20%5C%2C%20d%5Cmathbf%7Bz%7D%20%3D%20%5Cln%20%5Cmathbb%7BE%7D_%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%5Cleft%5B%20%5Cfrac%7Bp(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7Bz%7D)%7D%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Since%20the%20natural%20logarithm%20function%20%24%5Cln(%5Ccdot)%24%20is%20strictly%20concave%2C%20applying%20Jensen's%20inequality%20yields%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cln%20%5Cmathbb%7BE%7D_%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%5Cleft%5B%20%5Cfrac%7Bp(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7Bz%7D)%7D%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%20%5Cright%5D%20%5Cgeq%20%5Cmathbb%7BE%7D_%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%5Cleft%5B%20%5Cln%20%5Cfrac%7Bp(%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7Bz%7D)%7D%7Bq(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%7D%20%5Cright%5D%20%3D%20%5Ctext%7BELBO%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20difference%20between%20the%20marginal%20log-likelihood%20and%20the%20ELBO%20is%20the%20Kullback-Leibler%20divergence%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cln%20p(%5Cmathbf%7Bx%7D)%20-%20%5Ctext%7BELBO%7D%20%3D%20D_%7B%5Ctext%7BKL%7D%7D%5Cleft(%20q(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%20%5C%2C%5C%7C%5C%2C%20p(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%20%5Cright)%20%5Cgeq%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Maximizing%20the%20ELBO%20directly%20minimizes%20the%20divergence%20between%20the%20variational%20posterior%20%24q(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%24%20and%20the%20true%20posterior%20%24p(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%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%20Geometric%20Secant%20Chords%20and%20the%20Jensen%20Gap%0A%0A%20%20%20%20The%20interactive%20subplots%20below%20display%20the%20geometric%20foundation%20of%20Jensen's%20inequality%3A%0A%20%20%20%20*%20**Left%20Panel**%3A%20Convex%20parabola%20%24f(x)%20%3D%20x%5E2%24.%20A%20discrete%20random%20variable%20takes%20values%20%24x_1%20%3D%201.0%24%20(%2440%5C%25%24%20probability)%20and%20%24x_2%20%3D%205.0%24%20(%2460%5C%25%24%20probability).%20The%20secant%20chord%20connects%20the%20points%20on%20the%20curve.%20The%20mean%20%24%5Cmathbb%7BE%7D%5BX%5D%20%3D%203.4%24%20evaluates%20to%20%24f(%5Cmathbb%7BE%7D%5BX%5D)%20%3D%2011.56%24%20on%20the%20curve%2C%20while%20%24%5Cmathbb%7BE%7D%5Bf(X)%5D%20%3D%2015.40%24%20sits%20on%20the%20secant%20chord.%20The%20vertical%20purple%20bar%20marks%20the%20Jensen%20gap%20%24%5CDelta%20%3D%203.84%24.%0A%20%20%20%20*%20**Right%20Panel**%3A%20Concave%20logarithm%20%24g(x)%20%3D%20%5Cln(x)%24.%20Connecting%20%24x_1%20%3D%200.6%24%20and%20%24x_2%20%3D%204.5%24%20with%20equal%20weights%20(%24p%20%3D%200.5%24)%20shows%20the%20secant%20chord%20lying%20entirely%20beneath%20the%20curve%3A%20%24%5Cmathbb%7BE%7D%5B%5Cln%20X%5D%20%3D%200.50%20%5Cleq%20%5Cln(%5Cmathbb%7BE%7D%5BX%5D)%20%3D%200.94%24%2C%20demonstrating%20the%20classical%20AM-GM%20inequality.%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%20%23%20Left%20Panel%3A%20Convex%20Function%20f(x)%20%3D%20x%5E2%0A%20%20%20%20x_grid_left%20%3D%20np.linspace(0.4%2C%205.6%2C%20200)%0A%20%20%20%20f_convex%20%3D%20x_grid_left**2%0A%0A%20%20%20%20x1_val%2C%20x2_val%20%3D%201.0%2C%205.0%0A%20%20%20%20p1_val%2C%20p2_val%20%3D%200.4%2C%200.6%0A%20%20%20%20e_x_convex%20%3D%20p1_val%20*%20x1_val%20%2B%20p2_val%20*%20x2_val%20%20%23%203.4%0A%20%20%20%20f_e_x%20%3D%20e_x_convex**2%20%20%23%2011.56%0A%20%20%20%20e_f_x%20%3D%20p1_val%20*%20(x1_val**2)%20%2B%20p2_val%20*%20(x2_val**2)%20%20%23%2015.40%0A%20%20%20%20jensen_gap_convex%20%3D%20e_f_x%20-%20f_e_x%20%20%23%203.84%0A%0A%20%20%20%20%23%20Right%20Panel%3A%20Concave%20Function%20g(x)%20%3D%20ln(x)%0A%20%20%20%20x_grid_right%20%3D%20np.linspace(0.3%2C%205.5%2C%20200)%0A%20%20%20%20g_concave%20%3D%20np.log(x_grid_right)%0A%0A%20%20%20%20x1_log%2C%20x2_log%20%3D%200.6%2C%204.5%0A%20%20%20%20e_x_log%20%3D%200.5%20*%20x1_log%20%2B%200.5%20*%20x2_log%20%20%23%202.55%0A%20%20%20%20g_e_x%20%3D%20float(np.log(e_x_log))%20%20%23%200.936%0A%20%20%20%20e_g_x%20%3D%20float(0.5%20*%20np.log(x1_log)%20%2B%200.5%20*%20np.log(x2_log))%20%20%23%200.497%0A%20%20%20%20jensen_gap_log%20%3D%20g_e_x%20-%20e_g_x%20%20%23%200.439%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%20f%22Convex%20f(x)%20%3D%20x%C2%B2%3A%20f(E%5BX%5D)%20%E2%89%A4%20E%5Bf(X)%5D%20(Gap%20%3D%20%7Bjensen_gap_convex%3A.2f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Concave%20g(x)%20%3D%20ln(x)%3A%20E%5Bln%20X%5D%20%E2%89%A4%20ln(E%5BX%5D)%20(Gap%20%3D%20%7Bjensen_gap_log%3A.2f%7D)%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%20Curve%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_grid_left%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Df_convex%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%232563eb%22%2C%20width%3D3.0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Convex%20Curve%20f(x)%20%3D%20x%C2%B2%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Secant%20Chord%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%3D%5Bx1_val%2C%20x2_val%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bx1_val**2%2C%20x2_val**2%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ea580c%22%2C%20width%3D2.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D9%2C%20color%3D%22%23ea580c%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Secant%20Chord%20(Expectation%20Line)%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Point%20on%20curve%20f(E%5BX%5D)%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%3D%5Be_x_convex%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bf_e_x%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D12%2C%20color%3D%22%2316a34a%22%2C%20symbol%3D%22circle%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22f(E%5BX%5D)%20%3D%20%7Bf_e_x%3A.2f%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Point%20on%20chord%20E%5Bf(X)%5D%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%3D%5Be_x_convex%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Be_f_x%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D12%2C%20color%3D%22%23dc2626%22%2C%20symbol%3D%22diamond%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22E%5Bf(X)%5D%20%3D%20%7Be_f_x%3A.2f%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Jensen%20Gap%20Bar%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%3D%5Be_x_convex%2C%20e_x_convex%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bf_e_x%2C%20e_f_x%5D%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%237c3aed%22%2C%20width%3D3.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Jensen%20Gap%20%CE%94%20%3D%20%7Bjensen_gap_convex%3A.2f%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Right%3A%20Concave%20Log%20Curve%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_grid_right%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dg_concave%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%232563eb%22%2C%20width%3D3.0)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Concave%20Curve%20g(x)%20%3D%20ln(x)%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%20%23%20Right%3A%20Secant%20Chord%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%3D%5Bx1_log%2C%20x2_log%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bnp.log(x1_log)%2C%20np.log(x2_log)%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ea580c%22%2C%20width%3D2.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D9%2C%20color%3D%22%23ea580c%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Secant%20Chord%20E%5Bln%20X%5D%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%20%23%20Right%3A%20Point%20on%20curve%20ln(E%5BX%5D)%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%3D%5Be_x_log%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bg_e_x%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D12%2C%20color%3D%22%2316a34a%22%2C%20symbol%3D%22circle%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22ln(E%5BX%5D)%20%3D%20%7Bg_e_x%3A.2f%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%20%23%20Right%3A%20Point%20on%20chord%20E%5Bln%20X%5D%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%3D%5Be_x_log%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Be_g_x%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D12%2C%20color%3D%22%23dc2626%22%2C%20symbol%3D%22diamond%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22E%5Bln%20X%5D%20%3D%20%7Be_g_x%3A.2f%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%20%23%20Right%3A%20AM-GM%20Gap%20Bar%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%3D%5Be_x_log%2C%20e_x_log%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Be_g_x%2C%20g_e_x%5D%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%237c3aed%22%2C%20width%3D3.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22AM-GM%20Log%20Gap%20%3D%20%7Bjensen_gap_log%3A.2f%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%22x%22%2C%20range%3D%5B0.0%2C%205.8%5D%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22f(x)%20%3D%20x%C2%B2%22%2C%20range%3D%5B0%2C%2030%5D%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20xaxis2%3Ddict(title%3D%22x%22%2C%20range%3D%5B0.0%2C%205.8%5D%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20yaxis2%3Ddict(title%3D%22g(x)%20%3D%20ln(x)%22%2C%20range%3D%5B-1.2%2C%202.0%5D%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%20Numerical%20Verification%20of%20Jensen's%20Inequality%20and%20Second-Order%20Taylor%20Approximation%0A%0A%20%20%20%20In%20this%20example%2C%20we%20draw%20%24N%20%3D%20100%2C000%24%20samples%20from%20an%20Exponential%20distribution%20%24X%20%5Csim%20%5Ctext%7BExp%7D(%5Clambda%20%3D%201.5)%24%20(%24%5Cmu%20%3D%20%5Cfrac%7B1%7D%7B1.5%7D%20%5Capprox%200.667%2C%20%5Csigma%5E2%20%3D%20%5Cfrac%7B1%7D%7B1.5%5E2%7D%20%5Capprox%200.444%24).%0A%0A%20%20%20%20We%20evaluate%3A%0A%20%20%20%201.%20%24f_1(x)%20%3D%20x%5E2%24%20(convex)%0A%20%20%20%202.%20%24f_2(x)%20%3D%20e%5E%7B0.8%20x%7D%24%20(convex)%0A%20%20%20%203.%20%24g_1(x)%20%3D%20-%5Cln(x)%24%20(convex)%0A%20%20%20%204.%20%24g_2(x)%20%3D%20%5Csqrt%7Bx%7D%24%20(concave)%0A%0A%20%20%20%20For%20each%20function%2C%20we%20verify%20the%20inequality%20and%20compare%20the%20empirical%20Jensen%20gap%20with%20the%20second-order%20Taylor%20approximation%20%24%5CDelta_J%20%5Capprox%20%5Cfrac%7B1%7D%7B2%7D%20f''(%5Cmu)%20%5Csigma%5E2%24.%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_param_ex%20%3D%201.5%0A%20%20%20%20n_sample_ex%20%3D%20100000%0A%0A%20%20%20%20x_samples_ex%20%3D%20rng_ex1.exponential(scale%3D1.0%20%2F%20lambda_param_ex%2C%20size%3Dn_sample_ex)%0A%20%20%20%20mu_emp%20%3D%20float(np.mean(x_samples_ex))%0A%20%20%20%20var_emp%20%3D%20float(np.var(x_samples_ex))%0A%0A%20%20%20%20test_functions%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22Quadratic%20f(x)%20%3D%20x%C2%B2%22%2C%20lambda%20t%3A%20t**2%2C%20lambda%20t%3A%202.0%2C%20%22Convex%22%2C%20%22f(E%5BX%5D)%20%E2%89%A4%20E%5Bf(X)%5D%22)%2C%0A%20%20%20%20%20%20%20%20(%22Exponential%20f(x)%20%3D%20e%5E(0.8x)%22%2C%20lambda%20t%3A%20np.exp(0.8%20*%20t)%2C%20lambda%20t%3A%200.64%20*%20np.exp(0.8%20*%20t)%2C%20%22Convex%22%2C%20%22f(E%5BX%5D)%20%E2%89%A4%20E%5Bf(X)%5D%22)%2C%0A%20%20%20%20%20%20%20%20(%22Negative%20Log%20f(x)%20%3D%20-ln(x)%22%2C%20lambda%20t%3A%20-np.log(t)%2C%20lambda%20t%3A%201.0%20%2F%20(t**2)%2C%20%22Convex%22%2C%20%22f(E%5BX%5D)%20%E2%89%A4%20E%5Bf(X)%5D%22)%2C%0A%20%20%20%20%20%20%20%20(%22Square%20Root%20g(x)%20%3D%20%E2%88%9Ax%22%2C%20lambda%20t%3A%20np.sqrt(t)%2C%20lambda%20t%3A%20-0.25%20*%20(t**(-1.5))%2C%20%22Concave%22%2C%20%22E%5Bg(X)%5D%20%E2%89%A4%20g(E%5BX%5D)%22)%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20jensen_verification_records%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20name_func%2C%20fn%2C%20d2_fn%2C%20curvature_type%2C%20ineq_rule%20in%20test_functions%3A%0A%20%20%20%20%20%20%20%20f_of_mean%20%3D%20float(fn(mu_emp))%0A%20%20%20%20%20%20%20%20mean_of_f%20%3D%20float(np.mean(fn(x_samples_ex)))%0A%20%20%20%20%20%20%20%20empirical_gap%20%3D%20abs(mean_of_f%20-%20f_of_mean)%0A%20%20%20%20%20%20%20%20approx_taylor_gap%20%3D%20abs(0.5%20*%20d2_fn(mu_emp)%20*%20var_emp)%0A%0A%20%20%20%20%20%20%20%20if%20curvature_type%20%3D%3D%20%22Convex%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20satisfied%20%3D%20f_of_mean%20%3C%3D%20(mean_of_f%20%2B%201e-7)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20satisfied%20%3D%20mean_of_f%20%3C%3D%20(f_of_mean%20%2B%201e-7)%0A%0A%20%20%20%20%20%20%20%20jensen_verification_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%22Function%22%3A%20name_func%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Type%22%3A%20curvature_type%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22f(E%5BX%5D)%22%3A%20f%22%7Bf_of_mean%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22E%5Bf(X)%5D%22%3A%20f%22%7Bmean_of_f%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Empirical%20Gap%22%3A%20f%22%7Bempirical_gap%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Taylor%20Approx%20(0.5%20f''%20%CF%83%C2%B2)%22%3A%20f%22%7Bapprox_taylor_gap%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Inequality%20Satisfied%22%3A%20str(satisfied)%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(jensen_verification_records%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(jensen_verification_records%2C%20mo%2C%20pd)%3A%0A%20%20%20%20df_jensen%20%3D%20pd.DataFrame(jensen_verification_records)%0A%20%20%20%20mo.ui.table(df_jensen)%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%20Variational%20Autoencoder%20(VAE)%20ELBO%20Decomposition%0A%0A%20%20%20%20In%20a%20latent%20variable%20model%2C%20observed%20data%20%24x%24%20is%20generated%20via%20latent%20code%20%24z%20%5Csim%20%5Cmathcal%7BN%7D(0%2C%201)%24%20with%20emission%20probability%20%24p(x%7Cz)%20%3D%20%5Cmathcal%7BN%7D(z%2C%20%5Csigma_x%5E2%20%3D%201.0)%24.%0A%0A%20%20%20%20For%20an%20observation%20%24x%20%3D%202.0%24%3A%0A%20%20%20%201.%20The%20true%20marginal%20likelihood%20is%20computed%20via%20exact%20Gaussian%20convolution%3A%0A%0A%20%20%20%20%24%24p(x)%20%3D%20%5Cmathcal%7BN%7D(0%2C%20%5Csigma_z%5E2%20%2B%20%5Csigma_x%5E2%20%3D%202.0)%20%5Cimplies%20%5Cln%20p(x)%20%5Capprox%20-1.8663%24%24%0A%0A%20%20%20%202.%20We%20test%20three%20variational%20candidate%20posteriors%20%24q(z)%20%3D%20%5Cmathcal%7BN%7D(%5Cmu_q%2C%20%5Csigma_q%5E2)%24%3A%0A%20%20%20%20%20%20%20*%20**Poor%20Proposal**%3A%20%24%5Cmu_q%20%3D%20-1.0%2C%20%5Csigma_q%20%3D%200.5%24%0A%20%20%20%20%20%20%20*%20**Moderate%20Proposal**%3A%20%24%5Cmu_q%20%3D%200.5%2C%20%5Csigma_q%20%3D%200.8%24%0A%20%20%20%20%20%20%20*%20**Optimal%20Posterior**%3A%20%24%5Cmu_q%20%3D%201.0%2C%20%5Csigma_q%20%3D%201%20%2F%20%5Csqrt%7B2%7D%20%5Capprox%200.707%24%0A%0A%20%20%20%20We%20verify%20that%20%24%5Ctext%7BELBO%7D(q)%20%5Cleq%20%5Cln%20p(x)%24%20holds%20universally%2C%20with%20the%20Jensen%20gap%20exactly%20matching%20%24D_%7B%5Ctext%7BKL%7D%7D(q%20%5C%7C%20p(z%7Cx))%24.%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%20x_obs%20%3D%202.0%0A%20%20%20%20sigma_prior%20%3D%201.0%0A%20%20%20%20sigma_likelihood%20%3D%201.0%0A%0A%20%20%20%20%23%20True%20marginal%20likelihood%3A%20X%20~%20N(0%2C%201%20%2B%201%20%3D%202)%0A%20%20%20%20var_marginal%20%3D%20sigma_prior**2%20%2B%20sigma_likelihood**2%0A%20%20%20%20p_x_exact%20%3D%20(1.0%20%2F%20np.sqrt(2.0%20*%20np.pi%20*%20var_marginal))%20*%20np.exp(-0.5%20*%20(x_obs**2)%20%2F%20var_marginal)%0A%20%20%20%20log_p_exact%20%3D%20float(np.log(p_x_exact))%0A%0A%20%20%20%20%23%20True%20posterior%20p(z%7Cx)%3A%20N(x%2F2%20%3D%201.0%2C%201%2F2%20%3D%200.5)%0A%20%20%20%20mu_true_post%20%3D%20x_obs%20%2F%202.0%0A%20%20%20%20var_true_post%20%3D%200.5%0A%20%20%20%20sd_true_post%20%3D%20np.sqrt(var_true_post)%0A%0A%20%20%20%20variational_proposals%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22Candidate%20A%20(Poorly%20Fitted)%22%2C%20-1.0%2C%200.50)%2C%0A%20%20%20%20%20%20%20%20(%22Candidate%20B%20(Moderate%20Fit)%22%2C%200.50%2C%200.80)%2C%0A%20%20%20%20%20%20%20%20(%22Candidate%20C%20(Exact%20Posterior)%22%2C%20mu_true_post%2C%20sd_true_post)%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20elbo_decomposition_records%20%3D%20%5B%5D%0A%20%20%20%20rng_vae%20%3D%20np.random.default_rng(202)%0A%20%20%20%20num_mc_latent%20%3D%20200000%0A%0A%20%20%20%20for%20label_q%2C%20mu_q%2C%20sd_q%20in%20variational_proposals%3A%0A%20%20%20%20%20%20%20%20z_samples%20%3D%20rng_vae.normal(mu_q%2C%20sd_q%2C%20size%3Dnum_mc_latent)%0A%0A%20%20%20%20%20%20%20%20%23%20log%20p(z)%0A%20%20%20%20%20%20%20%20log_pz%20%3D%20-0.5%20*%20np.log(2.0%20*%20np.pi)%20-%200.5%20*%20(z_samples**2)%0A%20%20%20%20%20%20%20%20%23%20log%20p(x%7Cz)%0A%20%20%20%20%20%20%20%20log_px_given_z%20%3D%20-0.5%20*%20np.log(2.0%20*%20np.pi)%20-%200.5%20*%20((x_obs%20-%20z_samples)%20**%202)%0A%20%20%20%20%20%20%20%20%23%20log%20q(z)%0A%20%20%20%20%20%20%20%20log_qz%20%3D%20-0.5%20*%20np.log(2.0%20*%20np.pi%20*%20(sd_q**2))%20-%200.5%20*%20((z_samples%20-%20mu_q)%20**%202)%20%2F%20(sd_q**2)%0A%0A%20%20%20%20%20%20%20%20%23%20ELBO%20%3D%20E_q%20%5B%20log%20p(x%2C%20z)%20-%20log%20q(z)%20%5D%0A%20%20%20%20%20%20%20%20elbo_estimate%20%3D%20float(np.mean(log_pz%20%2B%20log_px_given_z%20-%20log_qz))%0A%0A%20%20%20%20%20%20%20%20%23%20KL%20divergence%20between%20Gaussian%20proposals%3A%20KL(N(mu_q%2C%20sd_q%5E2)%20%7C%7C%20N(mu_post%2C%20var_post))%0A%20%20%20%20%20%20%20%20kl_div%20%3D%20float(%0A%20%20%20%20%20%20%20%20%20%20%20%20np.log(sd_true_post%20%2F%20sd_q)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(sd_q**2%20%2B%20(mu_q%20-%20mu_true_post)%20**%202)%20%2F%20(2.0%20*%20var_true_post)%0A%20%20%20%20%20%20%20%20%20%20%20%20-%200.5%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20elbo_decomposition_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%22Variational%20Family%20q(z)%22%3A%20label_q%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Parameters%20(%CE%BC_q%2C%20%CF%83_q)%22%3A%20f%22%CE%BC%3D%7Bmu_q%3A.2f%7D%2C%20%CF%83%3D%7Bsd_q%3A.3f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22True%20log%20p(x)%22%3A%20f%22%7Blog_p_exact%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22ELBO%20Lower%20Bound%22%3A%20f%22%7Belbo_estimate%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Jensen%20Gap%20(KL%20Divergence)%22%3A%20f%22%7Bkl_div%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Lower%20Bound%20Holds%3F%22%3A%20str(elbo_estimate%20%3C%3D%20log_p_exact%20%2B%201e-4)%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(elbo_decomposition_records%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(elbo_decomposition_records%2C%20mo%2C%20pd)%3A%0A%20%20%20%20df_elbo%20%3D%20pd.DataFrame(elbo_decomposition_records)%0A%20%20%20%20mo.ui.table(df_elbo)%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
4d826e17791a3b805666414472288a06