/* Theta for the generalized Black and Scholes formula */ double theta(int fCall, double S, double X, double T, double r, double b, double v) { double st, d1, d2, sbrt, result; assert_valid_price(S); assert_valid_strike(X); assert_valid_time(T); assert_valid_interest_rate(r); assert_valid_cost_of_carry(b); assert_valid_volatility(v); st = sqrt(T); d1 = (log(S / X) + (b + pow2(v) / 2.0) * T) / (v * st); d2 = d1 - v * st; sbrt= S * exp((b - r) * T); if(fCall) { result = -sbrt * normdist(d1) * v / (2.0 * st) - (b - r) * sbrt * cnd(d1) - r * X * exp(-r * T) * cnd(d2); } else { result = -sbrt * normdist(d1) * v / (2.0 * st) + (b - r) * sbrt * cnd(-d1) + r * X * exp(-r * T) * cnd(-d2); } return result; }
Vector getRandVec() { //See http://mathworld.wolfram.com/SpherePointPicking.html std::normal_distribution<> normdist(0.0, (1.0 / sqrt(double(NDIM)))); Vector tmpVec; for (size_t iDim = 0; iDim < NDIM; iDim++) tmpVec[iDim] = normdist(_rng); return tmpVec; }
void gauss(T * arr, unsigned int n, T mean, T sigma) { //initialize random number generator std::random_device rd; std::mt19937 gen(rd()); // values near the mean are the most likely // standard deviation affects the dispersion of generated values from the mean std::normal_distribution<T> normdist(mean,sigma); for (unsigned int i=0; i < n; i++) arr[i] = normdist(gen); }
int main() { // std::default_random_engine generator; std::mt19937 generator; std::uniform_int_distribution<int> distribution(1,6); std::normal_distribution<double> normdist(0.0, 1.0); std::uniform_real_distribution<double> unifdist(0.0, 1.0); // Bind the generator to the first argument of distribution so that // we can call with rng() instead of dist(gen). auto rng = std::bind( unifdist, generator ); for( int i=0; i<25; ++i ) { std::cout << rng() << " "; } std::cout << std::endl; // Now create a random matrix std::valarray<double> v(100); for (int i=0; i < v.size(); i++ ) { v[i] = rng(); } return 0; }
int main(int argc, char** argv) { double T(1); double L(1.44e-6); unsigned N(100); double R(2.5e-9); double D(1e-12); if (argc == 6) { T = std::stod(argv[1]); L = std::stod(argv[2]); N = std::stoi(argv[3]); R = std::stod(argv[4]); D = std::stod(argv[5]); } const double dt(2*R*R/3/D); const unsigned nSim(T/dt); std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> unidist(0, L); std::normal_distribution<> normdist(0, pow(2*D*dt,0.5)); std::vector<Coordinate> mols; for (unsigned i(0); i < N; ++i) { Coordinate mol(unidist(gen), unidist(gen), unidist(gen)); mols.push_back(mol); } auto start = std::chrono::high_resolution_clock::now(); for (unsigned n(0); n < nSim; ++n) { for (unsigned j(0); j < N; ++j) { mols[j].x = mod(mols[j].x+normdist(gen), L); mols[j].y = mod(mols[j].y+normdist(gen), L); mols[j].z = mod(mols[j].z+normdist(gen), L); } } auto finish = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> elapsed = finish - start; std::cout << "elapsed:" << elapsed.count() << " s" << std::endl; }
// some random fitness function float fitness_function(const Optimization::ParticleSwarm::feature_vector& input) { double h = (log(S/(K*pv))/(input[0]*sqrt(T)))+(0.5)*input[0]*sqrt(T); double price = S*normdist(h)-K*pv*normdist(h-input[0]*sqrt(T)); return pow(price - P, 2); }