Created
March 19, 2020 22:50
-
-
Save Leva-kleva/e27a15d4dbc276e15337aa59323e9186 to your computer and use it in GitHub Desktop.
tasks in probability theory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from random import random | |
from math import log | |
def main() : | |
lmbd = float(input()) | |
eto = random() | |
print(-log(eto) / lmbd) | |
if __name__ == "__main__" : | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from random import random | |
from math import * | |
def transformation_box_muller(a1, a2): | |
return sqrt(2*log(1/a1))*sin(2*pi*a2) | |
def normal_distribution(x, sigma, mu): | |
return (1/(sigma*sqrt(2*pi)))*exp(-(x-mu)**2/(2*sigma**2)) | |
def main(): | |
a1 = random() | |
a2 = random() | |
x = transformation_box_muller(a1, a2) | |
print(normal_distribution(x, 1, 0)) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from math import * | |
import matplotlib.pyplot as plt | |
def BuildGraph(name, dataX, dataY) : | |
graph = plt.figure() | |
f = graph.add_subplot(111) | |
f.plot(dataX, dataY) | |
f.grid(True) | |
f.set_xlabel("n") | |
f.set_ylabel("p_n") | |
f.set_title(name) | |
plt.savefig(name + ".png") | |
plt.show() | |
def Cc(m, N) : | |
return factorial(N)/(factorial(N-m)*factorial(m)) | |
def FirstFunc(p, N) : | |
arr_pn = [] | |
arr_n = [] | |
for n in range(0, N+1) : | |
pn = Cc(n, N) * (p**n) * ((1-p)**(N-n)) | |
arr_pn.append(pn) | |
arr_n.append(n) | |
BuildGraph("FirstFunc", arr_n, arr_pn) | |
def SecondFunc(p, N) : | |
arr_pn = [] | |
arr_n = [] | |
for n in range(0, N+1) : | |
pn = (((N*p)**n) / factorial(n)) * exp(-N*p) | |
arr_pn.append(pn) | |
arr_n.append(n) | |
BuildGraph("SecondFunc", arr_n, arr_pn) | |
def ThirdFunc(p, N) : | |
arr_pn = [] | |
arr_n = [] | |
sigma_sqr = p * (1 - p) | |
dxn = 1/sqrt(N) | |
for n in range(0, N+1) : | |
xn = (n - N*p)/sqrt(N) | |
pn = (1 / sqrt(2*pi*sigma_sqr)) * exp(-xn**2/(2*sigma_sqr)) * dxn | |
arr_pn.append(pn) | |
arr_n.append(n) | |
BuildGraph("ThirdFunc", arr_n, arr_pn) | |
def main() : | |
p = float(input("p: ")) ## p = 0.5 | |
N = int(input("N: ")) ## N = 50 | |
FirstFunc(p, N) | |
SecondFunc(p, N) | |
ThirdFunc(p, N) | |
if __name__ == "__main__" : | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from math import * | |
import matplotlib.pyplot as plt | |
from random import random | |
from scipy.special import gamma, hyp2f1 | |
import numpy as np | |
def BuildGrpah(hist_data, graph_data) : | |
bins = np.linspace(0, 2.2, 50) | |
plt.hist(hist_data, bins, density=True) | |
plt.plot(graph_data[0], graph_data[1]) | |
plt.savefig("keks.png") | |
plt.show() | |
def Nomber4(a, b) : | |
def Distribution(a, b) : | |
return random()**a + random()**b | |
data = [] | |
for i in range(100000) : | |
g = Distribution(a, b) | |
data.append(g) | |
return data | |
def Nomber5(a, b) : | |
def P1(x) : | |
return (x**(1/a + 1/b - 1)) * (gamma(1+1/a)*gamma(1+1/b) / gamma(1/a+1/b)) | |
def P2(x) : | |
A = hyp2f1(1/a, 1-1/b, 1/a+1, 1/x) - (x-1)**(1/a)*hyp2f1(1/a, 1-1/b, 1/a+1, 1-1/x) | |
return A * x**(1/b-1)/b | |
def Distribution(x) : | |
if -0 <= x <= 1 : | |
return P1(x) | |
return P2(x) | |
dataX, dataY = [], [] | |
x = 0 | |
while x < 2 : | |
p = Distribution(x) | |
dataX.append(x) | |
dataY.append(p) | |
x += 0.001 | |
return [dataX, dataY] | |
def main() : | |
a = float(input("a: ")) #a = 0.5 | |
b = float(input("b: ")) #b = 0.1 | |
hist_data = Nomber4(a, b) | |
graph_data = Nomber5(a, b) | |
BuildGrpah(hist_data, graph_data) | |
if __name__ == "__main__" : | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment