mergeron 2025.739290.5__py3-none-any.whl → 2025.739290.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mergeron might be problematic. Click here for more details.

@@ -1,5 +1,6 @@
1
1
  import decimal
2
- from typing import Any, Literal, TypedDict
2
+ from collections.abc import Callable
3
+ from typing import Literal, TypedDict
3
4
 
4
5
  import matplotlib as mpl
5
6
  import matplotlib.axes as mpa
@@ -7,11 +8,10 @@ import matplotlib.patches as mpp
7
8
  import matplotlib.pyplot as plt
8
9
  import matplotlib.ticker as mpt
9
10
  import numpy as np
10
- from attrs import frozen
11
11
  from mpmath import mp, mpf # type: ignore
12
12
 
13
13
  from .. import DEFAULT_REC_RATIO, VERSION, ArrayBIGINT, ArrayDouble # noqa: TID252
14
- from . import MPFloat
14
+ from . import GuidelinesBoundary, MPFloat
15
15
 
16
16
  __version__ = VERSION
17
17
 
@@ -28,17 +28,6 @@ class ShareRatioBoundaryKeywords(TypedDict, total=False):
28
28
  weighting: Literal["own-share", "cross-product-share", None]
29
29
 
30
30
 
31
- @frozen
32
- class GuidelinesBoundary:
33
- """Output of a Guidelines boundary function."""
34
-
35
- coordinates: ArrayDouble
36
- """Market-share pairs as Cartesian coordinates of points on the boundary."""
37
-
38
- area: float
39
- """Area under the boundary."""
40
-
41
-
42
31
  def dh_area(_delta_bound: float | MPFloat = 0.01, /, *, dps: int = 9) -> float:
