Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""FRED Monte Carlo Lab: optional OpenCL acceleration, CPU fallback."""
|
| 2 |
+
import io
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
+
import tempfile
|
| 6 |
+
from functools import lru_cache
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import numpy as np
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import plotly.graph_objects as go
|
| 12 |
+
import requests
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
import pyopencl as cl
|
| 16 |
+
except Exception:
|
| 17 |
+
cl = None
|
| 18 |
+
|
| 19 |
+
SERIES = {
|
| 20 |
+
"Unemployment rate · UNRATE": "UNRATE",
|
| 21 |
+
"Consumer price index · CPIAUCSL": "CPIAUCSL",
|
| 22 |
+
"Fed funds rate · FEDFUNDS": "FEDFUNDS",
|
| 23 |
+
"10-year Treasury yield · DGS10": "DGS10",
|
| 24 |
+
"Real GDP · GDPC1": "GDPC1",
|
| 25 |
+
"Industrial production · INDPRO": "INDPRO",
|
| 26 |
+
"Housing starts · HOUST": "HOUST",
|
| 27 |
+
"M2 money stock · M2SL": "M2SL",
|
| 28 |
+
"30-year mortgage rate · MORTGAGE30US": "MORTGAGE30US",
|
| 29 |
+
"S&P 500 · SP500": "SP500",
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
OPENCL_SOURCE = r"""
|
| 33 |
+
uint rnd(uint x) {
|
| 34 |
+
x ^= x >> 16; x *= (uint)0x7feb352dU;
|
| 35 |
+
x ^= x >> 15; x *= (uint)0x846ca68bU;
|
| 36 |
+
x ^= x >> 16; return x;
|
| 37 |
+
}
|
| 38 |
+
float uniform01(uint x) {
|
| 39 |
+
return ((float)(rnd(x) & (uint)0x00ffffffU) + 1.0f) / 16777217.0f;
|
| 40 |
+
}
|
| 41 |
+
__kernel void simulate(__global float *out, const int steps,
|
| 42 |
+
const float initial, const float drift, const float phi,
|
| 43 |
+
const float volatility, const float last_delta,
|
| 44 |
+
const int logmode, const uint seed) {
|
| 45 |
+
uint id = get_global_id(0);
|
| 46 |
+
float val = initial;
|
| 47 |
+
float change = last_delta;
|
| 48 |
+
int offset = (int)id * (steps + 1);
|
| 49 |
+
out[offset] = logmode ? exp(val) : val;
|
| 50 |
+
for (int t = 1; t <= steps; ++t) {
|
| 51 |
+
uint base = seed ^ (id * (uint)747796405U) ^ ((uint)t * (uint)2891336453U);
|
| 52 |
+
float u1 = fmax(uniform01(base), 1.0e-7f);
|
| 53 |
+
float u2 = uniform01(base ^ (uint)0x9e3779b9U);
|
| 54 |
+
float z = sqrt(-2.0f * log(u1)) * cos(6.28318530718f * u2);
|
| 55 |
+
change = drift + phi * (change - drift) + volatility * z;
|
| 56 |
+
val += change;
|
| 57 |
+
out[offset + t] = logmode ? exp(val) : val;
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def devices():
|
| 64 |
+
choices = ["Auto (OpenCL → CPU)", "CPU (NumPy)"]
|