forked from Hvass-Labs/FinanceOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
53 lines (43 loc) · 1.69 KB
/
stats.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
########################################################################
#
# Various statistical functions.
#
########################################################################
#
# This file is part of FinanceOps:
#
# https://github.com/Hvass-Labs/FinanceOps
#
# Published under the MIT License. See the file LICENSE for details.
#
# Copyright 2021 by Magnus Erik Hvass Pedersen
#
########################################################################
import numpy as np
from scipy.stats import norm
########################################################################
def normal_prob_loss(mean, std):
"""
Probability of loss for a normal distribution.
:param mean: Float with mean.
:param std: Float with standard deviation.
:return: Float with probability of loss.
"""
return norm.cdf(0.0, loc=mean, scale=std)
def normal_prob_less_than(mean1, std1, mean2, std2):
"""
Probability that X1 < X2 for two independent and normal-distributed random
variables X1 and X2, where X1 ~ N(mean1, std1^2) and X2 ~ N(mean2, std2^2)
:param mean1: Float with mean for X1.
:param std1: Float with std.dev. for X1.
:param mean2: Float with mean for X2.
:param std2: Float with std.dev. for X2.
:return: Float with probability that X1 < X2.
"""
# The difference of two normal random variables is also a random normal
# variable with the following mean and std.dev. so we can simply calculate
# the probability of that new random variable being less than zero.
mean = mean1 - mean2
std = np.sqrt(std1 ** 2 + std2 ** 2)
return norm.cdf(0.0, loc=mean, scale=std)
########################################################################