Example #1
0
def simulate(df, seed_increment, switch_frequency, time_var):
    np.random.seed(seed_increment)
    print(seed_increment)
    no_duplicates = df.set_index(['state', 'month_id',
                                  'person']).index.is_unique
    if not no_duplicates:
        raise ValueError('Some months, the same person was in a state twice')
    grouped = df.groupby('state')
    df = grouped.apply(lambda x: reassign(x, time_var))
    people = set(df['person'])
    person_effect = dict(zip(people, burr.rvs(6, 10**5, size=len(people))))
    df['person_effect'] = df['person'].map(person_effect)
    df['person_effect'] = normalize(df['person_effect'])
    return df[['person', 'person_effect']]
Example #2
0
    def check_trust_table(self, block, step):
        if (block.owner_public_key in self.trust_table.keys()):
            if (block.is_malicious):
                self.trust_table[block.owner_public_key] = self.trust_table[
                    block.owner_public_key] - 1
                if (self.trust_table[block.owner_public_key] == (limiar - 1)):
                    born_step = step + random.randint(0, 101)
                    time_to_validate = burr.rvs(19.207726887411624,
                                                1.0485858296333794,
                                                -0.1563851244724221,
                                                9.799614489119339)
                    valid_step = born_step + int(round(time_to_validate))
                    new_transac = Transaction(4, born_step)
                    new_transac.target_public_key = block.owner_public_key
                    new_transac.origin_public_key = self.public_key.n
                    data = {}
                    data[self.topology.id_exp_trans] = []
                    data[self.topology.id_exp_trans].append({
                        "transac_type":
                        new_transac.transaction_type,
                        "born_step":
                        born_step,
                        "valid_step":
                        valid_step,
                        "target_public_key":
                        new_transac.target_public_key,
                        "origin_public_key":
                        new_transac.origin_public_key
                    })

                    content = "{}".format(data)
                    signature = rsa.sign(content.encode('utf-8'),
                                         self.private_key, 'SHA-1')

                    self.topology.exp_transacts_json.append(data)
                    self.topology.exp_transacts[block.owner_public_key][0] += 1
                    self.topology.exp_transacts[
                        block.owner_public_key][1].append(new_transac)
                    self.topology.exp_transacts[
                        block.owner_public_key][3].append(str(signature))
                    self.topology.id_exp_trans += 1
            else:
                self.trust_table[block.owner_public_key] = self.trust_table[
                    block.owner_public_key] + 1
                if (self.trust_table[block.owner_public_key] >= 10):
                    self.trust_table[block.owner_public_key] = 10

            if (self.topology.bc_control_turn > len(self.topology.miners) - 1):
                self.topology.bc_control_turn = 0
ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()

