Skip to content
Open
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
Copy link
Contributor Author

@lsabor lsabor Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to handle this? None of these instances should actually ever trigger. Should I just handle their values down the line by "value || 0.0" for instance?

It might be possible to create different Question types each with unique Forecast values, so we could skip having to verify there are no "null" values for continuous questions. I'm unsure if that makes things more complicated with little benefit, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume in this place we’re just rendering data coming from the backend, so there’s no actual validation happening. In this line we throw an error that blocks further rendering, and I don’t think that’s useful for the user since they can’t do anything about it

Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ const ContinuousAggregationChart: FC<Props> = ({
if (historyItem) {
charts.push({
pmf: cdfToPmf(historyItem.forecast_values),
cdf: historyItem.forecast_values,
cdf: historyItem.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
}),
Comment on lines +87 to +92
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not totally sure about throwing an error here – is it necessary to hard-fail if there’s a null in forecast_values?
If we do want this guard, could we move the check higher up so we don’t have to duplicate it in every chart?

type: "community",
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const QuestionInfo: React.FC<{

const getYesProbability = (
q: QuestionWithNumericForecasts
): number | undefined => {
): number | null | undefined => {
if (q.type !== QuestionType.Binary) return undefined;
const values =
q.aggregations?.[q.default_aggregation_method]?.latest?.forecast_values;
Expand Down
14 changes: 12 additions & 2 deletions front_end/src/components/charts/continuous_area_chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,12 @@ export function getContinuousAreaChartData({
if (latest && isForecastActive(latest)) {
chartData.push({
pmf: cdfToPmf(latest.forecast_values),
cdf: latest.forecast_values,
cdf: latest.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
}),
type: (isClosed ? "community_closed" : "community") as ContinuousAreaType,
});
}
Expand All @@ -1141,7 +1146,12 @@ export function getContinuousAreaChartData({
} else if (!!userForecast && isForecastActive(userForecast)) {
chartData.push({
pmf: cdfToPmf(userForecast.forecast_values),
cdf: userForecast.forecast_values,
cdf: userForecast.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
}),
type: "user" as ContinuousAreaType,
});
}
Expand Down
4 changes: 2 additions & 2 deletions front_end/src/components/charts/histogram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import ChartContainer from "./primitives/chart_container";

type HistogramProps = {
histogramData: { x: number; y: number }[];
median: number | undefined;
mean: number | undefined;
median: number | null | undefined;
mean: number | null | undefined;
color: "blue" | "gray";
width?: number;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,12 @@ export function getContinuousAreaChartData({
if (latest && isForecastActive(latest)) {
chartData.push({
pmf: cdfToPmf(latest.forecast_values),
cdf: latest.forecast_values,
cdf: latest.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
}),
type: (isClosed ? "community_closed" : "community") as ContinuousAreaType,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ const ConditionalChart: FC<Props> = ({
? [
{
pmf: cdfToPmf(aggregateLatest.forecast_values),
cdf: aggregateLatest.forecast_values,
cdf: aggregateLatest.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
}),
type: "community" as ContinuousAreaType,
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ const ContinuousPredictionChart: FC<Props> = ({
if (showCP && latestAggLatest && isForecastActive(latestAggLatest)) {
charts.push({
pmf: cdfToPmf(latestAggLatest.forecast_values),
cdf: latestAggLatest.forecast_values,
cdf: latestAggLatest.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
}),
type:
question.status === QuestionStatus.CLOSED
? "community_closed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,15 +676,25 @@ const ForecastMakerConditionalContinuous: FC<Props> = ({
).cdf;
const userPreviousCdf: number[] | undefined =
overlayPreviousForecast && previousForecast
? previousForecast.forecast_values
? previousForecast.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
})
: undefined;
const aggregateLatest =
activeOptionData?.question.aggregations[
activeOptionData.question.default_aggregation_method
].latest;
const communityCdf: number[] | undefined =
aggregateLatest && isForecastActive(aggregateLatest)
? aggregateLatest.forecast_values
? aggregateLatest.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
})
: undefined;

const predictButtonIsDisabled =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ const ContinuousInputWrapper: FC<PropsWithChildren<Props>> = ({
[option]
);

const rawPreviousCdf = previousForecast?.forecast_values;
const rawPreviousCdf = previousForecast?.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
});
const showWithdrawnRow = option.wasWithdrawn && !option.isDirty;
const showPreviousRowByCheckbox =
!showWithdrawnRow && overlayPreviousForecast;
Expand Down Expand Up @@ -238,9 +243,14 @@ const ContinuousInputWrapper: FC<PropsWithChildren<Props>> = ({

const withdraw = () => onWithdraw();

const communityCdf: number[] | undefined =
option.question.aggregations[option.question.default_aggregation_method]
.latest?.forecast_values;
const communityCdf: number[] | undefined = option.question.aggregations[
option.question.default_aggregation_method
].latest?.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
});

const questionDuration =
new Date(option.question.scheduled_close_time).getTime() -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,12 @@ const ForecastMakerGroupContinuous: FC<Props> = ({
questionId: id,
forecastEndTime: forecastExpirationToDate(forecastExpiration),
forecastData: {
continuousCdf: latest.forecast_values,
continuousCdf: latest.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
}),
probabilityYesPerCategory: null,
probabilityYes: null,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ const ForecastMakerContinuous: FC<Props> = ({

const overlayPreviousCdf =
overlayPreviousForecast && previousForecast?.forecast_values
? previousForecast.forecast_values
? previousForecast.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
})
: undefined;

// Update states of forecast maker after new forecast is made
Expand Down Expand Up @@ -210,12 +215,24 @@ const ForecastMakerContinuous: FC<Props> = ({
const userCdf: number[] = dataset.cdf;
const userPreviousCdf: number[] | undefined =
overlayPreviousForecast && previousForecast
? previousForecast.forecast_values
? previousForecast.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
})
: undefined;
const latest =
question.aggregations[question.default_aggregation_method].latest;
const communityCdf: number[] | undefined =
latest && isForecastActive(latest) ? latest?.forecast_values : undefined;
latest && isForecastActive(latest)
? latest?.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
})
: undefined;

const handleAddComponent = () => {
setSliderDistributionComponents([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,12 @@ function generateReaffirmData({
const hasActivePrediction = latest && isForecastActive(latest);

if (hasActivePrediction) {
forecastValues = latest.forecast_values;
forecastValues = latest.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
});
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ const QuestionContinuousTile: FC<Props> = ({
{
questionId: question.id,
forecastData: {
continuousCdf: activeForecast.forecast_values,
continuousCdf: activeForecast.forecast_values.map((v) => {
if (v === null) {
throw new Error("Forecast values contain null values");
}
return v;
}),
probabilityYes: null,
probabilityYesPerCategory: null,
},
Expand Down
2 changes: 1 addition & 1 deletion front_end/src/components/prediction_chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Size = "compact" | "large";
type Props = {
question: QuestionWithForecasts;
status: PostStatus;
predictionOverride?: number; // override displayed CP (e.g. for graph cursor), otherwise the latest CP is used
predictionOverride?: number | null; // override displayed CP (e.g. for graph cursor), otherwise the latest CP is used
size?: Size;
className?: string;
chipClassName?: string;
Expand Down
12 changes: 6 additions & 6 deletions front_end/src/types/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export type Forecast = {
question_id: number;
start_time: number;
end_time: number | null;
forecast_values: number[];
interval_lower_bounds: number[] | null;
centers: number[] | null;
interval_upper_bounds: number[] | null;
forecast_values: (number | null)[];
interval_lower_bounds: (number | null)[] | null;
centers: (number | null)[] | null;
interval_upper_bounds: (number | null)[] | null;
};

export type ScoreData = {
Expand Down Expand Up @@ -155,9 +155,9 @@ export type UserForecastHistory = {
export type AggregateForecast = Forecast & {
method: AggregationMethod;
forecaster_count: number;
means: number[] | null;
means: (number | null)[] | null;
histogram: number[][] | null;
forecast_values: number[] | null;
forecast_values: (number | null)[] | null;
};

export type AggregateForecastHistory = {
Expand Down
6 changes: 3 additions & 3 deletions front_end/src/utils/formatters/prediction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,9 @@ export function getUserPredictionDisplayValue({
return "...";
}

let center: number | undefined;
let lower: number | undefined = undefined;
let upper: number | undefined = undefined;
let center: number | null | undefined;
let lower: number | null | undefined = undefined;
let upper: number | null | undefined = undefined;
if (questionType === QuestionType.Binary) {
center = closestUserForecast.forecast_values[1];
} else {
Expand Down
30 changes: 20 additions & 10 deletions front_end/src/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ function logisticCDF(
);
}

export function cdfToPmf(cdf: number[]) {
export function cdfToPmf(cdf: (number | null)[]) {
const pdf = [];
/* eslint-disable @typescript-eslint/no-non-null-assertion */
for (let i = 0; i < cdf.length; i++) {
const value = cdf[i]!;
if (value === null) {
throw new Error("CDF contains null values");
}
if (i === 0) {
pdf.push(cdf[i]!);
pdf.push(value);
} else {
pdf.push(cdf[i]! - cdf[i - 1]!);
pdf.push(value - cdf[i - 1]!);
}
}
pdf.push(1 - cdf[cdf.length - 1]!);
Expand Down Expand Up @@ -111,36 +115,40 @@ export function cdfFromSliders(
}

export function computeQuartilesFromCDF(
cdf: number[],
cdf: (number | null)[],
extendedQuartiles: true,
discrete?: boolean
): ExtendedQuartiles;
export function computeQuartilesFromCDF(
cdf: number[],
cdf: (number | null)[],
extendedQuartiles?: false,
discrete?: boolean
): Quartiles;
export function computeQuartilesFromCDF(cdf: number[]): Quartiles;
export function computeQuartilesFromCDF(
cdf: number[],
cdf: (number | null)[],
extendedQuartiles?: boolean,
discrete?: boolean
): Quartiles | ExtendedQuartiles {
function findPercentile(cdf: number[], percentile: number) {
function findPercentile(cdf: (number | null)[], percentile: number) {
if (cdf === null) {
cdf = [];
}
const target = percentile / 100;
for (let i = 0; i < cdf.length; i++) {
/* eslint-disable @typescript-eslint/no-non-null-assertion */
if (cdf[i]! >= target) {
const value = cdf[i]!;
if (value === null) {
throw new Error("CDF contains null values");
}
if (value >= target) {
if (i === 0) return 0;

if (discrete) {
return (i - 0.5) / (cdf.length - 1);
}

const diff = cdf[i]! - cdf[i - 1]!;
const diff = value - cdf[i - 1]!;
const adjustedPercentile = (target - cdf[i - 1]!) / diff;
return (i - 1 + adjustedPercentile) / (cdf.length - 1);
}
Expand Down Expand Up @@ -172,7 +180,9 @@ export function computeQuartilesFromCDF(
}
}

export function getCdfBounds(cdf: number[] | undefined): Bounds | undefined {
export function getCdfBounds(
cdf: (number | null)[] | undefined
): Bounds | undefined {
if (!cdf) {
return;
}
Expand Down
Loading
Loading