def test_0(): out = fs.static(trial=trial, trials=500) means = out.mean(dim='trials') meanEps = float(means.loc['eps']) meanB = float(means.loc['b']) assert abs(meanEps - 13.0) < 13.0 assert abs(meanB - 26.0) < 0.01
def test_normal_0(): # specify correlation matrix rho = np.array([[1.0, 0.5], [0.5, 1.0]]) # set up function to perform a single trial def f(draw): eps = fs.normal(rho, draw) # vector of two correlated stand. normal draws return {"eps0": eps[0], "eps1": eps[1]} # perform simulations out = fs.static(trial=f, trials=2000) sampcorr = np.corrcoef(out, rowvar=False)[0, 1] assert abs(sampcorr - 0.5) < 0.05
sys.path.append( os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")) import numpy as np import funcsim as fs def trial(draw): # function to perform one trial. # simulate one std. norm variable, and one Bernoilli variable # indep. uniform draw using built-in stratified/Latin hypercube sampling u_LHS = next(draw) # indep. uniform draw using non-stratified sampling u_no_LHS = np.random.uniform(0.0, 1.0) # return dict with var names and values return {"u_LHS": u_LHS, "u_no_LHS": u_no_LHS} # simulate out = fs.static(trial=trial, trials=100) # sort simulated u values, send to screen srtd_u_LHS = np.sort(out[:, 0]) srtd_u_no_LHS = np.sort(out[:, 1]) print("\nu_LHS, u_no_LHS") for i in range(len(srtd_u_LHS)): print("%s, %s" % (srtd_u_LHS[i], srtd_u_no_LHS[i]))
import funcsim as fs import numpy as np # https://en.wikipedia.org/wiki/Monte_Carlo_integration def h(x, y): return 1.0 if x**2.0 + y**2.0 < 1.0 else 0.0 def trial(draw): x = 2.0 * next(draw) - 1.0 # uniform over [-1, 1] y = 2.0 * next(draw) - 1.0 return {"h": h(x, y)} """ out = fs.crosssec(trial=trial, trials=5000, multi=False) area = float(out.mean()) # area of a quarter circle sigf = float(out.std()) n = len(out) print("n: %s" % n) print("value of pi/4 is approximately %s" % area) print("sig_f: %s" % sigf) print("sig_area: %s" % (sigf / n**0.5)) """ for s in range(100): out = fs.static(trial=trial, trials=5000, multi=False, seed=s) area = float(out.mean()) # area of a quarter circle print(area)
os.path.join(os.path.dirname(os.path.realpath(__file__)), "../")) from scipy import stats import funcsim as fs def trial(draw, prob): # function to perform one trial. # simulate one std. norm variable, and one Bernoilli variable that is 1 # with probability 'prob' # independent uniform draws u1 = next(draw) u2 = next(draw) # inverse CDF transformations eps = stats.norm.ppf(u1) b = stats.bernoulli.ppf(u2, prob) # return dict with var names and values return {"eps": eps, "b": b} # simulate for first scenario, with 'prob' = 0.25 out = fs.static(trial=lambda dr: trial(dr, 0.25), trials=15, multi=True) print("\nMeans of the simulated variables:\n%s" % out.mean(dim='trials')) # simulate for second scenario, with 'prob' = 0.5 out = fs.static(trial=lambda dr: trial(dr, 0.5), trials=15, multi=True) print("\nMeans of the simulated variables:\n%s" % out.mean(dim='trials'))