@mark1russell7/capacity 0.1.2 → 0.2.1

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.
package/src/gate.ts CHANGED
@@ -70,6 +70,8 @@ export interface GateJob {
70
70
  export interface GateCase {
71
71
  readonly law: Law;
72
72
  readonly jobs: readonly GateJob[];
73
+ /** This count is the reads of the law in a new job, for example the points of the plan that the law reads. It gives `LawGateCase.more`. */
74
+ readonly again?: number;
73
75
  }
74
76
 
75
77
  /** These options set the level of the run range, the level of the family of tests and the count of jobs of a side. */
@@ -78,6 +80,16 @@ export interface LawGateOptions extends LevelOptions {
78
80
  readonly alpha?: number;
79
81
  /** This count is the jobs that each must show a side. The default is `GATE_JOBS`. */
80
82
  readonly jobs?: number;
83
+ /**
84
+ * This count is the laws of the family of tests. Each law gives two tests. A case that this look does not test adds two
85
+ * tests with a p of 1. The default is the count of cases. It cannot be less than the count of cases.
86
+ */
87
+ readonly family?: number;
88
+ /**
89
+ * These cases are the outcomes of the earlier looks of the same gate. A case with an outcome other than "cannot decide"
90
+ * keeps it, and this look does not test it. Give the same family to each look.
91
+ */
92
+ readonly decided?: readonly LawGateCase[];
81
93
  }
82
94
 
83
95
  /** A job count tells the reads of one job for one law, and the p of the count of each side. */
@@ -121,8 +133,16 @@ export interface LawGateCase extends GateCaseOutcome {
121
133
  readonly noRead: readonly NoReadJob[];
122
134
  /** These tests are the two sides, or an empty list for a case with too few jobs. */
123
135
  readonly sides: readonly SideTest[];
136
+ /**
137
+ * This count is the least count of new jobs with which the case can decide, each with `GateCase.again` reads. Only a
138
+ * case that cannot decide has it. A case with no `again`, or with no count up to `MAX_MORE_JOBS`, does not have it.
139
+ */
140
+ readonly more?: number;
124
141
  }
125
142
 
143
+ /** This count is the most new jobs of one case that `lawGate` examines for `LawGateCase.more`. */
144
+ export const MAX_MORE_JOBS = 16;
145
+
126
146
  /** A law gate is the gate of `@mark1russell7/rigor-bench` over the laws, with the facts that it uses. */
127
147
  export interface LawGate extends Gate {
128
148
  readonly level: number;
@@ -131,6 +151,8 @@ export interface LawGate extends Gate {
131
151
  readonly need: number;
132
152
  /** This chance is the chance of one side for one read of a job that agrees with its law: `(1 - level) / 2`. */
133
153
  readonly share: number;
154
+ /** This count is the laws of the family of tests (`LawGateOptions.family`). */
155
+ readonly family: number;
134
156
  readonly cases: readonly LawGateCase[];
135
157
  }
136
158
 
@@ -163,8 +185,30 @@ interface Counted {
163
185
  readonly law: Law;
164
186
  readonly jobs: readonly JobCount[];
165
187
  readonly noRead: readonly NoReadJob[];
188
+ readonly again: number | undefined;
166
189
  }
167
190
 
191
+ /** This function gives the best p of a side: the p with each read of each job on that side. */
192
+ const bestOf = (reads: readonly number[], share: number, need: number): number =>
193
+ partialConjunction(
194
+ reads.map((count) => binomialTest(count, count, share, "greater")),
195
+ need,
196
+ );
197
+
198
+ /**
199
+ * This function gives the least count of new jobs with which a case can decide. Each new job gives `again` reads.
200
+ * The case has jobs with `reads` reads. To decide, it needs `need` jobs or more, and a best p of `bound` or less.
201
+ * The function gives nothing when no count up to `MAX_MORE_JOBS` can decide.
202
+ */
203
+ const moreOf = (reads: readonly number[], again: number, share: number, need: number, bound: number): number | undefined => {
204
+ if (!Number.isInteger(again) || again < 1) return undefined;
205
+ for (let more = 1; more <= MAX_MORE_JOBS; more += 1) {
206
+ const all = [...reads, ...Array.from({ length: more }, () => again)];
207
+ if (all.length >= need && bestOf(all, share, need) <= bound) return more;
208
+ }
209
+ return undefined;
210
+ };
211
+
168
212
  /**
169
213
  * This function gives the gate of some laws. For each law, it tells if the measured values of the jobs stay in the run
170
214
  * range of the law. It is the rule of a gate of the laws of a subject, for example on a pull request.
@@ -175,11 +219,20 @@ interface Counted {
175
219
  * A job with `noRead`, or with no read in the range of the law, gives no read.
176
220
  * - **A side:** `partialConjunction` joins the p of the jobs for the claim "`jobs` jobs or more show this side". Thus one
177
221
  * job on an odd runner cannot block alone. The claim has no condition on how the reads of one job depend on each other.
178
- * - **The family:** `holm` corrects the tests of the two sides of each law at the level `alpha`. A rejected side makes the
179
- * case `beyond`. A low side is a block too: the law is no longer the law of the subject, and a new fit must replace it.
180
- * - **No read:** a case with fewer than `jobs` jobs that read is `cannot decide`. So is a case whose reads are too few to
181
- * reject a side, also with each read on that side. The gate of `@mark1russell7/rigor-bench` (`gateOf`) joins the cases.
182
- * A gate with no case, or with a case that cannot decide and no block, is "no read". It does not pass.
222
+ * - **The family:** `holm` corrects the tests of the two sides of each law at the level `alpha`. The family has two tests
223
+ * for each of `family` laws. A rejected side makes the case `beyond`. A low side is a block too: the law is no longer
224
+ * the law of the subject, and a new fit must replace it.
225
+ * - **No read:** a case with fewer than `jobs` jobs that read is `cannot decide`. So is a case with too few reads to reject
226
+ * a side at the first step of Holm, with each read on that side. These cases are not tested: each has a p of 1.
227
+ * The gate of `@mark1russell7/rigor-bench` (`gateOf`) joins the cases. A gate with no case, or with a case that cannot
228
+ * decide and no block, is "no read". It does not pass.
229
+ * - **More jobs:** a case that cannot decide has `more` when its `again` gives the reads of a new job. It is the least count
230
+ * of new jobs with which the case can decide.
231
+ * - **Looks:** the option `decided` gives the outcomes of the earlier looks, for example before the new jobs. Each look
232
+ * tests only the cases that no earlier look decided, with one family for all looks. A case without power at a look is
233
+ * not tested at that look. Thus each case is tested at one look at most. The count a(t) is the tests of look t with a law
234
+ * that holds, and A is the sum over all looks. Look t gives a false block with a chance of `alpha` × a(t) / A or less. The sum is
235
+ * `alpha` or less.
183
236
  *
184
237
  * The function throws a `RangeError` for a level, an alpha or a count of jobs that is not valid.
185
238
  */
@@ -191,33 +244,48 @@ export const lawGate = (cases: readonly GateCase[], options: LawGateOptions = {}
191
244
  if (!(alpha > 0 && alpha < 1)) throw new RangeError(`The alpha ${alpha} is not between 0 and 1.`);
192
245
  if (!Number.isInteger(need) || need < 1) throw new RangeError(`The count of jobs ${need} is not a whole number above 0.`);
193
246
  const share = (1 - level) / 2;
247
+ const family = options.family ?? cases.length;
248
+ if (!Number.isInteger(family) || family < cases.length) throw new RangeError(`The family ${family} is not a whole number of ${cases.length} cases or more.`);
249
+ const tests = family * 2;
250
+ const bound = alpha / tests;
251
+ const decided = new Map((options.decided ?? []).filter((each) => each.outcome !== "cannot decide").map((each) => [each.case, each]));
194
252
 
195
- const counted: Counted[] = cases.map(({ law, jobs }) => {
253
+ const counted: Counted[] = cases.map(({ law, jobs, again }) => {
196
254
  const found = jobs.map((job) => countOf(law, job, level, share));
197
- return { law, jobs: found.filter((each): each is JobCount => "reads" in each), noRead: found.filter((each): each is NoReadJob => "reason" in each) };
255
+ return { law, jobs: found.filter((each): each is JobCount => "reads" in each), noRead: found.filter((each): each is NoReadJob => "reason" in each), again };
198
256
  });
199
- const decidable = counted.filter((each) => each.jobs.length >= need);
200
- const tests = decidable.length * 2;
257
+ // A case of this look has enough jobs, and a best p at the first step of Holm or below. The best p depends only on the
258
+ // count of reads, not on their sides, thus this choice does not change the level of a test. Each other case has a p of 1.
259
+ const powered = (each: Counted): boolean => !decided.has(each.law.id) && each.jobs.length >= need && bestOf(each.jobs.map((job) => job.reads), share, need) <= bound;
260
+ const tested = counted.filter(powered);
201
261
  const pOf = (jobs: readonly JobCount[], side: "high" | "low"): number => partialConjunction(jobs.map((job) => (side === "high" ? job.pHigh : job.pLow)), need);
202
262
  const corrected = holm(
203
- decidable.flatMap((each) => [pOf(each.jobs, "high"), pOf(each.jobs, "low")]),
263
+ [...tested.flatMap((each) => [pOf(each.jobs, "high"), pOf(each.jobs, "low")]), ...Array.from({ length: tests - tested.length * 2 }, () => 1)],
204
264
  alpha,
205
265
  );
206
266
 
207
267
  let next = 0;
208
- const outcomes: LawGateCase[] = counted.map(({ law, jobs, noRead }) => {
268
+ const outcomes: LawGateCase[] = counted.map((each) => {
269
+ const { law, jobs, noRead, again } = each;
270
+ const earlier = decided.get(law.id);
271
+ if (earlier !== undefined) return earlier;
209
272
  const base = { case: law.id, jobs, noRead };
210
- if (jobs.length < need) {
211
- const reason = `${jobs.length} jobs read the law. The gate needs ${need} jobs or more.${noRead.length > 0 ? ` ${noRead[0]!.reason}` : ""}`;
212
- return { ...base, outcome: "cannot decide", reason, sides: [] };
273
+ if (!powered(each)) {
274
+ const more = again === undefined ? undefined : moreOf(jobs.map((job) => job.reads), again, share, need, bound);
275
+ const extra = more === undefined ? {} : { more };
276
+ if (jobs.length < need) {
277
+ const reason = `${jobs.length} jobs read the law. The gate needs ${need} jobs or more.${noRead.length > 0 ? ` ${noRead[0]!.reason}` : ""}`;
278
+ return { ...base, outcome: "cannot decide", reason, sides: [], ...extra };
279
+ }
280
+ const best = bestOf(jobs.map((job) => job.reads), share, need);
281
+ const reason = `The reads are too few to reject a side: with each read on one side, the p is ${short(best)}, above ${short(bound)}.`;
282
+ return { ...base, outcome: "cannot decide", reason, sides: [], ...extra };
213
283
  }
214
284
  const sides: SideTest[] = (["high", "low"] as const).map((side) => {
215
285
  const test = corrected[next];
216
286
  next += 1;
217
287
  return { side, p: test?.p ?? 1, adjusted: test?.adjusted ?? 1, reject: test?.reject ?? false };
218
288
  });
219
- // The best p of a side is the p with each read on that side. Above the first step of Holm, no count can reject the side.
220
- const best = partialConjunction(jobs.map((job) => binomialTest(job.reads, job.reads, share, "greater")), need);
221
289
  const reads = jobs.reduce((sum, job) => sum + job.reads, 0);
222
290
  const high = jobs.reduce((sum, job) => sum + job.high, 0);
223
291
  const low = jobs.reduce((sum, job) => sum + job.low, 0);
@@ -227,13 +295,9 @@ export const lawGate = (cases: readonly GateCase[], options: LawGateOptions = {}
227
295
  const what = rejected.side === "high" ? "The measure is above its law" : "The measure is below its law: the law is out of date, and a new fit must replace it";
228
296
  return { ...base, outcome: "beyond", reason: `${what}. ${counts}. The adjusted p is ${short(rejected.adjusted)}, below ${alpha}.`, sides };
229
297
  }
230
- if (best > alpha / tests) {
231
- const reason = `The reads are too few to reject a side: with each read on one side, the p is ${short(best)}, above ${short(alpha / tests)}.`;
232
- return { ...base, outcome: "cannot decide", reason, sides };
233
- }
234
298
  return { ...base, outcome: "within", reason: `${counts}, as the chance ${short(share)} of each side gives.`, sides };
235
299
  });
236
- return { ...gateOf(outcomes), level, alpha, need, share, cases: outcomes };
300
+ return { ...gateOf(outcomes), level, alpha, need, share, family, cases: outcomes };
237
301
  };
238
302
 
239
303
  /** This count is the most reads in one job that `gateReadsFor` examines. */
@@ -261,8 +325,7 @@ export const gateReadsFor = (jobs: number, laws: number, options: LawGateOptions
261
325
  if (!Number.isInteger(jobs) || jobs < need) throw new RangeError(`The count of jobs ${jobs} is not a whole number of ${need} or more.`);
262
326
  const share = (1 - level) / 2;
263
327
  for (let reads = 1; reads <= MAX_GATE_READS; reads += 1) {
264
- const best = partialConjunction(Array.from({ length: jobs }, () => binomialTest(reads, reads, share, "greater")), need);
265
- if (best <= alpha / (2 * laws)) return reads;
328
+ if (bestOf(Array.from({ length: jobs }, () => reads), share, need) <= alpha / (2 * laws)) return reads;
266
329
  }
267
330
  throw new RangeError(`At ${jobs} jobs and ${laws} laws, ${MAX_GATE_READS} reads in each job cannot decide a case.`);
268
331
  };
package/src/index.ts CHANGED
@@ -61,7 +61,7 @@ export { constantIntervals, lawValue } from "./value.ts";
61
61
  export type { Crossing, Floor, Knee, KneeOptions, KneeRule, OpenRange, OpenValue, Reach } from "./knee.ts";
62
62
  export { KNEE_STEPS, kneeRuleProblem, lawKnee, lawReach } from "./knee.ts";
63
63
  export type { GateCase, GateJob, GateRead, JobCount, LawGate, LawGateCase, LawGateOptions, LawRead, NoReadJob, ReadSide, SideTest } from "./gate.ts";
64
- export { GATE_ALPHA, GATE_JOBS, gateReadsFor, lawGate, lawRead } from "./gate.ts";
64
+ export { GATE_ALPHA, GATE_JOBS, gateReadsFor, lawGate, lawRead, MAX_MORE_JOBS } from "./gate.ts";
65
65
  export type {
66
66
  Amounts,
67
67
  Demand,