iam-git / WellMet (public) (License: MIT) (since 2021-08-31) (hash sha1)
WellMet is pure Python framework for spatial structural reliability analysis. Or, more specifically, for "failure probability estimation and detection of failure surfaces by adaptive sequential decomposition of the design domain".

/sball.py (df0e88ea13c95cd1463a8ba1391e27766b95c3a5) (6558 bytes) (mode 100644) (type blob)

#!/usr/bin/env python
# coding: utf-8

import numpy as np
import scipy.special as sc
from scipy import stats

#######################################################
# s-balls  -- tools to calc probabilities and radii ###
#######################################################


def get_ps_ball(d,R):
    "returns probability of falling inside d-ball with radius R"
    #return np.sum(np.exp(-(rho**2)/2)*rho**(d-1) )* R/n
    return sc.gammainc(d/2, R**2/2)

def get_pf_ball(d,R):
    "returns probability of falling outside d-ball with radius R"
    return sc.gammaincc(d/2, R**2/2)

def get_Radius_pf(d,pf_ball):
    "returns radius of an d-ball with probability pf outside"
    rsqdiv2 = sc.gammainccinv(d/2, pf_ball)
    return np.sqrt(2*rsqdiv2) #radius

def get_Radius_ps(d,ps_ball):
    "returns radius of an d-ball with probability ps inside"
    rsqdiv2 = sc.gammaincinv(d/2, ps_ball)
    return np.sqrt(2*rsqdiv2) #radius





# implement class compatible to the old ones

# dispatcher
def Sball(nvar):
    if nvar == 2:
        return Sball_2D(nvar)
    else:
        return Sball_nD(nvar)

class Sball_nD:
    def __init__(self, nvar):
        self.nvar = nvar
        self.a = nvar/2
        
    def get_pf(self, r):
        "returns pf, i.e. complementary part of multidimensional Gaussian distribution"
        return sc.gammaincc(self.a, r**2/2)
        
    def get_ps(self, r):
        "returns probability of falling inside d-ball with radius R"
        return sc.gammainc(self.a, r**2/2)
    
    def get_r(self, desired_pf):
        "sball inversion. Returns radius of the s-ball with probability pf outside"
        rsqdiv2 = sc.gammainccinv(self.a, desired_pf)
        return np.sqrt(2*rsqdiv2) #radius
    
    
    def get_r_iteration(self, desired_pf):
        "Same as .get_r(), just keeps compatibility with previous versions"
        return self.get_r(desired_pf), desired_pf
    
    # make it, finally, scipy.stats -compatible
    sf = get_pf
    isf = get_r
    cdf = get_ps
    def ppf(self, q):
        return get_Radius_ps(self.nvar, q)

    
class Sball_2D(Sball_nD):
    def get_pf(self, r):
        "returns pf, i.e. complementary part of multidimensional Gaussian distribution"
        return np.exp(-r**2/2)
    
    def get_r(self, desired_pf):
        "sball inversion. Returns radius of the s-ball with probability pf outside"
        return np.sqrt(-2*np.log(desired_pf))
    
    
# calculation is as fast as Sball_nD
# but I'm not sure about precision
class Sball_4D(Sball_nD):
    def get_pf(self, r):
        "returns pf, i.e. complementary part of multidimensional Gaussian distribution"
        return (r**2/2+1)*np.exp(-r**2/2)








#1/ univariate funkce pro bounded Gauss
# left-right-bounded univariate Gaussian
class Radial:
    def __init__(self, nvar, r=0, R=np.inf):
        self.sball = Sball(nvar)
        self.set_bounds(r, R)
        
    def set_bounds(self, r=0, R=np.inf):
        #č kbyby se někomu nechtělo naťukat "np.inf"
        self.r = r # left bound
        self.R = R # rigth bound
        
        self.ps = self.sball.get_ps(r)
        self.pf = self.sball.get_pf(R)
        #č obsah pravděpodobnosti v mezikruži
        # well, probability falling to the shell
        self.p_shell = self.sball.get_pf(r) - self.pf
    
    #č jen pro formu. Kdo by to potřeboval?
    def _pdf(self, x): return stats.chi.pdf(x, self.sball.nvar) / self.p_shell
    def _cdf(self, x): return (self.sball.get_ps(x) - self.ps) / self.p_shell
    def _sf(self, x): return (self.sball.get_pf(x) - self.pf) / self.p_shell
    
    def pdf(self, x):
        return np.piecewise(x, [x<=self.r, x>=self.R], [0, 0, self._pdf])
        
    def cdf(self, x):
        return np.piecewise(x, [x<=self.r, x>=self.R], [0, 1, self._cdf])
        
    def sf(self, x): # 1 - cdf
        return np.piecewise(x, [x<=self.r, x>=self.R], [1, 0, self._sf])

    def ppf(self, q):
        return get_Radius_ps(self.sball.nvar, q*self.p_shell + self.ps)
        
    def isf(self, q): # inverse of .sf()
        return self.sball.get_r(q*self.p_shell + self.pf)
        
        

def get_random_directions(ns, ndim):
    # rand_dir: prepare ns random directions on a unit d-sphere
    rand_dir = np.random.randn(ns, ndim) #random directions
    
    
    lengths = np.sum(np.square(rand_dir), axis=1)
    lengths = np.sqrt(lengths, out=lengths) #lengths of each radius-vector
    
    # scale all radii-vectors to unit length
    # use [:,None] to get an transposed 2D array
    rand_dir = np.divide(rand_dir, lengths[:,None], out=rand_dir) 
    
    return rand_dir



