diff options
| author | sanine <sanine.not@pm.me> | 2025-01-02 15:02:11 -0600 | 
|---|---|---|
| committer | sanine <sanine.not@pm.me> | 2025-01-02 15:02:11 -0600 | 
| commit | 81c38745be62a90de537c50cfbd777a9d582dadf (patch) | |
| tree | 575524bcae0e37578664490cf012921e9358adf9 /example.lua | |
| parent | 2a03fd9a56c82a711854ae82a7e596482ee8f67b (diff) | |
very bad stock prediction grammar
Diffstat (limited to 'example.lua')
| -rw-r--r-- | example.lua | 58 | 
1 files changed, 47 insertions, 11 deletions
| diff --git a/example.lua b/example.lua index fd61f06..2bd87fa 100644 --- a/example.lua +++ b/example.lua @@ -2,21 +2,50 @@ local Grammar = require 'grammar'  local GA = require 'ga' +local data = {} +local csvFile = io.open('HistoricalData_1735758980544-AAPL.csv') +local skip = true +for line in csvFile:lines() do +  if skip then +    skip = false +  else +    local tbl = {} +    for lineValue in string.gmatch(line, '[^,]+') do +      table.insert(tbl, lineValue) +    end +    table.insert(data, tbl) +  end +end + +print(#data) +for i, v in ipairs(data[1]) do +  print(i, v) +end +  local production = {}  production["<start>"]={{'<expr>'}} -production["<expr>"]={ {'<var>'}, {'(', '<expr>', '<op>', '<expr>', ')'} } +production["<expr>"]={ {'<data>'}, {'<number>'}, {'(','<expr>','<op>','<expr>',')'}, {'<func>','(','<expr>',')'} }  production["<op>"]={ {'+'}, {'-'}, {'*'}, {'/'} } -production["<var>"]={ {'x'} } +production["<func>"]={ {'math.log'}, {'math.exp'}, {'math.sin'}, {'math.cos'}, {'math.tan'}, } +production["<digit>"]={ {'0'}, {'1'}, {'2'}, {'3'}, {'4'}, {'5'}, {'6'}, {'7'}, {'8'}, {'9'} } +production["<lowdigit>"]={ {'0'}, {'1'}, {'2'} } +production["<data>"]={  +  {'high(','<date>',')'}, {'low(','<date>',')'},  +  {'volume(','<date>',')'},  +  {'open(','<date>',')'}, {'close(','<date>',')'} +} +production["<date>"]={ {'<lowdigit>','<digit>', '<digit>'} } +production["<number>"]={ {'0.','<digit>','<digit>','<digit>'} } -local grammar = Grammar.createGrammar(production) +local grammar = Grammar.createGrammar(production, {})  math.randomseed(0)  function randomGenome() -  local len = math.ceil(math.random() * 256) +  local len = math.ceil(math.random() * 300)    local genome = {}    for i=1,len do      table.insert(genome, math.floor(256 * math.random())) @@ -34,20 +63,27 @@ function randomPop()  end  function evaluate(genome) -  local s = 'return function(x) return ' .. grammar:expand(genome) .. ' end' +  local s = 'return function(close, volume, open, high, low) return ' .. grammar:expand(genome) .. ' end' +  -- print(s)    local f = assert(loadstring(s))() -  local target = function(x) return (x*x*x*x) + (x*x*x) + (x*x) + (x) end    local cost = 0 -  for n=0,100 do -    x = (n-50)/50 -    ok, y = pcall(f, x) +  for n=1,1000 do +    local start = math.ceil((#data-300) * math.random()) + 300 +    local dataView = function(offset, i) return tonumber(string.match(data[1 + start + offset][i], '[^$]+')) end +    local close = function(offset) return dataView(offset, 2) end +    local volume = function(offset) return dataView(offset, 3) end +    local open = function(offset) return dataView(offset, 4) end +    local high = function(offset) return dataView(offset, 5) end +    local low = function(offset) return dataView(offset, 6) end +    ok, y = pcall(f, close, volume, open, high, low) +    -- if ok and type(y) == 'table' then for k,v in pairs(y) do print(k, v) end end      if (not ok) or (y ~= y) then        cost = cost + 100      else -      cost = cost + math.abs(y - target(x)) +      cost = cost + math.abs(y - high(-30))      end    end -  return -((0.0001*#s) + cost) +  return -(cost)  end  local ga = GA.createGA(randomPop(), { | 
