Python

Optuna - Hyperparameter Optimization

Optuna - Hyperparameter Optimization

Optuna is a Python library for automated parameter optimization using adaptive search instead of manual tuning.

It shines whenever:

  • You have multiple knobs to tune.
  • The system returns one final numeric score.

This pattern shows up everywhere: machine learning metrics, simulation outcomes, and trading strategy ROI.

What Is a Hyperparameter?

A hyperparameter is a setting you choose before running an evaluation.

Examples:

  • A model’s learning rate or tree depth.
  • A trading rule’s lookback window or threshold.
  • A simulator’s step size or penalty weight.

Unlike learned parameters (like model weights), hyperparameters are not fit directly by gradient descent. You set them, run the system, and observe a final score.

Walrus Operator

Walrus Operator

The walrus operator (:=) in Python, introduced in Python 3.8, is also known as the assignment expression. It returns the value as well as assigning it. This can reduce many lines of code and greatly simplifies the language.

Example

# Without Walrus
numbers = [1,2,3]

numbers_length = len(numbers)
numbers_sum = sum(numbers)

numbers_description = {
    "length": numbers_length
    "sum": numbers_sum
}

# With Walrus - see how we save the variables and return the value
numbers = [1,2,3]

numbers_description = {
    "length": numbers_length := len(numbers)
    "sum": numbers_sum := sum(numbers)
}

Another Example

# Example code to read lines from a file and process non-empty lines
with open('example.txt', 'r') as file:
    while (line := file.readline().strip()):
        print(f"Processing line: {line}")

Managing Hatch Dependencies in VS Code

Managing Hatch Dependencies in vs Code

I recently started using hatch for python projects, and even though hatch will manage and install dependencies for you (just modify the pyproject.toml file), I noticed the import statements could not find the dependencies. This has to do with how Python and Hatch create virtual environments.

To fix this:

CMD + Shift + P
Python: Select Interpreter
Find the Python Environment that matches the Hatch environment.

Hatch - a great python project management tool

Hatch - A Great Python Project Management Tool

Hatch is a python packaging tool. Useful for building python projects. Out of the box it supports testing, building, managing dependencies, and linting/formatting support.

https://wwww.hatch.pypa.io/latest.intro

New Project

hatch new “Hatch Demo”

Adding to Existing Project

hatch new –init

Build a project

hatch build

Test a project with coverage

hatch test –cover

Running static analysis

hatch fmt

Open a shell in the project to run scripts

hatch shell

Create a python virtual env

python3 -m venv /tmp/hatch_demo/