我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用scipy.randn()。
def test_stop_acceptance_rate_too_low(db_path): set_acc_rate = 0.2 def model(x): return {"par": x["par"] + sp.randn()} def dist(x, y): return abs(x["par"] - y["par"]) abc = ABCSMC(model, Distribution(par=st.uniform(0, 10)), dist, 10) abc.new(db_path, {"par": .5}) history = abc.run(-1, 8, min_acceptance_rate=set_acc_rate) df = history.get_all_populations() df["acceptance_rate"] = df["particles"] / df["samples"] assert df["acceptance_rate"].iloc[-1] < set_acc_rate assert df["acceptance_rate"].iloc[-2] >= set_acc_rate
def test_resume(db_path, gt_model): def model(parameter): return {"data": parameter["mean"] + sp.randn()} prior = Distribution(mean=RV("uniform", 0, 5)) def distance(x, y): x_data = x["data"] y_data = y["data"] return abs(x_data - y_data) abc = ABCSMC(model, prior, distance) run_id = abc.new(db_path, {"data": 2.5}, gt_model=gt_model) print("Run ID:", run_id) hist_new = abc.run(minimum_epsilon=0, max_nr_populations=1) assert hist_new.n_populations == 1 abc_continued = ABCSMC(model, prior, distance) run_id_continued = abc_continued.load(db_path, run_id) print("Run ID continued:", run_id_continued) hist_contd = abc_continued.run(minimum_epsilon=0, max_nr_populations=1) assert hist_contd.n_populations == 2 assert hist_new.n_populations == 2
def aTest(ks): times = np.linspace(0,1,100) dt = times[1]-times[0] W = sp.randn(100) W[0] *=0 W *= np.sqrt(dt) W = np.cumsum(W)*dt return [np.sum(np.sin(k*times)*W) for k in ks]
def df(request): par = request.param if par == "empty": return pd.DataFrame() if par == "int": return pd.DataFrame({"a": sp.random.randint(-20, 20, 100), "b": sp.random.randint(-20, 20, 100)}) if par == "float": return pd.DataFrame({"a": sp.randn(100), "b": sp.randn(100)}) if par == "non_numeric_str": return pd.DataFrame({"a": ["foo", "bar"], "b": ["bar", "foo"]}) if par == "numeric_str": return pd.DataFrame({"a": list(map(str, sp.randn(100))), "b": list(map(str, sp.random.randint(-20, 20, 100)))}) if par == "int-float-numeric_str": return pd.DataFrame({"a": sp.random.randint(-20, 20, 100), "b": sp.randn(100), "c": list(map(str, sp.random.randint(-20, 20, 100)))}) if par == "int-float-non_numeric_str-str_ind": return pd.DataFrame({"a": [1, 2], "b": [1.1, 2.2], "c": ["foo", "bar"]}, index=["first", "second"]) if par == "int-float-numeric_str-str_ind": return pd.DataFrame({"a": [1, 2], "b": [1.1, 2.2], "c": ["1", "2"]}, index=["first", "second"]) raise Exception("Invalid Test DataFrame Type")
def object_(request): par = request.param if par == "empty": return pd.DataFrame() if par == "int": return pd.DataFrame({"a": sp.random.randint(-20, 20, 100), "b": sp.random.randint(-20, 20, 100)}) if par == "float": return pd.DataFrame({"a": sp.randn(100), "b": sp.randn(100)}) if par == "non_numeric_str": return pd.DataFrame({"a": ["foo", "bar"], "b": ["bar", "foo"]}) if par == "numeric_str": return pd.DataFrame({"a": list(map(str, sp.randn(100))), "b": list(map(str, sp.random.randint(-20, 20, 100)))}) if par == "int-float-numeric_str": return pd.DataFrame({"a": sp.random.randint(-20, 20, 100), "b": sp.randn(100), "c": list(map(str, sp.random.randint(-20, 20, 100)))}) if par == "int-float-non_numeric_str-str_ind": return pd.DataFrame({"a": [1, 2], "b": [1.1, 2.2], "c": ["foo", "bar"]}, index=["first", "second"]) if par == "int-float-numeric_str-str_ind": return pd.DataFrame({"a": [1, 2], "b": [1.1, 2.2], "c": ["1", "2"]}, index=["first", "second"]) if par == "py-int": return 42 if par == "py-float": return 42.42 if par == "py-str": return "foo bar" if par == "np-int": return sp.random.randint(-20, 20, 100) if par == "np-float": return sp.random.randn(100) if par == "r-df-cars": return r["mtcars"] if par == "r-df-iris": return r["iris"] raise Exception("Invalid Test DataFrame Type")