43
32
  R"""
44
33
  Area under the ΔHHI boundary.
@@ -56,7 +45,7 @@ def dh_area(_delta_bound: float | MPFloat = 0.01, /, *, dps: int = 9) -> float:
56
45
  .. math::
57
46
 
58
47
  2 s1 s_2 &= ΔHHI\\
59
- _s_1 + s_2 &= 1
48
+ s_1 + s_2 &= 1
60
49
 
61
50
  Parameters
62
51
  ----------
@@ -113,7 +102,7 @@ def hhi_delta_boundary(
113
102
  np.column_stack((_s_1, _delta_bound / (2 * _s_1))).astype(float),
114
103
  np.array([(mpf("0.0"), mpf("1.0"))], float),
115
104
  ))
116
- bdry = np.vstack((half_bdry[::-1], half_bdry[1:, ::-1]))
105
+ bdry = np.vstack((half_bdry[::-1], half_bdry[1:, ::-1]), dtype=float)
117
106
 
118
107
  return GuidelinesBoundary(bdry, dh_area(_delta_bound, dps=dps))
119
108
 
@@ -141,12 +130,12 @@ def hhi_pre_contrib_boundary(
141
130
 
142
131
  step_size = mp.power(10, -dps)
143
132
  # Range-limit is 0 less a step, which is -1 * step-size
144
- _s_1 = np.array(mp.arange(_s_mid, -step_size, -step_size))
145
- s_2 = np.sqrt(_hhi_bound - _s_1**2)
146
- half_bdry = np.column_stack((_s_1, s_2)).astype(float)
133
+ s_1 = np.array(mp.arange(_s_mid, -step_size, -step_size))
134
+ s_2 = np.sqrt(_hhi_bound - s_1**2)
135
+ half_bdry = np.column_stack((s_1, s_2)).astype(float)
147
136
 
148
137
  return GuidelinesBoundary(
149
- np.vstack((half_bdry[::-1], half_bdry[1:, ::-1])),
138
+ np.vstack((half_bdry[::-1], half_bdry[1:, ::-1]), dtype=float),
150
139
  round(float(mp.pi * _hhi_bound / 4), dps),
151
140
  )
152
141
 
@@ -178,7 +167,8 @@ def combined_share_boundary(
178
167
 
179
168
  _s1 = np.array([0, _s_mid, _s_intcpt], float)
180
169
  return GuidelinesBoundary(
181
- np.column_stack((_s1, _s1[::-1])), round(float(_s_intcpt * _s_mid), dps)
170
+ np.array(list(zip(_s1, _s1[::-1])), float),
171
+ round(float(_s_intcpt * _s_mid), dps),
182
172
  )
183
173
 
184
174
 
@@ -211,6 +201,7 @@ def hhi_post_contrib_boundary(
211
201
  )
212
202
 
213
203
 
204
+ # hand-rolled root finding
214
205
  def shrratio_boundary_wtd_avg( # noqa: PLR0914
215
206
  _delta_star: float = 0.075,
216
207
  _r_val: float = DEFAULT_REC_RATIO,
@@ -252,58 +243,57 @@ def shrratio_boundary_wtd_avg( # noqa: PLR0914
252
243
  is derived and plotted from y-intercept to the ray of symmetry as follows::
253
244
 
254
245
  from sympy import plot as symplot, solve, symbols
255
- _s_1, s_2 = symbols("_s_1 s_2", positive=True)
246
+ s_1, s_2 = symbols("s_1 s_2", positive=True)
256
247
 
257
248
  g_val, r_val, m_val = 0.06, 0.80, 0.30
258
249
  delta_star = g_val / (r_val * m_val)
259
250
 
260
251
  # recapture_form == "inside-out"
261
252
  oswag = solve(
262
- _s_1 * s_2 / (1 - _s_1)
263
- + s_2 * _s_1 / (1 - (r_val * s_2 + (1 - r_val) * _s_1))
264
- - (_s_1 + s_2) * delta_star,
253
+ s_1 * s_2 / (1 - s_1)
254
+ + s_2 * s_1 / (1 - (r_val * s_2 + (1 - r_val) * s_1))
255
+ - (s_1 + s_2) * delta_star,
265
256
  s_2
266
257
  )[0]
267
258
  symplot(
268
259
  oswag,
269
- (_s_1, 0., d_hat / (1 + d_hat)),
260
+ (s_1, 0., d_hat / (1 + d_hat)),
270
261
  ylabel=s_2
271
262
  )
272
263
 
273
264
  cpswag = solve(
274
- s_2 * s_2 / (1 - _s_1)
275
- + _s_1 * _s_1 / (1 - (r_val * s_2 + (1 - r_val) * _s_1))
276
- - (_s_1 + s_2) * delta_star,
265
+ s_2 * s_2 / (1 - s_1)
266
+ + s_1 * s_1 / (1 - (r_val * s_2 + (1 - r_val) * s_1))
267
+ - (s_1 + s_2) * delta_star,
277
268
  s_2
278
269
  )[1]
279
270
  symplot(
280
271
  cpwag,
281
- (_s_1, 0., d_hat / (1 + d_hat)),
282
- ylabel=s_2
272
+ (s_1, 0.0, d_hat / (1 + d_hat)), ylabel=s_2
283
273
  )
284
274
 
285
275
  # recapture_form == "proportional"
286
276
  oswag = solve(
287
- _s_1 * s_2 / (1 - _s_1)
288
- + s_2 * _s_1 / (1 - s_2)
289
- - (_s_1 + s_2) * delta_star,
277
+ s_1 * s_2 / (1 - s_1)
278
+ + s_2 * s_1 / (1 - s_2)
279
+ - (s_1 + s_2) * delta_star,
290
280
  s_2
291
281
  )[0]
292
282
  symplot(
293
283
  oswag,
294
- (_s_1, 0., d_hat / (1 + d_hat)),
284
+ (s_1, 0., d_hat / (1 + d_hat)),
295
285
  ylabel=s_2
296
286
  )
297
287
 
298
288
  cpswag = solve(
299
- s_2 * s_2 / (1 - _s_1)
300
- + _s_1 * _s_1 / (1 - s_2)
301
- - (_s_1 + s_2) * delta_star,
289
+ s_2 * s_2 / (1 - s_1)
290
+ + s_1 * s_1 / (1 - s_2)
291
+ - (s_1 + s_2) * delta_star,
302
292
  s_2
303
293
  )[1]
304
294
  symplot(
305
295
  cpswag,
306
- (_s_1, 0.0, d_hat / (1 + d_hat)),
296
+ (s_1, 0.0, d_hat / (1 + d_hat)),
307
297
  ylabel=s_2
308
298
  )
309
299
 
@@ -321,26 +311,22 @@ def shrratio_boundary_wtd_avg( # noqa: PLR0914
321
311
  # parameters for iteration
322
312
  _step_size = mp.power(10, -dps)
323
313
  theta_ = _step_size * (10 if weighting == "cross-product-share" else 1)
324
- for _s_1 in mp.arange(_s_mid - _step_size, 0, -_step_size):
314
+ for s_1 in mp.arange(_s_mid - _step_size, 0, -_step_size):
325
315
  # The wtd. avg. GUPPI is not always convex to the origin, so we
326
316
  # increment s_2 after each iteration in which our algorithm
327
317
  # finds (s1, s2) on the boundary
328
318
  s_2 = s_2_pre * (1 + theta_)
329
319
 
330
- if (_s_1 + s_2) > mpf("0.99875"):
331
- # Loss of accuracy at 3-9s and up
332
- break
333
-
334
320
  while True:
335
- de_1 = s_2 / (1 - _s_1)
321
+ de_1 = s_2 / (1 - s_1)
336
322
  de_2 = (
337
- _s_1 / (1 - lerp(_s_1, s_2, _r_val))
323
+ s_1 / (1 - lerp(s_1, s_2, _r_val))
338
324
  if recapture_form == "inside-out"
339
- else _s_1 / (1 - s_2)
325
+ else s_1 / (1 - s_2)
340
326
  )
341
327
 
342
328
  r_ = (
343
- mp.fdiv(_s_1 if weighting == "cross-product-share" else s_2, _s_1 + s_2)
329
+ mp.fdiv(s_1 if weighting == "cross-product-share" else s_2, s_1 + s_2)
344
330
  if weighting
345
331
  else 0.5
346
332
  )
@@ -365,7 +351,7 @@ def shrratio_boundary_wtd_avg( # noqa: PLR0914
365
351
  break
366
352
 
367
353
  # Build-up boundary points
368
- bdry.append((_s_1, s_2))
354
+ bdry.append((s_1, s_2))
369
355
 
370
356
  # Build up area terms
371
357
  s_2_oddsum += s_2 if s_2_oddval else 0
@@ -374,7 +360,11 @@ def shrratio_boundary_wtd_avg( # noqa: PLR0914
374
360
 
375
361
  # Hold share points
376
362
  s_2_pre = s_2
377
- s_1_pre = _s_1
363
+ s_1_pre = s_1
364
+
365
+ if (s_1 + s_2) > mpf("0.99875"):
366
+ # Loss of accuracy at 3-9s and up
367
+ break
378
368
 
379
369
  if s_2_oddval:
380
370
  s_2_evnsum -= s_2_pre
@@ -412,7 +402,7 @@ def shrratio_boundary_wtd_avg( # noqa: PLR0914
412
402
 
413
403
  # Points defining boundary to point-of-symmetry
414
404
  return GuidelinesBoundary(
415
- np.vstack((bdry_array[::-1], bdry_array[1:, ::-1])),
405
+ np.vstack((bdry_array[::-1], bdry_array[1:, ::-1]), dtype=float),
416
406
  round(float(bdry_area_total), dps),
417
407
  )
418
408
 
@@ -436,29 +426,29 @@ def shrratio_boundary_xact_avg( # noqa: PLR0914
436
426
 
437
427
  from sympy import latex, plot as symplot, solve, symbols
438
428
 
439
- _s_1, s_2 = symbols("_s_1 s_2")
429
+ s_1, s_2 = symbols("s_1 s_2")
440
430
 
441
431
  g_val, r_val, m_val = 0.06, 0.80, 0.30
442
432
  d_hat = g_val / (r_val * m_val)
443
433
 
444
434
  # recapture_form = "inside-out"
445
435
  sag = solve(
446
- (s_2 / (1 - _s_1))
447
- + (_s_1 / (1 - (r_val * s_2 + (1 - r_val) * _s_1)))
436
+ (s_2 / (1 - s_1))
437
+ + (s_1 / (1 - (r_val * s_2 + (1 - r_val) * s_1)))
448
438
  - 2 * d_hat,
449
439
  s_2
450
440
  )[0]
451
441
  symplot(
452
442
  sag,
453
- (_s_1, 0., d_hat / (1 + d_hat)),
443
+ (s_1, 0., d_hat / (1 + d_hat)),
454
444
  ylabel=s_2
455
445
  )
456
446
 
457
447
  # recapture_form = "proportional"
458
- sag = solve((s_2/(1 - _s_1)) + (_s_1/(1 - s_2)) - 2 * d_hat, s_2)[0]
448
+ sag = solve((s_2/(1 - s_1)) + (s_1/(1 - s_2)) - 2 * d_hat, s_2)[0]
459
449
  symplot(
460
450
  sag,
461
- (_s_1, 0., d_hat / (1 + d_hat)),
451
+ (s_1, 0., d_hat / (1 + d_hat)),
462
452
  ylabel=s_2
463
453
  )
464
454
 
@@ -614,7 +604,7 @@ def shrratio_boundary_min(
614
604
  _s_mid = _delta_star / (1 + _delta_star)
615
605
 
616
606
  if recapture_form == "inside-out":
617
- # ## Plot envelope of GUPPI boundaries with rk_ = r_bar if sk_ = min(_s_1, s_2)
607
+ # ## Plot envelope of GUPPI boundaries with rk_ = r_bar if sk_ = min(s_1, s_2)
618
608
  # ## See (si_, sj_) in equation~(44), or thereabouts, in paper
619
609
  smin_nr = _delta_star * (1 - _r_val)
620
610
  smax_nr = 1 - _delta_star * _r_val
@@ -626,7 +616,7 @@ def shrratio_boundary_min(
626
616
  s_1, bdry_area = np.array((0, _s_mid, _s_intcpt), float), _s_mid
627
617
 
628
618
  return GuidelinesBoundary(
629
- np.column_stack((s_1, s_1[::-1])), round(float(bdry_area), dps)
619
+ np.array(list(zip(s_1, s_1[::-1])), float), round(float(bdry_area), dps)
630
620
  )
631
621
 
632
622
 
@@ -659,7 +649,7 @@ def shrratio_boundary_max(
659
649
  _s1_pts = (0, _s_mid, _s_intcpt)
660
650
 
661
651
  return GuidelinesBoundary(
662
- np.column_stack((np.array(_s1_pts, float), np.array(_s1_pts[::-1], float))),
652
+ np.array(list(zip(_s1_pts, _s1_pts[::-1])), float),
663
653
  round(float(_s_intcpt * _s_mid), dps), # simplified calculation
664
654
  )
665
655
 
@@ -797,51 +787,57 @@ def round_cust(
797
787
  return float(f_ * (n_ / f_).quantize(e_, rounding=rounding_mode))
798
788
 
799
789
 
800
- def boundary_plot(*, mktshares_plot_flag: bool = True) -> tuple[Any, ...]:
790
+ def boundary_plot(
791
+ *,
792
+ mktshare_plot_flag: bool = True,
793
+ mktshare_axes_flag: bool = True,
794
+ backend: Literal["pgf"] | str | None = "pgf",
795
+ ) -> tuple[mpl.pyplot, mpl.pyplot.Figure, mpl.axes.Axes, Callable[..., mpl.axes.Axes]]:
801
796
  """Setup basic figure and axes for plots of safe harbor boundaries.
