Skip to main content

Where to find the code for ESRK1 and RSwM1 in the Julia library source code?

I'm trying to implement the SDE solver called ESRK1 and the adaptive stepsize algorithm called RSwM1 from Rackauckas & Nie (2017). I'm writing a python implementation, mainly to confirm to myself that I've understood the algorithm correctly. However, I'm running into a problem already at the implementation of ESRK1: When I test my implementation with shorter and shorter timesteps on a simple SDE describing geometric Brownian motion, the solution does not converge as dt becomes smaller, indicating that I have a mistake in my code.

I believe this algorithm is implemented as part of the library DifferentialEquations.jl in Julia, so I thought perhaps I could find some help by looking at the Julia code. However, I have had some trouble locating the relevant code. If someone could point me to the implementation of ESRK1 and RSwM1 in the relevant Julia librar(y/ies) (or indeed any other readable and correct implementation) of these algorithms, I would be most grateful.

I searched for ESRK and RSwM in the github repo of StochasticDiffEq.jl, but I didn't find anything I could really recognise as the method from the paper I'm reading:

https://github.com/search?q=repo%3ASciML%2FStochasticDiffEq.jl+rswm&type=code

Update: I found the code for ESRK1, as shown in my answer below, but I'm still unable to find the code for RSwM1.

For completeness, here is my own not-yet-correct implementation of ESRK1 in python:

def ESRK1(U, t, dt, f, g, dW, dZ):
    # Implementation of ESRK1, following Rackauckas & Nie (2017)
    # Eq. (2), (3) and (4) and Table 1
    
    # Stochastic integrals, taken from Eqs. (25) - (30) in Rackauckas & Nie (2017)
    I1   = dW
    I11  = (I1**2 - dt) / 2
    I111 = (I1**3 - 3*dt*I1) / 6
    I10  = (I1 + dZ/np.sqrt(3))*dt / 2
    
    # Coefficients, taken from Table 1 in Rackauckas & Nie (2017)
    # All coefficients not included below are zero
    c0_2 = 3/4
    c1_2, c1_3, c1_4 = 1/4, 1, 1/4
    A0_21 = 3/4
    B0_21 = 3/2
    A1_21 = 1/4
    A1_31 = 1
    A1_43 = 1/4
    B1_21 = 1/2
    B1_31 = -1
    B1_41, B1_42, B1_43 = -5, 3, 1/2
    alpha1, alpha2 = 1/2, 2/3
    alpha_tilde1, alpha_tilde2 = 1/2, 1/2
    beta1_1, beta1_2, beta1_3 = -1, 4/3, 2/3
    beta2_1, beta2_2, beta2_3 = -1, 4/3, -1/3
    beta3_1, beta3_2, beta3_3 =  2, -4/3, -2/3
    beta4_1, beta4_2, beta4_3, beta4_4 = -2, 5/3, -2/3, 1
    
    # Stages in the Runge-Kutta approximation
    # Eqs. (3) and (4) and Table 1 in Rackauckas & Nie (2017)
    # First stages
    H0_1 = U # H^(0)_1
    H1_1 = U
    # Second stages
    H0_2 = U + A0_21 * f(t, H0_1)*dt + B0_21 * g(t, H1_1)*I10/dt
    H1_2 = U + A1_21 * f(t, H0_1)*dt + B1_21 * g(t, H1_1)*np.sqrt(dt)
    # Third stages
    H0_3 = U
    H1_3 = U + A1_31 * f(t, H0_1) * dt + B1_31 * g(t, H1_1) * np.sqrt(dt)
    # Fourth stages
    H0_4 = U
    H1_4 = U + A1_43 * f(t, H0_3) * dt + (B1_41 * g(t, H1_1) + B1_42 * g(t+c1_2*dt, H1_2) + B1_43 * g(t+c1_3*dt, H1_3)) * np.sqrt(dt)
    
    # Construct next position
    # Eq. (2) and Table 1 in Rackauckas & Nie (2017)
    U_ = U  + (alpha1*f(t, H0_1) + alpha2*f(t+c0_2*dt, H0_2))*dt \
            + (beta1_1*I1 + beta2_1*I11/np.sqrt(dt) + beta3_1*I10/dt ) * g(t, H1_1) \
            + (beta1_2*I1 + beta2_2*I11/np.sqrt(dt) + beta3_2*I10/dt ) * g(t + c1_2*dt, H1_2) \
            + (beta1_3*I1 + beta2_3*I11/np.sqrt(dt) + beta3_3*I10/dt ) * g(t + c1_3*dt, H1_3) \
            + (beta4_4*I111/dt ) * g(t + c1_4*dt, H1_4)
    
    # Calculate error estimate
    # Eq. (9) and Table 1 in Rackauckas & Nie (2017)
    E = -dt*(f(t, H0_1) + f(t + c0_2*dt, H0_2))/6  \
        + (beta3_1*I10/dt + beta4_1*I111/dt)*g(t, H1_1) \
        + (beta3_2*I10/dt + beta4_2*I111/dt)*g(t + c1_2*dt, H1_2) \
        + (beta3_3*I10/dt + beta4_3*I111/dt)*g(t + c1_3*dt, H1_3) \
        + (beta4_4*I111/dt)*g(t + c1_4*dt, H1_4)

    # Return next position and error
    return U_, E

Rackauckas & Nie (2017): https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5844583/pdf/nihms920388.pdf



source https://stackoverflow.com/questions/77508717/where-to-find-the-code-for-esrk1-and-rswm1-in-the-julia-library-source-code

Comments

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...