(xu (xf ( vargl (xegl ( (xn gl (xn gl (xn gl ( hencethe optimal parameter lminimizes the function (le [ (xf ( )/gl ( )]which is unknownbut can be estimated from simulation to solve this stochastic minimization problemwe first use stochastic approximation thusat each step of the algorithmthe gradient of (lis estimated from realizations of (lh (xf ( )/gl ( )where fb as in the original problem (that isthe estimation of )the parameter should be large enough to avoid any bias in the estimator of lbut also small enough to ensure small variance the following python code implements particular instance of algorithm for sampling from fb herewe used instead of as this will improve the crude monte carlo estimation of lwithout noticeably affecting the bias the gradient of (lis estimated in lines - using the central difference estimator ( notice how for the ( - / and ( + / the same random vector [ ]is used this significantly reduces the variance of the gradient estimatorsee also exercise the (xt lt given the large gradient herewe choose step size bt should be such that bt - and decrease it each step by factor of figure shows how the sequence decreases towards approximately which we take as an estimator for the optimal importance sampling parameter lstochapprox py import numpy as np from numpy import pi import matplotlib pyplot as plt = choose large enough but not too large delta lambda ( * )** np exp(-np sqrt( ** ** / *np sin ( np sqrt( ** ** + ))* ** ** < ** /( )** lambda lamlam*np exp(-np sqrt( ** ** )*lam)/np sqrt( ** ** /( pibeta *- #step size very small as the gradient is large lam = lams np array (lam ] = ** for in range ( - * *np random rand( , - * *np random rand( , laml lam delta / lamr lam delta / estl np mean( ( , )** * / ( laml)estr np mean( ( , )** * / ( lamr)#use same , gr (estr -estl)delta gradient lam lam gr*beta gradient descend lams np hstack (lams lam)beta beta * lamsize range ( (lams size)plt plot(lamsize lamsplt show (