802
797
 
803
798
  See, https://matplotlib.org/stable/tutorials/text/pgf.html
804
799
  """
805
800
 
806
- mpl.use("pgf")
807
-
808
- plt.rcParams.update({
809
- "pgf.rcfonts": False,
810
- "pgf.texsystem": "lualatex",
811
- "pgf.preamble": "\n".join([
812
- R"\pdfvariable minorversion=7",
813
- R"\usepackage{fontspec}",
814
- R"\usepackage{luacode}",
815
- R"\begin{luacode}",
816
- R"local function embedfull(tfmdata)",
817
- R' tfmdata.embedding = "full"',
818
- R"end",
819
- R"",
820
- R"luatexbase.add_to_callback("
821
- R' "luaotfload.patch_font", embedfull, "embedfull"'
822
- R")",
823
- R"\end{luacode}",
824
- R"\usepackage{mathtools}",
825
- R"\usepackage{unicode-math}",
826
- R"\setmathfont[math-style=ISO]{STIX Two Math}",
827
- R"\setmainfont{STIX Two Text}",
828
- r"\setsansfont{Fira Sans Light}",
829
- R"\setmonofont[Scale=MatchLowercase,]{Fira Mono}",
830
- R"\defaultfontfeatures[\rmfamily]{",
831
- R" Ligatures={TeX, Common},",
832
- R" Numbers={Proportional, Lining},",
833
- R" }",
834
- R"\defaultfontfeatures[\sffamily]{",
835
- R" Ligatures={TeX, Common},",
836
- R" Numbers={Monospaced, Lining},",
837
- R" LetterSpace=0.50,",
838
- R" }",
839
- R"\usepackage[",
840
- R" activate={true, nocompatibility},",
841
- R" tracking=true,",
842
- R" ]{microtype}",
843
- ]),
844
- })
801
+ if backend == "pgf":
802
+ mpl.use("pgf")
803
+
804
+ plt.rcParams.update({
805
+ "pgf.rcfonts": False,
806
+ "pgf.texsystem": "lualatex",
807
+ "pgf.preamble": "\n".join([
808
+ R"\pdfvariable minorversion=7",
809
+ R"\usepackage{fontspec}",
810
+ R"\usepackage{luacode}",
811
+ R"\begin{luacode}",
812
+ R"local function embedfull(tfmdata)",
813
+ R' tfmdata.embedding = "full"',
814
+ R"end",
815
+ R"",
816
+ R"luatexbase.add_to_callback("
817
+ R' "luaotfload.patch_font", embedfull, "embedfull"'
818
+ R")",
819
+ R"\end{luacode}",
820
+ R"\usepackage{mathtools}",
821
+ R"\usepackage{unicode-math}",
822
+ R"\setmathfont[math-style=ISO]{STIX Two Math}",
823
+ R"\setmainfont{STIX Two Text}",
824
+ r"\setsansfont{Fira Sans Light}",
825
+ R"\setmonofont[Scale=MatchLowercase,]{Fira Mono}",
826
+ R"\defaultfontfeatures[\rmfamily]{",
827
+ R" Ligatures={TeX, Common},",
828
+ R" Numbers={Proportional, Lining},",
829
+ R" }",
830
+ R"\defaultfontfeatures[\sffamily]{",
831
+ R" Ligatures={TeX, Common},",
832
+ R" Numbers={Monospaced, Lining},",
833
+ R" LetterSpace=0.50,",
834
+ R" }",
835
+ R"\usepackage[",
836
+ R" activate={true, nocompatibility},",
837
+ R" tracking=true,",
838
+ R" ]{microtype}",
839
+ ]),
840
+ })
845
841
 
846
842
  # Initialize a canvas with a single figure (set of axes)
847
843
  fig_ = plt.figure(figsize=(5, 5), dpi=600)
@@ -851,8 +847,8 @@ def boundary_plot(*, mktshares_plot_flag: bool = True) -> tuple[Any, ...]:
851
847
  ax1_: mpa.Axes,
852
848
  /,
853
849
  *,
854
- mktshares_plot_flag: bool = False,
855
- mktshares_axlbls_flag: bool = False,
850
+ mktshare_plot_flag: bool = False,
851
+ mktshare_axes_flag: bool = False,
856
852
  ) -> mpa.Axes:
857
853
  # Set the width of axis grid lines, and tick marks:
858
854
  # both axes, both major and minor ticks
@@ -883,16 +879,7 @@ def boundary_plot(*, mktshares_plot_flag: bool = True) -> tuple[Any, ...]:
883
879
  ax1_.yaxis.get_majorticklabels(), horizontalalignment="right", fontsize=6
884
880
  )
885
881
 
886
- if mktshares_plot_flag:
887
- # Axis labels
888
- if mktshares_axlbls_flag:
889
- # x-axis
890
- ax1_.set_xlabel("Firm 1 Market Share, $_s_1$", fontsize=10)
891
- ax1_.xaxis.set_label_coords(0.75, -0.1)
892
- # y-axis
893
- ax1_.set_ylabel("Firm 2 Market Share, $s_2$", fontsize=10)
894
- ax1_.yaxis.set_label_coords(-0.1, 0.75)
895
-
882
+ if mktshare_plot_flag:
896
883
  # Plot the ray of symmetry
897
884
  ax1_.plot(
898
885
  [0, 1], [0, 1], linewidth=0.5, linestyle=":", color="grey", zorder=1
@@ -944,8 +931,21 @@ def boundary_plot(*, mktshares_plot_flag: bool = True) -> tuple[Any, ...]:
944
931
  for axl_ in ax1_.get_xticklabels(), ax1_.get_yticklabels():
945
932
  plt.setp(axl_[::2], visible=False)
946
933
 
934
+ # Axis labels
935
+ if mktshare_axes_flag:
936
+ # x-axis
937
+ ax1_.set_xlabel("Firm 1 Market Share, $s_1$", fontsize=10)
938
+ ax1_.xaxis.set_label_coords(0.75, -0.1)
939
+ # y-axis
940
+ ax1_.set_ylabel("Firm 2 Market Share, $s_2$", fontsize=10)
941
+ ax1_.yaxis.set_label_coords(-0.1, 0.75)
942
+
947
943
  return ax1_
948
944
 
949
- ax_out = _set_axis_def(ax_out, mktshares_plot_flag=mktshares_plot_flag)
945
+ ax_out = _set_axis_def(
946
+ ax_out,
947
+ mktshare_plot_flag=mktshare_plot_flag,
948
+ mktshare_axes_flag=mktshare_axes_flag,
949
+ )
950
950
 
951
951
  return plt, fig_, ax_out, _set_axis_def
@@ -17,6 +17,7 @@ from scipy.spatial.distance import minkowski as distance_function # type: ignor
17
17
  from sympy import lambdify, simplify, solve, symbols # type: ignore
18
18
 
19
19
  from .. import DEFAULT_REC_RATIO, VERSION, ArrayDouble # noqa: TID252
20
+ from . import GuidelinesBoundary, MPFloat
20
21
  from . import guidelines_boundary_functions as gbf
21
22
 
22
23
  __version__ = VERSION
@@ -529,3 +530,209 @@ def shrratio_boundary_xact_avg_mp( # noqa: PLR0914
529
530
  ) - mp.power(_s_mid, 2)
530
531
 
531
532
  return gbf.GuidelinesBoundary(bdry, float(mp.nstr(bdry_area_simpson, dps)))
533
+
534
+
535
+ # shrratio_boundary_wtd_avg_autoroot
536
+ # this function is about half as fast as the manual one! ... and a touch less precise
537
+ def _shrratio_boundary_wtd_avg_autoroot( # noqa: PLR0914
538
+ _delta_star: float = 0.075,
539
+ _r_val: float = DEFAULT_REC_RATIO,
540
+ /,
541
+ *,
542
+ agg_method: Literal[
543
+ "arithmetic mean", "geometric mean", "distance"
544
+ ] = "arithmetic mean",
545
+ weighting: Literal["own-share", "cross-product-share", None] = "own-share",
546
+ recapture_form: Literal["inside-out", "proportional"] = "inside-out",
547
+ dps: int = 5,
548
+ ) -> GuidelinesBoundary:
549
+ """
550
+ Share combinations on the share-weighted average diversion ratio boundary.
551
+
552
+ Parameters
553
+ ----------
554
+ _delta_star
555
+ Share ratio (:math:`\\overline{d} / \\overline{r}`)
556
+ _r_val
557
+ recapture ratio
558
+ agg_method
559
+ Whether "arithmetic mean", "geometric mean", or "distance".
560
+ weighting
561
+ Whether "own-share" or "cross-product-share" (or None for simple, unweighted average).
562
+ recapture_form
563
+ Whether recapture-ratio is MNL-consistent ("inside-out") or has fixed
564
+ value for both merging firms ("proportional").
565
+ dps
566
+ Number of decimal places for rounding returned shares and area.
567
+
568
+ Returns
569
+ -------
570
+ Array of share-pairs, area under boundary.
571
+
572
+ Notes
573
+ -----
574
+ An analytical expression for the share-weighted arithmetic mean boundary
575
+ is derived and plotted from y-intercept to the ray of symmetry as follows::
576
+
577
+ from sympy import plot as symplot, solve, symbols
578
+ s_1, s_2 = symbols("s_1 s_2", positive=True)
579
+
580
+ g_val, r_val, m_val = 0.06, 0.80, 0.30
581
+ delta_star = g_val / (r_val * m_val)
582
+
583
+ # recapture_form == "inside-out"
584
+ oswag = solve(
585
+ s_1 * s_2 / (1 - s_1)
586
+ + s_2 * s_1 / (1 - (r_val * s_2 + (1 - r_val) * s_1))
587
+ - (s_1 + s_2) * delta_star,
588
+ s_2
589
+ )[0]
590
+ symplot(
591
+ oswag,
592
+ (s_1, 0., d_hat / (1 + d_hat)),
593
+ ylabel=s_2
594
+ )
595
+
596
+ cpswag = solve(
597
+ s_2 * s_2 / (1 - s_1)
598
+ + s_1 * s_1 / (1 - (r_val * s_2 + (1 - r_val) * s_1))
599
+ - (s_1 + s_2) * delta_star,
600
+ s_2
601
+ )[1]
602
+ symplot(
603
+ cpwag,
604
+ (s_1, 0.0, d_hat / (1 + d_hat)), ylabel=s_2
605
+ )
606
+
607
+ # recapture_form == "proportional"
608
+ oswag = solve(
609
+ s_1 * s_2 / (1 - s_1)
610
+ + s_2 * s_1 / (1 - s_2)
611
+ - (s_1 + s_2) * delta_star,
612
+ s_2
613
+ )[0]
614
+ symplot(
615
+ oswag,
616
+ (s_1, 0., d_hat / (1 + d_hat)),
617
+ ylabel=s_2
618
+ )
619
+
620
+ cpswag = solve(
621
+ s_2 * s_2 / (1 - s_1)
622
+ + s_1 * s_1 / (1 - s_2)
623
+ - (s_1 + s_2) * delta_star,
624
+ s_2
625
+ )[1]
626
+ symplot(
627
+ cpswag,
628
+ (s_1, 0.0, d_hat / (1 + d_hat)),
629
+ ylabel=s_2
630
+ )
631
+
632
+
633
+ """
634
+
635
+ _delta_star, _r_val = (mpf(f"{_v}") for _v in (_delta_star, _r_val))
636
+ _s_mid = mp.fdiv(_delta_star, 1 + _delta_star)
637
+
638
+ # initial conditions
639
+ bdry = [(_s_mid, _s_mid)]
640
+ s_1_pre, s_2_pre = _s_mid, _s_mid
641
+ s_2_oddval, s_2_oddsum, s_2_evnsum = True, 0.0, 0.0
642
+
643
+ # parameters for iteration
644
+ _step_size = mp.power(10, -dps)
645
+ theta_ = _step_size * (10 if weighting == "cross-product-share" else 1)
646
+ for s_1 in mp.arange(_s_mid - _step_size, 0, -_step_size):
647
+
648
+ def delta_test(x: MPFloat) -> MPFloat:
649
+ _de_1 = x / (1 - s_1)
650
+ _de_2 = (
651
+ s_1 / (1 - gbf.lerp(s_1, x, _r_val))
652
+ if recapture_form == "inside-out"
653
+ else s_1 / (1 - x)
654
+ )
655
+ _w = (
656
+ mp.fdiv(s_1 if weighting == "cross-product-share" else x, s_1 + x)
657
+ if weighting
658
+ else 0.5
659
+ )
660
+
661
+ match agg_method:
662
+ case "geometric mean":
663
+ delta_test = mp.expm1(
664
+ gbf.lerp(mp.log1p(_de_1), mp.log1p(_de_2), _w)
665
+ )
666
+ case "distance":
667
+ delta_test = mp.sqrt(gbf.lerp(_de_1**2, _de_2**2, _w))
668
+ case _:
669
+ delta_test = gbf.lerp(_de_1, _de_2, _w)
670
+
671
+ return _delta_star - delta_test
672
+
673
+ try:
674
+ s_2 = mp.findroot(
675
+ delta_test,
676
+ x0=(s_2_pre * (1 - theta_), s_2_pre * (1 + theta_)),
677
+ tol=mp.sqrt(_step_size),
678
+ solver="ridder",
679
+ )
680
+ except (mp.ComplexResult, ValueError, ZeroDivisionError) as _e:
681
+ print(s_1, s_2_pre)
682
+ raise _e
683
+
684
+ # Build-up boundary points
685
+ bdry.append((s_1, s_2))
686
+
687
+ # Build up area terms
688
+ s_2_oddsum += s_2 if s_2_oddval else 0
689
+ s_2_evnsum += s_2 if not s_2_oddval else 0
690
+ s_2_oddval = not s_2_oddval
691
+
692
+ # Hold share points
693
+ s_2_pre = s_2
694
+ s_1_pre = s_1
695
+
696
+ if (s_1_pre + s_2_pre) > mpf("0.99875"):
697
+ # Loss of accuracy at 3-9s and up
698
+ break
699
+
700
+ if s_2_oddval:
701
+ s_2_evnsum -= s_2_pre
702
+ else:
703
+ s_2_oddsum -= s_1_pre
704
+
705
+ _s_intcpt = gbf._shrratio_boundary_intcpt(
706
+ s_2_pre,
707
+ _delta_star,
708
+ _r_val,
709
+ recapture_form=recapture_form,
710
+ agg_method=agg_method,
711
+ weighting=weighting,
712
+ )
713
+
714
+ if weighting == "own-share":
715
+ gbd_prtlarea = (
716
+ _step_size * (4 * s_2_oddsum + 2 * s_2_evnsum + _s_mid + s_2_pre) / 3
717
+ )
718
+ # Area under boundary
719
+ bdry_area_total = float(
720
+ 2 * (s_1_pre + gbd_prtlarea)
721
+ - (mp.power(_s_mid, "2") + mp.power(s_1_pre, "2"))
722
+ )
723
+
724
+ else:
725
+ gbd_prtlarea = (
726
+ _step_size * (4 * s_2_oddsum + 2 * s_2_evnsum + _s_mid + _s_intcpt) / 3
727
+ )
728
+ # Area under boundary
729
+ bdry_area_total = float(2 * gbd_prtlarea - mp.power(_s_mid, "2"))
730
+
731
+ bdry.append((mpf("0.0"), _s_intcpt))
732
+ bdry_array = np.array(bdry, float)
733
+
734
+ # Points defining boundary to point-of-symmetry
735
+ return GuidelinesBoundary(
736
+ np.vstack((bdry_array[::-1], bdry_array[1:, ::-1]), dtype=float),
737
+ round(float(bdry_area_total), dps),
738
+ )