forked from vedantmehta2808/vonMises-Package-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rvonmises.py
44 lines (35 loc) · 1.06 KB
/
rvonmises.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import scipy.integrate as integrate
from scipy.optimize import brentq as root
import math
import numpy as np
import scipy.special as scp
from scipy.special import iv
# In[2]:
def rvonmises(n, mu, kappa):
vm = np.zeros(n)
a = 1 + (1 + 4 * (kappa**2))**0.5
b = (a - (2 * a)**0.5)/(2 * kappa)
r = (1 + b**2)/(2 * b)
obs = 0
while (obs < n):
U1 = np.random.uniform(0, 1, 1)
z = np.cos(np.pi * U1)
f = (1 + r * z)/(r + z)
c = kappa * (r - f)
U2 = np.random.uniform(0, 1, 1)
if (c * (2 - c) - U2 > 0):
U3 = np.random.uniform(0, 1, 1)
vm[obs] = np.sign(U3 - 0.5) * math.acos(f) + mu
vm[obs] = vm[obs] % (2 * np.pi)
obs = obs + 1
else:
if (math.log(c/U2) + 1 - c >= 0):
U3 = np.random.uniform(0, 1, 1)
vm[obs] = np.sign(U3 - 0.5) * math.acos(f) + mu
vm[obs] = vm[obs] % (2 * math.pi)
obs = obs + 1
return(vm)