Пример #1
0
    def _update_canvas(self):
        """
        Update the figure when the user changes an input value.
        :return:
        """
        # Get the parameters from the form
        loc = float(self.loc.text())
        scale = float(self.scale.text())
        shape_factor = float(self.shape_factor.text())

        # Get the selected distribution from the form
        distribution_type = self.distribution_type.currentText()

        if distribution_type == 'Gaussian':
            x = linspace(norm.ppf(0.001, loc, scale),
                         norm.ppf(0.999, loc, scale), 200)
            y = norm.pdf(x, loc, scale)
        elif distribution_type == 'Weibull':
            x = linspace(weibull_min.ppf(0.001, shape_factor),
                         weibull_min.ppf(0.999, shape_factor), 200)
            y = weibull_min.pdf(x, shape_factor, loc, scale)
        elif distribution_type == 'Rayleigh':
            x = linspace(rayleigh.ppf(0.001, loc, scale),
                         rayleigh.ppf(0.999, loc, scale), 200)
            y = rayleigh.pdf(x, loc, scale)
        elif distribution_type == 'Rice':
            x = linspace(rice.ppf(0.001, shape_factor),
                         rice.ppf(0.999, shape_factor), 200)
            y = rice.pdf(x, shape_factor, loc, scale)
        elif distribution_type == 'Chi Squared':
            x = linspace(chi2.ppf(0.001, shape_factor),
                         chi2.ppf(0.999, shape_factor), 200)
            y = chi2.pdf(x, shape_factor, loc, scale)

        # Clear the axes for the updated plot
        self.axes1.clear()

        # Display the results
        self.axes1.plot(x, y, '')

        # Set the plot title and labels
        self.axes1.set_title('Probability Density Function', size=14)
        self.axes1.set_xlabel('x', size=12)
        self.axes1.set_ylabel('Probability p(x)', size=12)

        # Set the tick label size
        self.axes1.tick_params(labelsize=12)

        # Turn on the grid
        self.axes1.grid(linestyle=':', linewidth=0.5)

        # Update the canvas
        self.my_canvas.draw()
Пример #2
0
    def __init__(self, dataset, algorithm, model, batch_size, learning_rate, L,
                 num_glob_iters, local_epochs, users_per_round, similarity,
                 noise, times):
        super().__init__(dataset, algorithm, model[0], batch_size,
                         learning_rate, L, num_glob_iters, local_epochs,
                         users_per_round, similarity, noise, times)
        self.control_norms = []

        # Initialize data for all  users
        data = read_data(dataset)
        total_users = len(data[0])
        for i in range(total_users):
            id, train, test = read_user_data(i, data, dataset)
            user = UserSCAFFOLD(id, train, test, model, batch_size,
                                learning_rate, L, local_epochs)
            self.users.append(user)
            self.total_train_samples += user.train_samples

        if self.noise:
            self.communication_thresh = rayleigh.ppf(
                1 - users_per_round / total_users)  # h_min

        print("Number of users / total users:", users_per_round, " / ",
              total_users)

        self.server_controls = [
            torch.zeros_like(p.data) for p in self.model.parameters()
            if p.requires_grad
        ]

        print("Finished creating SCAFFOLD server.")
Пример #3
0
    def get_icdf(self, xx):
        """
        A Rayleigh inverse cumulative density function.

        :param Rayleigh self:
            An instance of the Rayleigh class.
        :param array xx:
            Points at which the inverse cumulative density function needs to be evaluated.
        :return:
            Inverse cumulative density function values of the Rayleigh distribution.
        """
        return rayleigh.ppf(xx, loc=0, scale=self.scale)
 def getiCDF(self, xx):
     """
     A Rayleigh inverse cumulative density function.
     
     :param Rayleigh self:
         An instance of the Rayleigh class.
     :param array xx:
         Points at which the inverse cumulative density function needs to be evaluated.
     :return:
         Inverse cumulative density function values of the Rayleigh distribution.
     """
     return rayleigh.ppf(xx, loc=0, scale=self.scale)
Пример #5
0
    def __init__(self, dataset, algorithm, model, batch_size, learning_rate, L,
                 num_glob_iters, local_epochs, users_per_round, similarity,
                 noise, times):
        super().__init__(dataset, algorithm, model[0], batch_size,
                         learning_rate, L, num_glob_iters, local_epochs,
                         users_per_round, similarity, noise, times)

        # Initialize data for all  users
        data = read_data(dataset)
        total_users = len(data[0])
        for i in range(total_users):
            id, train, test = read_user_data(i, data, dataset)
            user = UserAVG(id, train, test, model, batch_size, learning_rate,
                           L, local_epochs)
            self.users.append(user)
            self.total_train_samples += user.train_samples

        if self.noise:
            self.communication_thresh = rayleigh.ppf(
                1 - users_per_round / total_users)  # h_min

        print("Number of users / total users:", users_per_round, " / ",
              total_users)
        print("Finished creating FedAvg server.")
