-
Notifications
You must be signed in to change notification settings - Fork 0
/
Probablity_distributions.py
2414 lines (2038 loc) · 105 KB
/
Probablity_distributions.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
from matplotlib.pyplot import plot, show, grid, hist, figure, subplot
import matplotlib.pyplot as plt
from mathmatics import *
RNG = np.random.default_rng(20)
class ContinuousDistributions:
def __init__(self,
variance: np.ndarray = None,
sigma: np.ndarray = None,
mu: np.ndarray = None,
lb: np.ndarray = None,
ub: np.ndarray = None,
alpha: float = None,
a: np.ndarray = None,
b: np.ndarray = None,
c: np.ndarray = None,
beta: np.ndarray = None,
Lambda: np.ndarray = None,
kappa: np.ndarray = None,
nu: np.ndarray = None,
gamma: np.ndarray = None,
fixed_n_chains: bool = True,
chains: int = 1,
xm: float = None) -> None:
if isinstance(sigma, (np.ndarray, float, int)) and isinstance(variance, (np.ndarray, float, int)):
raise Exception('Please Enter either variance or standard deviation!')
if isinstance(sigma, (np.ndarray, float, int)) and not isinstance(variance, (np.ndarray, float, int)):
if sigma > 0:
self.sigma = sigma
self.variance = sigma ** 2
else:
raise Exception('The standard deviation should be a positive value!')
if not isinstance(sigma, (np.ndarray, float, int)) and isinstance(variance, (np.ndarray, float, int)):
if variance > 0:
self.sigma = np.sqrt(variance)
self.variance = variance
else:
raise Exception('The standard deviation should be a positive value!')
if sigma is None and variance is None:
self.sigma = None
self.variance = None
if isinstance(lb, (np.ndarray, float, int)):
self.lb = lb
elif lb is None:
self.lb = None
else:
raise Exception('The lower bound is not specified correctly!')
if isinstance(ub, (np.ndarray, float, int)):
self.ub = ub
elif ub is None:
self.ub = None
else:
raise Exception('The upper bound is not specified correctly!')
if isinstance(mu, (np.ndarray, float, int)):
self.mu = mu
elif mu is None:
self.mu = None
else:
raise Exception('The value of mu is not specified correctly!')
if isinstance(alpha, (np.ndarray, float, int)):
self.alpha = alpha
elif alpha is None:
self.alpha = None
else:
raise Exception('The value of alpha is not specified correctly!')
if isinstance(beta, (np.ndarray, float, int)):
self.beta = beta
elif beta is None:
self.beta = None
else:
raise Exception('The value of alpha is not specified correctly!')
if isinstance(Lambda, (np.ndarray, float, int)):
self.Lambda = Lambda
elif Lambda is None:
self.Lambda = None
else:
raise Exception('The value of lambda is not specified correctly!')
if isinstance(a, (np.ndarray, float, int)):
self.a = a
elif a is None:
self.a = None
else:
raise Exception('The value of a is not specified correctly!')
if isinstance(c, (np.ndarray, float, int)):
self.c = c
elif c is None:
self.c = None
else:
raise Exception('The value of c is not specified correctly!')
if isinstance(b, (np.ndarray, float, int)):
self.b = b
elif b is None:
self.b = None
else:
raise Exception('The value of b is not specified correctly!')
if isinstance(kappa, (np.ndarray, float, int)):
self.kappa = kappa
elif kappa is None:
self.kappa = None
else:
raise Exception('The value of kappa is not specified correctly!')
if isinstance(nu, (np.ndarray, float, int)):
self.nu = nu
elif nu is None:
self.nu = None
else:
raise Exception('The value of nu is not specified correctly!')
if isinstance(gamma, (np.ndarray, float, int)):
self.gamma = gamma
elif gamma is None:
self.gamma = None
else:
raise Exception('The value of nu is not specified correctly!')
if isinstance(fixed_n_chains, bool):
self.fixed_n_chains = fixed_n_chains
else:
raise Exception('Please specify whether the number of chains are fixed or not !')
if isinstance(chains, int):
if not self.fixed_n_chains:
raise Exception('The number of chains is specified while the variant number of chains are specified!')
else:
self.n_chains = chains
elif (not isinstance(chains, int)) and self.fixed_n_chains:
raise Exception('Please enter the number of chains(or the number of parallel evaluations) correctly!')
else:
print(f'-------------------------------------------------------------------------------------------------\n'
f'Variant number of chains is activated .'
f'--------------------------------------------------------------------------------------------------')
if isinstance(xm, (float, int, float)):
self.xm = xm
elif xm is None:
self.xm = None
else:
raise Exception('The type of xm is not entered correctly!')
def visualize(self, lower_lim: float = -10, upper_lim: float = -10):
"""
Visualizing the probability distribution
:param lower_lim: the lower limit used in plotting the probability distribution
:param upper_lim: the upper limit used in plotting the probability distribution
:return: a line plot from matplotlib library
"""
x_m = np.linspace(start=lower_lim, stop=upper_lim, num=1000)
y_m = list()
for i in range(len(x_m)):
y_m.append(self.pdf(x_m[i]))
plot(list(x_m.ravel()), y_m)
grid(which='both')
show()
class Uniform(ContinuousDistributions):
def __init__(self, a: float = None, b: float = None) -> None:
"""
Continuous uniform distribution
:param a: The lower limit of uniform distribution
:param b: The upper limit of uniform distribution
"""
super(Uniform, self).__init__(a=a, b=b)
if any(self.a >= self.b):
raise Exception('The lower limit of the uniform distribution is greater than the upper limit!')
@property
def statistics(self):
"""
Statistics calculated for the Uniform distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def pdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the probability of the Uniform distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable (Cx1, Cx1)
"""
in_range_index = (x > self.a) & (x < self.b)
prob = np.zeros((len(x), 1))
prob[in_range_index[:, 0], 0] = 1 / (self.b - self.a)
return prob
def pdf_diff(self, x: np.ndarray) -> np.ndarray:
return np.zeros((len(x), 1))
def log_prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the log (and its derivatives) of the Uniform distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The derivatives of the log probability of the occurrence of an independent variable Cx1
"""
in_range_index = (x > self.a) & (x < self.b)
log_prob = -np.inf * np.ones((len(x), 1))
log_prob[in_range_index[:, 0], 0] = -np.log(self.b - self.a)
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
in_range_index = (x > self.a) & (x < self.b)
derivatives_log_prob = -np.inf * np.ones((len(x), 1))
derivatives_log_prob[in_range_index[:, 0], 0] = 0
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Uniform distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function (and its derivatives) with respect to the input variable Cx1
"""
left_index = x <= self.a
right_index = x >= self.b
in_range_index = (x > self.a) & (x < self.b)
cdf = np.ones((len(x), 1))
cdf[left_index[:, 0], 0] = 0
cdf[right_index[:, 0], 0] = 1
cdf[in_range_index[:, 0], 0] = (x[in_range_index[:, 0], 0] - self.a) / (self.b - self.a)
return cdf
def sample(self, size: int = 100):
sample = RNG.uniform(low=self.a, high=self.b, size=size)
return sample
class Normal(ContinuousDistributions, ErfFcn):
def __init__(self, sigma: float = None, variance: float = None, mu: float = None) -> None:
"""
Normal distribution function
:param sigma: The standard deviation of the Normal distribution (sigma>0)
:param variance: The variance of the Normal distribution (variance>0)
:param mu: The mean of the Normal distribution
"""
super(Normal, self).__init__(sigma=sigma, variance=variance, mu=mu)
if self.mu is None or self.sigma is None:
raise Exception('The value of either mean or standard deviation is not specified (Normal distribution)!')
# self.Erf = ErfFcn(method='simpson', intervals=10000)
self.erf = ErfFcn(method='simpson', intervals=10000)
@property
def statistics(self):
"""
Statistics calculated for the ---- distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def pdf(self, x: np.ndarray) -> (np.ndarray, np.ndarray):
"""
Parallelized calculating the probability of the Normal distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable Cx1
"""
return (1 / (self.sigma * np.sqrt(2 * np.pi))) * np.exp(-((x - self.mu) ** 2) / (2 * self.sigma ** 2))
def pdf_diff(self, x: np.ndarray) -> np.ndarray:
diff_pdf = (-1 / (self.sigma ** 3)) * np.sqrt(2 / np.pi) * (x - self.mu) * np.exp(-((x - self.mu) ** 2) /
(2 * self.sigma ** 2))
return diff_pdf
def log_prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the log (and its derivatives) of the Normal distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
return -np.log(self.sigma * np.sqrt(2 * np.pi)) - ((x - self.mu) ** 2) / (2 * self.sigma ** 2)
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
return -(x - self.mu) / (self.sigma ** 2)
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Normal distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function with respect to the input variable
(Cx1, Cx1)
"""
z = (x - self.mu) / (self.sigma * np.sqrt(2))
erf_value = self.Erf.fcn_value(z)
return erf_value
def sample(self, size: int = 100):
uniform_dis = RNG.uniform(low=0.0, high=1.0, size=size)
return uniform_dis
class TruncatedNormal(ContinuousDistributions):
def __init__(self, lb: float = None, ub: float = None, sigma: float = None, variance: float = None,
mu: float = None) -> None:
"""
:param lb:
:param ub:
:param sigma:
:param variance:
:param mu:
"""
super(TruncatedNormal, self).__init__(lb=lb, ub=ub, mu=mu, sigma=sigma, variance=variance)
if self.lb >= self.ub:
raise Exception('The lower limit of the truncated Normal distribution is greater than the upper limit!')
if self.mu is None or self.sigma is None:
raise Exception(
'The value of either mean or standard deviation is not specified (Truncated Normal distribution)!')
self.Erf = ErfFcn(method='simpson', intervals=10000)
@property
def statistics(self):
"""
Statistics calculated for the Truncated Normal distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def pdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the probability of the Truncated Normal distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability of the occurrence of the given variable Cx1
"""
ind = (x >= self.lb) & (x <= self.ub)
arg_r = (self.ub - self.mu) / self.sigma
arg_l = (self.lb - self.mu) / self.sigma
erf_r = 0.5 * (1 + self.Erf.fcn_value(arg_r / np.sqrt(2)))
ert_l = 0.5 * (1 + self.Erf.fcn_value(arg_l / np.sqrt(2)))
normal_argument = (x[ind[:, 0], 0] - self.mu) / self.sigma
prob = np.zeros((len(x), 1))
normal_fcn_value = (1 / (np.sqrt(2 * np.pi))) * np.exp(-0.5 * normal_argument ** 2)
prob[ind[:, 0], 0] = (1 / self.sigma) * (normal_fcn_value / (erf_r - ert_l))
return prob
def pdf_diff(self, x: np.ndarray) -> np.ndarray:
ind = (x >= self.lb) & (x <= self.ub)
arg_r = (self.ub - self.mu) / self.sigma
arg_l = (self.lb - self.mu) / self.sigma
erf_r = 0.5 * (1 + self.Erf.fcn_value(arg_r / np.sqrt(2)))
ert_l = 0.5 * (1 + self.Erf.fcn_value(arg_l / np.sqrt(2)))
normal_argument = (x[ind[:, 0], 0] - self.mu) / self.sigma
derivatives_prob = np.zeros((len(x), 1))
derivatives_prob[ind[:, 0], 0] = (1 / self.sigma ** 2) * (1 / (erf_r - ert_l)) * (
-1 / (np.sqrt(2 * np.pi))) * normal_argument * np.exp(-0.5 * normal_argument ** 2)
return derivatives_prob
def log_prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the log (and its derivatives) of the Truncated Normal distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
ind = (x >= self.lb) & (x <= self.ub)
arg_r = (self.ub - self.mu) / self.sigma
arg_l = (self.lb - self.mu) / self.sigma
normal_argument = (x[ind[:, 0], 0] - self.mu) / self.sigma
erf_r = 0.5 * (1 + self.Erf.fcn_value(arg_r / np.sqrt(2)))
ert_l = 0.5 * (1 + self.Erf.fcn_value(arg_l / np.sqrt(2)))
log_prob = np.ones((len(x), 1)) * -np.inf
log_prob[ind[:, 0], 0] = -np.log(self.sigma) - np.log(erf_r - ert_l) - 0.5 * np.log(
2 * np.pi) - 0.5 * normal_argument ** 2
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
ind = (x >= self.lb) & (x <= self.ub)
derivatives_log_prob = np.ones((len(x), 1)) * -np.inf
derivatives_log_prob[ind[:, 0], 0] = (-1 / self.sigma ** 2) * (
x[ind[:, 0], 0] - self.mu)
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Truncated Normal distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function (and its derivatives) with respect to the input variable
(Cx1, Cx1)
"""
ind = x > self.ub
in_range_index = (x >= self.lb) & (x <= self.ub)
cdf = np.zeros((len(x), 1))
cdf[ind[:, 0], 0] = 1.0
b = (self.ub - self.mu) / self.sigma
a = (self.lb - self.mu) / self.sigma
xi = (x[in_range_index[:, 0], 0] - self.mu) / self.sigma
erf_r = 0.5 * (1 + self.Erf.fcn_value(b / np.sqrt(2)))
ert_l = 0.5 * (1 + self.Erf.fcn_value(a / np.sqrt(2)))
ert_xi = 0.5 * (1 + self.Erf.fcn_value(xi / np.sqrt(2)))
cdf[in_range_index[:, 0], 0] = (ert_xi - ert_l) / (erf_r - ert_l)
return cdf
class HalfNormal(ContinuousDistributions):
def __init__(self, sigma: float = None, variance: float = None) -> None:
"""
:param sigma:
:param variance:
"""
super(HalfNormal, self).__init__(sigma=sigma, variance=variance)
self.Erf = ErfFcn(method='simpson', intervals=10000)
@property
def statistics(self):
"""
Statistics calculated for the Half Normal distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the probability of the Half Normal distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable (Cx1, Cx1)
"""
ind = (x >= 0)
prob = np.zeros((len(x), 1))
prob[ind[:, 0], 0] = (np.sqrt(2 / np.pi) / self.sigma) * np.exp(
-((x[ind[:, 0], 0]) ** 2) / (2 * self.sigma ** 2))
return prob
def pdf_diff(self, x: np.ndarray) -> np.ndarray:
ind = (x >= 0)
derivatives_prob = np.zeros((len(x), 1))
derivatives_prob[ind[:, 0], 0] = (- np.sqrt(2 / np.pi) / (self.sigma ** 3)) * (
x[ind[:, 0], 0]) * np.exp(-((x[ind[:, 0], 0]) ** 2) / (2 * self.sigma ** 2))
return derivatives_prob
def log_prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the log (and its derivatives) of the Half Normal distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
in_range_index = (x >= 0)
log_prob = np.ones((len(x), 1)) * -np.inf
log_prob[in_range_index[:, 0], 0] = 0.5 * np.log(2 / np.pi) - np.log(self.sigma) - (
(x[in_range_index[:, 0], 0]) ** 2) / (2 * self.sigma ** 2)
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
in_range_index = (x >= 0)
derivatives_log_prob = np.ones((len(x), 1)) * -np.inf
derivatives_log_prob[in_range_index[:, 0], 0] = -x[in_range_index[:, 0], 0] / self.sigma ** 2
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for ---- distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function (and its derivatives) with respect to the input variable Cx1
"""
in_range_index = (x >= 0)
cdf = np.zeros((len(x), 1))
erf_value, _ = self.Erf(x[in_range_index[:, 0], 0] / (self.sigma * np.sqrt(2)))
cdf[in_range_index[:, 0], 0] = erf_value
return cdf
class SkewedNormal(ContinuousDistributions):
def __int__(self, mu: float = None, alpha: float = None, sigma: float = None, variance: float = None) -> None:
"""
:param mu:
:param alpha:
:param sigma:
:param variance:
:param return_der_pdf:
:param return_der_logpdf:
:param return_pdf:
:param return_log_pdf:
:return:
"""
super(SkewedNormal, self).__init__(mu=mu, alpha=alpha, sigma=sigma)
if self.mu is None or self.sigma is None:
raise Exception(
'The value of either mean or standard deviation is not specified (Skewed Normal distribution)!')
self.Erf = ErfFcn(method='simpson', intervals=10000)
@property
def statistics(self):
"""
Statistics calculated for the Skewed Normal distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def prob(self, x) -> (np.ndarray, np.ndarray):
"""
Parallelized calculating the probability of the Skewed Normal distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable (Cx1, Cx1)
"""
z = (x - self.mu) / self.sigma
erf_part, der_erf_part = 0.5 * (1 + self.Erf.fcn_value(z * (self.alpha / np.sqrt(2.0))))
normal_part = (1 / (np.sqrt(2 * np.pi))) * np.exp(-0.5 * (z ** 2))
prob = 2 * erf_part * normal_part
return prob
def pdf_diff(self, x: np.ndarray) -> np.ndarray:
z = (x - self.mu) / self.sigma
erf_part = 0.5 * (1 + self.Erf.fcn_value(z * (self.alpha / np.sqrt(2.0))))
der_erf_part = self.Erf.derivatives(z * (self.alpha / np.sqrt(2.0)))
derivatives_prob = -np.sqrt(2 / np.pi) * (z / self.sigma) * np.exp(-0.5 * (z ** 2)) * erf_part + (
self.alpha / self.sigma) * np.sqrt(2 / np.pi) * np.exp(-0.5 * (z ** 2)) * der_erf_part
return derivatives_prob
def log_prob(self, x: np.ndarray) -> (np.ndarray, np.ndarray):
"""
Parallelized calculating the log (and its derivatives) of the Skewed Normal distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
z = (x - self.mu) / self.sigma
erf_value = self.Erf.fcn_value((z * self.alpha) / np.sqrt(2))
log_prob = -0.5 * np.log(2 * np.pi) - 0.5 * (z ** 2) + np.log(1 + erf_value)
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
z = (x - self.mu) / self.sigma
erf_value, der_erf_value = self.Erf.fcn_value((z * self.alpha) / np.sqrt(2))
der_erf_value = self.Erf.derivatives((z * self.alpha) / np.sqrt(2))
derivatives_log_prob = -z * (1 / self.sigma) + (1 / (self.sigma * np.sqrt(2))) * (
der_erf_value / (1 + erf_value))
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Skewed Normal distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function with respect to the input variable Cx1
"""
return None
class BetaPdf(ContinuousDistributions):
def __init__(self, alpha: None, beta: None) -> None:
"""
:param alpha:
:param beta:
:param return_der_pdf:
:param return_der_logpdf:
:param return_pdf:
:param return_log_pdf:
"""
super(BetaPdf, self).__init__(alpha=alpha, beta=beta)
if self.alpha <= 0:
raise Exception('Parameter alpha (for calculating the beta distribution) should be positive')
if self.beta <= 0:
raise Exception('Parameter beta (for calculating the beta distribution) should be positive')
self.Beta = beta_fcn
@property
def statistics(self):
"""
Statistics calculated for the Beta distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the probability of the Beta distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable (Cx1, Cx1)
"""
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
term1 = (x ** (self.alpha - 1))
term2 = ((1 - x) ** (self.beta - 1))
prob = (term1 * term2) / self.Beta(self.alpha, self.beta)
return prob
def prob_diff(self, x: np.ndarray) -> np.ndarray:
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
term1 = (x ** (self.alpha - 1))
term2 = ((1 - x) ** (self.beta - 1))
derivatives_prob = (1 / self.Beta(self.alpha, self.beta)) * (
((self.alpha - 1) * x ** (self.alpha - 2)) * term2 - (self.beta - 1) * ((1 - x) ** (self.beta - 2))
* term1)
return derivatives_prob
def log_prob(self, x: np.ndarray) -> (np.ndarray, np.ndarray):
"""
Parallelized calculating the log (and its derivatives) of the Beta distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
log_prob = (self.alpha - 1) * np.log(x) + (self.beta - 1) * np.log(1 - x) - np.log(self.Beta(self.alpha,
self.beta))
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
derivatives_log_prob = ((self.alpha - 1) / x) - ((self.beta - 1) / (1 - x))
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Beta distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function with respect to the input variable Cx1
"""
return None
class Kumaraswamy(ContinuousDistributions):
def __int__(self, alpha: None, beta: None, return_der_pdf: bool = True, return_der_logpdf: bool = True,
return_pdf: bool = True, return_log_pdf: bool = True) -> None:
"""
:param alpha:
:param beta:
:return:
"""
super(Kumaraswamy, self).__init__(alpha=alpha, beta=beta)
if self.alpha <= 0:
raise Exception('Parameter alpha (for calculating the beta distribution) should be positive')
if self.beta <= 0:
raise Exception('Parameter beta (for calculating the beta distribution) should be positive')
@property
def statistics(self):
"""
Statistics calculated for the Kumaraswamy distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the probability of the Kumaraswamy distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable (Cx1, Cx1)
"""
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
term1 = (x ** (self.alpha - 1))
term2 = (1 - x ** self.alpha)
prob = self.beta * self.alpha * term1 * (term2 ** (self.beta - 1))
return prob
def prob_diff(self, x: np.ndarray) -> np.ndarray:
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
term1 = (x ** (self.alpha - 1))
term2 = (1 - x ** self.alpha)
derivatives_prob = self.beta * self.alpha * (self.alpha - 1) * (x ** (self.alpha - 2)) * term2 + \
self.beta * self.alpha * term1 * (self.beta - 1) * (-self.alpha) * (
x ** (self.alpha - 1)) * \
((1 - x ** self.alpha) ** (self.beta - 2))
return derivatives_prob
def log_prob(self, x: np.ndarray) -> (np.ndarray, np.ndarray):
"""
Parallelized calculating the log (and its derivatives) of the Kumaraswamy distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
log_prob = np.log(self.alpha * self.beta) + (self.alpha - 1) * np.log(x) + (self.beta - 1) * np.log(
(1 - x ** self.alpha))
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
"""
:param x:
:return:
"""
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
derivatives_log_prob = (self.alpha - 1) / x + ((self.beta - 1) * (-self.alpha * x ** (self.alpha - 1))) / (
1 - x ** self.alpha)
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Kumaraswamy distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function (and its derivatives) with respect to the input variable Cx1
"""
x = np.clip(a=x, a_min=np.finfo(float).eps, a_max=1)
cdf = 1 - (1 - x ** self.alpha) ** self.beta
return cdf
class Exponential(ContinuousDistributions):
def __init__(self, Lambda: None) -> None:
"""
:param Lambda:
:param return_der_pdf:
:param return_der_logpdf:
:param return_pdf:
:param return_log_pdf:
"""
super(Exponential, self).__init__(Lambda=Lambda)
if self.Lambda <= 0:
raise Exception('Parameter lambda (for calculating the Exponential distribution) should be positive')
@property
def statistics(self):
"""
Statistics calculated for the Exponential distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def prob(self, x: np.ndarray) -> (np.ndarray, np.ndarray):
"""
Parallelized calculating the probability of the Exponential distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable (Cx1, Cx1)
"""
in_range_index = x >= 0
prob = np.zeros((len(x), 1))
prob[in_range_index[:, 0], 0] = self.Lambda * np.exp(-self.Lambda * x[in_range_index[:, 0], 0])
return prob
def pdf_diff(self, x: np.ndarray) -> np.ndarray:
in_range_index = x >= 0
derivatives_prob = np.zeros((len(x), 1))
derivatives_prob[in_range_index[:, 0], 0] = -(self.Lambda ** 2) * np.exp(
-self.Lambda * x[in_range_index[:, 0], 0])
return derivatives_prob
def log_prob(self, x: np.ndarray) -> (np.ndarray, np.ndarray):
"""
Parallelized calculating the log (and its derivatives) of the Exponential distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
in_range_index = x >= 0
log_prob = np.ones((len(x), 1)) * -np.inf
log_prob[in_range_index[:, 0], 0] = np.log(self.Lambda) - self.Lambda * x[in_range_index[:, 0], 0]
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the log (and its derivatives) of the Exponential distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
in_range_index = x >= 0
derivatives_log_prob = np.ones((len(x), 1)) * -np.inf
derivatives_log_prob[in_range_index[:, 0], 0] = - self.Lambda
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Exponential distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function (and its derivatives) with respect to the input variable Cx1
"""
in_range_index = x >= 0
cdf = np.zeros((len(x), 1))
cdf[in_range_index[:, 0], 0] = 1 - np.exp(- self.Lambda * x[in_range_index[:, 0], 0])
return cdf
class Laplace(ContinuousDistributions):
def __init__(self, mu: None, b: None) -> None:
"""
:param mu:
:param b:
"""
super(Laplace, self).__init__(mu=mu, b=b)
if self.b <= 0:
raise Exception('The location parameter b (for calculating the Laplace distribution) should be positive')
@property
def statistics(self):
"""
Statistics calculated for the Laplace distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def prob(self, x: np.ndarray, ) -> np.ndarray:
"""
Parallelized calculating the probability of the Laplace distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable (Cx1, Cx1)
"""
prob = (1 / (2 * self.b)) * np.exp((-1 / self.b) * np.abs(x - self.mu))
return prob
def pdf_diff(self, x: np.ndarray) -> np.ndarray:
right_index = x >= self.mu
derivatives_prob = np.zeros((len(x), 1))
derivatives_prob[right_index[:, 0], 0] = (-1 / (2 * self.b ** 2)) * np.exp(
(-1 / self.b) * (x[right_index[:, 0], 0] - self.mu))
derivatives_prob[~right_index[:, 0], 0] = (1 / (2 * self.b ** 2)) * np.exp(
(1 / self.b) * (x[~right_index[:, 0], 0] - self.mu))
return derivatives_prob
def log_prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the log (and its derivatives) of the Laplace distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
right_index = x >= self.mu
log_prob = -np.log(2 * self.b) - (1 / self.b) * np.abs(x - self.mu)
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the log (and its derivatives) of the Laplace distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
right_index = x >= self.mu
derivatives_log_prob = np.zeros((len(x), 1))
derivatives_log_prob[right_index[:, 0], 0] = - (1 / self.b) * (x[right_index[:, 0], 0] - self.mu)
derivatives_log_prob[~right_index[:, 0], 0] = (1 / self.b) * (x[~right_index[:, 0], 0] - self.mu)
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Laplace distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function (and its derivatives) with respect to the input variable
(Cx1, Cx1)
"""
right_index = x >= self.mu
cdf = np.zeros((len(x), 1))
cdf[right_index[:, 0], 0] = 1 - 0.5 * np.exp((-1 / self.b) * (x[right_index[:, 0], 0] - self.mu))
cdf[~right_index[:, 0], 0] = 0.5 * np.exp((1 / self.b) * (x[~right_index[:, 0], 0] - self.mu))
return cdf
class AsymmetricLaplace(ContinuousDistributions):
def __init__(self, kappa: float = None, mu: float = None, b: float = None) -> None:
"""
:param kappa:
:param mu:
:param b:
"""
super(AsymmetricLaplace, self).__init__(kappa=kappa, mu=mu, b=b)
if self.kappa <= 0:
raise Exception('The values of Symmetric parameter should be positive(Asymmetric Laplace distribution)!')
if self.b <= 0:
raise Exception(
'The rate of the change of the exponential term should be positive(Asymmetric Laplace distribution)!')
@property
def statistics(self):
"""
Statistics calculated for the Asymmetric Laplace distribution function given distribution parameters
:return: A dictionary of calculated metrics
"""
return None
def prob(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the probability of the Asymmetric Laplace distribution
:param x: An numpy array values determining the variable we are calculating its probability distribution (Cx1)
:return: The probability (and the derivative) of the occurrence of the given variable (Cx1, Cx1)
"""
in_range_index = x >= self.mu
coefficient = self.b / (self.kappa + 1 / self.kappa)
prob = np.zeros((len(x), 1))
prob[in_range_index[:, 0], 0] = coefficient * np.exp(-self.b * self.kappa * (x[in_range_index[:, 0], 0] -
self.mu))
prob[~in_range_index[:, 0], 0] = coefficient * np.exp((self.b / self.kappa) * (x[~in_range_index[:, 0], 0] -
self.mu))
return prob
def pdf_diff(self, x: np.ndarray) -> np.ndarray:
in_range_index = x >= self.mu
coefficient = self.b / (self.kappa + 1 / self.kappa)
derivatives_prob = np.zeros((len(x), 1))
derivatives_prob[in_range_index[:, 0], 0] = coefficient * (-self.b * self.kappa) * np.exp(
-self.b * self.kappa * (x[in_range_index[:, 0], 0] - self.mu))
derivatives_prob[~in_range_index[:, 0], 0] = coefficient * (self.b / self.kappa) * np.exp(
-self.b * self.kappa * (x[~in_range_index[:, 0], 0] - self.mu))
return derivatives_prob
def log_prob(self, x: np.ndarray) -> (np.ndarray, np.ndarray):
"""
Parallelized calculating the log (and its derivatives) of the Asymmetric Laplace distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
in_range_index = x >= self.mu
log_prob = np.zeros((len(x), 1))
coef = self.b / (self.kappa + 1 / self.kappa)
log_prob[in_range_index[:, 0], 0] = np.log(coef) + (
-self.b * self.kappa * (x[in_range_index[:, 0], 0] - self.mu))
log_prob[~in_range_index[:, 0], 0] = np.log(coef) + (
(self.b / self.kappa) * (x[~in_range_index[:, 0], 0] - self.mu))
return log_prob
def log_prob_diff(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the log (and its derivatives) of the Asymmetric Laplace distribution
:param x: An integer array determining the variable we are calculating its probability distribution (Cx1)
:return: The log probability and derivatives of the log probability of the occurrence of an independent variable
(Cx1, Cx1)
"""
in_range_index = x >= self.mu
derivatives_log_prob = np.zeros((len(x), 1))
derivatives_log_prob[in_range_index[:, 0], 0] = -self.b * self.kappa
derivatives_log_prob[~in_range_index[:, 0], 0] = (self.b / self.kappa)
return derivatives_log_prob
def cdf(self, x: np.ndarray) -> np.ndarray:
"""
Parallelized calculating the cumulative distribution function for Asymmetric Laplace distribution
:param x: An array of the input variable (Cx1)
:return: The cumulative distribution function (and its derivatives) with respect to the input variable
(Cx1, Cx1)
"""
cdf = np.zeros((len(x), 1))
in_range_index = x >= self.mu
cdf[in_range_index[:, 0], 0] = 1 - (1 / (1 + self.kappa ** 2)) * np.exp(
-self.b * self.kappa * (x[in_range_index[:, 0], 0] - self.mu))
cdf[~in_range_index[:, 0], 0] = (self.kappa ** 2 / (1 + self.kappa ** 2)) * np.exp(
(self.b / self.kappa) * (~x[in_range_index[:, 0], 0] - self.mu))
return cdf
class StudentT(ContinuousDistributions):
def __init__(self, nu: float = None, mu: float = None, Lambda: float = None) -> None:
"""
:param nu:
:param mu:
:param Lambda:
:param return_der_pdf:
:param return_der_logpdf:
:param return_pdf:
:param return_log_pdf:
"""
super(StudentT, self).__init__(nu=nu, mu=mu, Lambda=Lambda)
if self.nu <= 0:
raise Exception('The value of nu should be positive (Student-t distribution)!')
if self.sigma <= 0:
raise Exception('The value of sigma should be positive (Student-t distribution)!')