Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > MapReduce for Monte-Carlo Simulations > CreditRiskExample.java
CreditRiskExample.java
Added by Nikita Ivanov, last edited by Nikita Ivanov on Jul 22, 2007  (view change)
Labels: 
(None)


This is main class in this example. It basically starts grid locally (local node) and executes credit risk calculation. If there are grid nodes available this calculation will be gridified to other nodes.

Full Source Code

CreditRiskExample.java
package org.gridgain.examples.montecarlo;

import java.util.*;
import org.apache.log4j.*;
import org.gridgain.grid.*;
import org.gridgain.grid.gridify.aop.spring.*;
import org.gridgain.grid.gridify.*;

public final class CreditRiskExample {
    /**
     * Ensure singleton.
     */
    private CreditRiskExample() {
        // No-op.
    }

    /**
     * Method creates portfolio and calculates credit risks.
     *
     * @param args Command line arguments, none required.
     * @throws Exception If example execution failed.
     */
    public static void main(String[] args) throws Exception {
        // Start GridGain instance with default configuration.
        GridFactory.start();

        try {
            // Create portfolio.
            Credit[] portfolio = new Credit[5000];

            Random rnd = new Random();

            // Generate some test portfolio items.
            for (int i = 0; i < portfolio.length; i++) {
                portfolio[i] = new Credit(
                    50000 * rnd.nextDouble(), // Credit amount.
                    rnd.nextInt(1000), // Credit term in days.
                    rnd.nextDouble() / 10, // APR.
                    rnd.nextDouble() / 20 + 0.02 // EDF.
                );
            }

            // Forecast horizon in days.
            final int horizon = 365;

            // Percentile.
            double percentile = 0.95;

            // Number of Monte-Carlo iterations.
            int iter = 10000;

            // Instance of credit risk manager.
            CreditRiskManager riskMgr = new CreditRiskManager();

            // Mark the stopwatch.
            long start = System.currentTimeMillis();

            // Calculate credit risk and print it out.
            // As you can see the grid enabling is completely hidden from the caller
            // and it is fully transparent to him. In fact, the caller is never directly
            // aware if method was executed just locally on the 100s of grid nodes.
            // Credit risk crdRisk is the minimal amount that creditor has to have
            // available to cover possible defaults.
            double crdRisk = riskMgr.calculateCreditRiskMonteCarlo(portfolio, horizon, iter, percentile);

            System.out.println("Credit risk [crdRisk=" + crdRisk + ", duration=" +
                (System.currentTimeMillis() - start) + "ms.]");
        }
        // We specifically don't do any error handling here to
        // simplify the example. Real application may want to
        // add error handling and application specific recovery.
        finally {
            // Stop GridGain instance.
            GridFactory.stop(true);
        }
    }
}

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators