/sball_old.py (bac994ae58f1df80c7f8b3f33955af5402f5a4f3) (5553 bytes) (mode 100644) (type blob)
#!/usr/bin/env python
# coding: utf-8
import numpy as np
#from scipy import stats
from scipy import special # for S_ball
from scipy import integrate # for S_ball
# нельзя просто так взять и написать Ньютонову методу
# fails on nvar = 501, fails on Sball(500).get_r(0), fails on Sball(800).get_r(0.999)
class Sball:
def __init__(self, nvar):
self.nvar = nvar
if nvar != 2:
self.C = 2**(1-nvar/2) / special.gamma(nvar/2)
self.logC = (1-nvar/2)*np.log(2) - special.gammaln(nvar/2)
self.flex = self.current_r = np.sqrt(self.nvar-1)
self.flex_pf = self.current_pf = self.get_pf(self.flex)
def get_pf(self, r):
"""
returns pf, i.e. complementary part of multidimensional Gaussian distribution
"""
if self.nvar == 1:
#return 1 - 2**(1-nvar/2) / special.gamma(nvar/2) * (np.sqrt(np.pi)*special.erf(r/np.sqrt(2)))/np.sqrt(2)
return 1 - special.erf(r/1.4142135623730951)
elif self.nvar == 2:
return np.exp(-r**2/2)
elif self.nvar == 3:
#return 1 - 2**(1-nvar/2) / special.gamma(nvar/2) * (np.exp(-r**2/2)*(np.sqrt(np.pi)*np.exp(r**2/2)*special.erf(r/np.sqrt(2))-np.sqrt(2)*r))/np.sqrt(2)
return 1 - 0.5641895835477564 * (np.exp(-r**2/2)*(np.sqrt(np.pi)*np.exp(r**2/2)*special.erf(r/np.sqrt(2))-np.sqrt(2)*r))
elif self.nvar == 4:
return (r**2/2+1)*np.exp(-r**2/2)
elif self.nvar == 6:
return (r**4+4*r**2+8)*np.exp(-r**2/2)/8
# nvar=8: (48-(r^6+6*r^4+24*r^2+48)*e^(-r^2/2) / 2**(nvar/2))/48
# hračička ve hračce
# nemám žádnou jistotu, že tohle počítá přesněji
# ale ve výsokých dimenzích aspoň počítá
elif self.nvar % 2 == 0: # sudé
poly = [1]
for i in range(self.nvar-2, 0, -2):
poly.append(0)
poly.append(i*poly[-2])
return np.polyval(np.array(poly) / poly[-1], r) * np.exp(-r**2/2)
else:
try:
pf = self.C * integrate.quad(lambda x: np.exp(-(x**2)/2)*x**(self.nvar-1), r, np.inf)[0]
except OverflowError:
pf = 1 - self.C * integrate.quad(lambda x: np.exp(-(x**2)/2)*x**(self.nvar-1), 0, r)[0]
return pf
def get_r(self, desired_pf):
"""
sball_inversion
returns r
"""
if self.nvar == 2:
return np.sqrt(-2*np.log(desired_pf))
elif self.flex_pf == desired_pf:
return self.flex
else:
# je to jistější
self.current_r = self.flex
self.current_pf = previous_pf = self.flex_pf
self.__do_iter(desired_pf)
self.current_pf = self.get_pf(self.current_r)
# hrůza
# pokračujeme, dokud to nezkonverguje, přenejmenším pokud to konvergue a neosciluje.
while self.current_pf != previous_pf and self.current_pf != desired_pf\
and (self.current_pf > desired_pf or previous_pf < desired_pf):
previous_pf = self.current_pf
self.__do_iter(desired_pf)
self.current_pf = self.get_pf(self.current_r)
return self.current_r
def __do_iter(self, desired_pf):
r = self.current_r
denominator = (self.C * np.exp(-(r**2)/2)*r**(self.nvar-1))
if denominator != 0 and not np.isnan(denominator):
self.current_r += (self.current_pf - desired_pf) / denominator
else:
# zkombinujeme C a r^nvar, ale stejně nikoho to nezahraní
log_delta = np.log(abs(self.current_pf - desired_pf)) + (r**2)/2 - (np.log(r)*(self.nvar-1) + self.logC)
self.current_r += np.exp(log_delta)*np.sign(self.current_pf - desired_pf)
if self.current_r < 0:
self.current_r = r/2
def get_r_iteration(self, desired_pf):
"""
Same as get_r, but do just one iteration
"""
if self.nvar == 2:
return np.sqrt(-2*np.log(desired_pf)), desired_pf
# logaritmus je na nulu citelný
elif self.current_pf - desired_pf != 0:
# hrůza, nečitelný
# pokud je současné r-ko v jiné straně od chtěného r-ka, tak se vrátíme do inflexního bodu
if (self.flex_pf > self.current_pf) is (self.flex_pf < desired_pf):
# vstupní kontrola
self.current_r = self.flex
self.current_pf = self.flex_pf
r = self.current_r # pro výstupní kontrolu
self.__do_iter(desired_pf)
# vystupní kontrola
if (self.flex > self.current_r) is (self.flex < r):
# preskočili jsme inflexní bod
self.current_r = self.flex
self.current_pf = self.flex_pf
# ještě jednou
self.__do_iter(desired_pf)
self.current_pf = self.get_pf(self.current_r) # ne že bychom pf potrebovali v tomto kroce, ale...
return self.current_r, self.current_pf
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
28117 |
0907e38499eeca10471c7d104d4b4db30b8b7084 |
IS_stat.py |
100644 |
blob |
6 |
0916b75b752887809bac2330f3de246c42c245cd |
__init__.py |
100644 |
blob |
73368 |
3d245b8568158ac63c80fa0847631776a140db0f |
blackbox.py |
100644 |
blob |
11243 |
10c424c2ce5e8cdd0da97a5aba74c54d1ca71e0d |
candybox.py |
100644 |
blob |
26958 |
5acc5a7b512691d13a511deee135fd90c86e55c8 |
convex_hull.py |
100644 |
blob |
84851 |
811ef4d818e55cbe2d0305da62fcc2e1cada359a |
dicebox.py |
100644 |
blob |
36930 |
a775d1114bc205bbd1da0a10879297283cca0d4c |
estimation.py |
100644 |
blob |
34394 |
3f0ab9294a9352a071de18553aa687c2a9e6917a |
f_models.py |
100644 |
blob |
31540 |
a577087003a885ca7499d1ee9451e703fa9d2d36 |
g_models.py |
100644 |
blob |
20552 |
d121d3a9f8fe544aaaf22f7524355e480b1e767f |
ghull.py |
100644 |
blob |
42820 |
1092b3b9f05b11d0c53b3aa63df2460ec355085d |
gl_plot.py |
100644 |
blob |
2718 |
5d721d117448dbb96c554ea8f0e4651ffe9ac457 |
gp_plot.py |
100644 |
blob |
29393 |
96162a5d181b8307507ba2f44bafe984aa939163 |
lukiskon.py |
100644 |
blob |
2004 |
6ea8dc8f50a656c48f786d5a00bd6398276c9741 |
misc.py |
040000 |
tree |
- |
0e9b1f6d2b2c5a92806b461940f0d2d4333bdc6c |
mplot |
100644 |
blob |
1450 |
4849f178b588e252b8c7f6a940d2d82ad35f6914 |
plot.py |
100644 |
blob |
2807 |
1feb1d43e90e027f35bbd0a6730ab18501cef63a |
plotly_plot.py |
100644 |
blob |
143853 |
ef70c013dbcb917af28451fac91dacba877aa029 |
qt_plot.py |
100644 |
blob |
8206 |
5981023118262109fca8309d9b313b521a25f88f |
reader.py |
100644 |
blob |
4284 |
a0e0b4e593204ff6254f23a67652804db07800a6 |
samplebox.py |
100644 |
blob |
6558 |
df0e88ea13c95cd1463a8ba1391e27766b95c3a5 |
sball.py |
100644 |
blob |
5553 |
bac994ae58f1df80c7f8b3f33955af5402f5a4f3 |
sball_old.py |
100644 |
blob |
2605 |
0034d2e3f14c056541888235e59127e8f28b131d |
schemes.py |
100644 |
blob |
21623 |
281aef80556b8d22842b8659f6f0b7dab0ad71af |
shapeshare.py |
100644 |
blob |
48537 |
10f90c5614e9a04f0cd9f78e75f0db4a6becb3e4 |
simplex.py |
100644 |
blob |
13090 |
2b9681eed730ecfadc6c61b234d2fb19db95d87d |
spring.py |
100644 |
blob |
10940 |
6965eabdb5599bb22773e7fef1178f9b2bb51efe |
stm_df.py |
100644 |
blob |
3433 |
3063a1b6a132cbb5440ab95f1b6af1f1ff4266ac |
testcases_2D.py |
100644 |
blob |
2465 |
d829bff1dd721bdb8bbbed9a53db73efac471dac |
welford.py |
100644 |
blob |
22048 |
4a6014ca5255aa96059ff9ed5a7e29df98d26ffc |
whitebox.py |
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/iam-git/WellMet
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/iam-git/WellMet
Clone this repository using git:
git clone git://git.rocketgit.com/user/iam-git/WellMet
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main