Esempio n. 1
0
def run_single():

    start_time = time.time()

    # initialise the model with some parameters
    params = parameters.get_default_parameters().copy()
    model = TransactionModel(params)

    # run the simulation until termination
    while not model.terminated:
        model.step()

    # get the collected data
    agent_vars = model.log_collector.get_agent_vars_dataframe()
    agent_vars.index = agent_vars.index.droplevel(1)

    print('customers left:', len(model.customers))
    print('fraudsters left:', len(model.fraudsters))
    print('simulation took ', round((time.time() - start_time)/60., 2), ' minutes')

    # make sure we didn't accidentally changed the default parameters
    result_handling.check_parameter_consistency(params)

    # save the results
    result_handling.save_results(model)
Esempio n. 2
0
    def __init__(self,
                 model_parameters,
                 authenticator=NeverSecondAuthenticator(),
                 scheduler=None):
        super().__init__(seed=123)

        # load parameters
        if model_parameters is None:
            model_parameters = parameters.get_default_parameters()
        self.parameters = model_parameters

        # calculate the intrinsic transaction motivation per customer (proportional to number of customers/fraudsters)
        # we keep this separate because then we can play around with the number of customers/fraudsters,
        # but individual behaviour doesn't change
        self.parameters['transaction_motivation'] = np.array([
            1. / self.parameters['num_customers'],
            1. / self.parameters['num_fraudsters']
        ])

        # save authenticator for checking transactions
        self.authenticator = authenticator

        # random internal state
        self.random_state = np.random.RandomState(self.parameters["seed"])

        # current date
        self.curr_global_date = self.parameters['start_date']

        # set termination status
        self.terminated = False

        # create merchants, customers and fraudsters
        self.next_customer_id = 0
        self.next_fraudster_id = 0
        self.next_card_id = 0
        self.merchants = self.initialise_merchants()
        self.customers = self.initialise_customers()
        self.fraudsters = self.initialise_fraudsters()

        # set up a scheduler
        self.schedule = scheduler if scheduler is not None else RandomActivation(
            self)

        # we add to the log collector whether transaction was successful
        self.log_collector = self.initialise_log_collector()
Esempio n. 3
0
    def __init__(self, seed=123, stay_prob_genuine=0.9, stay_prob_fraud=0.99,
                 end_date=datetime(2999, 12, 31), params=None, random_schedule=False):
        """
        Creates an object that can be used to run the simulator online / interactively. This means
        that we can have it generate a bit of data, do something with the data, generate a bit more
        data, do something again, etc. (as opposed to, generating one large batch of data, storing it
        in a file, and then using it in a different program).

        :param end_date:
            Final possible date in the simulation. By default set to 31st December 2999, which allows for
            a sufficiently long simulation run. If set to anything other than None, will override the
            end_date as specified in params
        :param params:
            Parameters passed on to the UniMausTransactionModel. Will use the default parameters if None
        :param random_schedule:
            False by default. If set to True, we use a RandomActivation schedule to shuffle the order in
            which agents are updated every step.
        """
        if params is None:
            params = parameters.get_default_parameters()

        if end_date is not None:
            params['end_date'] = end_date

        if stay_prob_genuine is not None:
            params['stay_prob'][0] = stay_prob_genuine

        if stay_prob_fraud is not None:
            params['stay_prob'][1] = stay_prob_fraud

        if seed is not None:
            params['seed'] = seed

        if random_schedule:
            self.model = TransactionModel(params)
        else:
            self.model = TransactionModel(params, scheduler=BaseScheduler(None))

        self.params = params

        self.aggregate_feature_constructor = None
        self.apate_graph_feature_constructor = None
