Skip to content

Instantly share code, notes, and snippets.

@uberscientist
Created July 18, 2017 23:36
Show Gist options
  • Save uberscientist/db5a138d26da29a82af9fb30ccbaaf91 to your computer and use it in GitHub Desktop.
Save uberscientist/db5a138d26da29a82af9fb30ccbaaf91 to your computer and use it in GitHub Desktop.
Attempt to calculate black-scholes on GPU
var gpu = new GPU();
gpu.addFunction(function CND(x) {
var k = 1 / (1 + .2316419 * x);
if(x < 0) {
x = x * -1
return 1 - ( 1 - Math.exp(-x * x / 2)/ Math.sqrt(2*Math.PI) * k * (.31938153 + k * (-.356563782 + k * (1.781477937 + k * (-1.821255978 + k * 1.330274429)))) );
} else {
return ( 1 - Math.exp(-x * x / 2)/ Math.sqrt(2*Math.PI) * k * (.31938153 + k * (-.356563782 + k * (1.781477937 + k * (-1.821255978 + k * 1.330274429)))) );
}
});
gpu.addFunction(function d1(S, X, T, r, v) {
return (Math.log(S / X) + (r + v * v / 2) * T) / (v * Math.sqrt(T));
});
gpu.addFunction(function d2(S, X, T, r, v) {
return (Math.log(S / X) + (r + v * v / 2) * T) / (v * Math.sqrt(T)) - v * Math.sqrt(T);
});
const BS = gpu.createKernel(function(S, X, T, r, v) {
return ( S * CND(d1(S, X, T, r, v))-X * Math.exp(-r * T) * CND(d2(S, X, T, r, v)) );
}).setDimensions([1]);
console.log(BS(1,2,3,4,5));
@uberscientist
Copy link
Author

This is an attempt to use this library to calculate this function: https://gist.github.com/santacruz123/3623310

I've run into this error:

An error occurred compiling the shaders: ERROR: 0:138: 'Math' : undeclared identifier
ERROR: 0:138: 'PI' : field selection requires structure or vector on left hand side
ERROR: 0:140: 'Math' : undeclared identifier
ERROR: 0:140: 'PI' : field selection requires structure or vector on left hand side

value @ gpu.min.js:16
gpu.min.js:16 Uncaught Error compiling fragment shader

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment