@mark1russell7/rigor-statistics 0.1.29 → 0.2.0

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/README.md CHANGED
@@ -171,7 +171,9 @@ choosePair(pool, { seed: "duel-1" }); // the next pair of a duel, th
171
171
 
172
172
  `bradleyTerrySparse` has the model, the prior and the tie models of `bradleyTerry`. Its memory is in proportion to the items plus the distinct pairs. At 10,000 items, the Hessian of `bradleyTerry` has 800 MB. The sparse fit uses a few MB.
173
173
 
174
- - The fit uses MM steps. The first part of a step minimizes a quadratic bound of the strengths (Böhning and Lindsay, 1988). The second part moves each connected group to the prior mean. The third part uses the tie bound of Hunter (2004).
174
+ - The fit uses Newton steps, as `bradleyTerry` does. Preconditioned conjugate gradients solve each Newton system, so the fit does not factor the Hessian. One product with the Hessian reads each pair one time.
175
+ - The solve is loose far from the minimum and tight near it (an inexact Newton method). A line search with the Armijo rule makes the objective smaller at each step.
176
+ - The option `start` of both fits sets the start point. A start from the fit before the last comparisons saves steps, and the optimum does not change.
175
177
  - The objective is strongly convex. Thus the norm of the gradient over the smallest prior precision is a bound of the distance from the exact optimum. The fit stops when `bound` is `tolerance` (1e-8) or less.
176
178
  - A property test compares the two fits on small logs. The strengths agree to 1e-7, and the distance is at most `bound`.
177
179
  - The standard errors are approximate. The variance of an item is `1 / (n * p) + (1 - 1 / n) / h`. Here `n` is the number of items of its connected group, `p` is the prior precision and `h` is the diagonal entry of the Hessian.
package/build/index.d.ts CHANGED
@@ -1613,6 +1613,19 @@ interface BradleyTerryOptions {
1613
1613
  readonly maxIterations?: number;
1614
1614
  /** The fit stops when the full Newton step moves no parameter more than this value. The default is 1e-10. */
1615
1615
  readonly tolerance?: number;
1616
+ /**
1617
+ * The point where the fit starts. The default is the prior mean for each log-strength, and 1 for the tie parameter.
1618
+ * A start near the optimum saves steps. An example is the fit of the log before its last comparisons. The optimum
1619
+ * does not change.
1620
+ */
1621
+ readonly start?: BradleyTerryStart;
1622
+ }
1623
+ /** The start of a Bradley-Terry fit: a log-strength for each item, and the Davidson tie parameter. */
1624
+ interface BradleyTerryStart {
1625
+ /** The log-strength of each item, in the order of the indexes. Each value is a finite number. */
1626
+ readonly strengths: readonly number[];
1627
+ /** The Davidson tie parameter, a finite number above 0. The default is 1. A fit with no Davidson tie does not use it. */
1628
+ readonly tie?: number;
1616
1629
  }
1617
1630
  /** The result of `bradleyTerry`. The lists have one value for each item, in the order of the indexes. */
