Quant.Strategy.Optimization.Ranges (quant v0.1.0-alpha.1)
Utilities for generating parameter ranges and combinations.
This module provides functions to generate parameter grids similar to numpy's meshgrid functionality, supporting various range types and sampling strategies.
Summary
Functions
Generate Fibonacci sequence within a range.
Generate linearly spaced values similar to numpy.linspace.
Generate logarithmic range.
Generate all combinations of parameters from a parameter map.
Generate random parameter combinations.
Generate a range similar to numpy.arange.
Functions
@spec fibonacci_sequence(pos_integer(), pos_integer()) :: [pos_integer()]
Generate Fibonacci sequence within a range.
Useful for parameter values that benefit from Fibonacci spacing.
Examples
iex> fibonacci_sequence(1, 100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
@spec linspace(number(), number(), pos_integer()) :: [float()]
Generate linearly spaced values similar to numpy.linspace.
Examples
iex> linspace(0, 10, 5)
[0.0, 2.5, 5.0, 7.5, 10.0]
@spec logarithmic_range(pos_integer(), pos_integer(), pos_integer()) :: [ pos_integer() ]
Generate logarithmic range.
Creates exponentially spaced values, useful for parameters like periods or thresholds that work better on a log scale.
Examples
iex> logarithmic_range(1, 64, 2)
[1, 2, 4, 8, 16, 32]
@spec parameter_grid(%{required(atom()) => Range.t() | [any()] | any()}) :: {:ok, [%{required(atom()) => any()}]} | {:error, term()}
Generate all combinations of parameters from a parameter map.
Takes a map where keys are parameter names and values are either:
- Ranges (e.g., 1..10)
- Lists of specific values (e.g., [5, 10, 15, 20])
- Single values (treated as a list with one element)
Examples
iex> param_map = %{fast_period: 5..7, slow_period: [20, 25]}
iex> {:ok, combinations} = parameter_grid(param_map)
iex> length(combinations)
6
iex> Enum.all?(combinations, &Map.has_key?(&1, :fast_period))
true
@spec random_search(%{required(atom()) => Range.t() | [any()]}, pos_integer()) :: {:ok, [%{required(atom()) => any()}]} | {:error, term()}
Generate random parameter combinations.
Useful for large parameter spaces where exhaustive search is impractical.
Examples
iex> param_map = %{fast_period: 5..50, slow_period: 20..100}
iex> {:ok, samples} = random_search(param_map, 10)
iex> length(samples)
10
Generate a range similar to numpy.arange.
Examples
iex> range(1, 5)
[1, 2, 3, 4]
iex> range(0, 10, 2)
[0, 2, 4, 6, 8]