Esempio n. 4
0
def check_parameter_consistency(params1):

    params2 = parameters.get_default_parameters()

    # make sure we didn't accidentally change the input parameters
    for key in params1.keys():
        try:
            if isinstance(params1[key], np.ndarray):
                assert np.sum(params1[key] - params2[key]) == 0
            elif isinstance(params1[key], float) or isinstance(
                    params1[key], int):
                assert params1[key] - params2[key] == 0
            elif isinstance(params1[key], datetime.date):
                pass
            elif isinstance(params1[key], pd.DataFrame):
                assert np.sum(params1[key].values - params2[key].values) == 0
            elif isinstance(params1[key], list):
                for i in range(len(params1[key])):
                    assert np.sum(params1[key][i].values -
                                  params2[key][i].values) == 0
            else:
                print("unknown type", key, type(params1[key]))
        except AssertionError:
            print("!! params changed:", key)
Esempio n. 5
0
def run_single():

    # get the parameters for the simulation
    params = parameters.get_default_parameters()
    params['init_satisfaction'] = 0.9

    # increase the probability of making another transaction
    new_stay_prob = [0.8, 0.5]
    print('changing stay prob from', params['stay_prob'], 'to', new_stay_prob)
    params['stay_prob'] = new_stay_prob

    plt.figure(figsize=(10, 5))
    for a in [
            'random', 'oracle', 'never_second', 'heuristic', 'always_second'
    ]:

        # the authenticator
        authenticator = get_authenticator(a)

        # initialise transaction model
        model = TransactionModel(params, authenticator)

        # run the simulation until termination
        while not model.terminated:
            model.step()

        # get the collected data
        agent_vars = model.log_collector.get_agent_vars_dataframe()
        agent_vars.index = agent_vars.index.droplevel(1)

        model_vars = model.log_collector.get_model_vars_dataframe()

        # save the results
        result_handling.save_results(model)

        reward_fraud = rewards.money_lost_per_timestep(agent_vars)
        reward_genuine = rewards.money_made_per_timestep(agent_vars)
        monetary_rewards = rewards.monetary_reward_per_timestep(agent_vars)
        true_satisfactions = rewards.satisfaction_per_timestep(model_vars)

        # plt.subplot(1, 4, 1)
        plt.ylabel('revenue (total)')
        plt.plot(range(len(monetary_rewards)),
                 np.cumsum(monetary_rewards),
                 label=a)
        plt.legend()

        # plt.subplot(1, 4, 2)
        # plt.ylabel('cumulative satisfaction')
        # plt.plot(range(len(true_satisfactions)), np.cumsum(true_satisfactions), label=a)
        #
        # plt.subplot(1, 4, 3)
        # plt.ylabel('revenue (money lost by fraud)')
        # plt.plot(range(len(true_satisfactions)), np.cumsum(true_satisfactions), label=a)
        #
        # plt.subplot(1, 4, 4)
        # plt.ylabel('revenue (money gained by genuine transactions)')
        # plt.plot(range(len(true_satisfactions)), np.cumsum(true_satisfactions), label=a)

    plt.tight_layout()
    plt.show()
Esempio n. 6
0
        authenticator, auth_name = auths[k]
    else:  # if we just did Q-Learning, run it again with the pre-trained one
        auth_name = 'Q-Learning (pre-trained)'

    seed = 666

    print("-----")
    print(auth_name)
    print("-----")

    sum_monetary_rewards = None

    for i in range(1):

        # the parameters for the simulation
        params = parameters.get_default_parameters()
        params['seed'] = seed
        params['init_satisfaction'] = 0.9
        params['stay_prob'] = [0.9, 0.6]
        params['num_customers'] = 100
        params['num_fraudsters'] = 10
        params['end_date'] = datetime(
            2016, 12, 31).replace(tzinfo=timezone('US/Pacific'))

        path = 'results/{}_{}_{}_{}_{}_{}'.format(
            auth_name, seed, int(params['init_satisfaction'] * 10),
            params['num_customers'], params['num_fraudsters'],
            params['end_date'].year)

        if os.path.exists(path + '.npy'):
            monetary_rewards = np.load(path + '.npy')