def ismc(g, xdists, u_to_x, T, seed, maxitr, tol, ftol, eps): """ Importance sampling Monte Carlo. """ # Seed the random number generator if required if seed == -1: prng = RandomState() else: prng = RandomState(seed) # Use FORM to get estimate of u* u_beta = slsqp(g, xdists, u_to_x, T, maxitr, tol, ftol, eps)['u_beta'] # Generate standard normal samples centred at u* covmat = eye(len(xdists)) v = prng.multivariate_normal(u_beta, covmat, size=maxitr).T g_mc = g(u_to_x(v, xdists, T)) # Define importance sampling functions weighting functions fv = multivariate_normal(zeros(len(xdists)), covmat).pdf hv = multivariate_normal(u_beta, covmat).pdf # Convert g-function output to pass/fail indicator function and estimate pf g_mc[g_mc>0] = 0 g_mc[g_mc<0] = 1 indfunc = g_mc * fv(v.T)/hv(v.T) mu_pf = indfunc.mean() beta = -norm.ppf(mu_pf) if mu_pf < 0.5 else norm.ppf(mu_pf) # Convergence metrics (standard deviation, standard error, CoV of s.e.) std_pf = indfunc.std(ddof=1) # Calculate sample standard deviation se_pf = std_pf/sqrt(maxitr) cv_pf = se_pf/mu_pf return {'vars': xdists, 'beta': beta, 'Pf': mu_pf, 'stderr': se_pf, 'stdcv': cv_pf}
def relython(inp): """ Main function. """ # Validate input err = io.validinp(inp) # Proceed if there are no validation errors if len(err) == 0: res = {} # Parse and prepare input print('Parsing input...') gfuncs, xdists, cmat_x = io.parseinp(inp) inp['xdists'] = xdists # So can show mus and sigs in pprint # Convert correlation matrix from X- to U-space print('Converting correlation matrix to U-space...') cmat_u = tr.corrmat_u(cmat_x, xdists) # Generate transformation matrix from standard to correlated U-space print('Generating transformation from standard to correlated U...') T = tr.utrans(cmat_u) for i, g in enumerate(gfuncs): res[i] = [] for j, slvr in enumerate(inp['solver']): print('Solving: {0}...'.format(slvr)) # Direct estimation methods if slvr.upper() == 'HLRF': res[i].append(sl.hlrf(g, xdists, tr.u_to_x, T, inp['maxitr'][j], inp['tol'], inp['ftol'], inp['eps'])) elif slvr.upper() == 'IHLRF': res[i].append(sl.ihlrf(g, xdists, tr.u_to_x, T, inp['maxitr'][j], inp['tol'], inp['ftol'], inp['eps'])) elif slvr.upper() == 'SLSQP': res[i].append(sl.slsqp(g, xdists, tr.u_to_x, T, inp['maxitr'][j], inp['tol'], inp['ftol'], inp['eps'])) # Monte Carlo simulation methods elif slvr.upper() == 'CMC': res[i].append(sl.cmc(g, xdists, tr.u_to_x, T, inp['seed'], inp['maxitr'][j])) elif slvr.upper() == 'ISMC': res[i].append(sl.ismc(g, xdists, tr.u_to_x, T, inp['seed'], inp['maxitr'][j], inp['tol'], inp['ftol'], inp['eps'])) elif slvr.upper() == 'DSIM': res[i].append(sl.dsim(g, xdists, tr.u_to_x, T, inp['seed'], inp['maxitr'][j], inp['tol'], inp['ftol'], inp['eps'])) else: continue output = io.pprint(inp, cmat_x, cmat_u, res, __version__, __author__) if 'outpath' in inp.keys(): try: with open(inp['outpath'], 'w') as f: f.write(output) except: print('\nUnable to write to {0}\n'.format(inp['outpath'])) print(output) return res else: if inp['showresults'] == 1: print('\nErrors:\n{0}'.format('\n'.join(err))) return err