Skip to content

Commit 10c9c58

Browse files
authored
Merge pull request #550 from hpcflow/fix/sus-acs
Fix adaptive conditional sampling variant of the toy model subset simulation workflow
2 parents 3ecec82 + d6a5886 commit 10c9c58

13 files changed

Lines changed: 463 additions & 100 deletions

matflow/data/scripts/uq/collate_results.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ def collate_results(g, x, p_0, all_g, all_x, all_accept, level_cov):
5151
if all_g:
5252
# from multiple Markov chains:
5353
g_unsrt = np.concatenate([i[:] for i in all_g])
54-
accept = np.vstack([i[:] for i in all_accept])
54+
all_all_accept = []
55+
for iter_dat in all_accept.values():
56+
if iter_dat["value"]:
57+
all_all_accept.append(np.vstack([i[:] for i in iter_dat["value"]]))
58+
accept_rate = np.mean(all_all_accept, axis=(1, 2))
5559
x = np.vstack([i[:] for i in all_x])
56-
accept_rate = np.mean(accept)
5760
else:
5861
# from initial direct Monte Carlo samples:
5962
g_unsrt = np.array(g)

matflow/data/scripts/uq/generate_next_state.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import os
33

44
import numpy as np
5-
from numpy.typing import NDArray
6-
from scipy.stats import norm
5+
import scipy.stats
76

87

98
def set_up_logger():
@@ -35,15 +34,16 @@ def _init_rng(chain_index, loop_idx):
3534
return rng
3635

3736

38-
def generate_next_state(x, prop_std, rng, chain_index):
37+
def generate_next_state(x, proposal, rng, chain_index):
3938
"""Generate the next candidate state in a modified Metropolis algorithm.
4039
4140
Parameters
4241
----------
4342
x
4443
Current state on which the candidate state will depend.
45-
prop_std
46-
Proposal distribution standard deviation.
44+
proposal
45+
Type and arguments to a Scipy distribution that should be used as the proposal.
46+
The proposal must be a symmetrical distribution, centred on zero.
4747
rng
4848
Random number generator to be used in this function.
4949
chain_index
@@ -59,6 +59,9 @@ def generate_next_state(x, prop_std, rng, chain_index):
5959
for this chain.
6060
"""
6161

