//  ------------------------------------------------------
//  -------  Uebung 4.14      Ib03        ----------------
//  -------                               ----------------
//  -------  NTF (Part 2)                 ----------------
//  -------                               ----------------
//  -------  21.05.2005    M. Vogel       ----------------
//  ------------------------------------------------------

import java.util.*;

public class NTF2
{
	final static int M = 1000000 ;  // Anzahl Simulationen

	final static double[] mu   =  { 10.0 , 9.0 , 6.0 , 12.0 , 10.0 } ;  // Parameter
	final static double[] sigma=  {  1.0 , 0.7 , 0.6 ,  1.5 ,  0.8 } ;  
	final static double bo    =   3.0 ;  
	final static double ma    =   1.75;  
	final static double capp  =  10.0 ;  
	final static double capm  =   8.0 ;  
	static double muA=0.0;
	static double sigmaA=0.0;
		

	public static void main(String[] args){
   	double sum =0.0 ;	
   	double sum2=0.0 ;
   	double f;	
		
   	Random gauss = new Random();

//    Simulation zur Bestimmung von muA und sigmaA	
	  for (int n = 0; n<M; n++) {
	     f=0.0;	  
	     for (int j = 0; j<5; j++) f+= mu[j]+sigma[j]*gauss.nextGaussian( );
	     sum = sum + f ;
	     sum2= sum2+ f*f ;
	  }	

	  sum/=M;
	  for (int j = 0; j<5; j++) {
         muA    += mu[j];
		 sigmaA += sigma[j]*sigma[j] ;
	  }
	  sigmaA=Math.sqrt(sigmaA);
	  
	  System.out.println() ;
	  System.out.println(" mu A    (theoretical) = " + muA ) ;
	  System.out.println(" sigma A (theoretical) = " + sigmaA ) ;
	  System.out.println() ;
	  System.out.println(" mu A    (simulated)   = " + sum ) ;
	  System.out.println(" sigma A (simulated)   = " + Math.sqrt(sum2/M - sum*sum) ) ;

	  sum =0.0 ;	
	  sum2=0.0 ;
	  
//    Simulation zur Bestimmung des Bonus/Malus für das Projekt A	
	  for (int n = 0; n<M; n++) {
	     f=0.0;	  
	     for (int j = 0; j<5; j++) f+= mu[j]+sigma[j]*gauss.nextGaussian( );
	     f= bonusMalus( f );
	     sum = sum + f ;
	     sum2= sum2+ f*f ;
	  }	

	  sum/=M;
	  System.out.println() ; System.out.println() ;
	  System.out.println(" mean gain/loss (simulated) = " + sum ) ;
	  System.out.println(" risk           (simulated) = " + Math.sqrt(sum2/M - sum*sum) ) ;
			
	}
	
	public static double bonusMalus(double x) {
		double boMa=0.0;
		if( x > muA+sigmaA ) boMa = Math.min( capp , bo * (x- (muA+sigmaA)) );
		if( x < muA-sigmaA ) boMa =-Math.min( capm , ma * ((muA-sigmaA)-x)  );

		return boMa ;
	}
}

