<jupyter_start><jupyter_text>Demonstrate how to use DensityDistUtilize a scipy.stats.rayleigh distribution to demonstrate<jupyter_code>import pymc3 as pm
import seaborn as sns
import scipy
import scipy.stats
import numpy as np
import spacepy.toolbox as tb
import matplotlib.pyplot as plt
from theano import theano, tensor as tt

sns.set(font_scale=1.5)<jupyter_output><empty_output><jupyter_text>Given examplehttps://pymc3.readthedocs.io/en/latest/notebooks/normalizing_flows_overview.html?highlight=densitydist<jupyter_code>from pymc3.distributions.dist_math import bound

def pot1(z):
    z = z.T
    return .5*((z.norm(2, axis=0)-2.)/.4)**2 - tt.log(tt.exp(-.5*((z[0]-2.)/.6)**2) +
                                                      tt.exp(-.5*((z[0]+2.)/.6)**2))


def cust_logp(z):
    # this works to cut things off, useful or not but 0 is a nature place a lot of times
    return bound(-pot1(z), z>-5, z<5) 
#     return -pot1(z)

with pm.Model() as pot1m:
    pm.DensityDist('pot1', logp=cust_logp, shape=(2,))
    trace = pm.sample(1000)
pm.traceplot(trace);
pm.plot_posterior(trace);<jupyter_output><empty_output><jupyter_text>Now can we do this from a histogram?<jupyter_code>dist = scipy.stats.rayleigh(loc=2, scale=20)
rvs = dist.rvs(10000)
sns.distplot(rvs)
# need to make a callable function of the histogram of this. 
h, b = np.histogram(rvs, 30)
b = tb.bin_edges_to_center(b)
plt.plot(b, h)
f = scipy.interpolate.interp1d(b,h, bounds_error=False, fill_value=0)
XX = np.linspace(0, 100, 1000)
plt.plot(XX, f(XX))

from theano.tensor import _shared

@theano.as_op(itypes=[theano.tensor.dscalar],otypes=[theano.tensor.dscalar])
def dens_fn(x):
    return tt.log(f(x) )
# dens_fn(_shared(np.linspace(-1, 5, 10)))
with pm.Model() as model:
    dens = pm.DensityDist('density', logp=dens_fn)
    trace = pm.sample(5000)
    
pm.traceplot(trace);
pm.plot_posterior(trace);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14308093994778068, 'fraction_numerical': 0.03237597911227154, 'mean_word_length': 4.397183098591549, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '30707', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 793, 'n_tokens_neox': 753, 'n_words': 156, 'path': 'Distributions/DensityDist Example.ipynb', 'repo_name': 'balarsen/pymc_learning', 'size': '1915'}