commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Artem Barger (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (RNG-5) Create API usage demo example application.
Date Wed, 10 Aug 2016 21:52:20 GMT

    [ https://issues.apache.org/jira/browse/RNG-5?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15416084#comment-15416084
] 

Artem Barger commented on RNG-5:
--------------------------------

This is how integral computation looks like: 

{code:java}
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.rng;

/**
 * A Demo Application to show how one can use PRNG to numerically approximate
 * integral value, computation based on Monte-Carlo method.
 *
 * @see <a href="https://en.wikipedia.org/wiki/Monte_Carlo_method">Wikipedia</a>
 * <p>
 * The integral we would like to compute in our demo is sin(cos(x))dx on interval [0..1].
 */
public class NumericIntegralDemo {

    /**
     * Desired approximation error bound
     */
    private double error;

    /**
     * Random source provider.
     */
    private UniformRandomProvider provider;

    /**
     * Number of generated points
     */
    private int generatedNums;

    /**
     * Actual approximation error received in computation.
     */
    private double actualError;


    public NumericIntegralDemo(final double error, final RandomSource source) {
        this.error = error;
        this.provider = RandomSource.create(source);
    }

    public double value() {
        double newValue = Double.MAX_VALUE;
        double oldValue = Double.MIN_VALUE;

        double result = 0;
        while ((actualError = Math.abs(newValue - oldValue)) > error) {
            result += Math.sin(Math.cos(provider.nextDouble()));
            generatedNums ++;
            oldValue = newValue;
            newValue = result / generatedNums;
        }

        return newValue;
    }

    /**
     * Returns the amount of numbers required to generate to converge with desired error value.
     *
     * @return amount of numbers generated
     */
    public int generatedNums() {
        return generatedNums;
    }

    /**
     * Returns the absolute error of approximated PI value.
     *
     * @return the actual approximation error
     */
    public double getError() {
        return actualError;
    }

    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Invalid input parameters, expected input format is: ");
            System.out.println("NumericIntegralDemo <error> <randomSource>");

            System.out.println("<error> - convergence error rate, stop computation when
" +
                    "absolute error between updated PI value and value from " +
                    "previous cycle is less than error value");

            System.out.println("<randomSource> - random source");

            final StringBuilder allowedSourcesNames = new StringBuilder();
            for (RandomSource source : RandomSource.values()) {
                allowedSourcesNames.append(", ").append(source.name());
            }

            System.out.println("\t\t <randomSource> parameter value could be one of:
"
                    + allowedSourcesNames.toString().replaceFirst(", ", ""));

            throw new IllegalStateException("Invalid number of input parameters.");
        }

        final Double error = Double.valueOf(args[0]);

        final RandomSource randomSource = RandomSource.valueOf(args[1]);

        final NumericIntegralDemo integralApprox = new NumericIntegralDemo(error, randomSource);
        System.out.println("Computed PI value = " + integralApprox.value()
                + ", required (" + integralApprox.generatedNums()
                + ") numbers to get error (" + integralApprox.getError() + ")");
    }

}

{code}

Which makes me think to generalize both "pi" and this demo, by adding an abstract class for
any Monte-Carlo based computation, since they all pretty the same, the only thing which is
different is the actual computation and it could be extracted into abstract method. 

> Create API usage demo example application.
> ------------------------------------------
>
>                 Key: RNG-5
>                 URL: https://issues.apache.org/jira/browse/RNG-5
>             Project: Commons RNG
>          Issue Type: Task
>            Reporter: Artem Barger
>         Attachments: ComputePiDemo.java
>
>
> Need to create a demo application for user guide to demonstrate how to use the API and
show possible application.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message