def initialize_component_surrogates(self, component_options): """ Initialize a surrogate of each system component. Parameters ---------- component_options : iterable List of dictionary of options containing the arguments necessary to initialize each surrogate See documentation of :func:`pyapprox.approximate.adaptive_approximate_sparse_grid` """ # self.random_samples_for_refinement_test = \ # generate_independent_random_samples( # self.variables, self.nrefinement_samples) marginal_icdfs = [v.ppf for v in self.variables.all_variables()] self.random_samples_for_refinement_test = \ transformed_halton_sequence( marginal_icdfs, len(marginal_icdfs), self.nrefinement_samples) surr_graph = self.surrogate_network.graph functions = [] for nid in surr_graph.nodes: options = component_options[nid] functions.append( self.initialize_surrogate(surr_graph.nodes[nid], **options)) self.surrogate_network.set_functions(functions) # Add first index of each variable to active set of respective grid self.component_output_ranges = [] for nid in surr_graph.nodes: if self.verbose > 0: print('------------------------------------') print(f'Refining component {surr_graph.nodes[nid]["label"]}') surr_graph.nodes[nid]['functions'].refine() if self.verbose > 0: print('------------------------------------')
def generate_samples(num_samples): from pyapprox.low_discrepancy_sequences import \ transformed_halton_sequence samples = transformed_halton_sequence(None, num_vars, num_samples) samples = samples*2.-1. return samples
def plot_lognormal_example_exact_quantities( num_samples=int(2e5), plot=False, mu=0, sigma=1): num_vars = 1 if plot: assert num_samples <= 1e5 else: assert num_samples >= 1e4 f, f_cdf, f_pdf, VaR, CVaR, ssd, ssd_disutil = \ get_lognormal_example_exact_quantities(mu, sigma) from pyapprox.low_discrepancy_sequences import transformed_halton_sequence #samples = np.random.normal(mu,sigma,(num_vars,num_samples)) #values = f(samples)[:,0] samples = transformed_halton_sequence( [partial(stats.norm.ppf, loc=mu, scale=sigma)], num_vars, num_samples) values = f(samples)[:, 0] fig, axs = plt.subplots(1, 6, sharey=False, figsize=(16, 6)) from pyapprox.density import EmpiricalCDF if plot: ygrid = np.linspace(-1, 5, 100) #ecdf = EmpiricalCDF(values) # axs[0].plot(ygrid,ecdf(ygrid),'-') axs[0].plot(ygrid, f_cdf(ygrid), '--') # axs[0].set_xlim(ygrid.min(),ygrid.max()) axs[0].set_title('CDF') #ecdf = EmpiricalCDF(-values) # axs[0].plot(-ygrid,ecdf(-ygrid),'-') ygrid = np.linspace(-1, 20, 100) # axs[1].hist(values,bins='auto',density=True) axs[1].plot(ygrid, f_pdf(ygrid), '--') axs[1].set_xlim(ygrid.min(), ygrid.max()) axs[1].set_title('PDF') pgrid = np.linspace(1e-2, 1 - 1e-2, 100) evar = np.array([value_at_risk(values, p)[0] for p in pgrid]) # print(np.linalg.norm(evar.squeeze()-VaR(pgrid),ord=np.inf)) if plot: axs[2].plot(pgrid, evar, '-') axs[2].plot(pgrid, VaR(pgrid), '--') axs[2].set_title('VaR') else: assert np.allclose(evar.squeeze(), VaR(pgrid), atol=2e-1) pgrid = np.linspace(1e-2, 1 - 1e-2, 100) ecvar = np.array([conditional_value_at_risk(values, y) for y in pgrid]) # print(np.linalg.norm(ecvar.squeeze()-CVaR(pgrid).squeeze(),ord=np.inf)) print(CVaR(0.8)) if plot: axs[3].plot(pgrid, ecvar, '-') axs[3].plot(pgrid, CVaR(pgrid), '--') axs[3].set_xlim(pgrid.min(), pgrid.max()) axs[3].set_title('CVaR') else: assert np.allclose(ecvar.squeeze(), CVaR(pgrid).squeeze(), rtol=4e-2) #ygrid = np.linspace(-1,10,100) ygrid = np.linspace(logstats.norm.ppf(0.0, np.exp(mu), sigma), logstats.norm.ppf(0.9, np.exp(mu), sigma), 101) essd = compute_conditional_expectations(ygrid, values, False) # print(np.linalg.norm(essd.squeeze()-ssd(ygrid),ord=np.inf)) if plot: axs[4].plot(ygrid, essd, '-') axs[4].plot(ygrid, ssd(ygrid), '--') axs[4].set_xlim(ygrid.min(), ygrid.max()) axs[4].set_title(r'$E[(\eta-Y)^+]$') axs[4].set_xlabel(r'$\eta$') else: assert np.allclose(essd.squeeze(), ssd(ygrid), atol=1e-3) # zoom into ygrid over high probability region of -Y ygrid = -ygrid[::-1] disutil_essd = compute_conditional_expectations(ygrid, values, True) assert np.allclose(disutil_essd, compute_conditional_expectations(ygrid, -values, False)) # print(np.linalg.norm(disutil_essd.squeeze()-ssd_disutil(ygrid),ord=np.inf)) if plot: axs[5].plot(ygrid, disutil_essd, '-', label='Empirical') axs[5].plot(ygrid, ssd_disutil(ygrid), '--', label='Exact') axs[5].set_xlim((ygrid).min(), (ygrid).max()) axs[5].set_title(r'$E[(\eta-(-Y))^+]$') axs[5].set_xlabel(r'$\eta$') axs[5].plot([0], [np.exp(mu + sigma**2 / 2)], 'o') axs[5].legend() plt.show() else: assert np.allclose(disutil_essd.squeeze(), ssd_disutil(ygrid), atol=1e-3)