Пример #6
0
from scipy.stats import rayleigh
import numpy as np

x = np.arange(0, 1, 0.001)
plt.title("Rayleigh(57) PPF")
plt.plot(x, rayleigh.ppf(x, scale=57))
Пример #7
0
def robust_regression(output, input, ref, output_level):

    if output_level > 0:
        print('\t[INFO] Starting robust regression. Number of samples: ' +
              str(int(len(output))))
        s_time = time.time()

    # Initializing variables for robust regression
    z_error = [0., 0.]
    rquantile = rayleigh.ppf(1 - 1. / len(output), loc=0, scale=1)
    bquantile = beta.ppf(1 - 1. / len(output), 2, len(output) - 2)
    weights = np.asarray([1. for sample in output])
    u_mat = np.diag([np.complex(1, 0) for j in range(len(output))])

    # Preparing arrays for Fortran call
    output = np.asarray(output, order='F')
    input = np.asarray(input, order='F').transpose()
    ref = np.asarray(ref, order='F').transpose()
    weights = np.asarray(weights, order='F')
    hat = hat_matrix(input, u_mat)

    # Preparing matrices
    s_time = time.time()
    G, GR, d, cm, dm = libMatrix.prepare_matrices(output, input, ref, weights,
                                                  len(output))
    if output_level > 2:
        print("\t\t\t[INFO_TIME] Elapsed time for matrix organization: " +
              str(time.time() - s_time) + ' seconds.')

    # Inverting least-squares solutions
    s_time = time.time()
    m = invert_tensor(cm, dm)
    if output_level > 2:
        print("\t\t\t[INFO_TIME] Elapsed time for matrix inversion: " +
              str(time.time() - s_time) + ' seconds.')

    # Calculting error on output field
    s_time = time.time()
    abs_error, sum_error = libMatrix.error_on_output(
        np.asarray(m, order='F').transpose(), d, G, weights, len(output))
    scale = np.median(np.abs(abs_error - np.median(abs_error))) / 0.44845
    if output_level > 2:
        print("\t\t\t[INFO_TIME] Elapsed time for error calculation: " +
              str(time.time() - s_time) + ' seconds.')

    count = 0  # counter to prevent infinite loop if unbalanced regression
    while count < 10:  # Robust regression loop
        if output_level > 0:
            print('\t[INFO] Robust regression. Iteration ' + str(count))
        sum_error_pre = sum_error

        # Calculting new weights based on distance error in regression plot
        weights = new_weights(abs_error, scale)  # HUBER weights
        l_weights = leverage_weights(hat, u_mat, bquantile)  # LEVERAGE weights
        weights = weights * np.asarray(l_weights)  # Weights combination
        u_mat = np.diag(weights)

        # Calculating new solution
        s_time = time.time()
        G, GR, d, cm, dm = libMatrix.prepare_matrices(output, input, ref,
                                                      weights, len(output))
        if output_level > 2:
            print("\t\t\t[INFO_TIME] Elapsed time for matrix organization: " +
                  str(time.time() - s_time) + ' seconds.')

        # Calculating new solutions using new weights
        s_time = time.time()
        m = invert_tensor(cm, dm)
        if output_level > 2:
            print("\t\t\t[INFO_TIME] Elapsed time for matrix inversion: " +
                  str(time.time() - s_time) + ' seconds.')

        # Calculating new scale and new weighted error
        s_time = time.time()
        abs_error, sum_error = libMatrix.error_on_output(
            np.asarray(m, order='F').transpose(), d, G, weights, len(output))
        scale = np.median(np.abs(abs_error - np.median(abs_error))) / 0.44845
        if output_level > 2:
            print("\t\t\t[INFO_TIME] Elapsed time for error calculation: " +
                  str(time.time() - s_time) + ' seconds.')

        # Comparison to see if convergence
        if (np.abs(sum_error - sum_error_pre) / sum_error_pre < 0.01):
            break
        else:
            count += 1

        # Update hat matrix
        hat = hat_matrix(input, u_mat)

    if count >= 10:
        print('\t[WARNING] No convergence found. Returning NaN.')
        return [
            np.nan + np.complex(0, 1) * np.nan,
            np.nan + np.complex(0, 1) * np.nan
        ]
    else:
        if output_level > 0:
            print('\t[INFO] Convergence found.')

    z_regression = [m[0][0], m[1][0]]

    if output_level > 0:
        print('\t[INFO] Starting jackknife procedure.')
    z_jackknife = [[] for n in range(len(output))]

    # Using full matrices to derive delete-ones
    cm_0, d_0 = libMatrix.prepare_tensor_inversion(output, input, ref, weights,
                                                   len(output))
    for n in range(len(output)):
        if output_level > 1:
            print('\t[INFO] Computing jackknife sample ' + str(n + 1) + '/' +
                  str(len(output)))

        # Removing jackknife sample, a bit of algebra shows it's just a substraction
        cm = cm_0 - np.asarray([[
            ref[n][0] * weights[n] * input[n][0],
            ref[n][0] * weights[n] * input[n][1]
        ],
                                [
                                    ref[n][1] * weights[n] * input[n][0],
                                    ref[n][1] * weights[n] * input[n][1]
                                ]])
        dm = d_0 - np.asarray([[ref[n][0] * weights[n] * output[n]],
                               [ref[1][0] * weights[n] * output[n]]])

        if output_level > 1:
            print(
                '\t[INFO] Computing least-squares solution for jackknife sample: '
                + str(n))

        s_time = time.time()
        m = invert_tensor(cm, dm)
        if output_level > 2:
            print("\t\t\t[INFO_TIME] Elapsed time for matrix inversion: " +
                  str(time.time() - s_time) + ' seconds.')
        z_jackknife[n] = m

    _, z_error[0] = jackknife_statistics(
        [z_jackknife[i][0] for i in range(len(z_jackknife))])
    _, z_error[1] = jackknife_statistics(
        [z_jackknife[i][1] for i in range(len(z_jackknife))])

    return z_regression, z_error
