# Build custom random method with reject sampling

**URL:** <https://discourse.pymc.io/t/build-custom-random-method-with-reject-sampling/4022>\
**Category:** Questions\
**Created:** [October 26, 2019, 10:33am UTC](https://discourse.pymc.io/t/build-custom-random-method-with-reject-sampling/4022 "2019-10-26T10:33:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SosUts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sosuts/32/1906_2.png) [@SosUts](https://discourse.pymc.io/u/SosUts)\
**Post date:** [October 26, 2019, 10:33am UTC](https://discourse.pymc.io/t/build-custom-random-method-with-reject-sampling/4022/1 "2019-10-26T10:33:58Z")

</div>

I am trying to define sine-skewed von mises distribution(SSVM).  
SSVM was not implemented in `scipy.stats` so I have to write it by my self.

Most of the `random` methods in pymc3 seem to be using `scipy.stats.rvs` or inverse transform sampling.  
**Is it ok to implement custom `_random` method with reject sampling?**  
(the cumulative density function of SSVM looks too complicated for inverse transform sampling…)

If it’s ok, how can I include it properly in custom density distribution?

* * *

Sine-skewed von mises distribution(SSVM)  
Reference : [Characterization of Sine- Skewed von Mises Distribution](https://arxiv.org/abs/1902.02579)

1.Probability density function  
f(\theta)=(1+\lambda\sin(\theta-\mu)) \frac{1}{2\pi I\_{0}(\kappa)}\exp(\kappa \cos(\theta - \mu))  
where \lambda is skewness(|\lambda| \leq 1), \mu is the mean(|\mu| \leq \pi),  
\kappa is the concentration(0 \leq \kappa), I\_{0} is the modified Bessel function of order zero.

2.Cumulative density function  
F(\theta) = \frac{1}{2\pi} \left[(\pi + \theta -\mu) + \ 2\sum\_{j=1}^{\infty} \frac{I\_j(\kappa)}{j} \sin(j(\theta-\mu)) \right] + \ \frac{\lambda}{2\pi I\_0(\kappa)}(\exp^{-\kappa} - \exp^{\kappa\cos(\theta-\mu)})  
where  
I\_j(\kappa) = \left( \frac{\kappa}{2} \right)^j \sum\_{i=0}^{\infty} \ \left[\left( \frac{\kappa}{2} \right)^{2i} \left( \frac{1}{i!(j+1)} \right) \right]

* * *

Simple rejection sampling works well.

 ![rejection](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/7/741c154184645316904bf98338f54314e7e215df.png)

---

<div class="post-metadata">

**Author:** ![colcarroll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/colcarroll/32/9_2.png) [@colcarroll](https://discourse.pymc.io/u/colcarroll)\
**Post date:** [October 26, 2019, 7:20pm UTC](https://discourse.pymc.io/t/build-custom-random-method-with-reject-sampling/4022/2 "2019-10-26T19:20:36Z")

</div>

This is interesting! Adding the custom `random` method will allow prior predictive sampling, or posterior predictive sampling, whenever the RV is observed.

I see no reason you could not include it - the hardest part will be dealing with shapes. You will need to allow for vectorized inputs, and return similarly vectorized outputs. I would try copying a bunch of the code from [the existing Von Mises](https://github.com/pymc-devs/pymc3/blob/master/pymc3/distributions/continuous.py#L3273) distribution as a starting point, and implementing a custom `generate_samples`.

---

<div class="post-metadata">

**Author:** ![SosUts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sosuts/32/1906_2.png) [@SosUts](https://discourse.pymc.io/u/SosUts)\
**Post date:** [October 27, 2019, 1:50pm UTC](https://discourse.pymc.io/t/build-custom-random-method-with-reject-sampling/4022/3 "2019-10-27T13:50:53Z")

</div>

Thank you so much for your kind reply!

I have two another questions.  
1.Is there any sample of custom `generate_samples`?  
2.Is there any other simple way to implement `random` of complex distribution?

To be honest, I almost have no statistics nor programming background so I’ll start with studying `generate_samples`.  
Using pymc3 inference is not so hard but understanding the inside is difficult for me now…
