<jupyter_start><jupyter_text>Stochastic Reduce Order Model Example 2 Author: <NAME> Date: June 4, 2018 In this example, Stratified sampling is used to generate samples from Gamma distribution and weights are defined using Stochastic Reduce Order Model (SROM). This example illustrate how to define same weights for each sample of a random variable. Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to import the STS and SROM class from UQpy.<jupyter_code>from UQpy.Surrogates import SROM
from UQpy.SampleMethods import STS
import scipy.stats as stats
import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Using UQpy STS class to generate samples for two random variables having Gamma distribution with shape, shift and scale parameters as 2, 1 and 3.<jupyter_code>x = STS(dimension=2, dist_name=['Gamma', 'Gamma'], dist_params=[[2, 1, 3], [2, 1, 3]], 
        sts_design=[4, 4])<jupyter_output>UQpy: Successful execution of STS design..<jupyter_text>Define the Gamma probability density function up to a scale factor. Here the pdf is defined directly in the python script.<jupyter_code>def Gamma(z,params):
     return stats.gamma.cdf(z, params[0], loc=params[1], scale=params[2])<jupyter_output><empty_output><jupyter_text>Run SROM using the defined Gamma distribution. Here we use the following parameters.- Gamma distribution with shape, shift and scale parameters as 2, 1 and 3.- First and second order moments about origin are 6 and 54.- Notice that pdf_target references the Gamma function directly and does not designate it as a string.- Samples are uncorrelated, i.e. also default value of correlation.<jupyter_code>y1 = SROM(samples=x.samples, cdf_target=[Gamma], moments=[[6., 6.], [54., 54.]], properties=[True, True, True, False], 
          cdf_target_params=[2, 1, 3])<jupyter_output>UQpy: Performing SROM...
Done!<jupyter_text>In this case, sample_weights are generated using default values of weights_distribution, weights_moments and weights_correlation. Default values are:<jupyter_code>print('weights_distribution', '\n', y1.weights_distribution, '\n', 'weights_moments', '\n', y1.weights_moments, '\n', 
      'weights_correlation', '\n', y1.weights_correlation)
y2 = SROM(samples=x.samples, cdf_target=[Gamma], moments=[[6., 6.], [54., 54.]], properties=[True, True, True, False], 
          cdf_target_params=[2, 1, 3], weights_distribution=[[0.4, 0.5]], weights_moments=[[0.2, 0.7]], 
          weights_correlation=[[0.3, 0.4], [0.4, 0.6]])<jupyter_output>UQpy: Performing SROM...
Done!<jupyter_text>In second case, weights_distribution is modified by SROM class. First, it defines an array of size 2×16 with all elements equal to 1 and then multiply first column by 0.4 and second column by 0.5 . Similarly, weights_moments and weights_correlation are modified.<jupyter_code>print('weights_distribution', '\n', y2.weights_distribution, '\n', 'weights_moments', '\n', y2.weights_moments, '\n', 
      'weights_correlation', '\n', y2.weights_correlation)<jupyter_output>weights_distribution 
 [[0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]
 [0.4 0.5]] 
 weights_moments 
 [[0.2 0.7]
 [0.2 0.7]] 
 weights_correlation 
 [[0.3 0.4]
 [0.4 0.6]]<jupyter_text>Plot below shows the comparison of samples weights generated using two different weights with the actual CDF of gamma distribution.<jupyter_code>c1 = np.concatenate((y1.samples, y1.sample_weights.reshape(y1.sample_weights.shape[0], 1)), axis=1)
d1 = c1[c1[:, 0].argsort()]
c2 = np.concatenate((y2.samples, y2.sample_weights.reshape(y2.sample_weights.shape[0], 1)), axis=1)
d2 = c2[c2[:, 0].argsort()]
plt.plot(d1[:, 0], np.cumsum(d1[:, 2], axis=0), 'x')
plt.plot(d2[:, 0], np.cumsum(d2[:, 2], axis=0), 'o')
plt.plot(np.arange(1,15,0.1), stats.gamma.cdf(np.arange(1,15,0.1), 2, loc=1, scale=3))
plt.legend(['Case 1','Case 2','CDF'])
plt.title('1st random variable')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1397220190197513, 'fraction_numerical': 0.049987807851743477, 'mean_word_length': 5.059084194977843, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '435843', 'license': 'MIT', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1414, 'n_words': 439, 'path': 'example/SROM/SROM_Example2.ipynb', 'repo_name': 'volpatto/UQpy', 'size': '4111'}