Ejemplo n.º 1
0
def valueMonteCarloGPU(ctx,queue,S_init,nPaths,Exp_Time, dtMonte,Strike,Int_Rate,Vol,PTYPE, nMonteLoops=1):
    
    nextStepPathKernel = ElementwiseKernel(ctx,"float *latestStep, float *ran, float Strike, float Int_Rate, float Exp_Time, float dt, float Vol","float rval = exp((Int_Rate - 0.5f * Vol*Vol)*dt + Vol * sqrt(dt) * ran[i]); latestStep[i] *= rval;","nextStepPathKernel")
    
    excersisePriceKernel = ElementwiseKernel(ctx,"float *latestStep, float Strike, float Int_Rate, float Exp_Time","float rval = (latestStep[i]-Strike); latestStep[i] = exp(-Int_Rate*Exp_Time)  * max(rval,0.0f);","excersisePriceKernel")
    
    
    sumKernel = ReductionKernel(ctx, numpy.float32, neutral="0", reduce_expr="a+b", map_expr="x[i]", arguments="__global float *x")
    
    maxWorkItems = 1*2**9
    multiplier = 1
    
    if(nPaths > maxWorkItems):
        multiplier = math.ceil(nPaths/maxWorkItems)
        nPaths = multiplier * maxWorkItems
    else:
        maxWorkItems = nPaths
    #print(maxWorkItems, multiplier, nPaths)
    nTimeStepsMonte = math.ceil(Exp_Time/dtMonte)
    #print(nTimeStepsMonte,nMonteLoops)
    #set up random number generator
    gen = RanluxGenerator(queue, maxWorkItems, luxury=4, seed=time.time())

#the arrays
    ran = cl.array.zeros(queue, maxWorkItems, numpy.float32)
    latestStep = cl.array.zeros_like(ran)
    
    means = numpy.zeros(nMonteLoops)
    theMean = 0
    
    #the loop
    for loop in range(nMonteLoops):
        
        theSum = 0
        
        for mult in range(multiplier):
            
            latestStep.fill(S_init)
            
            
            
            for t in range(nTimeStepsMonte):
                gen.fill_normal(ran)
                gen.synchronize(queue)
                nextStepPathKernel(latestStep, ran, Strike, Int_Rate, Exp_Time, dtMonte, Vol)
            
            
            excersisePriceKernel(latestStep, Strike, Int_Rate, Exp_Time)
            #print(latestStep)
            
            #add to array
            
            theSum += sumKernel(latestStep, queue).get()
        means[loop] = theSum / nPaths
    
    monteAverage = numpy.mean(means)
    monteStdDeviation = numpy.std(means)
    
    return monteAverage,dtMonte, monteStdDeviation
Ejemplo n.º 2
0
ran = cl.array.zeros(queue, nPaths, numpy.float32)
latestStep = cl.array.empty_like(ran)

averages = numpy.zeros(nLoops)
#averages = cl.array.zeros(queue, nLoops, numpy.float32)

tStartMonte = time.time()
theSum = 0

for loop in range(0,nLoops):
    
    latestStep.fill(S_init)
    
    for t in range(0,nTimeStepsMonte):
        gen.fill_normal(ran)
        gen.synchronize(queue)
        nextStepPathKernel(latestStep, ran, Strike, Int_Rate, Exp_Time, dtMonte, Vol)


    excersisePriceKernel(latestStep, Strike, Int_Rate, Exp_Time)

    #theSum = cl.array.sum(latestStep).get()

    latestStep_numpy = latestStep.get()
    theSum = numpy.sum(latestStep_numpy)

    #theSum = sumKernel(latestStep).get()

    if(theSum == 0):
        print("theSum was zero")
Ejemplo n.º 3
0
def valueMonteCarloGPU(ctx,
                       queue,
                       S_init,
                       nPaths,
                       Exp_Time,
                       dtMonte,
                       Strike,
                       Int_Rate,
                       Vol,
                       PTYPE,
                       nMonteLoops=1):

    nextStepPathKernel = ElementwiseKernel(
        ctx,
        "float *latestStep, float *ran, float Strike, float Int_Rate, float Exp_Time, float dt, float Vol",
        "float rval = exp((Int_Rate - 0.5f * Vol*Vol)*dt + Vol * sqrt(dt) * ran[i]); latestStep[i] *= rval;",
        "nextStepPathKernel")

    excersisePriceKernel = ElementwiseKernel(
        ctx, "float *latestStep, float Strike, float Int_Rate, float Exp_Time",
        "float rval = (latestStep[i]-Strike); latestStep[i] = exp(-Int_Rate*Exp_Time)  * max(rval,0.0f);",
        "excersisePriceKernel")

    sumKernel = ReductionKernel(ctx,
                                numpy.float32,
                                neutral="0",
                                reduce_expr="a+b",
                                map_expr="x[i]",
                                arguments="__global float *x")

    maxWorkItems = 1 * 2**9
    multiplier = 1

    if (nPaths > maxWorkItems):
        multiplier = math.ceil(nPaths / maxWorkItems)
        nPaths = multiplier * maxWorkItems
    else:
        maxWorkItems = nPaths
    #print(maxWorkItems, multiplier, nPaths)
    nTimeStepsMonte = math.ceil(Exp_Time / dtMonte)
    #print(nTimeStepsMonte,nMonteLoops)
    #set up random number generator
    gen = RanluxGenerator(queue, maxWorkItems, luxury=4, seed=time.time())

    #the arrays
    ran = cl.array.zeros(queue, maxWorkItems, numpy.float32)
    latestStep = cl.array.zeros_like(ran)

    means = numpy.zeros(nMonteLoops)
    theMean = 0

    #the loop
    for loop in range(nMonteLoops):

        theSum = 0

        for mult in range(multiplier):

            latestStep.fill(S_init)

            for t in range(nTimeStepsMonte):
                gen.fill_normal(ran)
                gen.synchronize(queue)
                nextStepPathKernel(latestStep, ran, Strike, Int_Rate, Exp_Time,
                                   dtMonte, Vol)

            excersisePriceKernel(latestStep, Strike, Int_Rate, Exp_Time)
            #print(latestStep)

            #add to array

            theSum += sumKernel(latestStep, queue).get()
        means[loop] = theSum / nPaths

    monteAverage = numpy.mean(means)
    monteStdDeviation = numpy.std(means)

    return monteAverage, dtMonte, monteStdDeviation
Ejemplo n.º 4
0
ran = cl.array.zeros(queue, nPaths, numpy.float32)
latestStep = cl.array.empty_like(ran)

averages = numpy.zeros(nLoops)
#averages = cl.array.zeros(queue, nLoops, numpy.float32)

tStartMonte = time.time()
theSum = 0

for loop in range(0, nLoops):

    latestStep.fill(S_init)

    for t in range(0, nTimeStepsMonte):
        gen.fill_normal(ran)
        gen.synchronize(queue)
        nextStepPathKernel(latestStep, ran, Strike, Int_Rate, Exp_Time,
                           dtMonte, Vol)

    excersisePriceKernel(latestStep, Strike, Int_Rate, Exp_Time)

    #theSum = cl.array.sum(latestStep).get()

    latestStep_numpy = latestStep.get()
    theSum = numpy.sum(latestStep_numpy)

    #theSum = sumKernel(latestStep).get()

    if (theSum == 0):
        print("theSum was zero")