#burr Continuous distributions¶
from scipy.stats import burr
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(1, 1)
#Calculate a few first moments:
c, d = 10.5, 4.3
mean, var, skew, kurt = burr.stats(c, d, moments='mvsk')
#Display the probability density function (pdf):
x = np.linspace(burr.ppf(0.01, c, d), burr.ppf(0.99, c, d), 100)
ax.plot(x, burr.pdf(x, c, d), 'r-', lw=5, alpha=0.6, label='burr 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 = burr(c, d)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
#Check accuracy of cdf and ppf:
vals = burr.ppf([0.001, 0.5, 0.999], c, d)
np.allclose([0.001, 0.5, 0.999], burr.cdf(vals, c, d))
True
#Generate random numbers:
r = burr.rvs(c, d, size=1000)
#And compare the histogram:
ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()
Example #4
0
    def do_miner_step(self, step):
        if (self.topology.miners[self.topology.bc_control_turn] == self):
            block = Block(key=self.public_key.n)

            if (self.is_malicious):
                prob = random.random() * 100
                if (block_malicious_rate > prob):
                    block.is_malicious = True
                    self.grades_flush.append(-1)
                    for node in self.topology.everything:
                        node.check_trust_table

                else:
                    self.grades_flush.append(1)
                    if (len(self.topology.final_exp_trans) > 0):
                        for k in self.topology.final_exp_trans.keys():
                            if self.topology.final_exp_trans[k][0] == False:
                                for miner in self.topology.miners:
                                    if (k == miner.public_key.n):
                                        self.topology.data[
                                            self.topology.iden] = [
                                                miner.grades_flush,
                                                miner.is_malicious
                                            ]
                                        self.topology.iden += 1
                                        self.topology.miners.remove(miner)
                                        self.topology.everything.remove(miner)
                                self.topology.final_exp_trans[k][
                                    3].signatures = self.topology.final_exp_trans[
                                        k][2]
                                self.topology.final_exp_trans[k][0] = True
                                block.transactions.append(
                                    self.topology.final_exp_trans[k][3])
                                self.topology.expss += 1

                    for key in self.topology.exp_transacts.keys():
                        if (self.topology.exp_transacts[key][2] == False
                                and (len(self.topology.exp_transacts[key][1]) >
                                     (judges // 2 + 1))):
                            self.topology.exp_transacts[key][2] = True
                            born_step = step + random.randint(0, 101)
                            time_to_validate = burr.rvs(
                                19.207726887411624, 1.0485858296333794,
                                -0.1563851244724221, 9.799614489119339)
                            valid_step = born_step + int(
                                round(time_to_validate))
                            new_transac = Transaction(5, born_step)
                            new_transac.target_public_key = key
                            new_transac.origin_public_key = self.topology.exp_transacts[
                                key][1][0].origin_public_key
                            new_transac.validation_step = valid_step
                            self.topology.final_exp_trans[key] = [
                                False, self.topology.exp_transacts[key][1],
                                self.topology.exp_transacts[key][3],
                                new_transac
                            ]

            else:
                self.grades_flush.append(1)

                if (len(self.topology.final_exp_trans) > 0):
                    for k in self.topology.final_exp_trans.keys():
                        if self.topology.final_exp_trans[k][0] == False:
                            for miner in self.topology.miners:
                                if (k == miner.public_key.n):
                                    self.topology.data[self.topology.iden] = [
                                        miner.grades_flush, miner.is_malicious
                                    ]
                                    self.topology.iden += 1
                                    self.topology.miners.remove(miner)
                                    self.topology.everything.remove(miner)
                            self.topology.final_exp_trans[k][
                                3].signatures = self.topology.final_exp_trans[
                                    k][2]
                            self.topology.final_exp_trans[k][0] = True
                            block.transactions.append(
                                self.topology.final_exp_trans[k][3])
                            self.topology.expss += 1

                for key in self.topology.exp_transacts.keys():
                    if (self.topology.exp_transacts[key][2] == False
                            and (len(self.topology.exp_transacts[key][1]) >
                                 (judges // 2 + 1))):
                        self.topology.exp_transacts[key][2] = True
                        born_step = step + random.randint(0, 101)
                        time_to_validate = burr.rvs(19.207726887411624,
                                                    1.0485858296333794,
                                                    -0.1563851244724221,
                                                    9.799614489119339)
                        valid_step = born_step + int(round(time_to_validate))
                        new_transac = Transaction(5, born_step)
                        new_transac.target_public_key = key
                        new_transac.origin_public_key = self.topology.exp_transacts[
                            key][1][0].origin_public_key
                        new_transac.validation_step = valid_step
                        self.topology.final_exp_trans[key] = [
                            False, self.topology.exp_transacts[key][1],
                            self.topology.exp_transacts[key][3], new_transac
                        ]

            block_data = {}
            array_of_transacs = []
            block_data[self.topology.id_block_control] = []

            if (len(block.transactions) > 0):
                for trans in block.transactions:
                    transac = {}
                    transac[self.topology.trans_in_blocks_id] = []
                    transac[self.topology.trans_in_blocks_id].append({
                        "transac_type":
                        trans.transaction_type,
                        "miner_step":
                        step,
                        "valid_step":
                        trans.validation_step,
                        "born_step":
                        trans.born_step,
                        "target_public_key":
                        trans.target_public_key,
                        "origin_public_key":
                        trans.origin_public_key,
                        "signatures":
                        trans.signatures
                    })
                    self.topology.trans_in_blocks_id += 1
                    array_of_transacs.append(transac)
            block_data[self.topology.id_block_control].append({
                "miner_step":
                step,
                "miner_pub_key":
                self.public_key.n,
                "transactions":
                array_of_transacs,
                "is_malicious":
                block.is_malicious
            })
            self.topology.id_block_control += 1
            self.topology.blocks_from_controlbc_json.append(block_data)

            if (self.topology.bc_turn > len(self.topology.miners) - 1):
                self.topology.bc_turn = 0
Example #5
0
"""

import numpy as np
from numpy import random
from scipy.stats import burr
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
c, d = 10.5, 4.3
mean, var, skew, kurt = burr.stats(c, d, moments='mvsk')
x = np.linspace(burr.ppf(0.01, c, d), burr.ppf(0.99, c, d), 100)
ax.plot(x, burr.pdf(x, c, d), 'r-', lw=5, alpha=0.6, label='burr pdf')
rv = burr(c, d)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
vals = burr.ppf([0.001, 0.5, 0.999], c, d)
np.allclose([0.001, 0.5, 0.999], burr.cdf(vals, c, d))
r = burr.rvs(c, d, size=1)
print(r)
ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)
ax.legend(loc='best', frameon=False)
plt.show()

fig, ax = plt.subplots(1, 1)
c, d = 5.1, 3.8
mean, var, skew, kurt = burr.stats(c, d, moments='mvsk')
x = np.linspace(burr.ppf(0.01, c, d), burr.ppf(0.99, c, d), 100)
ax.plot(x, burr.pdf(x, c, d), 'r-', lw=5, alpha=0.6, label='burr pdf')
rv = burr(c, d)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')
vals = burr.ppf([0.001, 0.5, 0.999], c, d)
np.allclose([0.001, 0.5, 0.999], burr.cdf(vals, c, d))
r = burr.rvs(c, d, size=5)