<jupyter_start><jupyter_text>Quick tutorial`chaospy` is created to work well inside numerical [Python](https://python.org) ecosystem through `numpy`.It is therefore often necessary to import and use `numpy` along side `chaospy` when working on problems the latter is meant to solve:<jupyter_code>import numpy
import chaospy<jupyter_output><empty_output><jupyter_text>`chaospy` is very much problem agnostic, so you can use your own code using any means you find fit.Typically, for the sake of the guide, we assume that whatever you are modeling, it can be placed inside a python function.It will assume that the function input is a vector parameters and the output is the quantity of interest, where both should be compatible with `numpy.ndarray` format.For this example, let us assume that our model of interest is a simple exponential model that takes 2 parameters in and 100 coordinates out:<jupyter_code>coordinates = numpy.linspace(0, 10, 1000)


def model_solver(parameters, coordinates=coordinates):
    """Simple ordinary differential equation solver."""
    alpha, beta = parameters
    return alpha*numpy.e**(-coordinates*beta)<jupyter_output><empty_output><jupyter_text>We assume that the parameters are unknown, the model shape will not have a fixed shape.For example:<jupyter_code>from matplotlib import pyplot

for params in [(1.3, 0.13), (1.7, 0.17), (1.1, 0.19), (1.9, 0.11)]:
    pyplot.plot(coordinates, model_solver(params))
pyplot.show()<jupyter_output><empty_output><jupyter_text>We here assume that `parameters` contains aleatory variability with known probability.We formalize this probability in `chaospy` as a joint probability distribution.For example:<jupyter_code>alpha = chaospy.Normal(1.5, 0.2)
beta = chaospy.Uniform(0.1, 0.2)
joint = chaospy.J(alpha, beta)<jupyter_output><empty_output><jupyter_text>which we can visualize as follows:<jupyter_code>grid = numpy.mgrid[0.9:2.1:100j, 0.09:0.21:100j]
pyplot.contourf(grid[0], grid[1], joint.pdf(grid), 30)
pyplot.scatter(*joint.sample(100, rule="sobol"))
pyplot.show()<jupyter_output><empty_output><jupyter_text>Most probability distributions have an associated expansion of orthogonal polynomials.These can be automatically constructed:<jupyter_code>expansion = chaospy.generate_expansion(8, joint)
expansion[:5].round(8)<jupyter_output><empty_output><jupyter_text>Here the polynomial is defined positional, such that `q0` and `q1` refers to the uniform and normal distribution respectively.The distribution can also be used to create (pseudo-)random samples and low-discrepancy sequences.For example to create samples from the Sobol sequence:<jupyter_code>samples = joint.sample(1000, rule="sobol")
samples[:, :4].round(8)<jupyter_output><empty_output><jupyter_text>We can evaluating the forward solver using these samples:<jupyter_code>evaluations = numpy.array([model_solver(sample) for sample in samples.T])
evaluations[:3, :5].round(8)
pyplot.plot(coordinates, evaluations.T, alpha=0.02)
pyplot.show()<jupyter_output><empty_output><jupyter_text>Having all these components in place, we have enough components to perform point collocation.Or in other words, we can create a polynomial approximation of ``forward_solver``:<jupyter_code>approx_solver = chaospy.fit_regression(expansion, samples, evaluations)
approx_solver[:2].round(4)
pyplot.plot(coordinates, approx_solver(*samples), alpha=0.02)
pyplot.show()<jupyter_output><empty_output><jupyter_text>Since the model approximations are polynomials, we can do inference on them directly.For example:<jupyter_code>expected = chaospy.E(approx_solver, joint)
expected[:5].round(8)
deviation = chaospy.Std(approx_solver, joint)
deviation[:5].round(8)
pyplot.fill_between(
    coordinates, expected-2*deviation, expected+2*deviation, alpha=0.4)
pyplot.plot(coordinates, expected)
pyplot.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1025441329179647, 'fraction_numerical': 0.023883696780893044, 'mean_word_length': 7.286021505376344, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 1, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '106133', 'license': 'MIT', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1188, 'n_words': 356, 'path': 'docs/user_guide/quick_tutorial.ipynb', 'repo_name': 'utsekaj42/chaospy', 'size': '3852'}