.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "user/_auto_tutorials/MultipleInput.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_user__auto_tutorials_MultipleInput.py: Multiple input model: bathtub demo =================================== This is a demo for bathtub water temperature and volume model using CUQIpy. We have measurements of the temperature and volume of the water in the bathtub and want to infer the temperature and volume of the hot water and the cold water that were used to fill in the bathtub .. GENERATED FROM PYTHON SOURCE LINES 12-14 Import libraries ---------------- .. GENERATED FROM PYTHON SOURCE LINES 14-18 .. code-block:: Python import cuqi import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 19-24 Define the forward map -------------------------- `h_v` is the volume of hot water, `h_t` is the temperature of hot water, `c_v` is the volume of cold water, and `c_t` is the temperature of cold water. .. GENERATED FROM PYTHON SOURCE LINES 24-33 .. code-block:: Python def forward_map(h_v, h_t, c_v, c_t): # volume volume = h_v + c_v # temperature temp = (h_v * h_t + c_v * c_t) / (h_v + c_v) return np.array([volume, temp]).reshape(2,) .. GENERATED FROM PYTHON SOURCE LINES 34-36 Define gradient functions with respect to the unknown parameters ----------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-63 .. code-block:: Python # Define the gradient with respect to h_v def gradient_h_v(direction, h_v, h_t, c_v, c_t): return ( direction[0] + (h_t / (h_v + c_v) - (h_v * h_t + c_v * c_t) / (h_v + c_v) ** 2) * direction[1] ) # Define the gradient with respect to h_t def gradient_h_t(direction, h_v, h_t, c_v, c_t): return (h_v / (h_v + c_v)) * direction[1] # Define the gradient with respect to c_v def gradient_c_v(direction, h_v, h_t, c_v, c_t): return ( direction[0] + (c_t / (h_v + c_v) - (h_v * h_t + c_v * c_t) / (h_v + c_v) ** 2) * direction[1] ) # Define the gradient with respect to c_t def gradient_c_t(direction, h_v, h_t, c_v, c_t): return (c_v / (h_v + c_v)) * direction[1] .. GENERATED FROM PYTHON SOURCE LINES 64-66 Define domain geometry and range geometry --------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 66-76 .. code-block:: Python domain_geometry = ( cuqi.geometry.Discrete(['h_v']), cuqi.geometry.Discrete(['h_t']), cuqi.geometry.Discrete(['c_v']), cuqi.geometry.Discrete(['c_t']) ) range_geometry = cuqi.geometry.Discrete(['temperature','volume']) .. GENERATED FROM PYTHON SOURCE LINES 77-79 Define the forward model object -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 79-87 .. code-block:: Python model = cuqi.model.Model( forward=forward_map, gradient=(gradient_h_v, gradient_h_t, gradient_c_v, gradient_c_t), domain_geometry=domain_geometry, range_geometry=range_geometry ) .. GENERATED FROM PYTHON SOURCE LINES 88-90 Experiment with partial evaluation of the model --------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 90-100 .. code-block:: Python print("\nmodel()\n", model()) print("\nmodel(h_v = 50)\n", model(h_v=50)) print("\nmodel(h_v = 50, h_t = 60)\n", model(h_v=50, h_t=60)) print("\nmodel(h_v = 50, h_t = 60, c_v = 30)\n", model(h_v=50, h_t=60, c_v=30)) print( "\nmodel(h_v = 50, h_t = 60, c_v = 30, c_t = 10)\n", model(h_v=50, h_t=60, c_v=30, c_t=10), ) .. rst-class:: sphx-glr-script-out .. code-block:: none model() CUQI Model: _ProductGeometry( Discrete[1] Discrete[1] Discrete[1] Discrete[1] ) -> Discrete[2]. Forward parameters: ['h_v', 'h_t', 'c_v', 'c_t']. model(h_v = 50) CUQI Model: _ProductGeometry( Discrete[1] Discrete[1] Discrete[1] ) -> Discrete[2]. Forward parameters: ['h_t', 'c_v', 'c_t']. model(h_v = 50, h_t = 60) CUQI Model: _ProductGeometry( Discrete[1] Discrete[1] ) -> Discrete[2]. Forward parameters: ['c_v', 'c_t']. model(h_v = 50, h_t = 60, c_v = 30) CUQI Model: Discrete[1] -> Discrete[2]. Forward parameters: ['c_t']. model(h_v = 50, h_t = 60, c_v = 30, c_t = 10) [80. 41.25] .. GENERATED FROM PYTHON SOURCE LINES 101-103 Define prior distributions for the unknown parameters ------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 103-111 .. code-block:: Python h_v_dist = cuqi.distribution.Uniform(0, 60, geometry=domain_geometry[0]) h_t_dist = cuqi.distribution.Uniform(40, 70, geometry=domain_geometry[1]) c_v_dist = cuqi.distribution.Uniform(0, 60, geometry=domain_geometry[2]) c_t_dist = cuqi.distribution.TruncatedNormal( 10, 2**2, 7, 15, geometry=domain_geometry[3] ) .. GENERATED FROM PYTHON SOURCE LINES 112-114 Define a data distribution ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 114-120 .. code-block:: Python data_dist = cuqi.distribution.Gaussian( mean=model(h_v_dist, h_t_dist, c_v_dist, c_t_dist), cov=np.array([[1**2, 0], [0, 0.5**2]]) ) .. GENERATED FROM PYTHON SOURCE LINES 121-123 Define a joint distribution of prior and data distributions --------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 123-132 .. code-block:: Python joint_dist = cuqi.distribution.JointDistribution( data_dist, h_v_dist, h_t_dist, c_v_dist, c_t_dist ) .. GENERATED FROM PYTHON SOURCE LINES 133-135 Define the posterior distribution by setting the observed data ------------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 135-140 .. code-block:: Python # Assume measured volume is 60 gallons and measured temperature is 38 degrees # celsius posterior = joint_dist(data_dist=np.array([60, 38])) .. GENERATED FROM PYTHON SOURCE LINES 141-143 Experiment with conditioning the posterior distribution ------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 143-152 .. code-block:: Python print("posterior", posterior) print("\nposterior(h_v_dist = 50)\n", posterior(h_v_dist=50)) print("\nposterior(h_v_dist = 50, h_t_dist = 60)\n", posterior(h_v_dist=50, h_t_dist=60)) print( "\nposterior(h_v_dist = 50, h_t_dist = 60, c_v_dist = 30)\n", posterior(h_v_dist=50, h_t_dist=60, c_v_dist=30), ) .. rst-class:: sphx-glr-script-out .. code-block:: none posterior JointDistribution( Equation: p(h_v_dist,h_t_dist,c_v_dist,c_t_dist|data_dist) ∝ L(h_t_dist,c_t_dist,c_v_dist,h_v_dist|data_dist)p(h_v_dist)p(h_t_dist)p(c_v_dist)p(c_t_dist) Densities: data_dist ~ CUQI Gaussian Likelihood function. Parameters ['h_v_dist', 'h_t_dist', 'c_v_dist', 'c_t_dist']. h_v_dist ~ CUQI Uniform. h_t_dist ~ CUQI Uniform. c_v_dist ~ CUQI Uniform. c_t_dist ~ CUQI TruncatedNormal. ) posterior(h_v_dist = 50) JointDistribution( Equation: p(h_t_dist,c_v_dist,c_t_dist|data_dist,h_v_dist) ∝ L(h_t_dist,c_t_dist,c_v_dist|data_dist)p(h_t_dist)p(c_v_dist)p(c_t_dist) Densities: data_dist ~ CUQI Gaussian Likelihood function. Parameters ['h_t_dist', 'c_v_dist', 'c_t_dist']. h_v_dist ~ EvaluatedDensity(-4.0943445622221) h_t_dist ~ CUQI Uniform. c_v_dist ~ CUQI Uniform. c_t_dist ~ CUQI TruncatedNormal. ) posterior(h_v_dist = 50, h_t_dist = 60) JointDistribution( Equation: p(c_v_dist,c_t_dist|data_dist,h_v_dist,h_t_dist) ∝ L(c_v_dist,c_t_dist|data_dist)p(c_v_dist)p(c_t_dist) Densities: data_dist ~ CUQI Gaussian Likelihood function. Parameters ['c_v_dist', 'c_t_dist']. h_v_dist ~ EvaluatedDensity(-4.0943445622221) h_t_dist ~ EvaluatedDensity(-3.4011973816621555) c_v_dist ~ CUQI Uniform. c_t_dist ~ CUQI TruncatedNormal. ) posterior(h_v_dist = 50, h_t_dist = 60, c_v_dist = 30) Posterior( Equation: p(c_t_dist|data_dist) ∝ L(c_t_dist|data_dist)p(c_t_dist) Densities: data_dist ~ CUQI Gaussian Likelihood function. Parameters ['c_t_dist']. c_t_dist ~ CUQI TruncatedNormal. ) .. GENERATED FROM PYTHON SOURCE LINES 153-157 Sample from the joint (posterior) distribution ------------------------------------------------------------ First define sampling strategy for Gibbs sampling .. GENERATED FROM PYTHON SOURCE LINES 157-169 .. code-block:: Python sampling_strategy = { "h_v_dist": cuqi.experimental.mcmc.MH( scale=0.2, initial_point=np.array([30])), "h_t_dist": cuqi.experimental.mcmc.MALA( scale=0.6, initial_point=np.array([50])), "c_v_dist": cuqi.experimental.mcmc.MALA( scale=0.6, initial_point=np.array([30])), "c_t_dist": cuqi.experimental.mcmc.MALA( scale=0.6, initial_point=np.array([10])), } .. GENERATED FROM PYTHON SOURCE LINES 170-171 Then create the sampler and sample the posterior distribution .. GENERATED FROM PYTHON SOURCE LINES 171-180 .. code-block:: Python hybridGibbs = cuqi.experimental.mcmc.HybridGibbs( posterior, sampling_strategy=sampling_strategy) hybridGibbs.warmup(100) hybridGibbs.sample(2000) samples = hybridGibbs.get_samples() .. rst-class:: sphx-glr-script-out .. code-block:: none Warmup: 0%| | 0/100 [00:00, ]], dtype=object) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.827 seconds) .. _sphx_glr_download_user__auto_tutorials_MultipleInput.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: MultipleInput.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: MultipleInput.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: MultipleInput.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_