예제 #1
0
def biased_regresser(size, ty, beta=1.0):
    #
    value = []
    #
    if dataset_name == "SS":
        ## Best fit for citation dataset
        value = lognorm.rvs(1.604389429520587,
                            48.91174576443938,
                            77.36426476362374,
                            size=size)
    elif dataset_name == "JEE":
        # Best fit for JEE scoress
        if ty == 0:
            value = johnsonsu.rvs(-1.3358254338685507,
                                  1.228621987785165,
                                  -16.10471198333935,
                                  25.658144591068066,
                                  size=size)  ## Men
        if ty == 1:
            value = johnsonsu.rvs(-1.1504808824385124,
                                  1.3649066883190795,
                                  -12.879957294149737,
                                  27.482272133428403,
                                  size=size)  ## Women
    else:
        print("Unknown dataset_name=%x", dataset_name)
        exit()
    #
    if ty == 1: value *= (beta + 1e-4)
    #
    return [{'val': val, 'real_type': ty} for val in value]
예제 #2
0
    def johnson_su_pdf(self, system_type, order=None, _error=None, size=1):
        """

        Parameters
        ----------
        system_type
        order
        _error

        Returns
        -------

        """
        params = self.config[_error][system_type][order][1]
        shape, shape, location, _scale, bounds = params
        rv = johnsonsu.rvs(shape, shape, loc=location, scale=_scale, size=size)

        import pdb
        pdb.set_trace()
        while not ((-100 < rv) & (rv < 100)):
            rv = johnsonsu.rvs(shape,
                               shape,
                               loc=location,
                               scale=_scale,
                               size=size)
        return rv / 100
예제 #3
0
 def headway_j(self, flow_rate):
     assert flow_rate >= F_RATES[
         0] / 2, f'Input flow rate ({flow_rate}) should not be smaller than {F_RATES[0]/2}v/h for calculating the headway!'
     #assert flow_rate <= F_RATES[-1], f'Input flow rate ({flow_rate}) should not be bigger than {F_RATES[-1]}v/h, the saturation rate of one lane!'
     headway = {
         F_RATES[0]: lambda: johnsonsb.rvs(0.3, 1.8, loc=0.85, scale=130),
         F_RATES[1]: lambda: johnsonsb.rvs(0.9, 0.71, loc=0.85, scale=60),
         F_RATES[2]:
         lambda: johnsonsb.rvs(2.49, 0.71, loc=0.85, scale=104.57),
         F_RATES[3]:
         lambda: johnsonsb.rvs(3.71, 0.98, loc=0.85, scale=104.57),
         F_RATES[4]:
         lambda: johnsonsu.rvs(-2.18, 1.15, loc=0.8, scale=0.52),
         F_RATES[5]:
         lambda: johnsonsu.rvs(-2.54, 1.31, loc=0.47, scale=0.46),
         F_RATES[6]:
         lambda: johnsonsu.rvs(-2.49, 1.49, loc=0.55, scale=0.49)
     }
     if flow_rate <= F_RATES[0]:
         result = min(
             900, headway[F_RATES[0]]() * F_RATES[0] / flow_rate
         )  # headyway should be smaller than 900s, which is the normal measure period of traffic flow
         return result
     for i in range(1, len(F_RATES)):
         if flow_rate <= F_RATES[i]:
             ratio = (flow_rate - F_RATES[i - 1]) / (F_RATES[i] -
                                                     F_RATES[i - 1])
             result = (1 - ratio) * headway[F_RATES[
                 i - 1]]() + ratio * headway[F_RATES[i]]()
             result = max(result, 0)
             return result
     result = max(headway[F_RATES[-1]]() * F_RATES[-1] / flow_rate,
                  0)  # headyway should be bigger than 0
     return result
