-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics.py
53 lines (43 loc) · 1.56 KB
/
basics.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
44
45
46
47
48
49
50
51
52
import numpy as np
from scipy.stats import norm
def rnorm(n, mean=0, sd=1):
"""
Random generation for the normal distribution with mean
equal to mean and standard deviation equation to sd
same functions as rnorm in r: ``rnorm(n, mean=0, sd=1)``
:param n: the number of the observations
:param mean: vector of means
:param sd: vector of standard deviations
:return: the vector of the random numbers
:author: Wenqiang Feng
:email: von198@gmail.com
"""
return norm.rvs(loc=mean, scale=sd, size=n)
def dnorm(x, mean=0, sd=1, log=False):
"""
Density of the normal distribution with mean
equal to mean and standard deviation equation to sd
same functions as rnorm in r: ``dnorm(x, mean=0, sd=1, log=FALSE)``
:param x: the vector od quantiles
:param mean: vector of means
:param sd: vector of standard deviations
:return: the list of the density
:author: Wenqiang Feng
:email: von198@gmail.com
"""
if log:
return np.log(norm.pdf(x=x, loc=mean, scale=sd))
else:
return norm.pdf(x=x, loc=mean, scale=sd)
def runif(n, min=0, max=1):
"""
Random generation from the uniform distribution
same functions as rnorm in r: ``runif(n, min=0, max=1)``
:param n: the number of the observations
:param min: the lower limit of the distribution
:param max: the upper limit of the distribution
:return: the list of n uniform random numers
:author: Wenqiang Feng
:email: von198@gmail.com
"""
return np.random.uniform(min, max, size=n)