montecarlo

C# Mersenne Twister random integer generator implementation (SFMT) monte carlo simulation

只愿长相守 提交于 2019-12-03 05:19:15
问题 So far I've been using the C# Mersenne Twister found here to generate random numbers: http://www.centerspace.net/resources.php I just discovered SFMT which is supposed to be twice as fast here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/ Can anyone point me at a C# implementation of SFMT ? My requirements are to generate an integer between (and including) 0 and 2^20 (1048576). I need to do this trillions of times everyday for a simulation running on a 24 hour clock so I am prepared

Code for Monte Carlo simulation: generate samples of given size in R

北战南征 提交于 2019-12-03 05:04:04
I started by generating a sample of 500 uniformly-distributed random numbers between 0 and 1 using the code below: set.seed(1234) X<-runif(500, min=0, max=1) Now, I need to write a psuedocode that generates 10000 samples of N=500 for a MC simulation, compute the mean of my newly created X, and store the iteration number and mean value in a result object. I have never attempted this, and so far I have this: n.iter <-(10000*500) results <- matrix (0, n.iter, 4) Finally, once this is accomplished, I'm to run it, then obtain median, mean, and min/max of the accrued sample means and save them to a

Where can I learn more about “ant colony” optimizations?

半城伤御伤魂 提交于 2019-12-03 00:23:30
I've been reading things here and there for a while now about using an "ant colony" model as a heuristic approach to optimizing various types of algorithms. However, I have yet to find an article or book that discusses ant colony optimizations in an introductory manner, or even in a lot of detail. Can anyone point me at some resources where I can learn more about this idea? On the off chance that you know German (yes, sorry …), a friend and I have written an introduction with code about this subject which I myself find quite passable. The text and code uses the example of TSP to introduce the

C# Mersenne Twister random integer generator implementation (SFMT) monte carlo simulation

前提是你 提交于 2019-12-02 18:36:15
So far I've been using the C# Mersenne Twister found here to generate random numbers: http://www.centerspace.net/resources.php I just discovered SFMT which is supposed to be twice as fast here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/ Can anyone point me at a C# implementation of SFMT ? My requirements are to generate an integer between (and including) 0 and 2^20 (1048576). I need to do this trillions of times everyday for a simulation running on a 24 hour clock so I am prepared to spend days tweaking this to perfection. Currently I've tweaked the Center Space Mersenne Twister by

Stock Price Simulation R code - Slow - Monte Carlo

為{幸葍}努か 提交于 2019-12-02 03:37:02
问题 I need to perform a stock price simulation using R code. The problem is that the code is a little bit slow. Basically I need to simulate the stock price for each time step (daily) and store it in a matrix. An example assuming the stock process is Geometric Brownian Motion for(j in 1:100000){ for(i in 1:252){ S[i] <- S[i-1]*exp((r-v^2/2)*dt+v*sqrt(dt)*rnorm(1)) } U[j,] <- S } Any suggestion to improve and speed up the code? 回答1: Assuming S[0] = 1 , you can build U as a follows: Ncols <- 252

Parallelization for Monte Carlo pi approximation

大兔子大兔子 提交于 2019-12-02 03:01:09
I am writing a c script to parallelize pi approximation with OpenMp. I think my code works fine with a convincing output. I am running it with 4 threads now. What I am not sure is that if this code is vulnerable to race condition? and if it is, how do I coordinate the thread action in this code ? the code looks as follows: #include <stdlib.h> #include <stdio.h> #include <time.h> #include <math.h> #include <omp.h> double sample_interval(double a, double b) { double x = ((double) rand())/((double) RAND_MAX); return (b-a)*x + a; } int main (int argc, char **argv) { int N = atoi( argv[1] ); //

Monte Carlo integration using importance sampling given a proposal function

痞子三分冷 提交于 2019-12-01 14:11:37
Given a Laplace Distribution proposal: g(x) = 1/2*e^(-|x|) and sample size n = 1000 , I want to Conduct the Monte Carlo (MC) integration for estimating θ: via importance sampling. Eventually I want to calculate the mean and standard deviation of this MC estimate in R once I get there. Edit (arrived late after the answer below) This is what I have for my R code so far: library(VGAM) n = 1000 x = rexp(n,0.5) hx = mean(2*exp(-sqrt(x))*(sin(x))^2) gx = rlaplace(n, location = 0, scale = 1) Now we can write a simple R function to sample from Laplace distribution: ## `n` is sample size rlaplace <-

Monte Carlo integration using importance sampling given a proposal function

十年热恋 提交于 2019-12-01 10:31:23
问题 Given a Laplace Distribution proposal: g(x) = 1/2*e^(-|x|) and sample size n = 1000 , I want to Conduct the Monte Carlo (MC) integration for estimating θ: via importance sampling. Eventually I want to calculate the mean and standard deviation of this MC estimate in R once I get there. Edit (arrived late after the answer below) This is what I have for my R code so far: library(VGAM) n = 1000 x = rexp(n,0.5) hx = mean(2*exp(-sqrt(x))*(sin(x))^2) gx = rlaplace(n, location = 0, scale = 1) 回答1:

Thread-safe random number generation for Monte-Carlo integration

微笑、不失礼 提交于 2019-12-01 06:51:49
问题 Im trying to write something which very quickly calculates random numbers and can be applied on multiple threads. My current code is: /* Approximating PI using a Monte-Carlo method. */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <omp.h> #define N 1000000000 /* As lareg as possible for increased accuracy */ double random_function(void); int main(void) { int i = 0; double X, Y; double count_inside_temp = 0.0, count_inside = 0.0; unsigned int th_id = omp

Can Random Number Generator of Fortran 90 be trusted for Monte Carlo Integration?

穿精又带淫゛_ 提交于 2019-11-30 13:29:58
I have written a short monte carlo integration algorithm to calculate an integral in Fortran 90. I once compared the result obtained by solving the integral with respect to some parameter using the intrinsic random number generator with the random number generator method ran1 presented in Numerical Recipes for Fortran90 Volume 2. Running the same algorithm twice, once calling the intrinsic random_seed(), then always call random_number() and once calling the ran1() method provided in the Numerical Recipe book I obtain as result in principal the same shape but the intrinsic result is a