.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_custom_density.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_custom_density.py: ================================== Using a custom density with Picard ================================== This example shows how to use custom densities using Picard .. GENERATED FROM PYTHON SOURCE LINES 9-20 .. code-block:: default # Author: Pierre Ablin # Alexandre Gramfort # License: BSD 3 clause import numpy as np import matplotlib.pyplot as plt from picard import picard, permute print(__doc__) .. GENERATED FROM PYTHON SOURCE LINES 21-22 Build a custom density where the score function is x + tanh(x) .. GENERATED FROM PYTHON SOURCE LINES 22-35 .. code-block:: default class CustomDensity(object): def log_lik(self, Y): return Y ** 2 / 2 + np.log(np.cosh(Y)) def score_and_der(self, Y): tanhY = np.tanh(Y) return Y + tanhY, 2 - tanhY ** 2 custom_density = CustomDensity() .. GENERATED FROM PYTHON SOURCE LINES 36-37 Plot the corresponding functions .. GENERATED FROM PYTHON SOURCE LINES 37-51 .. code-block:: default x = np.linspace(-2, 2, 100) log_likelihood = custom_density.log_lik(x) psi, psi_der = custom_density.score_and_der(x) names = ['log-likelihood', 'score', 'score derivative'] plt.figure() for values, name in zip([log_likelihood, psi, psi_der], names): plt.plot(x, values, label=name) plt.legend() plt.title("Custom density") plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_custom_density_001.png :alt: Custom density :srcset: /auto_examples/images/sphx_glr_plot_custom_density_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 52-53 Run Picard on toy dataset using this density .. GENERATED FROM PYTHON SOURCE LINES 53-65 .. code-block:: default rng = np.random.RandomState(0) N, T = 5, 1000 S = rng.laplace(size=(N, T)) A = rng.randn(N, N) X = np.dot(A, S) K, W, Y = picard(X, fun=custom_density, random_state=0) plt.figure() plt.imshow(permute(W.dot(K).dot(A)), interpolation='nearest') plt.title('Product between the estimated unmixing matrix and the mixing' 'matrix') plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_custom_density_002.png :alt: Product between the estimated unmixing matrix and the mixingmatrix :srcset: /auto_examples/images/sphx_glr_plot_custom_density_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.264 seconds) .. _sphx_glr_download_auto_examples_plot_custom_density.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_custom_density.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_custom_density.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_