예제 #1
0
def computeRmseN(numSamples):
    from numpy import zeros, arange
    from mc_utilities import computeRmse, calcFncValues, integrateFncValues

#    print("In computeRmseN")
    fncDim = 1  # Dimension of uni- or multi-variate integrand function
    mu = 0
    sigma = 1
    fncExpnt = 4
    numExperiments = 1000

    mcIntegral = zeros(numExperiments)

    analyticIntegral =  calcAnalyticIntegral(sigma,fncExpnt)   
    print "Analytic calculation of integral = %s" % analyticIntegral

    for idx in arange(numExperiments):

        normalPoints = drawNormalPoints(numSamples,mu,sigma)
#    print"NormalPoints = %s" % normalPoints
    
#        fncValuesArray = calcFncValues(numSamples,normalPoints,fncExpnt)    
        fncValuesArray = calcFncValues(numSamples,fncDim,normalPoints,fnc2integrate,fncExpnt)
#    print"Function values = %s" % fncValuesArray  

#        pdb.set_trace()
    
        mcIntegral[idx] = integrateFncValues(fncValuesArray,numSamples)
#        print "Monte Carlo estimate = %s" % mcIntegral[idx]    
        
    
    rmse = computeRmse(analyticIntegral,mcIntegral)
    print "RMSE of Monte Carlo estimate = %s" % rmse
    
    return rmse    
예제 #2
0
def computeFracRmseN(numSamples):
    """Return the fractional root-mean-square error 
    in a Monte-Carlo integration of Khairoutdinov-Kogan autoconversion.
    
    As we go, optionally produce plots."""    
    
    
    from numpy import zeros, arange, copy, cov, corrcoef, any, nan, \
                            clip, finfo, amax, multiply, mean, divide, power, \
                            floor, concatenate
    from mc_utilities import computeRmse, calcFncValues, integrateFncValues
    from math import isnan
    import matplotlib.pyplot as plt
    import sys

#    print("In computeRmseN")
    fncDim = 2  # Dimension of uni- or multi-variate integrand function
    muChi = 0
    sigmaChi = 1
    muNcn = 0
    sigmaNcn = 0.5
    rhoChiNcn = 0.5
    alpha = 2.47 #2.47
    beta = -1.79 #-1.79
    # Control variate parameters
    alphaDelta = -0.3  # Increment to alpha for control variates function, h
    betaDelta = -0.3  # Increment to beta for control variates function, h

    # Importance sampling parameters: Importance values - Basic MC values
    muChiDeltaImp = 1.8 * sigmaChi  # 1.4 * sigmaChi
    sigmaChiDeltaImp = -0.00 * sigmaChi
    muNcnDeltaImp = -1.0 * sigmaNcn
    sigmaNcnDeltaImp = -0.00 * sigmaNcn
    
    # Defensive sampling parameter
    alphaDefQ = 0.5  # Fraction of points drawn from q(x) rather than P(x)

    numExperiments = 100#1000
    
    createCVScatterplots = False

    mcIntegral = zeros(numExperiments)
    mcIntegralImp = zeros(numExperiments)
    mcIntegralCV = zeros(numExperiments)

    analyticIntegral =  calcAutoconversionIntegral( muChi,sigmaChi,
                                                    muNcn,sigmaNcn,
                                                    rhoChiNcn,
                                                    alpha,beta
                                                  )
    #print "Analytic calculation of true integral = %s" % analyticIntegral

    analyticIntegralCV =  calcAutoconversionIntegral( muChi,sigmaChi,
                                                    muNcn,sigmaNcn,
                                                    rhoChiNcn,
                                                    alpha+alphaDelta,beta+betaDelta
                                                  )
    #print "Analytic calculation of CV integral = %s" % analyticIntegralCV

    muChiQ = muChi + muChiDeltaImp
    sigmaChiQ = sigmaChi + sigmaChiDeltaImp
    muNcnQ = muNcn + muNcnDeltaImp
    sigmaNcnQ = sigmaNcn+sigmaNcnDeltaImp

#    pdb.set_trace()

    for idx in arange(numExperiments):

        #pdb.set_trace()

        samplePoints = drawNormalLognormalPoints( numSamples,
                                                  muChi,sigmaChi,
                                                  muNcn,sigmaNcn,
                                                  rhoChiNcn)

