diff --git a/front_end/src/utils/math.ts b/front_end/src/utils/math.ts index c0065166a0..c87e6dc070 100644 --- a/front_end/src/utils/math.ts +++ b/front_end/src/utils/math.ts @@ -267,14 +267,20 @@ export function unscaleNominalLocation(x: number, scaling: Scaling) { */ export function getCdfAt(x: number, cdf: number[], scaling: Scaling) { const location = unscaleNominalLocation(x, scaling); - const floatIndex = location * (cdf.length - 1); - if (floatIndex <= 0) { + if (location <= 0) { return cdf.at(0); } - if (floatIndex >= 1) { + if (location >= 1) { return cdf.at(-1); } - return cdf.at(Math.round(floatIndex)); + const floatIndex = location * (cdf.length - 1); + // linear interpolation + const lowerIndex = Math.floor(floatIndex); + const upperIndex = Math.ceil(floatIndex); + const weight = floatIndex - lowerIndex; + /* eslint-disable @typescript-eslint/no-non-null-assertion */ + return cdf[lowerIndex]! * (1 - weight) + cdf[upperIndex]! * weight; + /* eslint-enable @typescript-eslint/no-non-null-assertion */ } /** diff --git a/front_end/src/utils/questions/helpers.ts b/front_end/src/utils/questions/helpers.ts index f20b0f937e..f5f8dbe96b 100644 --- a/front_end/src/utils/questions/helpers.ts +++ b/front_end/src/utils/questions/helpers.ts @@ -135,15 +135,11 @@ export function getContinuousGroupScaling( range_min: rangeMinPoints.length > 0 ? Math.min(...rangeMinPoints) : null, // set zero_point to null if any are linearly scaled zero_point: - zeroPoints.length > 0 && !zeroPoints.some((p) => p !== null) - ? Math.min(...zeroPoints) - : null, + zeroPoints.length === questions.length ? Math.min(...zeroPoints) : null, }; - // we can have mixes of log and linear scaled options - // which leads to a derived zero point inside the range which is invalid - // so just ignore the log scaling in this case + // if zero_point ends up lying within the range, remove the scaling if ( - scaling.zero_point !== null && + !isNil(scaling.zero_point) && !isNil(scaling.range_min) && !isNil(scaling.range_max) && scaling.range_min <= scaling.zero_point &&