Econ_Data / app.py
dibend's picture
Create app.py
7938fa3 verified
Raw History Blame Contribute Delete
1.95 kB
"""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)"]