"""Skewness and excess kurtosis of the Tracy-Widom distributions -- numberdb.org/T445

This generator transcribes the standardized cumulants printed by Bornemann:
skewness for n = 3 and excess kurtosis for n = 4, for beta = 1, 2, 4.

Run it with SageMath:

    $ sage -pip install numberdb
    $ sage -python generate.py
    $ sage -python generate.py --publish

Inside this checkout, use agents/sage.sh instead:

    $ agents/sage.sh generate.py
    $ cat "$NUMBERDB_KEY_FILE" | NUMBERDB_KEY_FROM_STDIN=1 \
          NUMBERDB_PUBLISH=1 agents/sage.sh generate.py

The values are stored as measured decimal intervals. They are the correctly
truncated digits from Bornemann's statistical-property tables. The n = 4 rows
are excess kurtosis, so adding 3 gives Pearson kurtosis.
"""

import numberdb.sage as numberdb

import os
import sys


TABLE = os.environ.get("NUMBERDB_TABLE") or "T445"

VALUES = {
    ("1", "3"): "0.29346452408",
    ("1", "4"): "0.1652429384",
    ("2", "3"): "0.224084203610",
    ("2", "4"): "0.0934480876",
    ("4", "3"): "0.16550949435",
    ("4", "4"): "0.0491951565",
}


def _key_from_stdin():
    if os.environ.get("NUMBERDB_KEY_FROM_STDIN") != "1":
        return
    token = sys.stdin.read().strip()
    if "=" in token and token.split("=", 1)[0].isupper():
        token = token.split("=", 1)[1].strip().strip("'\"")
    if token:
        os.environ["NUMBERDB_API_KEY"] = token


class TracyWidomStandardizedCumulants(numberdb.Generator):

    table = TABLE
    parameters = ("beta", "n")
    type = "R"
    digits = 1
    rigour = "measured"

    def enumerate(self):
        for beta in ("1", "2", "4"):
            for n in ("3", "4"):
                yield {"beta": beta, "n": n}

    def value(self, params, digits):
        return {"number": VALUES[(str(params["beta"]), str(params["n"]))]}


if __name__ == "__main__":
    _key_from_stdin()
    generator = TracyWidomStandardizedCumulants()
    if os.environ.get("NUMBERDB_PUBLISH") == "1" or "--publish" in sys.argv:
        print(generator.publish(
            message="skewness and excess kurtosis of Tracy-Widom laws",
            assisted_by=os.environ.get("NUMBERDB_ASSISTED_BY", "")))
    else:
        report = generator.verify(sample=None)
        print(report)
        sys.exit(0 if report.ok else 1)