예제 #4
0
def simulateDelay(currentNetwork):
    '''
    @description: generates a delay based on the appropriate distribution
    '''
    wifiDelay = [3.0659475327,
                 14.6918344498]  # min and max delay observed for wifi
    cellularDelay = [4.2531193161,
                     14.3172883892]  # min and max delay observed for 3G

    if currentNetwork == 1:  # wifi
        # johnson su in python (fitter.Fitter.fit()) and t location-scale in matlab (allfitdist)
        # in python, error is higher for t compared to johnson su
        delay = min(
            max(
                johnsonsu.rvs(0.29822254217554717,
                              0.71688524931466857,
                              loc=6.6093350624107909,
                              scale=0.5595970482712973), wifiDelay[0]),
            wifiDelay[1])
    else:
        # t in python (fitter.Fitter.fit()) and t location-scale in matlab (allfitdist)
        delay = min(
            max(
                t.rvs(0.43925241212097499,
                      loc=4.4877772816533934,
                      scale=0.024357324434644639), cellularDelay[0]),
            cellularDelay[1])
    if DEBUG >= 1:
        print(
            colored(
                "Delay for " + str(availableNetworkName[currentNetwork - 1]) +
                ": " + str(delay), "cyan"))
    # input()
    return delay
예제 #5
0
    def computeDelay(self):
        '''
        description: generates a delay for switching between WiFi networks, which is modeled using Johnson’s SU distribution (identified as a best fit to 500 delay values),
                     and delay for switching between WiFi and cellular networks, modeled using Student's t-distribution (identified as best fit to 500 delay values)
        args:        self
        returns:     a delay value
        '''
        wifiDelay = [3.0659475327, 14.6918344498]       # min and max delay observed for wifi in some real experiments; used as caps for the delay generated
        cellularDelay = [4.2531193161, 14.3172883892]   # min and max delay observed for 3G in some real experiments; used as caps for the delay generated

        if networkList[getListIndex(networkList, self.currentNetwork)].getWirelessTechnology() == 'WiFi':
            delay = min(max(johnsonsu.rvs(0.29822254217554717, 0.71688524931466857, loc=6.6093350624107909, scale=0.5595970482712973), wifiDelay[0]), wifiDelay[1])
        else:
            delay = min(max(t.rvs(0.43925241212097499, loc=4.4877772816533934, scale=0.024357324434644639), cellularDelay[0]), cellularDelay[1])
        return delay
예제 #6
0
    def lift(self):
        NStates = int(-2.5 + 0.5 * np.sqrt(25 + 4 * len(self.state)))
        Means = self.state[:NStates]
        Cov = self.state[NStates:NStates + NStates**2].reshape(
            NStates, NStates)
        Params = self.state[NStates + NStates**2:]
        vals, vecs = np.linalg.eigh(Cov)
        vals[vals < 0] = 0.0

        randvals = np.zeros((NStates, self.NRuns))
        for ii in xrange(0, NStates):
            randvals[ii, :] = johnsonsu.rvs(Params[4 * ii],
                                            Params[4 * ii + 1],
                                            loc=Params[4 * ii + 2],
                                            scale=Params[4 * ii + 3],
                                            size=self.NRuns)
        states = np.dot(vecs, randvals).T

        randvec = states.reshape(-1) + np.tile(Means, self.NRuns)

        return microMeanJohnson(self.time, probRound(randvec), self.perad)
예제 #7
0
 def gen_sample(self, n):
     return johnsonsu.rvs(self.a,
                          self.b,
                          loc=self.mu,
                          scale=self.sigma,
                          size=n)
예제 #8
0
x = np.linspace(johnsonsu.ppf(0.01, a, b),
                johnsonsu.ppf(0.99, a, b), 100)
ax.plot(x, johnsonsu.pdf(x, a, b),
       'r-', lw=5, alpha=0.6, label='johnsonsu pdf')

# Alternatively, the distribution object can be called (as a function)
# to fix the shape, location and scale parameters. This returns a "frozen"
# RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen ``pdf``:

rv = johnsonsu(a, b)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

# Check accuracy of ``cdf`` and ``ppf``:

vals = johnsonsu.ppf([0.001, 0.5, 0.999], a, b)
np.allclose([0.001, 0.5, 0.999], johnsonsu.cdf(vals, a, b))
# True

