Kann mir jemand sagen, was mit dieser Parallelisierung mithilfe von Concurrent.futures.Processpoolexecutor in Python losPython

Python-Programme
Anonymous
 Kann mir jemand sagen, was mit dieser Parallelisierung mithilfe von Concurrent.futures.Processpoolexecutor in Python los

Post by Anonymous »

Ich versuche parallel zu simulieren, und es ist das erste Mal, dass ich Parallelisierung in Python verwende. Run_simulation ist die Kernfunktion, die ich an jeden Arbeiter übergeben möchte, um Simulationen zu beschleunigen. Der Code ist ausführbar. < /P>

Code: Select all

import numpy as np
from scipy.stats import norm
import pandas as pd
import scipy as sp
import sys
from concurrent.futures import ProcessPoolExecutor

def BSFormulaCall(S0, K, T, r, sigma):
x = np.log(S0 / K) + r * T
sig = sigma * np.sqrt(T)
d1 = x / sig + sig / 2
d2 = d1 - sig
pv = np.exp(-r * T)
price = S0 * norm.cdf(d1) - pv * K * norm.cdf(d2)
return price

def BSFormulaPut(S0, K, T, r, sigma):
x = np.log(S0 / K) + r * T
sig = sigma * np.sqrt(T)
d1 = x / sig + sig / 2
d2 = d1 - sig
pv = np.exp(- r * T)
price = S0 * norm.cdf(d1) - pv * K * norm.cdf(d2) + pv * K - S0
return price

def BSImpliedVolCall(S0, K, T, r, C):
nK = 1 if np.ndim(K) == 0 else len(K)  # Number of strike prices
sigmaL = np.full(nK, 1e-10)  # Lower bound for volatility
CL = BSFormulaCall(S0, K, T, r, sigmaL)  # Call option prices for low volatility
sigmaH = np.full(nK, 500)  # Upper bound for volatility
CH = BSFormulaCall(S0, K, T, r, sigmaH)  # Call option prices for high volatility
while np.mean(sigmaH - sigmaL) > 1e-10:
sigma = (sigmaL + sigmaH) / 2
CM = BSFormulaCall(S0, K, T, r, sigma)  # Calculate option price for current sigma
CL = CL + (CM < C) * (CM - CL)  # Update lower bound for price
sigmaL = sigmaL + (CM < C) * (sigma - sigmaL)  # Adjust lower bound volatility
CH = CH + (CM >= C) * (CM - CH)  # Update upper bound for price
sigmaH = sigmaH + (CM >= C) * (sigma - sigmaH)  # Adjust upper bound volatility
if nK == 1:
sigma = sigma[0]
return sigma

def BSImpliedVolPut(S0, K, T, r, P):
nK = 1 if np.ndim(K) == 0 else len(K)  # Number of strike prices
sigmaL = np.full(nK, 1e-10)  # Lower bound for volatility
PL = BSFormulaPut(S0, K, T, r, sigmaL)  # Put option prices for low volatility
sigmaH = np.full(nK, 500)  # Upper bound for volatility
PH = BSFormulaPut(S0, K, T, r, sigmaH)  # Put option prices for high volatility
while np.mean(sigmaH - sigmaL) > 1e-10:
sigma = (sigmaL + sigmaH) / 2
PM = BSFormulaPut(S0, K, T, r, sigma)  # Calculate option price for current sigma
PL = PL + (PM < P) * (PM - PL)  # Update lower bound for price
sigmaL = sigmaL + (PM <  P) * (sigma - sigmaL)  # Adjust lower bound volatility
PH = PH + (PM >= P) * (PM - PH)  # Update upper bound for price
sigmaH = sigmaH + (PM >= P) * (sigma - sigmaH)  # Adjust upper bound volatility
if nK == 1:
sigma = sigma[0]
return sigma

def f_ou(k, t, n = 1):
vWN = np.random.normal(loc = 0, scale = 1, size = int(1e6))
return np.sqrt((1 - np.exp(- 2 * k * t)) / (2 * k)) * vWN

def f_vix_mb(w1, w2, lam, k, t, ksi0, n_int = 300, n_samp = int(1e6)):
d_vix = 30/365.25
grid = np.linspace(start = t + d_vix/n_int, stop = t + d_vix, num = n_int)

# randomness
vOU = f_ou(k, t, n_samp)
varOU = (1 - np.exp(- 2 * k * t))/(2 * k)

# computing instantaneous forward variance
def f_gridpoint(pp):
return ksi0 * (
lam *
np.exp(w1 * np.exp(- k * (grid[pp] - t)) * vOU -
(w1 ** 2) / 2 * np.exp(- 2 * k * (grid[pp] - t)) * varOU) +
(1 - lam) *
np.exp(w2 * np.exp(- k * (grid[pp] - t)) * vOU -
(w2 ** 2) / 2 * np.exp(- 2 * k * (grid[pp] - t)) * varOU)
)
mKsi = np.vstack(list(map(f_gridpoint, range(n_int))))
del vOU
# integrating to come up with VIX
vVIX2 = np.mean(mKsi, axis = 0)
# return mKsi
del mKsi
return np.sqrt(vVIX2)

def f_nsfwdv_rnd(t):
val = - 1
while val 
BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

Process SpawnProcess-1:
Traceback (most recent call last):
File "multiprocessing\process.py", line 315, in _bootstrap
File "multiprocessing\process.py", line 108, in run
File "concurrent\futures\process.py", line 233, in _process_worker
File "multiprocessing\queues.py", line 116, in get
AttributeError: Can't get attribute 'run_simulation' on 

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post