Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ const ContinuousSlider: FC<Props> = ({
defaultValue={weights[index]}
round={true}
onChange={(value) => {
const newWeights = normWeights([
const newWeights = [
...weights.slice(0, index),
value,
...weights.slice(index + 1, forecast.length),
]);
];
onChange(forecast, newWeights);
}}
disabled={disabled}
Expand All @@ -110,10 +110,10 @@ const ContinuousSlider: FC<Props> = ({
...forecast.slice(0, index),
...forecast.slice(index + 1, forecast.length),
];
const newWeights = normWeights([
const newWeights = [
...weights.slice(0, index),
...weights.slice(index + 1, forecast.length),
]);
];
onChange(newForecast, newWeights);
}}
/>
Expand All @@ -126,8 +126,4 @@ const ContinuousSlider: FC<Props> = ({
);
};

function normWeights(weights: number[]) {
return weights.map((x) => x / weights.reduce((a, b) => a + b));
}

export default ContinuousSlider;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Quartiles, QuestionWithNumericForecasts } from "@/types/question";
import {
extractPrevNumericForecastValue,
getNumericForecastDataset,
normalizeWeights,
} from "@/utils/forecasts";
import { computeQuartilesFromCDF } from "@/utils/math";

Expand Down Expand Up @@ -194,7 +193,7 @@ const ForecastMakerConditionalContinuous: FC<Props> = ({
...prevChoice.sliderForecast,
{ left: 0.4, center: 0.5, right: 0.6 },
],
weights: normalizeWeights([...prevChoice.weights, 1]),
weights: [...prevChoice.weights, 1],
isDirty: true,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { QuestionWithNumericForecasts } from "@/types/question";
import {
extractPrevNumericForecastValue,
getNumericForecastDataset,
normalizeWeights,
} from "@/utils/forecasts";
import { computeQuartilesFromCDF } from "@/utils/math";
import { extractQuestionGroupName, formatResolution } from "@/utils/questions";
Expand Down Expand Up @@ -133,7 +132,7 @@ const ForecastMakerGroupContinuous: FC<Props> = ({
...prevChoice.userForecast,
{ left: 0.4, center: 0.5, right: 0.6 },
];
const newWeights = normalizeWeights([...prevChoice.userWeights, 1]);
const newWeights = [...prevChoice.userWeights, 1];
const newUserQuartiles = getUserQuartiles(
newUserForecast,
newWeights
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { QuestionWithNumericForecasts } from "@/types/question";
import {
extractPrevNumericForecastValue,
getNumericForecastDataset,
normalizeWeights,
} from "@/utils/forecasts";
import { computeQuartilesFromCDF } from "@/utils/math";

Expand Down Expand Up @@ -84,7 +83,7 @@ const ForecastMakerContinuous: FC<Props> = ({
center: 0.5,
},
]);
setWeights(normalizeWeights([...weights, 1]));
setWeights([...weights, 1]);
};

const handlePredictSubmit = async () => {
Expand Down
11 changes: 5 additions & 6 deletions front_end/src/utils/forecasts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,23 @@ export function extractPrevNumericForecastValue(prevForecast: any): {
return result;
}

export function normalizeWeights(weights: number[]) {
return weights.map((x) => x / weights.reduce((a, b) => a + b));
}

export function getNumericForecastDataset(
forecast: MultiSliderValue[],
weights: number[],
lowerOpen: boolean,
upperOpen: boolean
) {
const normalizedWeights = weights.map(
(x) => x / weights.reduce((a, b) => a + b)
);
const result: { cdf: number[]; pmf: number[] } = forecast
.map((x) =>
binWeightsFromSliders(x.left, x.center, x.right, lowerOpen, upperOpen)
)
.map((x, index) => {
return {
pmf: math.multiply(x.pmf, weights[index]) as number[],
cdf: math.multiply(x.cdf, weights[index]) as number[],
pmf: math.multiply(x.pmf, normalizedWeights[index]) as number[],
cdf: math.multiply(x.cdf, normalizedWeights[index]) as number[],
};
})
.reduce((acc, curr) => {
Expand Down