# Generate random numbers:

r = johnsonsu.rvs(a, b, size=1000)

# And compare the histogram:

ax.hist(r, normed=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()
def SAimage_fft_2(N_total=500,
                  rms=3,
                  skewness=1,
                  kurtosis=3,
                  corlength_x=10,
                  corlength_y=10,
                  alpha=0.9,
                  th=1000.0,
                  non_Gauss=True,
                  corr=False):
    '''
	Input:
	
	Generation of non-Gaussian surfaces with predetermined 
	1. N_total: The dimension of our plane
	2. Standard deviation (rms)
	3. Skewness (skew)
	4. Kurtosis (kurt)
	5. Correlation lengths (ksix, ksiy)
	6. Roughness exponent (aplha)
	7. The threshold we choose to binarize our image
	8. non_Gaus: is True if we want the non-Gaussian simulation, and False when we want the Gaussian one
	9. corr: True if you want to illustrate correlation functions 

	The input values of kurtosis and skewness should satisfy the inequality
	kurtosis > 1 + skewness^2
	The method is described in Yang et al. CMES, vol.103, no.4, pp.251-279,2014
	We use pearson translation to transform random noise to non-gaussian series and 
	inverse Fourier transform to generate Gaussian surfaces.
	
	The method is implemented in three steps
	1. Generation of a Gaussian self-affine surface z_gs with rms, ksix,ksiy,alpha
	2. Generation of non-Gaussian noise z_ngn with mu,rms,skew,kurt
	3. Arranging z_ngn according to the spatial arrangement of z_g to get a non-Gaussian self-affine surface z_ngs
	'''

    ##################################################################
    #
    # Coded in Python by SEBASTIAN KORSAK © 2021 ([email protected])
    # Original file in MATLAB by Dr. V. Costantoudis
    #
    ##################################################################

    # Input
    N = N_total
    numpoints = N
    hhcf_y = np.zeros(int(N / 2))
    Pyy = 0.0

    # input moments and spatial parameters
    skew = skewness
    kurt = kurtosis
    ksix = corlength_x
    ksiy = corlength_y
    alpha = alpha
    mu = 0

    # 1st step: Generation of a Gaussian surface

    # Determine the autocorrelation function R(tx,ty)
    R = np.zeros([numpoints + 1, numpoints + 1])
    txmin, txmax, tymin, tymax = -numpoints / 2, numpoints / 2, -numpoints / 2, numpoints / 2
    dtx, dty = (txmax - txmin) / (numpoints), (tymax - tymin) / (numpoints)

    tx = np.arange(txmin, txmax, dtx)
    ty = np.arange(tymin, tymax, dty)

    for txx in tx:
        for tyy in ty:
            R[int(txx + txmax + 1), int(tyy + tymax + 1)] = (
                (rms**2) *
                np.exp(-(abs(np.sqrt((txx / ksix)**2 +
                                     (tyy / ksiy)**2)))**(2 * alpha)))

    # According to the Wiener-Khinchine theorem FR is the power spectrum of the desired profile
    FR = np.fft.fft2(R, s=[numpoints, numpoints])
    AMPR = np.sqrt(dtx**2 + dty**2) * abs(FR)

    # 2nd step: Generate a white noise, normalize it and take its Fourier transform
    X = np.random.rand(numpoints, numpoints)
    aveX = X.mean(axis=0).mean(axis=0)
    dif2X = (X - aveX)**2
    stdX = np.sqrt(dif2X.mean(axis=0).mean(axis=0))
    X = X / stdX
    XF = np.fft.fft2(X, s=[numpoints, numpoints])

    # 3nd step: Multiply the two Fourier transforms
    YF = XF * np.sqrt(AMPR)

    # 4th step: Perform the inverse Fourier transform of YF and get the desired surface
    zaf = np.fft.ifft2(YF, s=[numpoints, numpoints])
    z = np.real(zaf)
    avez = z.mean(axis=0).mean(axis=0)
    dif2z = (z - avez)**2
    stdz = np.sqrt(dif2z.mean(axis=0).mean(axis=0))
    z = ((z - avez) * rms) / stdz

    # Define the fraction of the surface to be analysed
    xmin, xmax, ymin, ymax = 0, N, 0, N
    z_gs = z[xmin:xmax, ymin:ymax]
    print(z_gs.shape)
    Nh = xmax - xmin + 1

    # For the Gaussian Surface Simulation
    if not non_Gauss:
        X = np.arange(0, N)
        Y = np.arange(0, N)
        X, Y = np.meshgrid(X, Y)

        fig = plt.figure()
        fig.set_figwidth(10)
        fig.set_figheight(10)
        ax = plt.axes(projection='3d')
        ax.plot_surface(X, Y, z_gs, cmap='viridis', edgecolor='none')
        ax.view_init(30, 150)
        plt.title('Gaussian Surface', fontsize=16)
        plt.show()

        plt.imshow(z_gs < th,
                   cmap='gray',
                   origin='lower',
                   interpolation='none')
        plt.title('Binarized Image from Gaussian Surface')
        plt.show()

    del R, AMPR, z

    # For the Non-Gaussian Surface Simulator
    if non_Gauss:
        #2nd step: Generation of a non-Gaussian noise NxN
        ## Finding the parameters of JohnsonSU distribution
        ## Special thank to Max Pierini for this part of the code
        coef, j_type, err = f_johnson_M(mu, rms, skew, kurt)

        gamma, delta, xi, lam = coef

        ## Simulating from JohnsonSU distribution
        z_ngn = johnsonsu.rvs(a=gamma, b=delta, loc=xi, scale=lam, size=[N, N])

        #3rd step: Combination of z_gs with z_ngn to output a z_ngs
        v_gs = z_gs.reshape(-1)
        v_ngn = z_ngn.reshape(-1)
        Igs = np.argsort(v_gs)
        Ingn = np.argsort(v_ngn)
        vs_gs = np.sort(v_gs)
        vs_ngn = np.sort(v_ngn)

        v_ngs = np.zeros(np.max(Igs) + 1)

        for iv in range(N * N):
            ivs = Igs[iv]
            v_ngs[ivs] = vs_ngn[iv]

        X = np.arange(0, N)
        Y = np.arange(0, N)
        X, Y = np.meshgrid(X, Y)
        z_ngs = -v_ngs.reshape(N, N)
        # Creating color map
        my_cmap = plt.get_cmap('hot')

        fig = plt.figure()
        fig.set_figwidth(10)
        fig.set_figheight(10)
        ax = plt.axes(projection='3d')
        ax.set_xlim(0, N)
        ax.set_ylim(0, N)
        ax.set_zlim(-1000, 5000)
        ax.plot_surface(X, Y, z_ngs, cmap=my_cmap, edgecolor='black')
        ax.view_init(30, 150)
        plt.title('Non-Gaussian Surface', fontsize=16)
        # Add a color bar which maps values to colors.
        plt.show()

        plt.imshow(z_ngs > th,
                   cmap='gray',
                   origin='lower',
                   interpolation='none')
        plt.title('Binarized Image from Non-Gaussian Surface', fontsize=16)
        plt.show()

        if corr:
            # Correlation Functions
            # a. 1-D height-height correlation function
            inpsur = z_ngs
            hhcf1d = np.zeros(N // 2)
            rdif = np.zeros(N // 2)
            for ndif in range(N // 2):
                surf1 = inpsur[0:N, 0:(N - ndif)]
                surf2 = inpsur[0:N, ndif:N]
                difsur2 = (surf1 - surf2)**2
                hhcf1d[ndif] = np.sqrt(difsur2.mean(axis=0).mean(axis=0))
                rdif[ndif] = ndif

            plt.loglog(rdif, hhcf1d)
            plt.grid()
            plt.xlabel('log(r(nm))')
            plt.ylabel('log(G(r) (nm))')
            plt.title(
                '1-D height-height correlation function (non-Gaussian surface)'
            )
            plt.show()