Skip to content

Commit

Permalink
Remove config options, simplify, final deviation=0
Browse files Browse the repository at this point in the history
  • Loading branch information
nicfv committed Mar 19, 2024
1 parent 24f02d0 commit 9824b9b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
36 changes: 7 additions & 29 deletions datafit/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { SMath } from 'smath';
import { Config, Datum, Fit, VariableType, fx } from './types';

/**
* Defines the default configuration options for `fit`
*/
const defaultConfig: Config = {
generations: 1000,
initialDeviation: 100,
finalDeviation: 1,
};
import { Datum, Fit, VariableType, fx } from './types';

/**
* Minimize the sum of squared errors to fit a set of data
Expand Down Expand Up @@ -37,7 +28,7 @@ const defaultConfig: Config = {
* };
* ```
*/
export function fit<T extends VariableType>(f: fx<T>, data: Array<Datum<T>>, params_initial: Array<number> = [], config: Config = defaultConfig): Fit {
export function fit<T extends VariableType>(f: fx<T>, data: Array<Datum<T>>, params_initial: Array<number> = [], iterations: number = 1e3, maxDeviation: number = 100): Fit {
const N_params: number = f.length - 1;
if (params_initial.length === 0) {
params_initial.length = N_params;
Expand All @@ -46,16 +37,12 @@ export function fit<T extends VariableType>(f: fx<T>, data: Array<Datum<T>>, par
if (params_initial.length !== N_params) {
throw new Error('The initial guess should contain ' + N_params + ' parameters.');
}
// Clean up a potentially incomplete config object by filling it in with default options
const conf: Config = {
generations: config.generations ?? defaultConfig.generations,
initialDeviation: config.initialDeviation ?? defaultConfig.initialDeviation,
finalDeviation: config.finalDeviation ?? defaultConfig.finalDeviation,
};
if (maxDeviation <= 0) {
throw new Error('Maximum deviation should be a positive value.');
}
let best: Fit = { params: params_initial, err: err(f, params_initial, data) };
for (let generation = 0; generation < conf.generations; generation++) {
// Mutate a random parent from the prior generation of survivors
const params: Array<number> = mutate(best.params, SMath.translate(generation, 0, conf.generations, conf.initialDeviation, conf.finalDeviation)),
for (let i = 0; i < iterations; i++) {
const params: Array<number> = mutate(best.params, SMath.translate(i, 0, iterations, maxDeviation, 0)),
error: number = err(f, params, data);
if (error < best.err) {
best = { params: params, err: error };
Expand Down Expand Up @@ -83,13 +70,4 @@ function err<T extends VariableType>(f: fx<T>, params: Array<number>, data: Arra
*/
function mutate(params: Array<number>, deviation: number): Array<number> {
return params.map(c => c += SMath.expand(Math.random(), -deviation, deviation) * Math.max(1, c) / 100);
}
/**
* Generate a random integer between `min, max`
* @param min Minimum value
* @param max Maximum value
* @returns A random integer
*/
function randInt(min: number, max: number): number {
return Math.floor(SMath.expand(Math.random(), min, max));
}
17 changes: 0 additions & 17 deletions datafit/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,4 @@ export interface Fit {
* This is the residual sum of squared errors.
*/
readonly err: number;
}
/**
* Configuration options for curve fitting.
*/
export interface Config {
/**
* Determines the number of generations, or iterations.
*/
readonly generations: number;
/**
* Determines the percentage of how much a set of parameters can mutate on the first generation.
*/
readonly initialDeviation: number;
/**
* Determines the percentage of how much a set of parameters can mutate on the final generation.
*/
readonly finalDeviation: number;
}

0 comments on commit 9824b9b

Please sign in to comment.