Пример #1
0
def main(argv):
    if len(argv) > 1:
        raise app.UsageError('Too many command-line arguments.')
    np.random.seed(100)
    group_0_prob = 0.5
    maximize_reward_result = lending.Experiment(
        group_0_prob=group_0_prob,
        interest_rate=1.0,
        bank_starting_cash=10000,
        seed=200,
        num_steps=FLAGS.num_steps,
        burnin=200,
        cluster_shift_increment=0.01,
        include_cumulative_loans=True,
        return_json=False,
        threshold_policy=MAXIMIZE_REWARD).run()
    equality_of_opportunity_result = lending.Experiment(
        group_0_prob=group_0_prob,
        interest_rate=1.0,
        bank_starting_cash=10000,
        seed=200,
        num_steps=FLAGS.num_steps,
        burnin=200,
        cluster_shift_increment=0.01,
        include_cumulative_loans=True,
        return_json=False,
        threshold_policy=EQUALIZE_OPPORTUNITY).run()

    lending_plots.do_plotting(maximize_reward_result,
                  equality_of_opportunity_result,
                  equality_of_opportunity_result,
                  # static_equality_of_opportunity_result,
                  FLAGS.plots_directory,
                  options=None)
Пример #2
0
 def test_short_run_recall_is_perfect(self):
     # Run for fewer steps than the burnin - this should give 100% recall
     # since during the burnin period, all loans are accepted.
     result = lending.Experiment(num_steps=10).run()
     result = json.loads(result)
     self.assertEqual(result['metric_results']['recall'], {
         '0': 1.0,
         '1': 1.0
     })
Пример #3
0
  def test_plotting(self):
    plotting_dir = self.create_tempdir().full_path

    self.assert_contains_no_pdfs(plotting_dir)

    # Run a single experiment to create the results.
    result = lending.Experiment(
        num_steps=100, include_cumulative_loans=True, return_json=False).run()
    # Tests that all_plots can execute.
    lending_plots.do_plotting(result, result, result, plotting_dir)
    # There should be at least one .pdf file created.
    self.assert_contains_pdfs(plotting_dir)
Пример #4
0
 def test_experiment_runs_with_default_parameters(self):
     # Tests that the experiment can run.
     result = lending.Experiment(num_steps=100).run()
     # Tests that the result is a valid json string.
     result = json.loads(result)
Пример #5
0
def main(argv):
    if len(argv) > 1:
        raise app.UsageError('Too many command-line arguments.')

    np.random.seed(100)
    group_0_prob = 0.5
    result = lending.Experiment(
        group_0_prob=group_0_prob,
        interest_rate=1.0,
        bank_starting_cash=10000,
        seed=200,
        num_steps=FLAGS.num_steps,
        burnin=200,
        cluster_shift_increment=0.01,
        include_cumulative_loans=True,
        return_json=False,
        threshold_policy=(EQUALIZE_OPPORTUNITY if FLAGS.equalize_opportunity
                          else MAXIMIZE_REWARD)).run()

    title = ('Eq. opportunity' if FLAGS.equalize_opportunity else 'Max reward')
    metrics = result['metric_results']

    # Standalone figure of initial credit distribution
    fig = plt.figure(figsize=(4, 4))
    lending.plot_credit_distribution(
        metrics['initial_credit_distribution'],
        'Initial',
        path=os.path.join(FLAGS.plots_directory,
                          'initial_credit_distribution.png')
        if FLAGS.plots_directory else None,
        include_median=True,
        figure=fig)

    # Initial and final credit distributions next to each other.
    fig = plt.figure(figsize=(8, 4))
    plt.subplot(1, 2, 1)
    lending.plot_credit_distribution(metrics['initial_credit_distribution'],
                                     'Initial',
                                     path=None,
                                     include_median=True,
                                     figure=fig)
    plt.subplot(1, 2, 2)

    lending.plot_credit_distribution(
        metrics['final_credit_distributions'],
        'Final - %s' % title,
        path=os.path.join(FLAGS.plots_directory,
                          'final_credit_distribution.png')
        if FLAGS.plots_directory else None,
        include_median=True,
        figure=fig)

    fig = plt.figure()
    lending.plot_bars(metrics['recall'],
                      title='Recall - %s' % title,
                      path=os.path.join(FLAGS.plots_directory, 'recall.png')
                      if FLAGS.plots_directory else None,
                      figure=fig)

    fig = plt.figure()
    lending.plot_bars(metrics['precision'],
                      title='Precision - %s' % title,
                      ylabel='Precision',
                      path=os.path.join(FLAGS.plots_directory, 'precision.png')
                      if FLAGS.plots_directory else None,
                      figure=fig)

    fig = plt.figure()
    lending.plot_cumulative_loans(
        {'demo - %s' % title: metrics['cumulative_loans']},
        path=os.path.join(FLAGS.plots_directory, 'cumulative_loans.png')
        if FLAGS.plots_directory else None,
        figure=fig)

    print('Profit %s %f' % (title, result['metric_results']['profit rate']))
    plt.show()

    if FLAGS.outfile:
        with open(FLAGS.outfile, 'w') as f:
            f.write(result)