Package com.cedarsoft.business.calc.roi

Source Code of com.cedarsoft.business.calc.roi.PAngV2000Calculator

package com.cedarsoft.business.calc.roi;

import com.cedarsoft.business.Money;
import com.cedarsoft.business.MutableMoney;
import com.cedarsoft.business.calc.TimeSystem;
import com.cedarsoft.business.payment.DefaultPayment;
import com.cedarsoft.business.payment.Payment;
import org.jetbrains.annotations.NotNull;
import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* Calculates the PAngV 2000.
*/
public class PAngV2000Calculator {
  @NotNull
  public static final Money TARGET_DELTA = new Money( 0.005 );
  public static final double INITIAL_RATE_GUESS = 0.5;
  public static final double MIN_CHANGE = 0.000004;

  /**
   * The payments
   */
  private final List<Payment> payments = new ArrayList<Payment>();

  /**
   * The base date for the calculation
   */
  @NotNull
  private final LocalDate baseDate;

  /**
   * Creates a new pang calculator for the given base date
   *
   * @param baseDate the base date
   */
  public PAngV2000Calculator( @NotNull LocalDate baseDate ) {
    this.baseDate = baseDate;
  }

  /**
   * Adds a (default) payment.
   *
   * @param amount the amount
   * @param date   the date
   */
  public void addPayment( @NotNull Money amount, @NotNull LocalDate date ) {
    addPayment( new DefaultPayment( amount, date ) );
  }

  /**
   * Adds a payment
   *
   * @param payment the payment
   */
  public void addPayment( @NotNull Payment payment ) {
    payments.add( payment );
  }

  /**
   * Adds the given payments
   *
   * @param additionalPayments the payments
   */
  public void addPayments( @NotNull Collection<? extends Payment> additionalPayments ) {
    this.payments.addAll( additionalPayments );
  }

  /**
   * Returns the amount of the payments
   *
   * @return the amount of payments
   */
  @NotNull
  public Money getPaymentsAmount() {
    MutableMoney amount = new MutableMoney();
    for ( Payment payment : payments ) {
      amount.plus( payment.getAmount() );
    }
    return amount.getMoney();
  }

  /**
   * Returns the payments
   *
   * @return
   */
  @NotNull
  public List<? extends Payment> getPayments() {
    return Collections.unmodifiableList( payments );
  }

  /**
   * Calculates the PangV
   *
   * @return the PangV
   */
  @NotNull
  public PangV calculate() {
    if ( payments.size() < 2 ) {
      return PangV.NULL;
    }

    MutableMoney sum = new MutableMoney();
    for ( Payment payment : payments ) {
      sum.plus( payment.getAmount() );
    }

    boolean sumPositive = sum.getEstimatedValue() > 0;

    //test it using bisection

    double actualGuess;
    if ( sumPositive ) {
      actualGuess = -INITIAL_RATE_GUESS;
    } else {
      actualGuess = INITIAL_RATE_GUESS;
    }

    int counter = 0;

    Money actualValue = Money.BIG;
    while ( actualValue.abs().isGreaterThan( TARGET_DELTA ) ) {
      double change = INITIAL_RATE_GUESS / Math.pow( 2, counter );

      if ( change < MIN_CHANGE ) {
        return new PangV( actualGuess, actualValue, counter, sum.immutable() );
      }

      //modify guess
      if ( !sumPositive ^ actualValue.isGreaterThan( Money.ZERO ) ) {
        actualGuess += change;
      } else {
        actualGuess -= change;
      }

      actualValue = calculateCashValue( actualGuess );
      counter++;
    }

    return new PangV( actualGuess, actualValue, counter, sum.immutable() );
  }

  /**
   * Calculates the cash value for all payments
   *
   * @param rate the rate
   * @return the cash value for all payments
   */
  @NotNull
  public Money calculateCashValue( double rate ) {
    MutableMoney cashValue = new MutableMoney();
    for ( Payment payment : payments ) {
      cashValue.plus( calulateCashValue( baseDate, payment, rate ) );
    }
    return cashValue.immutable();
  }

  /**
   * Calculates the cash value of a payment for a given rate.
   * The base for this calculation is a year with 365 days
   *
   * @param payment
   * @param rate    the rate
   * @return the value
   */
  @NotNull
  public static Money calulateCashValue( @NotNull LocalDate base, @NotNull Payment payment, double rate ) {
    double years = TimeSystem.BANK_304_365.calculateYears( base, payment.getDate() );
    double divider = Math.pow( 1 + rate, years );
    return payment.getAmount().divide( divider );
  }
}
TOP

Related Classes of com.cedarsoft.business.calc.roi.PAngV2000Calculator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.