<s> from copperhead import * <EOL> import numpy as np <EOL> @ cu <EOL> def cnd ( d ) : <EOL> A1 = <NUM_LIT> <EOL> A2 = - <NUM_LIT> <EOL> A3 = <NUM_LIT> <EOL> A4 = - <NUM_LIT> <EOL> A5 = <NUM_LIT> <EOL> RSQRT2PI = <NUM_LIT> <EOL> K = <NUM_LIT:1.0> / ( <NUM_LIT:1.0> + <NUM_LIT> * abs ( d ) ) <EOL> cnd = RSQRT2PI * exp ( - <NUM_LIT:0.5> * d * d ) * ( K * ( A1 + K * ( A2 + K * ( A3 + K * ( A4 + K * A5 ) ) ) ) ) <EOL> if d > <NUM_LIT:0> : <EOL> return <NUM_LIT:1.0> - cnd <EOL> else : <EOL> return cnd <EOL> @ cu <EOL> def black_scholes ( S , X , T , R , V ) : <EOL> def black_scholes_el ( si , xi , ti ) : <EOL> sqrt_ti = sqrt ( ti ) <EOL> d1 = ( log ( si / xi ) + ( R + <NUM_LIT> * V * V ) * ti ) / ( V * sqrt_ti ) <EOL> d2 = d1 - V * sqrt_ti <EOL> cnd_d1 = cnd ( d1 ) <EOL> cnd_d2 = cnd ( d2 ) <EOL> exp_Rti = exp ( - R * ti ) <EOL> call_result = si * cnd_d1 - xi * exp_Rti * cnd_d2 ; <EOL> put_result = xi * exp_Rti * ( <NUM_LIT:1.0> - cnd_d2 ) - si * ( <NUM_LIT:1.0> - cnd_d1 ) <EOL> return call_result , put_result <EOL> return map ( black_scholes_el , S , X , T ) <EOL> def rand_floats ( n , min , max ) : <EOL> diff = np . float32 ( max ) - np . float32 ( min ) <EOL> rands = np . array ( np . random . random ( n ) , dtype = np . float32 ) <EOL> rands = rands * diff <EOL> rands = rands + np . float32 ( min ) <EOL> return cuarray ( rands ) <EOL> n = <NUM_LIT:100> <EOL> S = rand_floats ( n , <NUM_LIT:5> , <NUM_LIT:30> ) <EOL> X = rand_floats ( n , <NUM_LIT:1> , <NUM_LIT:100> ) <EOL> T = rand_floats ( n , <NUM_LIT> , <NUM_LIT:10> ) <EOL> R = np . float32 ( <NUM_LIT> ) <EOL> V = np . float32 ( <NUM_LIT> ) <EOL> r = black_scholes ( S , X , T , R , V ) </s>
