예제 #1
0
  def scenario_builder(self):
    """Returns an agent and environment pair."""
    env_params = lending_params.DelayedImpactParams(
        applicant_distribution=lending_params.two_group_credit_clusters(
            cluster_probabilities=self.cluster_probabilities,
            group_likelihoods=[self.group_0_prob, 1 - self.group_0_prob]),
        bank_starting_cash=self.bank_starting_cash,
        interest_rate=self.interest_rate,
        cluster_shift_increment=self.cluster_shift_increment,
    )
    env = lending.DelayedImpactEnv(env_params)

    agent_params = classifier_agents.ScoringAgentParams(
        feature_keys=['applicant_features'],
        group_key='group',
        default_action_fn=(lambda: 1),
        burnin=self.burnin,
        convert_one_hot_to_integer=True,
        threshold_policy=self.threshold_policy,
        skip_retraining_fn=lambda action, observation: action == 0,
        cost_matrix=params.CostMatrix(
            fn=0, fp=-1, tp=env_params.interest_rate, tn=0))

    agent = oracle_lending_agent.OracleThresholdAgent(
        action_space=env.action_space,
        reward_fn=rewards.BinarizedScalarDeltaReward(
            'bank_cash', baseline=env.initial_params.bank_starting_cash),
        observation_space=env.observation_space,
        params=agent_params,
        env=env)
    agent.seed(100)
    return env, agent
  def test_measure_distribution_change_measurement(self):

    # The lower cluster has a 100% success rate and the upper cluster has a 0%
    # success rate. This causes applicants to move constantly between clusters.
    clusters = distributions.Mixture(
        components=[
            lending_params._credit_cluster_builder(
                group_membership=[1, 0],
                cluster_probs=[0.1, 0.9],
                success_probs=[1., 0.])(),
            lending_params._credit_cluster_builder(
                group_membership=[0, 1],
                cluster_probs=[0.8, 0.2],
                success_probs=[1., 0.])(),
        ],
        weights=(0.5, 0.5))

    env = lending.DelayedImpactEnv(
        lending_params.DelayedImpactParams(applicant_distribution=clusters))
    initial_distribution = lending_metrics.CreditDistribution(env, 0)
    final_distribution = lending_metrics.CreditDistribution(env, -1)

    # Giving a loan should change the distribution.
    env.step(np.asarray(1))
    # Take another step to move current state into history. This step does not
    # change the distribution because the loan is rejected.
    env.step(np.asarray(0))

    self.assertEqual({
        '0': [0.1, 0.9],
        '1': [0.8, 0.2]
    }, initial_distribution.measure(env))
    self.assertNotEqual({
        '0': [0.1, 0.9],
        '1': [0.8, 0.2]
    }, final_distribution.measure(env))