1618
1631
  interface BradleyTerryFit {
@@ -1640,7 +1653,7 @@ interface BradleyTerryFit {
1640
1653
  export declare function bradleyTerry(items: number, comparisons: readonly PairComparison[], options?: BradleyTerryOptions): BradleyTerryFit;
1641
1654
  /** The options of `bradleyTerrySparse`. The prior and the tie model are the same as in `bradleyTerry`. */
1642
1655
  interface BradleyTerrySparseOptions extends BradleyTerryOptions {
1643
- /** The largest number of MM steps. The default is 10000. */
1656
+ /** The largest number of Newton steps. The default is 100. */
1644
1657
  readonly maxIterations?: number;
1645
1658
  /**
1646
1659
  * The largest distance of the parameters from the exact optimum at the stop. The default is 1e-8. The distance is the
@@ -1657,7 +1670,7 @@ interface BradleyTerrySparseFit extends BradleyTerryFit {
1657
1670
  * has the prior SD, as in `bradleyTerry`.
1658
1671
  */
1659
1672
  readonly se: number[];
1660
- /** The number of MM steps. */
1673
+ /** The number of Newton steps. */
1661
1674
  readonly iterations: number;
1662
1675
  /**
1663
1676
  * True when `bound` is the tolerance or less, or when the gradient is at the rounding level of floating point. At the
@@ -1825,5 +1838,5 @@ interface PairChoice {
1825
1838
  * function gives `undefined` when there is no permitted pair. Each step has its own stream of the seed.
1826
1839
  */
1827
1840
  export declare function choosePair<T extends RatedItem>(items: readonly T[], options: PairChoiceOptions<T>): PairChoice | undefined;
1828
- export { type BinomialSide, type BootstrapOptions, type BradleyTerryFit, type BradleyTerryOptions, type BradleyTerrySparseFit, type BradleyTerrySparseOptions, type CalibrationLevel, type CategoricalDimension, type CompareOptions, type Comparison, type ContinuousDimension, type Corrected, type CountInterval, type CoverageBar, type CoverageCell, type CoverageMinSample, type CoverageReason, type CoverageStep, type Effect, type EloOptions, type EloReplayOptions, type EloUpdate, type Forecast, type Glicko2Game, type Glicko2Options, type Glicko2Rating, type Glicko2ReplayOptions, type Interval, type Kappa, type LeastSquaresFit, type LeastSquaresOptions, type MedianInterval, type ModeOptions, type NoiseOptions, type NoiseState, type NoiseVerdict, type OptionFloorOptions, type OrdinalDimension, type PairChoice, type PairChoiceOptions, type PairComparison, type PairOutcome, type PairProbabilities, type ParsedResponseRecord, type PermutationOptions, type PermutationResult, type Proportion, type QuantileInterval, type QuantileMethod, type RatedItem, type RatingFit, type RatingFitOptions, type RatingModel, type RatingModelChoice, type RatingModelOptions, type RatingModelScore, type RatingScale, type ResponseAbsentPoint, type ResponseBand, type ResponseCurvePoint, type ResponseData, type ResponseDimension, type ResponseDimensionBase, type ResponseFactor, type ResponseFault, type ResponseFitOptions, type ResponseFold, type ResponseFoldError, type ResponseHeldOutError, type ResponseInputValue, type ResponseMeasure, type ResponseModel, type ResponseModelBase, type ResponsePoint, type ResponseReason, type ResponseRecord, type ResponseSlice, type ResponseSliceOptions, type ResponseSlicePoint, type ResponseSurface, type ResponseSurfaceAbsentPoint, type ResponseSurfaceOptions, type ResponseSurfacePoint, type ResponseTableCell, type ResponseValue, type ResponseViewOptions, type ResponseWhen, type RidgeInput, type RidgeInputBase, type RidgeLevelInput, type RidgeNumberInput, type RidgeOptions, type RidgeRankInput, type RidgeResponseModel, type RidgeTerm, type RunningSummary, type SampleTotalOptions, type SideP, type SignFlipOptions, type SignFlipResult, type SmoothLaw, type SmoothLawScore, type SobolIndex, type SobolIndices, type SobolOptions, type Summary, type TableResponseModel, type TieModel, type TwoSamplePermutationOptions, type TwoSamplePermutationResult, type UStatisticInterval, type UStatisticMean, type UStatisticMethod, type UStatisticOptions, normalCdf, normalQuantile };
1841
+ export { type BinomialSide, type BootstrapOptions, type BradleyTerryFit, type BradleyTerryOptions, type BradleyTerrySparseFit, type BradleyTerrySparseOptions, type BradleyTerryStart, type CalibrationLevel, type CategoricalDimension, type CompareOptions, type Comparison, type ContinuousDimension, type Corrected, type CountInterval, type CoverageBar, type CoverageCell, type CoverageMinSample, type CoverageReason, type CoverageStep, type Effect, type EloOptions, type EloReplayOptions, type EloUpdate, type Forecast, type Glicko2Game, type Glicko2Options, type Glicko2Rating, type Glicko2ReplayOptions, type Interval, type Kappa, type LeastSquaresFit, type LeastSquaresOptions, type MedianInterval, type ModeOptions, type NoiseOptions, type NoiseState, type NoiseVerdict, type OptionFloorOptions, type OrdinalDimension, type PairChoice, type PairChoiceOptions, type PairComparison, type PairOutcome, type PairProbabilities, type ParsedResponseRecord, type PermutationOptions, type PermutationResult, type Proportion, type QuantileInterval, type QuantileMethod, type RatedItem, type RatingFit, type RatingFitOptions, type RatingModel, type RatingModelChoice, type RatingModelOptions, type RatingModelScore, type RatingScale, type ResponseAbsentPoint, type ResponseBand, type ResponseCurvePoint, type ResponseData, type ResponseDimension, type ResponseDimensionBase, type ResponseFactor, type ResponseFault, type ResponseFitOptions, type ResponseFold, type ResponseFoldError, type ResponseHeldOutError, type ResponseInputValue, type ResponseMeasure, type ResponseModel, type ResponseModelBase, type ResponsePoint, type ResponseReason, type ResponseRecord, type ResponseSlice, type ResponseSliceOptions, type ResponseSlicePoint, type ResponseSurface, type ResponseSurfaceAbsentPoint, type ResponseSurfaceOptions, type ResponseSurfacePoint, type ResponseTableCell, type ResponseValue, type ResponseViewOptions, type ResponseWhen, type RidgeInput, type RidgeInputBase, type RidgeLevelInput, type RidgeNumberInput, type RidgeOptions, type RidgeRankInput, type RidgeResponseModel, type RidgeTerm, type RunningSummary, type SampleTotalOptions, type SideP, type SignFlipOptions, type SignFlipResult, type SmoothLaw, type SmoothLawScore, type SobolIndex, type SobolIndices, type SobolOptions, type Summary, type TableResponseModel, type TieModel, type TwoSamplePermutationOptions, type TwoSamplePermutationResult, type UStatisticInterval, type UStatisticMean, type UStatisticMethod, type UStatisticOptions, normalCdf, normalQuantile };
1829
1842
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/describe.ts","../src/running.ts","../src/regression.ts","../src/bootstrap.ts","../src/distribution.ts","../src/ridge.ts","../src/response.ts","../src/response-parse.ts","../src/response-fit.ts","../src/response-sobol.ts","../src/tests.ts","../src/agreement.ts","../src/rank.ts","../src/discrete.ts","../src/compare.ts","../src/power.ts","../src/coverage.ts","../src/u-statistic.ts","../src/rating.ts","../src/bradley-terry.ts","../src/bradley-terry-sparse.ts","../src/rating-models.ts","../src/pair-choice.ts"],"mappings":";;;;;;;KAOY;;wBAmBI,OAAO;wBAIP,KAAK;;;;;wBAgBL,GAAG;;;;;;;wBAgBH,eAAe,iCAAiC,WAAW,SAAQ;;wBAkBnE,SAAS,2BAA2B,WAAW,SAAQ;;;;;wBASvD,OAAO;;UAQN;WACN;WACA;;WAEA;WACA;;WAEA;WACA;;WAEA;WACA;;WAEA;;wBAGK,UAAU,4BAA4B;;;;;;;;;;UCvGrC;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;;qBAIE,eAAe;;;;;wBAMZ,aAAa,GAAG,gBAAgB,gBAAgB;;wBAWhD,UAAU,QAAQ,mBAAmB;;;;;wBAUrC,aAAa,GAAG,gBAAgB,GAAG,iBAAiB;;wBAepD,YAAY,GAAG;;;;;wBASf,UAAU,GAAG;;;;;;UCrEZ;;WAEN;;UAGM;;WAEN;;WAEA;;;;;WAKA;;WAEA;;WAEA;;;;;;;;;;;wBAkBK,aAAa,mCAAmC,sBAAsB,UAAS,sBAA2B;UC7BzG;;WAEN;;WAEA;;WAEA;;WAEA,SAAS;;;;;;WAMT;;;;;WAKA;;;UAIM;;WAEN;;WAEA;;WAEA;;UAGM,iBAAiB,QAAQ;;WAE/B;WACA;WACA;WACA;WACA;;WAEA;;WAEA;;;;;;wBAOK,UAAU,GAAG,eAAe,KAAK,YAAY,iBAAiB,gBAAgB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;;;UAIM,2BAA2B;;WAEjC;;WAEA;;WAEA,kBAAkB;;;;;;;wBAWb,kBAAkB,eAAe,sBAAsB,kBAAkB,UAAS,qBAA0B;;;;;qBC3K/G;;qBAGA;;UAGI;;WAEN;;WAEA;;WAEA;;WAEA;;;wBAIK,kBAAkB,MAAM,KAAK;;;;;;wBAS7B,gBAAgB,GAAG,WAAW,GAAG,WAAW,UAAS;;UAMpD,kBAAkB,UAAU,YAAY;;WAE9C;;WAEA;;WAEA;;;;;WAKA,eAAe,GAAG,GAAG,GAAG;;;UAIlB;WACN;WACA;;WAEA;;;;;;;;;;;;;;;wBAqCK,WAAW,UAAU,WAAW,gBAAgB,KAAK,SAAS,kBAAkB,KAAK"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/describe.ts","../src/running.ts","../src/regression.ts","../src/bootstrap.ts","../src/distribution.ts","../src/ridge.ts","../src/response.ts","../src/response-parse.ts","../src/response-fit.ts","../src/response-sobol.ts","../src/tests.ts","../src/agreement.ts","../src/rank.ts","../src/discrete.ts","../src/compare.ts","../src/power.ts","../src/coverage.ts","../src/u-statistic.ts","../src/rating.ts","../src/bradley-terry.ts","../src/bradley-terry-sparse.ts","../src/rating-models.ts","../src/pair-choice.ts"],"mappings":";;;;;;;KAOY;;wBAmBI,OAAO;wBAIP,KAAK;;;;;wBAgBL,GAAG;;;;;;;wBAgBH,eAAe,iCAAiC,WAAW,SAAQ;;wBAkBnE,SAAS,2BAA2B,WAAW,SAAQ;;;;;wBASvD,OAAO;;UAQN;WACN;WACA;;WAEA;WACA;;WAEA;WACA;;WAEA;WACA;;WAEA;;wBAGK,UAAU,4BAA4B;;;;;;;;;;UCvGrC;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;;qBAIE,eAAe;;;;;wBAMZ,aAAa,GAAG,gBAAgB,gBAAgB;;wBAWhD,UAAU,QAAQ,mBAAmB;;;;;wBAUrC,aAAa,GAAG,gBAAgB,GAAG,iBAAiB;;wBAepD,YAAY,GAA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vXnH,yBAAyB;;WAE/B;;;UAIM,6BAA6B;;WAEnC,QAAQ;;;;;;wBA2BH,UAAU,eAAe,sBAAsB,kBAAkB,UAAS;;;;;;wBAoB1E,cAAc,eAAe,sBAAsB,kBAAkB,UAAS,uBAA4B;;KAgB9G;;UAGK;;WAEN,eAAe;;WAEf;;WAEA,MAAM;;WAEN,UAAU;;;UAIJ;;WAEN;;;;;WAKA;;WAEA;;;qBAIE;;;;;;wBAOG,WAAW,OAAO,aAAa,eAAe,sBAAsB,kBAAkB,UAAS,mBAAwB;;UA6BtH;WACN,OAAO;;WAEP;;;UAIM;;WAEN,OAAO;;WAEP,QAAQ;;WAER;;WAEA;;;UAIM,2BAA2B;;WAEjC;;WAEA;;WAEA,kBAAkB;;;;;;;wBAWb,kBAAkB,eAAe,sBAAsB,kBAAkB,UAAS,qBAA0B;;;;;qBC3K/G;;qBAGA;;UAGI;;WAEN;;WAEA;;WAEA;;WAEA;;;wBAIK,kBAAkB,MAAM,KAAK;;;;;;wBAS7B,gBAAgB,GAAG,WAAW,GAAG,WAAW,UAAS;;UAMpD,kBAAkB,UAAU,YAAY;;WAE9C;;WAEA;;WAEA;;;;;WAKA,eAAe,GAAG,GAAG,GAAG;;;UAIlB;WACN;WACA;;WAEA;;;;;;;;;;;;;;;wBAqCK,WAAW,UAAU,WAAW,gBAAgB,KAAK,SAAS,kBAAkB,KAAK"}
package/build/index.js CHANGED
@@ -3842,8 +3842,28 @@ function ratingToStrength(rating, options = {}) {
3842
3842
  * Ties use the Davidson (1970) model, or count as half a win and half a loss. The fit uses Newton steps with a line search on the negative log posterior. This function is
3843
3843
  * convex, so the steps go to its one minimum.
3844
3844
  */
3845
- const ARMIJO = 1e-4;
3846
- const HALVINGS = 60;
3845
+ /**
3846
+ * This function gives the start of a fit: the log-strengths, and the log of the tie parameter. It throws a RangeError
3847
+ * for a start with the wrong number of items. It also throws for a log-strength that is not finite, or a tie parameter
3848
+ * of 0 or less.
3849
+ */
3850
+ function startOf(name, items, priorMean, start) {
3851
+ if (start === void 0) return {
3852
+ strengths: new Float64Array(items).fill(priorMean),
3853
+ logTie: 0
3854
+ };
3855
+ if (start.strengths.length !== items) throw new RangeError(`${name}: the start has ${start.strengths.length} log-strengths, and the fit has ${items} items.`);
3856
+ const strengths = Float64Array.from(start.strengths);
3857
+ for (const [i, value] of strengths.entries()) if (!Number.isFinite(value)) throw new RangeError(`${name}: the start log-strength of item ${i} is a finite number, not ${start.strengths[i]}.`);
3858
+ const tie = start.tie ?? 1;
3859
+ if (!(tie > 0 && Number.isFinite(tie))) throw new RangeError(`${name}: the start tie parameter is a finite number above 0, not ${tie}.`);
3860
+ return {
3861
+ strengths,
3862
+ logTie: Math.log(tie)
3863
+ };
3864
+ }
3865
+ const ARMIJO$1 = 1e-4;
3866
+ const HALVINGS$1 = 60;
3847
3867
  const ROUNDING$1 = 64 * Number.EPSILON;
3848
3868
  function checkedTerms(n, comparisons, ties) {
3849
3869
  const terms = [];
@@ -3888,6 +3908,7 @@ function bradleyTerry(items, comparisons, options = {}) {
3888
3908
  positiveOption$1("the tolerance", tolerance);
3889
3909
  if (ties !== "davidson" && ties !== "half") throw new RangeError(`bradleyTerry: the tie model is "davidson" or "half", not ${String(ties)}.`);
3890
3910
  if (!Number.isSafeInteger(maxIterations) || maxIterations < 1) throw new RangeError(`bradleyTerry: maxIterations is a whole number of 1 or more, not ${maxIterations}.`);
3911
+ const begin = startOf("bradleyTerry", items, priorMean, options.start);
3891
3912
  const terms = checkedTerms(items, comparisons, ties);
3892
3913
  const fitTie = terms.some((t) => t.tie === 1);
3893
3914
  const k = fitTie ? items + 1 : items;
@@ -3899,8 +3920,9 @@ function bradleyTerry(items, comparisons, options = {}) {
3899
3920
  priorPrecision: 1 / (priorSd * priorSd),
3900
3921
  tiePrecision: 1 / (tiePriorSd * tiePriorSd)
3901
3922
  };
3902
- const x = new Float64Array(k).fill(priorMean);
3903
- if (fitTie) x[items] = 0;
3923
+ const x = new Float64Array(k);
3924
+ x.set(begin.strengths);
3925
+ if (fitTie) x[items] = begin.logTie;
3904
3926
  let iterations = 0;
3905
3927
  let converged = false;
3906
3928
  let value = objective(model, x);
@@ -3914,7 +3936,7 @@ function bradleyTerry(items, comparisons, options = {}) {
3914
3936
  let next = moved(x, step, t);
3915
3937
  let nextValue = objective(model, next);
3916
3938
  const slack = ROUNDING$1 * (Math.abs(value) + 1);
3917
- for (let h = 0; h < HALVINGS && !(nextValue <= value + ARMIJO * t * slope + slack); h++) {
3939
+ for (let h = 0; h < HALVINGS$1 && !(nextValue <= value + ARMIJO$1 * t * slope + slack); h++) {
3918
3940
  t /= 2;
3919
3941
  next = moved(x, step, t);
3920
3942
  nextValue = objective(model, next);
@@ -4057,10 +4079,30 @@ function inverseDiagonal(hessian, k) {
4057
4079
  return out;
4058
4080
  }
4059
4081
  // src/bradley-terry-sparse.ts
4082
+ /**
4083
+ * The sparse Bradley-Terry fit. It has the model, the prior and the tie model of `bradleyTerry`, so the two fits have
4084
+ * one optimum. This fit keeps one record for each distinct pair of items, not a matrix of all the items. Thus its
4085
+ * memory is in proportion to the items and the pairs. The dense fit of 10,000 items keeps a Hessian of 800 MB.
4086
+ *
4087
+ * The fit uses Newton steps on the negative log posterior, as `bradleyTerry` does. It does not factor the Hessian.
4088
+ * Preconditioned conjugate gradients solve each Newton system, and the diagonal of the Hessian is the preconditioner.
4089
+ * One product of the Hessian and a vector reads each pair one time.
4090
+ *
4091
+ * The prior makes the objective strongly convex, so the Hessian is positive definite. Thus each step of the solve
4092
+ * gives a direction that goes down. The solve is loose far from the minimum and tight near it (an inexact Newton
4093
+ * method). A line search with the Armijo rule makes the objective smaller at each step. The steps go to the one
4094
+ * minimum, and the fit stops when the gradient puts each parameter inside the tolerance of that minimum.
4095
+ */
4060
4096
  /** The largest number of items. The key of a pair is `low * items + high`, and it must be a safe integer. */
4061
4097
  const MAX_ITEMS = Math.floor(Math.sqrt(Number.MAX_SAFE_INTEGER));
4062
4098
  const ROUNDING = 64 * Number.EPSILON;
4063
4099
  const NAME = "bradleyTerrySparse";
4100
+ /** The Armijo constant of the line search: a step gives at least this part of the decrease that the slope forecasts. */
4101
+ const ARMIJO = 1e-4;
4102
+ /** The largest number of halvings of a step in the line search. */
4103
+ const HALVINGS = 60;
4104
+ /** The largest number of conjugate-gradient steps for one Newton system. */
4105
+ const SOLVE_LIMIT = 500;
4064
4106
  function positiveOption(label, value) {
4065
4107
  if (!(value > 0 && Number.isFinite(value))) throw new RangeError(`${NAME}: ${label} is a finite number above 0, not ${value}.`);
4066
4108
  }
@@ -4161,22 +4203,149 @@ function outcomes(d, logNu, out) {
4161
4203
  out[2] = et / sum;
4162
4204
  out[3] = top + Math.log(sum);
4163
4205
  }
4206
+ /** This function makes an empty point for `items` items and `count` pairs. */
4207
+ function pointOf(items, count) {
4208
+ return {
4209
+ s: new Float64Array(items),
4210
+ lambda: 0,
4211
+ value: 0,
4212
+ logLikelihood: 0,
4213
+ gradient: new Float64Array(items),
4214
+ tieGradient: 0,
4215
+ norm: 0,
4216
+ curvature: new Float64Array(count),
4217
+ diagonal: new Float64Array(items),
4218
+ cross: new Float64Array(items),
4219
+ tieCurvature: 0
4220
+ };
4221
+ }
4222
+ /** This function writes the objective, the gradient and the Hessian at the parameters of `point` into `point`. */
4223
+ function evaluate(problem, point, out) {
4224
+ const { items, pairs, fitTie, priorMean, p, tau, tieWeight } = problem;
4225
+ const { s, gradient, curvature, diagonal, cross, lambda } = point;
4226
+ const logNu = fitTie ? lambda : Number.NEGATIVE_INFINITY;
4227
+ let prior = 0;
4228
+ for (let i = 0; i < items; i++) {
4229
+ const offset = s[i] - priorMean;
4230
+ prior += offset * offset;
4231
+ gradient[i] = p * offset;
4232
+ diagonal[i] = p;
4233
+ cross[i] = 0;
4234
+ }
4235
+ let logLikelihood = fitTie ? tieWeight * lambda : 0;
4236
+ let tieGradient = fitTie ? tau * lambda - tieWeight : 0;
4237
+ let tieCurvature = fitTie ? tau : 0;
4238
+ for (let q = 0; q < pairs.count; q++) {
4239
+ const lo = pairs.low[q];
4240
+ const hi = pairs.high[q];
4241
+ const total = pairs.total[q];
4242
+ const net = pairs.net[q];
4243
+ const d = s[lo] - s[hi];
4244
+ outcomes(d, logNu, out);
4245
+ const pa = out[0];
4246
+ const pb = out[1];
4247
+ const pt = out[2];
4248
+ const half = (pa - pb) / 2;
4249
+ logLikelihood += net / 2 * d - total * out[3];
4250
+ const g = total * half - net / 2;
4251
+ gradient[lo] = gradient[lo] + g;
4252
+ gradient[hi] = gradient[hi] - g;
4253
+ const c = total * (pa * pb + pt * (1 - pt) / 4);
4254
+ curvature[q] = c;
4255
+ diagonal[lo] = diagonal[lo] + c;
4256
+ diagonal[hi] = diagonal[hi] + c;
4257
+ if (fitTie) {
4258
+ tieGradient += total * pt;
4259
+ tieCurvature += total * pt * (1 - pt);
4260
+ const b = -total * pt * half;
4261
+ cross[lo] = cross[lo] + b;
4262
+ cross[hi] = cross[hi] - b;
4263
+ }
4264
+ }
4265
+ let norm = tieGradient * tieGradient;
4266
+ for (let i = 0; i < items; i++) norm += gradient[i] ** 2;
4267
+ point.value = p * prior / 2 + (fitTie ? tau * lambda * lambda / 2 : 0) - logLikelihood;
4268
+ point.logLikelihood = logLikelihood;
4269
+ point.tieGradient = tieGradient;
4270
+ point.tieCurvature = tieCurvature;
4271
+ point.norm = Math.sqrt(norm);
4272
+ }
4164
4273
  /**
4165
- * The minimum over `z` of `s * exp(z) + tau * (lambda + z) ^ 2 / 2 - t * z`: the root of
4166
- * `s * exp(z) + tau * z = t - tau * lambda`. The left side increases and is convex. Newton steps from a point right of
4167
- * the root thus go down to the root, and they do not go past it.
4274
+ * This function writes the product of the Hessian at `point` and the vector (`v`, `vTie`) into `out`. It gives the
4275
+ * tie part of the product. The product reads each pair one time, and it makes no arrays.
4168
4276
  */
4169
- function tieStep(s, t, tau, lambda) {
4170
- const c = t - tau * lambda;
4171
- if (s === 0) return c / tau;
4172
- let z = c > 0 ? Math.max(0, Math.log(c / s)) : 0;
4173
- for (let step = 0; step < 200; step++) {
4174
- const ez = s * Math.exp(z);
4175
- const move = (ez + tau * z - c) / (ez + tau);
4176
- z -= move;
4177
- if (!(Math.abs(move) > ROUNDING * (1 + Math.abs(z)))) break;
4277
+ function multiply(problem, point, v, vTie, out) {
4278
+ const { items, pairs, fitTie, p } = problem;
4279
+ const { curvature, cross } = point;
4280
+ for (let i = 0; i < items; i++) out[i] = p * v[i] + cross[i] * vTie;
4281
+ for (let q = 0; q < pairs.count; q++) {
4282
+ const lo = pairs.low[q];
4283
+ const hi = pairs.high[q];
4284
+ const t = curvature[q] * (v[lo] - v[hi]);
4285
+ out[lo] = out[lo] + t;
4286
+ out[hi] = out[hi] - t;
4178
4287
  }
4179
- return z;
4288
+ if (!fitTie) return 0;
4289
+ let tie = point.tieCurvature * vTie;
4290
+ for (let i = 0; i < items; i++) tie += cross[i] * v[i];
4291
+ return tie;
4292
+ }
4293
+ /**
4294
+ * This function solves the Newton system `H x = -g` at `point` with preconditioned conjugate gradients. It writes the
4295
+ * log-strength part of `x` into `step`, and it gives the tie part. The solve stops when the norm of the residual is
4296
+ * `forcing` times the norm of the gradient or less. The solve starts at 0, so each step of it gives a descent direction.
4297
+ */
4298
+ function newtonStep(problem, point, forcing, step, work) {
4299
+ const { items, fitTie } = problem;
4300
+ const { residual: r, scaled: z, direction: d, product: ad } = work;
4301
+ const { gradient, diagonal } = point;
4302
+ let rz = 0;
4303
+ for (let i = 0; i < items; i++) {
4304
+ const ri = -gradient[i];
4305
+ const zi = ri / diagonal[i];
4306
+ r[i] = ri;
4307
+ z[i] = zi;
4308
+ d[i] = zi;
4309
+ rz += ri * zi;
4310
+ }
4311
+ let rTie = fitTie ? -point.tieGradient : 0;
4312
+ let dTie = fitTie ? rTie / point.tieCurvature : 0;
4313
+ rz += rTie * dTie;
4314
+ step.fill(0);
4315
+ let stepTie = 0;
4316
+ const target = forcing * point.norm;
4317
+ const limit = Math.min(SOLVE_LIMIT, items + 1);
4318
+ for (let k = 0; k < limit; k++) {
4319
+ const adTie = multiply(problem, point, d, dTie, ad);
4320
+ let dad = dTie * adTie;
4321
+ for (let i = 0; i < items; i++) dad += d[i] * ad[i];
4322
+ if (!(dad > 0)) break;
4323
+ const alpha = rz / dad;
4324
+ let residual = 0;
4325
+ for (let i = 0; i < items; i++) {
4326
+ step[i] = step[i] + alpha * d[i];
4327
+ const ri = r[i] - alpha * ad[i];
4328
+ r[i] = ri;
4329
+ residual += ri * ri;
4330
+ }
4331
+ stepTie += alpha * dTie;
4332
+ rTie -= alpha * adTie;
4333
+ residual += rTie * rTie;
4334
+ if (Math.sqrt(residual) <= target) break;
4335
+ let next = 0;
4336
+ for (let i = 0; i < items; i++) {
4337
+ const zi = r[i] / diagonal[i];
4338
+ z[i] = zi;
4339
+ next += r[i] * zi;
4340
+ }
4341
+ const zTie = fitTie ? rTie / point.tieCurvature : 0;
4342
+ next += rTie * zTie;
4343
+ const beta = next / rz;
4344
+ rz = next;
4345
+ for (let i = 0; i < items; i++) d[i] = z[i] + beta * d[i];
4346
+ dTie = zTie + beta * dTie;
4347
+ }
4348
+ return stepTie;
4180
4349
  }
4181
4350
  /**
4182
4351
  * The sparse Bradley-Terry fit of `comparisons` among `items` items. It has the model, the options and the checks of
@@ -4189,7 +4358,7 @@ function bradleyTerrySparse(items, comparisons, options = {}) {
4189
4358
  const priorSd = options.priorSd ?? 1;
4190
4359
  const ties = options.ties ?? "davidson";
4191
4360
  const tiePriorSd = options.tiePriorSd ?? 10;
4192
- const maxIterations = options.maxIterations ?? 1e4;
4361
+ const maxIterations = options.maxIterations ?? 100;
4193
4362
  const tolerance = options.tolerance ?? 1e-8;
4194
4363
  if (!Number.isFinite(priorMean)) throw new RangeError(`${NAME}: the prior mean is a finite number, not ${priorMean}.`);
4195
4364
  positiveOption("the prior SD", priorSd);
@@ -4197,88 +4366,74 @@ function bradleyTerrySparse(items, comparisons, options = {}) {
4197
4366
  positiveOption("the tolerance", tolerance);
4198
4367
  if (ties !== "davidson" && ties !== "half") throw new RangeError(`${NAME}: the tie model is "davidson" or "half", not ${String(ties)}.`);
4199
4368
  if (!Number.isSafeInteger(maxIterations) || maxIterations < 1) throw new RangeError(`${NAME}: maxIterations is a whole number of 1 or more, not ${maxIterations}.`);
4369
+ const begin = startOf(NAME, items, priorMean, options.start);
4200
4370
  const pairs = pairsOf(items, comparisons, ties);
4201
- const { group, size, count: groupCount } = groupsOf(items, pairs);
4202
- const p = 1 / (priorSd * priorSd);
4203
- const tau = 1 / (tiePriorSd * tiePriorSd);
4204
4371
  let tieWeight = 0;
4205
4372
  let totalWeight = 0;
4206
- const weight = new Float64Array(items);
4207
4373
  for (let q = 0; q < pairs.count; q++) {
4208
4374
  tieWeight += pairs.ties[q];
4209
4375
  totalWeight += pairs.total[q];
4210
- weight[pairs.low[q]] = weight[pairs.low[q]] + pairs.total[q];
4211
- weight[pairs.high[q]] = weight[pairs.high[q]] + pairs.total[q];
4212
4376
  }
4213
- const fitTie = tieWeight > 0;
4214
- const smallest = fitTie ? Math.min(p, tau) : p;
4377
+ const p = 1 / (priorSd * priorSd);
4378
+ const tau = 1 / (tiePriorSd * tiePriorSd);
4379
+ const problem = {
4380
+ items,
4381
+ pairs,
4382
+ fitTie: tieWeight > 0,
4383
+ priorMean,
4384
+ p,
4385
+ tau,
4386
+ tieWeight
4387
+ };
4388
+ const smallest = problem.fitTie ? Math.min(p, tau) : p;
4215
4389
  const floor = 4 * Number.EPSILON * (totalWeight + 1);
4216
- const s = new Float64Array(items).fill(priorMean);
4217
- const gradient = new Float64Array(items);
4218
- const sums = new Float64Array(groupCount);
4390
+ let current = pointOf(items, pairs.count);
4391
+ let trial = pointOf(items, pairs.count);
4392
+ current.s.set(begin.strengths);
4393
+ current.lambda = problem.fitTie ? begin.logTie : 0;
4219
4394
  const out = /* @__PURE__ */ new Float64Array(4);
4220
- let lambda = 0;
4395
+ const step = new Float64Array(items);
4396
+ const work = {
4397
+ residual: new Float64Array(items),
4398
+ scaled: new Float64Array(items),
4399
+ direction: new Float64Array(items),
4400
+ product: new Float64Array(items)
4401
+ };
4402
+ evaluate(problem, current, out);
4221
4403
  let iterations = 0;
4222
- let bound = Number.POSITIVE_INFINITY;
4223
- let rounded = false;
4224
- for (;;) {
4225
- const logNu = fitTie ? lambda : Number.NEGATIVE_INFINITY;
4226
- for (let i = 0; i < items; i++) gradient[i] = p * (s[i] - priorMean);
4227
- let tieGradient = fitTie ? tau * lambda - tieWeight : 0;
4228
- for (let q = 0; q < pairs.count; q++) {
4229
- const lo = pairs.low[q];
4230
- const hi = pairs.high[q];
4231
- const total = pairs.total[q];
4232
- outcomes(s[lo] - s[hi], logNu, out);
4233
- const g = total * ((out[0] - out[1]) / 2) - pairs.net[q] / 2;
4234
- gradient[lo] = gradient[lo] + g;
4235
- gradient[hi] = gradient[hi] - g;
4236
- if (fitTie) tieGradient += total * out[2];
4237
- }
4238
- let norm = tieGradient * tieGradient;
4239
- for (let i = 0; i < items; i++) norm += gradient[i] ** 2;
4240
- norm = Math.sqrt(norm);
4241
- bound = norm / smallest;
4242
- rounded = norm <= floor;
4243
- if (bound <= tolerance || rounded || iterations >= maxIterations) break;
4404
+ let bound = current.norm / smallest;
4405
+ let rounded = current.norm <= floor;
4406
+ while (!(bound <= tolerance || rounded) && iterations < maxIterations) {
4244
4407
  iterations++;
4245
- for (let i = 0; i < items; i++) s[i] = s[i] - gradient[i] / (weight[i] / 2 + p);
4246
- sums.fill(0);
4247
- for (let i = 0; i < items; i++) sums[group[i]] = sums[group[i]] + s[i];
4248
- for (let i = 0; i < items; i++) s[i] = s[i] + priorMean - sums[group[i]] / size[group[i]];
4249
- if (fitTie) {
4250
- let expected = 0;
4251
- for (let q = 0; q < pairs.count; q++) {
4252
- outcomes(s[pairs.low[q]] - s[pairs.high[q]], lambda, out);
4253
- expected += pairs.total[q] * out[2];
4254
- }
4255
- lambda += tieStep(expected, tieWeight, tau, lambda);
4408
+ const forcing = Math.min(.5, Math.sqrt(current.norm));
4409
+ const stepTie = newtonStep(problem, current, forcing, step, work);
4410
+ let slope = stepTie * current.tieGradient;
4411
+ for (let i = 0; i < items; i++) slope += step[i] * current.gradient[i];
4412
+ const slack = ROUNDING * (Math.abs(current.value) + 1);
4413
+ let t = 1;
4414
+ let accepted = false;
4415
+ for (let h = 0; h <= HALVINGS && !accepted; h++) {
4416
+ for (let i = 0; i < items; i++) trial.s[i] = current.s[i] + t * step[i];
4417
+ trial.lambda = current.lambda + t * stepTie;
4418
+ evaluate(problem, trial, out);
4419
+ accepted = trial.value <= current.value + ARMIJO * t * slope + slack;
4420
+ if (!accepted) t /= 2;
4256
4421
  }
4422
+ if (!accepted) break;
4423
+ [current, trial] = [trial, current];
4424
+ bound = current.norm / smallest;
4425
+ rounded = current.norm <= floor;
4257
4426
  }
4258
- const logNu = fitTie ? lambda : Number.NEGATIVE_INFINITY;
4259
- const diagonal = new Float64Array(items).fill(p);
4260
- let logLikelihood = 0;
4261
- for (let q = 0; q < pairs.count; q++) {
4262
- const lo = pairs.low[q];
4263
- const hi = pairs.high[q];
4264
- const total = pairs.total[q];
4265
- const d = s[lo] - s[hi];
4266
- outcomes(d, logNu, out);
4267
- const half = (out[0] - out[1]) / 2;
4268
- const curvature = total * ((out[0] + out[1]) / 4 - half * half);
4269
- diagonal[lo] = diagonal[lo] + curvature;
4270
- diagonal[hi] = diagonal[hi] + curvature;
4271
- logLikelihood += pairs.net[q] / 2 * d - total * out[3] + (fitTie ? pairs.ties[q] * lambda : 0);
4272
- }
4273
- const se = Array.from(s, (_, i) => {
4427
+ const { group, size } = groupsOf(items, pairs);
4428
+ const se = Array.from(current.s, (_, i) => {
4274
4429
  const n = size[group[i]];
4275
- return Math.sqrt(1 / (n * p) + (1 - 1 / n) / diagonal[i]);
4430
+ return Math.sqrt(1 / (n * p) + (1 - 1 / n) / current.diagonal[i]);
4276
4431
  });
4277
4432
  return {
4278
- strengths: Array.from(s),
4433
+ strengths: Array.from(current.s),
4279
4434
  se,
4280
- tie: fitTie ? Math.exp(lambda) : 0,
4281
- logLikelihood,
4435
+ tie: problem.fitTie ? Math.exp(current.lambda) : 0,
4436
+ logLikelihood: current.logLikelihood,
4282
4437
  iterations,
4283
4438
  converged: bound <= tolerance || rounded,
4284
4439
  bound,