62+
dist_type = proposal.pop("type")
63+
prop_dist = getattr(scipy.stats, dist_type)(**proposal)
64+
6265
loop_idx = {
6366
loop_name: int(loop_idx)
6467
for loop_name, loop_idx in (
@@ -76,12 +79,13 @@ def generate_next_state(x, prop_std, rng, chain_index):
7679
current_state = x
7780
xi = np.empty(dim)
7881

79-
proposal = norm(loc=current_state, scale=prop_std)
80-
xi_hat = np.atleast_1d(proposal.rvs(random_state=rng))
81-
accept_ratios = np.divide(*norm.pdf([xi_hat, current_state]))
82+
xi_hat = np.atleast_1d(current_state + prop_dist.rvs(size=dim, random_state=rng))
83+
accept_ratios = np.divide(*scipy.stats.norm.pdf([xi_hat, current_state]))
8284
accept_idx = rng.random(len(accept_ratios)) < np.minimum(1, accept_ratios)
8385

8486
xi[accept_idx] = xi_hat[accept_idx]
8587
xi[~accept_idx] = current_state[~accept_idx]
8688

87-
return {"x": xi, "rng": rng}
89+
mcmc_accept_rate = np.mean(accept_idx)
90+
91+
return {"x": xi, "mcmc_accept_rate": mcmc_accept_rate, "rng": rng}

matflow/data/template_components/task_schemas.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@
18071807
group: all
18081808
default_value: null
18091809
- parameter: level_cov
1810-
default_value: null
1810+
default_value: null
18111811
- parameter: p_0
18121812
outputs:
18131813
- parameter: chain_seeds
@@ -1826,6 +1826,7 @@
18261826
script_data_in:
18271827
g: { format: direct, all_iterations: true }
18281828
level_cov: { format: direct, all_iterations: true }
1829+
all_accept: { format: direct, all_iterations: true }
18291830
"*": direct
18301831
script_data_out: direct
18311832
script_exe: python_script
@@ -1886,12 +1887,13 @@
18861887
- objective: generate_next_state
18871888
inputs:
18881889
- parameter: x
1889-
- parameter: prop_std
1890+
- parameter: proposal
18901891
- parameter: chain_index
18911892
- parameter: rng
18921893
default_value: null
18931894
outputs:
18941895
- parameter: x
1896+
- parameter: mcmc_accept_rate
18951897
- parameter: rng
18961898
actions:
18971899
- script: <<script:uq/generate_next_state.py>>
@@ -1910,7 +1912,7 @@
19101912
- parameter: prop_std
19111913
- parameter: chain_index
19121914
- parameter: rng
1913-
default_value: null
1915+
default_value: null
19141916
outputs:
19151917
- parameter: x
19161918
- parameter: rng
@@ -1932,7 +1934,7 @@
19321934
- parameter: lambda_
19331935
- parameter: chain_index
19341936
- parameter: rng
1935-
default_value: null
1937+
default_value: null
19361938
outputs:
19371939
- parameter: x
19381940
- parameter: rng

matflow/data/workflows/subset_simulation_DAMASK_Al.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ tasks:
117117
step: 1
118118
- schema: generate_next_state # [inner loop]
119119
inputs:
120-
prop_std: 0.5
120+
proposal:
121+
type: norm
122+
scale: 0.5
121123
- schema: system_analysis # [inner loop]
122124
- schema: increment_chain # [inner loop]
123125
groups:

matflow/data/workflows/subset_simulation_DAMASK_Mg.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ tasks:
256256
step: 1
257257
- schema: generate_next_state # [inner loop]
258258
inputs:
259-
prop_std: 1.0
259+
proposal:
260+
type: norm
261+
scale: 1.0
260262
- schema: system_analysis # [inner loop]
261263
- schema: increment_chain # [inner loop]
262264
groups:

matflow/data/workflows/subset_simulation_DAMASK_Mg_two_level.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,9 @@ tasks:
476476

477477
- schema: generate_next_state # [inner MC loop; outer MC loop] # 6
478478
inputs:
479-
prop_std: 1.0
479+
proposal:
480+
type: norm
481+
scale: 1.0
480482
- schema: system_analysis_16 # [inner MC loop; outer MC loop] # 7,8,9
481483
- schema: increment_chain_inner # [inner MC loop; outer MC loop] # outputs x, like `generate_next_state` does # 10
482484

matflow/data/workflows/subset_simulation_toy_model.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
loops:
22
- name: markov_chain_state # [inner loop]
3-
tasks: [4, 5, 6]
3+
tasks: [ 4, 5, 6 ]
44
num_iterations: 9 # num_states - 1: (1 / p_0) - 1
55
- name: levels # [outer loop]
6-
tasks: [2, 3, 4, 5, 6]
6+
tasks: [ 2, 3, 4, 5, 6 ]
77
num_iterations: 7
88
termination_task: 2
99
termination:
@@ -26,7 +26,7 @@ tasks:
2626
- schema: system_analysis_toy_model
2727
inputs:
2828
dimension: 200
29-
target_pf: 1.0e-4
29+
target_pf: 1e-4
3030
groups:
3131
- name: all
3232
- schema: collate_results # [outer loop]
@@ -41,11 +41,13 @@ tasks:
4141
step: 1
4242
- schema: generate_next_state # [inner loop]
4343
inputs:
44-
prop_std: 1.0
44+
proposal:
45+
type: norm
46+
scale: 1.0
4547
- schema: system_analysis_toy_model # [inner loop]
4648
inputs:
4749
dimension: 200
48-
target_pf: 1.0e-4
50+
target_pf: 1e-4
4951
- schema: increment_chain # [inner loop]
5052
groups:
5153
- name: all

matflow/data/workflows/subset_simulation_toy_model_delayed_acceptance.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ tasks:
4747

4848
- schema: generate_next_state # [inner MC loop; outer MC loop]
4949
inputs:
50-
prop_std: 1.0
50+
proposal:
51+
type: norm
52+
scale: 1.0
5153

5254
- schema: system_analysis_DA_coarse # [inner MC loop; outer MC loop]
5355
resources:

matflow/data/workflows/subset_simulation_toy_model_external.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ tasks:
5353
step: 1
5454
- schema: generate_next_state # [inner loop]
5555
inputs:
56-
prop_std: 0.5
56+
proposal:
57+
type: norm
58+
scale: 0.5
5759
- schema: system_analysis # [inner loop]
5860
- schema: increment_chain # [inner loop]
5961
groups:

matflow/data/workflows/subset_simulation_toy_model_two_level.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ tasks:
4545

4646
- schema: generate_next_state # [inner MC loop; outer MC loop]
4747
inputs:
48-
prop_std: 1.0
48+
proposal:
49+
type: norm
50+
scale: 1.0
4951
- schema: system_analysis_toy_model # [inner MC loop; outer MC loop]
5052
inputs:
5153
dimension: 200

0 commit comments

Comments
 (0)