parameter_expressions.py 4.04 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
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
# Copyright (c) Microsoft Corporation
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge,
# to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
parameter_expression.py
'''

import numpy as np


def choice(options, random_state):
    '''
    options: 1-D array-like or int
    random_state: an object of numpy.random.RandomState
    '''
    return random_state.choice(options)


def randint(upper, random_state):
    '''
    upper: an int that represent an upper bound
    random_state: an object of numpy.random.RandomState
    '''
    return random_state.randint(upper)


def uniform(low, high, random_state):
    '''
    low: an float that represent an lower bound
    high: an float that represent an upper bound
    random_state: an object of numpy.random.RandomState
    '''
49
    assert high > low, 'Upper bound must be larger than lower bound'
Deshui Yu's avatar
Deshui Yu committed
50
51
52
53
54
55
56
57
58
59
    return random_state.uniform(low, high)


def quniform(low, high, q, random_state):
    '''
    low: an float that represent an lower bound
    high: an float that represent an upper bound
    q: sample step
    random_state: an object of numpy.random.RandomState
    '''
60
    return randint(np.floor((high-low)/q)+1, random_state) * q + low
Deshui Yu's avatar
Deshui Yu committed
61
62
63
64
65
66
67
68


def loguniform(low, high, random_state):
    '''
    low: an float that represent an lower bound
    high: an float that represent an upper bound
    random_state: an object of numpy.random.RandomState
    '''
69
70
    assert low > 0, 'Lower bound must be positive'
    return np.exp(uniform(np.log(low), np.log(high), random_state))
Deshui Yu's avatar
Deshui Yu committed
71
72
73
74
75
76
77
78
79


def qloguniform(low, high, q, random_state):
    '''
    low: an float that represent an lower bound
    high: an float that represent an upper bound
    q: sample step
    random_state: an object of numpy.random.RandomState
    '''
suiguoxin's avatar
suiguoxin committed
80
    return np.floor((loguniform(low, high, random_state) - low) / q) * q + low
Deshui Yu's avatar
Deshui Yu committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122


def normal(mu, sigma, random_state):
    '''
    The probability density function of the normal distribution,
    first derived by De Moivre and 200 years later by both Gauss and Laplace independently.
    mu: float or array_like of floats
        Mean (“centre”) of the distribution.
    sigma: float or array_like of floats
           Standard deviation (spread or “width”) of the distribution.
    random_state: an object of numpy.random.RandomState
    '''
    return random_state.normal(mu, sigma)


def qnormal(mu, sigma, q, random_state):
    '''
    mu: float or array_like of floats
    sigma: float or array_like of floats
    q: sample step
    random_state: an object of numpy.random.RandomState
    '''
    return np.round(normal(mu, sigma, random_state) / q) * q


def lognormal(mu, sigma, random_state):
    '''
    mu: float or array_like of floats
    sigma: float or array_like of floats
    random_state: an object of numpy.random.RandomState
    '''
    return np.exp(normal(mu, sigma, random_state))


def qlognormal(mu, sigma, q, random_state):
    '''
    mu: float or array_like of floats
    sigma: float or array_like of floats
    q: sample step
    random_state: an object of numpy.random.RandomState
    '''
    return np.round(lognormal(mu, sigma, random_state) / q) * q