Bookmark this: tool.io/estimation/particle-filter/source-code
Particle Filter Estimator Source Code
Browse the implementation files for Particle Filter Estimator.tool.ts typescript
import { parseFiniteNumberList } from '../../parse/numeric-dataset/tool';
import { parseFiniteNumber } from '../../parse/number/tool';
import { parseInteger } from '../../parse/integer/tool';
import { randomNormal } from '../../probability/normal-box-muller-sampler/tool';
import { createSeededRandom } from '../../random/seeded-number/tool';
export const estimateWithParticleFilter = (
observationsInput: unknown,
particleCountInput: unknown,
processNoiseInput: unknown,
measurementNoiseInput: unknown,
initialMeanInput: unknown,
initialDeviationInput: unknown,
seedInput: unknown
): string => {
const observations = parseFiniteNumberList(observationsInput);
const particleCount = parseInteger(particleCountInput);
const processNoise = parseFiniteNumber(processNoiseInput);
const measurementNoise = parseFiniteNumber(
measurementNoiseInput
);
const initialMean = parseFiniteNumber(initialMeanInput);
const initialDeviation = parseFiniteNumber(
initialDeviationInput
);
if (
!observations ||
particleCount === null ||
particleCount < 2 ||
particleCount > 100_000 ||
processNoise === null ||
processNoise < 0 ||
measurementNoise === null ||
measurementNoise <= 0 ||
initialMean === null ||
initialDeviation === null ||
initialDeviation < 0 ||
observations.length * particleCount > 10_000_000 ||
typeof seedInput !== 'string' ||
!seedInput.trim()
)
return '';
const random = createSeededRandom(seedInput);
let particles: number[] = [];
for (let index = 0; index < particleCount; index += 1) {
const normal = randomNormal(random);
if (normal === null) return '';
particles.push(initialMean + initialDeviation * normal);
}
const estimates: number[] = [];
for (const observation of observations) {
const advancedParticles: number[] = [];
for (const particle of particles) {
const normal = randomNormal(random);
if (normal === null) return '';
advancedParticles.push(particle + processNoise * normal);
}
particles = advancedParticles;
if (
particles.some((particle) => !Number.isFinite(particle))
)
return '';
const logWeights = particles.map(
(particle) =>
-0.5 *
((observation - particle) / measurementNoise) ** 2
);
const maximumLogWeight = logWeights.reduce(
(maximum, weight) => Math.max(maximum, weight),
-Infinity
);
if (!Number.isFinite(maximumLogWeight)) return '';
const weights = logWeights.map((weight) =>
Math.exp(weight - maximumLogWeight)
);
const total = weights.reduce(
(sum, weight) => sum + weight,
0
);
if (!(total > 0) || !Number.isFinite(total)) return '';
const estimate = particles.reduce(
(sum, particle, index) =>
sum + particle * (weights[index] / total),
0
);
if (!Number.isFinite(estimate)) return '';
const cumulative: number[] = [];
weights.reduce(
(sum, weight, index) =>
(cumulative[index] = sum + weight / total),
0
);
const resampled: number[] = [];
const offset = random() / particleCount;
let index = 0;
for (let sample = 0; sample < particleCount; sample += 1) {
const target = offset + sample / particleCount;
while (
index < cumulative.length - 1 &&
target > cumulative[index]
)
index += 1;
resampled.push(particles[index]);
}
particles = resampled;
estimates.push(Number(estimate.toPrecision(10)));
}
return JSON.stringify(
{
estimates,
finalEstimate: estimates.at(-1),
particleCount,
seed: seedInput
},
null,
2
);
};
export const Tool: TypeTool = {
name: 'Particle Filter Estimator',
slug: 'estimation/particle-filter',
intro:
'Estimate a one-dimensional hidden state with a reproducible bootstrap particle filter.',
tool: estimateWithParticleFilter,
i: [
{
example: '1, 1.2, 0.9, 1.4, 1.3',
label: 'Observations',
placeholder: 'Numeric measurements',
render: 'textarea',
type: 'text'
},
{
example: '500',
label: 'Particles',
placeholder: 'Particle count',
type: 'number'
},
{
example: '0.1',
label: 'Process Noise',
placeholder: 'Nonnegative standard deviation',
type: 'number'
},
{
example: '0.2',
label: 'Measurement Noise',
placeholder: 'Positive standard deviation',
type: 'number'
},
{
example: '0',
label: 'Initial Mean',
placeholder: 'Initial state mean',
type: 'number'
},
{
example: '1',
label: 'Initial Deviation',
placeholder: 'Nonnegative standard deviation',
type: 'number'
},
{
example: 'tool.io',
label: 'Seed',
placeholder: 'Reproducible seed',
type: 'text'
}
],
o: [
{
example: '{ "finalEstimate": 1.2 }',
label: 'Filtered Estimates',
placeholder: 'Estimates will appear here...',
render: 'textarea',
type: 'text'
}
],
rerunnable: true,
cite: [
{
title: 'Particle filter - Wikipedia',
url: 'https://en.wikipedia.org/wiki/Particle_filter'
}
]
};
export default Tool;
Description
Browse the implementation files for Particle Filter Estimator.