1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

金融モデルの深層学習をjuliaでやってみた。

1
Posted at

金融モデルの深層学習をjuliaでやってみた。

2026年3月28日 09:03

############################################################

Deep Learning Financial Model in Julia (Flux + LSTM)

############################################################

using Flux

using Statistics

using Random

############################################################

1. データ生成(実運用ではCSV or APIに置き換え)

############################################################

function generate_price_series(n=2000)

prices = zeros(Float32, n)

prices[1] = 100.0

for i in 2:n

    prices[i] = prices[i-1] * (1 + 0.001 * randn())

end

return prices

end

prices = generate_price_series()

############################################################

2. 特徴量(リターン)

############################################################

function compute_returns(prices)

returns = diff(log.(prices))

return returns

end

returns = compute_returns(prices)

############################################################

3. データセット作成(時系列)

############################################################

function create_dataset(data, seq_len=20)

X = []

Y = []

for i in 1:(length(data) - seq_len - 1)

    push!(X, data[i:i+seq_len-1])

    push!(Y, data[i+seq_len])

end

return X, Y

end

seq_len = 20

X, Y = create_dataset(returns, seq_len)

############################################################

4. テンソル化

############################################################

function to_tensor(X, Y)

X_tensor = [reshape(x, :, 1) for x in X]

Y_tensor = [reshape(y, 1, 1) for y in Y]

return X_tensor, Y_tensor

end

X_tensor, Y_tensor = to_tensor(X, Y)

############################################################

5. モデル(LSTM)

############################################################

model = Chain(

LSTM(1, 32),

Dense(32, 1)

)

############################################################

6. 損失関数

############################################################

loss(x, y) = Flux.Losses.mse(model(x)[end], y)

############################################################

7. 学習

############################################################

opt = ADAM(0.001)

epochs = 5

for epoch in 1:epochs

total_loss = 0.0

for (x, y) in zip(X_tensor, Y_tensor)

    gs = gradient(() -> loss(x, y), Flux.params(model))

    Flux.Optimise.update!(opt, Flux.params(model), gs)

    total_loss += loss(x, y)

end

println("Epoch $epoch Loss: $(total_loss / length(X_tensor))")

end

############################################################

8. 予測

############################################################

function predict(model, X)

preds = []

for x in X

    ŷ = model(x)[end][1]

    push!(preds, ŷ)

end

return preds

end

predictions = predict(model, X_tensor)

############################################################

9. シグナル生成

############################################################

function generate_signals(preds, threshold=0.0)

signals = []

for p in preds

    if p > threshold

        push!(signals, 1) # BUY

    else

        push!(signals, -1) # SELL

    end

end

return signals

end

signals = generate_signals(predictions)

############################################################

10. バックテスト

############################################################

function backtest(signals, returns)

capital = 1.0

equity_curve = []



for i in 1:length(signals)

    capital *= (1 + signals[i] * returns[i])

    push!(equity_curve, capital)

end



return equity_curve

end

equity = backtest(signals, returns[seq_len+1:end])

############################################################

11. 結果

############################################################

println("Final Capital: ", last(equity))

############################################################

END

############################################################

本noteの内容は情報の提供だけを行なっております。したがって、運用はお客様自身の責任と判断によって

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?