Repository navigation
Study: fractal landscapes via midpoint displacement #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Fractal landscapes: midpoint displacement | ||
|
|
||
| A 1D fractal landscape generator (Fournier, Fussell, Carpenter 1982) and a Hurst-estimator that recovers H from the generated series, verifying D = 2 - H. Part of the fractals repo. No dependencies. | ||
|
|
||
| Run: node tests/fractal-landscapes.mjs | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* Fractal landscapes via 1D midpoint displacement (Fournier-Fussell-Carpenter 1982). | ||
| * Hurst parameter H in (0,1): smaller H = rougher. Fractal dimension D = 2 - H. | ||
| * No dependencies. Loads in Node (require) and browser (window.FL). | ||
| */ | ||
| (function (root) { | ||
| 'use strict'; | ||
|
|
||
| function generate(n, H, seed) { | ||
| // deterministic-ish via simple LCG if seed given | ||
| let rng = seed == null ? Math.random : (() => { let s = seed >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 0x100000000; }; })(); | ||
| const N = 1 << n; | ||
| const y = new Array(N + 1).fill(0); | ||
| let step = N, amp = 1; | ||
| while (step > 1) { | ||
| const half = step / 2; | ||
| for (let i = half; i < N; i += step) { | ||
|
Comment on lines
+12
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocking (with the README). y[0] and y[N] stay 0, so every landscape is a bridge. This is the largest bias for H ≥ 0.5. Either draw y[N] at random, with variance (1/3)/(2^(−2H) − 1/4) in these units, or document the bridge and fit against the bridge-corrected variogram. Do not detrend instead. Generated by Claude Code |
||
| const mid = (y[i - half] + y[i + half]) / 2; | ||
| y[i] = mid + (rng() * 2 - 1) * amp; | ||
| } | ||
| step = half; | ||
| amp *= Math.pow(0.5, H); | ||
| } | ||
| return y; | ||
| } | ||
|
|
||
| function estimateH(y) { | ||
| const lags = []; | ||
| for (let k = 0; (1 << k) < y.length / 2; k++) lags.push(1 << k); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking but related to the bias: the lags run up to N/2, where each profile has few independent pairs. That adds log-averaging bias and lets the pinned ends in. Cap the lags well below N (e.g. N/16), or weight the fit by each lag's variance. Generated by Claude Code |
||
| const pts = lags.map(lag => { | ||
| let s = 0, n = 0; | ||
| for (let i = 0; i + lag < y.length; i++) { const d = y[i + lag] - y[i]; s += d * d; n++; } | ||
| return { lag, v: s / n }; | ||
| }); | ||
| const xs = pts.map(p => Math.log(p.lag)); | ||
| const ys = pts.map(p => Math.log(p.v)); | ||
| const n = xs.length; | ||
| const mx = xs.reduce((a, b) => a + b, 0) / n; | ||
| const my = ys.reduce((a, b) => a + b, 0) / n; | ||
| let num = 0, den = 0; | ||
| for (let i = 0; i < n; i++) { num += (xs[i] - mx) * (ys[i] - my); den += (xs[i] - mx) ** 2; } | ||
| return num / den / 2; | ||
| } | ||
|
|
||
| const api = { generate, estimateH }; | ||
| if (typeof module !== 'undefined' && module.exports) module.exports = api; | ||
| if (typeof window !== 'undefined') window.FL = api; | ||
| })(typeof globalThis !== 'undefined' ? globalThis : this); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { createRequire } from 'node:module'; | ||
| const require = createRequire(import.meta.url); | ||
| const { generate, estimateH } = require('../fractal-landscapes/model.js'); | ||
|
|
||
| let fails = 0; | ||
| function check(name, ok, detail) { | ||
| console.log(`${ok ? 'PASS' : 'FAIL'} ${name}\n ${detail}`); | ||
| if (!ok) fails++; | ||
| } | ||
|
|
||
| for (const H of [0.25, 0.5, 0.75]) { | ||
| let ests = []; | ||
| for (let t = 0; t < 30; t++) ests.push(estimateH(generate(11, H, 12345 + t))); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocking. Consecutive LCG seeds give first draws of 0.0204–0.0316, so the largest displacement is shared across all 30 runs. With independent seeds (mulberry32, 3000 runs) the same code gives 0.261 / 0.455 / 0.649. The H = 0.75 check then fails in 191 of 300 independent 30-run blocks. Seed through a mixing generator (e.g. mulberry32(seed + t)) and re-derive the tolerances. Generated by Claude Code |
||
| const mean = ests.reduce((a, b) => a + b, 0) / ests.length; | ||
| check(`H=${H}: estimated H within 0.1`, Math.abs(mean - H) < 0.1, `estimated ${mean.toFixed(3)} (D=${(2-mean).toFixed(3)})`); | ||
| } | ||
|
|
||
| // D = 2 - H | ||
| check('D = 2 - H identity', Math.abs((2 - 0.5) - 1.5) < 1e-12, 'D(0.5)=1.5'); | ||
|
Comment on lines
+18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocking. This compares literals, Generated by Claude Code |
||
|
|
||
| if (fails) { console.log(`\n${fails} check(s) failed`); process.exit(1); } | ||
| else console.log('\nall checks pass'); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking. Both "recovers H" and "verifying D = 2 - H" overclaim. With independent seeds the estimator returns 0.261 / 0.455 / 0.649 for H = 0.25 / 0.5 / 0.75, and nothing measures D independently. Suggest: present D = 2 − H as the known result for fBm graphs (Orey 1970, Adler 1981), assumed and not tested here; report the estimator's measured bias and its causes (pinned ends, lags up to N/2); add the Status, Checks and Sources sections the other studies use.
Generated by Claude Code