Skip to content

Commit 15a0c7d

Browse files
committed
Update: optional antithetic pairs of random numbers
1 parent a7b904c commit 15a0c7d

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ will print a detailed usage messages to the screen, like
2424
usage: kaleidoscope-scatter [-h] --source-type
2525
{esa-cci-oc,esa-scope-cs,esa-scope-pp,ghrsst,glorys}
2626
--selector {0,1,2,...,100}
27+
[--antithetic]
2728
[--engine-reader {h5netcdf,netcdf4,zarr}]
2829
[--engine-writer {h5netcdf,netcdf4,zarr}]
2930
[--log-level {debug,info,warning,error,off}]
@@ -46,6 +47,8 @@ will print a detailed usage messages to the screen, like
4647
--selector {0,1,2,...,100}
4748
the Monte Carlo stream selector. An integral number
4849
which must not be negative. (default: None)
50+
--antithetic enable pairwise antithetic Monte Carlo simulation.
51+
(default: False)
4952
--engine-reader {h5netcdf,netcdf4,zarr}
5053
specify the engine used to read the source product
5154
file. (default: None)

kaleidoscope/config/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Copyright (c) Brockmann Consult GmbH, 2025
22
# License: MIT
33

4-
## The default source type.
5-
source_type: _
4+
## No pairwise antithetic Monte Carlo simulation.
5+
antithetic: False
66

77
## Detect the reader engine automatically
88
engine_reader:

kaleidoscope/main/scatter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ def _add_options(parser):
111111
required=True,
112112
dest="selector",
113113
)
114+
parser.add_argument(
115+
"--antithetic",
116+
help="enable pairwise antithetic Monte Carlo simulation.",
117+
action="store_true",
118+
required=False,
119+
dest="antithetic",
120+
)
114121
parser.add_argument(
115122
"--engine-reader",
116123
help="specify the engine used to read the source product file.",

kaleidoscope/operators/scatterop.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ def randomize(
108108
z = da.clip(z, config["clip"][0], config["clip"][1])
109109
elif "uncertainty" in config:
110110
s = self.seed(self.uuid(v))
111-
f = Randomize(m=x.ndim, dist=config["distribution"], seed=s)
111+
f = Randomize(
112+
m=x.ndim,
113+
dist=config["distribution"],
114+
seed=s,
115+
antithetic=self._args.antithetic,
116+
)
112117
u = (
113118
target[config["uncertainty"]]
114119
if isinstance(config["uncertainty"], str)
@@ -130,7 +135,12 @@ def randomize(
130135
)
131136
else:
132137
s = self.seed(self.uuid(v))
133-
f = Randomize(m=x.ndim, dist=config["distribution"], seed=s)
138+
f = Randomize(
139+
m=x.ndim,
140+
dist=config["distribution"],
141+
seed=s,
142+
antithetic=self._args.antithetic,
143+
)
134144
b = target[config["bias"]]
135145
r = target[config["rmsd"]]
136146
z = f.apply_to(
@@ -177,7 +187,11 @@ def seed(self, uuid: uuid.UUID, n: int = 4) -> np.ndarray:
177187
"""
178188
from numpy.random import Philox
179189

180-
seed = uuid.int + self._args.selector
190+
seed = uuid.int + (
191+
self._args.selector
192+
if not self._args.antithetic
193+
else (self._args.selector + 1) // 2
194+
)
181195
g = DefaultGenerator(Philox(seed))
182196
return np.array([g.next() for _ in range(n)], dtype=np.int64)
183197

0 commit comments

Comments
 (0)