File size: 1,948 Bytes
7938fa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""FRED Monte Carlo Lab: optional OpenCL acceleration, CPU fallback."""
import io
import os
import re
import tempfile
from functools import lru_cache

import gradio as gr
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import requests

try:
    import pyopencl as cl
except Exception:
    cl = None

SERIES = {
    "Unemployment rate · UNRATE": "UNRATE",
    "Consumer price index · CPIAUCSL": "CPIAUCSL",
    "Fed funds rate · FEDFUNDS": "FEDFUNDS",
    "10-year Treasury yield · DGS10": "DGS10",
    "Real GDP · GDPC1": "GDPC1",
    "Industrial production · INDPRO": "INDPRO",
    "Housing starts · HOUST": "HOUST",
    "M2 money stock · M2SL": "M2SL",
    "30-year mortgage rate · MORTGAGE30US": "MORTGAGE30US",
    "S&P 500 · SP500": "SP500",
}

OPENCL_SOURCE = r"""
uint rnd(uint x) {
    x ^= x >> 16; x *= (uint)0x7feb352dU;
    x ^= x >> 15; x *= (uint)0x846ca68bU;
    x ^= x >> 16; return x;
}
float uniform01(uint x) {
    return ((float)(rnd(x) & (uint)0x00ffffffU) + 1.0f) / 16777217.0f;
}
__kernel void simulate(__global float *out, const int steps,
    const float initial, const float drift, const float phi,
    const float volatility, const float last_delta,
    const int logmode, const uint seed) {
    uint id = get_global_id(0);
    float val = initial;
    float change = last_delta;
    int offset = (int)id * (steps + 1);
    out[offset] = logmode ? exp(val) : val;
    for (int t = 1; t <= steps; ++t) {
        uint base = seed ^ (id * (uint)747796405U) ^ ((uint)t * (uint)2891336453U);
        float u1 = fmax(uniform01(base), 1.0e-7f);
        float u2 = uniform01(base ^ (uint)0x9e3779b9U);
        float z = sqrt(-2.0f * log(u1)) * cos(6.28318530718f * u2);
        change = drift + phi * (change - drift) + volatility * z;
        val += change;
        out[offset + t] = logmode ? exp(val) : val;
    }
}
"""


def devices():
    choices = ["Auto (OpenCL → CPU)", "CPU (NumPy)"]