Пример #8
0
from scipy.stats import rayleigh
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)

# Calculate a few first moments:

mean, var, skew, kurt = rayleigh.stats(moments='mvsk')

# Display the probability density function (``pdf``):

x = np.linspace(rayleigh.ppf(0.01), rayleigh.ppf(0.99), 100)
ax.plot(x, rayleigh.pdf(x), 'r-', lw=5, alpha=0.6, label='rayleigh 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 = rayleigh()
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')

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

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

# Generate random numbers:

r = rayleigh.rvs(size=1000)
Пример #9
0
# python3
import math
import random
import numpy as np
from scipy.stats import rayleigh

from matplotlib import pyplot as plt


def inv_rayleigh_cdf(u):
    return math.sqrt(-2 * math.log(1 - u))

random.seed(1001)

# random samples from Unif(0, 1)
random_numbers = [random.random() for _ in range(10000)]

# random samples of Rayleigh Dist. through the inverse transform
random_numbers_from_rayleigh = [inv_rayleigh_cdf(random_number)
                                for random_number in random_numbers]

x = np.linspace(rayleigh.ppf(0), rayleigh.ppf(0.999), 100)

plt.hist(random_numbers_from_rayleigh, 60, facecolor='green', normed=True,
         alpha=0.6, label='random numbers')
plt.plot(x, rayleigh.pdf(x), lw=2, alpha=0.7, label='Rayleigh pdf')
plt.legend(loc='best')

Пример #10
0
    nb = nw * nt * nq
    sharedsize = 0  #byte
    x = np.zeros(nb)
    x = x.astype(np.float32)
    dev_x = cuda.mem_alloc(x.nbytes)
    cuda.memcpy_htod(dev_x, x)

    source_module = gabcrm_module()
    pkernel = source_module.get_function("rayleighgen")
    pkernel(dev_x,
            np.float32(alpha),
            np.float32(beta),
            block=(int(nw), 1, 1),
            grid=(int(nt), int(nq)),
            shared=sharedsize)
    cuda.memcpy_dtoh(x, dev_x)

    plt.hist(x, bins=100, density=True)
    #    plt.hist(np.log10(x[x>0]),bins=100,density=True)
    #plt.yscale("log")
    #    plt.xscale("log")

    xl = np.linspace(rayleighfunc.ppf(0.001), rayleighfunc.ppf(0.999), 1000)
    plt.plot(xl, rayleighfunc.pdf(xl))
    #    plt.axvline(np.log10(np.mean(x)),color="red")
    plt.axvline(np.mean(x), color="red")
    #    plt.yscale("log")
    print("mean=", np.mean(x))
    plt.title("Rayleigh Distribution")
    plt.show()