#        pdb.set_trace()    
    
        fncValuesArray = calcFncValues(numSamples,fncDim,samplePoints,
                                       autoconversionRate,alpha,beta)
        #    print"Function values = %s" % fncValuesArray

        mcIntegral[idx] = integrateFncValues(fncValuesArray,numSamples)
        #print "Monte Carlo estimate = %s" % mcIntegral[idx]

        #########################################
        #
        # Calculate integral using control variates
        #
        ############################################

        fncValuesArrayCV = calcFncValues(numSamples,fncDim,samplePoints,
                                       autoconversionRate,alpha+alphaDelta,beta+betaDelta)                                       

        if any(fncValuesArrayCV==nan):
            pdb.set_trace()

        #pdb.set_trace()
        # Compute optimal beta (pre-factor for control variate)
        covCV = cov(fncValuesArray,fncValuesArrayCV)
        #print "covCV = %s" % covCV

        # Optimal beta
        betaOpt = covCV[0,1]/amax([ covCV[1,1] , finfo(float).eps ])
        #betaOpt = clip(betaOpt, 0.0, 1.0)
        #print "betaOpt = %s" % betaOpt

        corrCV = corrcoef(fncValuesArray,fncValuesArrayCV)

        # pdb.set_trace()
           
        mcIntegralCV[idx] = integrateFncValues(fncValuesArray-betaOpt*fncValuesArrayCV,numSamples) \
                            + betaOpt*analyticIntegralCV
        #print "CV Monte Carlo estimate = %s" % mcIntegralCV[idx] 
    
        #pdb.set_trace()    
        if isnan(mcIntegralCV[idx]):
            pdb.set_trace()


        #########################################
        #
        # Calculate integral using importance sampling (+ control variate)
        #
        ############################################

        # Number of samples drawn from q(x) ( and not P(x) ) in defensive importance sampling
        numSamplesDefQ = floor( alphaDefQ * numSamples ) 

        # Number of samples drawn from q(x) ( and not P(x) ) in defensive importance sampling
        numSamplesDefP = numSamples-numSamplesDefQ 
        
        # Draw numSamplesDefQ samples from q(x), without including defensive points from P(x)
        samplePointsQOnly = drawNormalLognormalPoints( numSamplesDefQ,
                                                  muChiQ,sigmaChiQ,muNcnQ,sigmaNcnQ,
                                                  rhoChiNcn)

        # Concatenate sample points drawn from q(x) and P(x) 
        # P(x) points come first
        samplePointsQ = concatenate( ( samplePoints[0:numSamplesDefP,:], samplePointsQOnly ),
                                    axis=0 )   # Add rows to the bottom of the 2-column array                                     

        #pdb.set_trace()
                                    
        # Assertion check:
        if ( samplePointsQ.shape != samplePoints.shape  ):
            print "ERROR: Defensive importance sampling generates the wrong number of sample points!!!!" 
            sys.exit(1)        

        fncValuesArrayQ = calcFncValues(numSamples,fncDim,samplePointsQ,
                                       autoconversionRate,alpha,beta)

        fncValuesArrayCVQ = calcFncValues(numSamples,fncDim,samplePointsQ,
                                       autoconversionRate,alpha+alphaDelta,beta+betaDelta)                                       

        weightsArrayImp = calcWeightsArrayImp(samplePointsQ,alphaDefQ,
                        muChi,sigmaChi,muNcn,sigmaNcn,rhoChiNcn,
                        muChiQ,sigmaChiQ,muNcnQ,sigmaNcnQ)

        # Effective sample size
        neOnN = divide( (mean(weightsArrayImp))**2,
                        mean(power(weightsArrayImp,2)) 
                      )
        #print "Effective sample size = neOnN = %s" % neOnN
 
        betaCVQ = 0.0
        #betaCVQ = betaOpt # betaOpt is the optimal beta for non-importance sampling

        integrandArrayImp = multiply( fncValuesArrayQ-betaCVQ*fncValuesArrayCVQ, 
                                     weightsArrayImp  )

        mcIntegralImp[idx] = integrateFncValues(integrandArrayImp,numSamples) \
                            + betaCVQ*analyticIntegralCV

        #pdb.set_trace()
   
    fracRmse = computeRmse(analyticIntegral,mcIntegral)/analyticIntegral
    print "Fractional RMSE of Monte Carlo estimate = %s" % fracRmse

#    pdb.set_trace()
    
    fracRmseImp = computeRmse(analyticIntegral,mcIntegralImp)/analyticIntegral
    print "Fractional RMSE of Monte Carlo estimate = %s" % fracRmse    
    
    fracRmseCV = computeRmse(analyticIntegral,mcIntegralCV)/analyticIntegral
    print "Fractional RMSE of CV Monte Carlo estimate = %s" % fracRmseCV    

#    if isnan(fracRmseCV):
#    pdb.set_trace
    if ( createCVScatterplots == True ):
        plt.scatter(fncValuesArray,fncValuesArrayCV)
        plt.plot([min(fncValuesArray), max(fncValuesArray)], 
                 [min(fncValuesArray), max(fncValuesArray)])
        plt.grid()
        plt.xlabel('Original function values')
        plt.ylabel('Control variate function values')
        plt.show()

#    pdb.set_trace()
    
    return (fracRmse, fracRmseImp, fracRmseCV, corrCV[0,1])