#č nebyl to úplně ideální napad dědit od Radial
#č cdf, ppf, sf a isf metody nejsou pro Shell aplikovatelné!
#č Ještě jednou, bacha, davejte pozor, co vztahuje k 1D radiálnímu rozdělení,
#č co - k optimálnímu IS rozdělení proporcionálnímu nD Gaussu.
# We apologize for inconvenience
class Shell(Radial):
    """
    Optimal sampling density for Nv-ball (gaussian samples outside Nv-ball)
    with density proportional to Gaussian density
    """
    def rvs(self, size=1): # keyword size is scipy.stats-compatible
        "Generování­ vzorků (kandidátů a integračních bodů)"
        ns = size
        # rand_dir: prepare ns random directions on a unit d-sphere
        rand_dir = get_random_directions(ns, self.sball.nvar) #random directions
        
        # generate sampling probabilites
        p = np.linspace(1, 0, ns, endpoint=False) # probabilities for the radius
        
        # convert probabilitites into random radii
        # (distances from origin that are greater than r and less than R)
        r = self.isf(p) # actually, it is the same as CDF inverse
        
        #finally a random sample from the optimal IS density:
        sample_G = rand_dir*r[:,None]
        
        return sample_G

    #оӵ кулэ ӧвӧл #č multi nebudem použivat
    def _pdf_multi(self, x): return np.prod(stats.norm.pdf(x), axis=1)/self.p_shell
    
    def _pdf_norm(self, rx): 
        # stats.norm.pdf(0) == 0.3989422804014327
        c = 0.3989422804014327**(self.sball.nvar-1)
        return stats.norm.pdf(rx)*c/self.p_shell
    
    #č Vypocet hustot optimalni vzorkovaci veliciny (left-right-bounded)
    def pdf(self, x):
        """density of optimal IS variable h, 
        i.e. Gaussian variable with zero density inside d-ball"""
        rx = np.sum(x**2, axis=1)
        rx = np.sqrt(rx, out=rx) 
        return np.piecewise(rx, [rx<=self.r, rx>=self.R], [0, 0, self._pdf_norm])



Mode Type Size Ref File
100644 blob 28117 0907e38499eeca10471c7d104d4b4db30b8b7084 IS_stat.py
100644 blob 6 0916b75b752887809bac2330f3de246c42c245cd __init__.py
100644 blob 72 458b7e2ca46acd9ec0d2caf3cc4d72e515bb73dc __main__.py
100644 blob 73368 3d245b8568158ac63c80fa0847631776a140db0f blackbox.py
100644 blob 11243 10c424c2ce5e8cdd0da97a5aba74c54d1ca71e0d candybox.py
100644 blob 29927 066a2d10ea1d21daa6feb79fa067e87941299ec4 convex_hull.py
100644 blob 102798 059ae717e71c651975673420cd8230fbef171e5e dicebox.py
100644 blob 36930 a775d1114bc205bbd1da0a10879297283cca0d4c estimation.py
100644 blob 34394 3f0ab9294a9352a071de18553aa687c2a9e6917a f_models.py
100644 blob 31142 3e14ac49d16a724bb43ab266e8bea23114e47958 g_models.py
100644 blob 20908 457329fe567f1c0a9950c21c7c494cccf38193cc ghull.py
100644 blob 2718 5d721d117448dbb96c554ea8f0e4651ffe9ac457 gp_plot.py
100644 blob 29393 96162a5d181b8307507ba2f44bafe984aa939163 lukiskon.py
100644 blob 2004 6ea8dc8f50a656c48f786d5a00bd6398276c9741 misc.py
040000 tree - 2a0527abf425507d6fcf54c34fd7c3c431a66973 mplot
100644 blob 1462 437b0d372b6544c74fea0d2c480bb9fd218e1854 plot.py
100644 blob 2807 1feb1d43e90e027f35bbd0a6730ab18501cef63a plotly_plot.py
040000 tree - 54d0d3d9089d02fe60dfc620e22388b6d5a7755a qt_gui
100644 blob 8566 5c8f8cc2a34798a0f25cb9bf50b5da8e86becf64 reader.py
100644 blob 4284 a0e0b4e593204ff6254f23a67652804db07800a6 samplebox.py
100644 blob 6558 df0e88ea13c95cd1463a8ba1391e27766b95c3a5 sball.py
100644 blob 6739 0b6f1878277910356c460674c04d35abd80acf13 schemes.py
100644 blob 76 11b2fde4aa744a1bc9fa1b419bdfd29a25c4d3e8 shapeshare.py
100644 blob 54074 ba978868adb487385157afa5b3420f9ad90e4f46 simplex.py
100644 blob 13090 2b9681eed730ecfadc6c61b234d2fb19db95d87d spring.py
100644 blob 10953 da8a8aaa8cac328ec0d1320e83cb802b562864e2 stm_df.py
040000 tree - e266ef72bdc7ce6e020b53c6df695051954c9a4d testcases
100644 blob 2465 d829bff1dd721bdb8bbbed9a53db73efac471dac welford.py
100644 blob 20204 1a281748b81481f8d51c3793bcf46b0034082152 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