简体   繁体   中英

Fit a 2d linear stochastic differential equation with R or python

I have the following linear SDE

在此处输入图像描述

where all the parameters are positive. B is the Brownian Motion. I would like to fit the SDE to the following data, estimating the parameters:

dput(p)
c(-0.00499887430417606, 0.00136580945429492, 0.00272603530923862, 
0.0164251639533925, -0.00268156585269141, -0.00134348430418168, 
-0.00224315930756744, -0.00112346939128404, 0.00134801188689457, 
-0.00292299252521065, 0.000675295467418322, -0.00315528772033935, 
-0.0036182762224996, -0.00204104844640167, 0, 0.00158784200311316, 
0, 0.0042971908260964, -0.00611207335691889, -0.000227092086816839, 
-0.000454338944662375, -0.00113675128757942, -0.00068267154773241, 
0.00136487737294026, -0.00524217724690259, -0.00182982667707612, 
0.000457770664894863, 0.0018289899931725, -0.0013714287863791, 
0.00319707968140442, -0.00273972774112044, -0.00550207715398342, 
-0.00599494532363079, -0.0060311015566431, 0.000930232625219401, 
-0.00116292605268019, 0.000697917906658642, -0.00536319345474507, 
0.00070118034893385, -0.000233672159025698, 0.000700852732811347, 
0.00116699744834905, -0.00890352648316559, -0.00424528939474403, 
-0.00450077746620137, -0.000474946577438918, 0.00473934536389657, 
0.00777114472488652, -0.00211342100502786, -0.000470256298343952, 
0.00305272046394103, -0.00281756467214583, 0.0046915403903914, 
0.00140318077279183, 0.00536069341085852, 0.00394386313302582, 
-0.00417634017696479, 0.00994802883003507, -0.00832182333749233, 
-0.00116130543767949, 0.000464684023231398, 0.000232261062477207, 
-0.00558920877468028, 0.00349691462536695, 0.000697917906658642, 
0.00209083593418432, -0.00581870604819864, -0.00608758611421459, 
0.00117357131178549, 0.00234301887881456, 0.00163685293181769, 
0.0009341429917967, -0.000466962418011279, 0.00186654277243464, 
0.00349040493976835, -0.000929584078094808, -0.000930449008741273, 
-0.00209668102435707, 0.00139827568808834, 0.00232612340289427, 
-0.00186046565291968, -0.000232801770344946, -0.00069873066776668, 
-0.00537321699262691, 0.000468384083504467, -0.00728472261546302, 
0.00235571369245898, 0.0133226231356482, 0.00301379618404539, 
0.00161905898672875, -0.000462320858905021, -0.000925283434046639, 
0.008296890185509, -0.000918484565144687, 0.00252380540066386, 
0.00821547295339109, 0.00385619169017375, -0.000906002326979116, 
0.000453103768513167, -0.0164424805032385, -0.000460617235228273, 
-0.00323027507470883, -0.000231133711880787, 0.00253953732236223, 
0.000691483259084255, -0.00392474242681868, -0.00626524847893115, 
-0.00396501977188385, 0.00303348733377229, -0.00303348733377223, 
0.00163455961670538, 0.00349365675021421, -0.00442736363410828, 
0.000466962418011304, 0.000466744465882939, 0.00186480240520401, 
0.00116373805755544, -0.00653444565481517, 0, 0.00233863530347956, 
-0.00327562295694599, -0.000937866422789794, 0.00187485407625607, 
-0.00210945818290409, -0.00517526443255885, 0.00353149278147094, 
0.00211292483820612, 0.00257641552518017, -0.00374971144764706, 
0.00140779000256583, -0.00587891169977589, -0.00023587687336644, 
0.000235876873366413, -0.000707797599444269, 0.00188634811888855, 
-0.00353982670512395, -0.00355240160436799, 0, -0.00356506616449603, 
-0.00333890126551471, -0.00382958821309789, -0.00480770156810301, 
-0.00192957126921453, -0.000241458409962255, -0.00265989759048882, 
-0.000726656202496675, 0, 0.00266182855290664, 0.0098594041424272, 
-0.00215594765506836, -0.00216060580990593, 0.00240038521400408, 
0.0100191677820712, -0.00356676159843821, -0.0011917532098158, 
-0.00167084416481749, 0.0045286694011034, -0.00405100041662098, 
-0.00215130954251191, 0.013075189246895, 0.00400707666397984, 
-0.0130225588426025, 0.00404329246282893, 0.00308093619698536, 
-0.000236658384727885, -0.00118413276121669, -0.000236994905718736, 
-0.00498991174376034, -0.00573341274343224, -0.00263884044217062, 
-0.00144230794233805, -0.00289087168324221, 0.00409293938418453, 
0.00192030783926729, 0.000719165798705419, -0.00239923339657715, 
-0.00192353990542683, 0.00288392413290471, 0.0166591192041059, 
-0.00165347858623546, -0.013087634755368, 0.0126147097051724, 
0.00401275175531954, -0.00117855051944425, -0.00259771075351491, 
-0.00236742534814982, -0.00189798396241085, -0.00237755699254591, 
0.00427554095495684, -0.00498991174376034, -0.00837225318560294, 
-0.00312763397023146, -0.00968530573446365, -0.00830691314766503, 
-0.00344065220228523, 0.00368596067892333, -0.00196415481756345, 
0.000491400501288784, -0.00122895431468684, 0.00147456404206685, 
0.00294262105419736, 0.000978952598984199, 0.00390625496706521, 
0.00146092063967487, -0.00219218209822116, -0.00268587635521186, 
-0.00195790566705731, 0.00659262551361974, -0.00561319350584229, 
0.00195599084366741, -0.000244289728831934, -0.00588092849071303, 
0.00539216992770867, 0.0119063488899613, -0.00193423657983087, 
0.00434678298521502, 0.00504748097220122, 0.00930683247948184, 
-0.00786936217925184, -0.00648495127243114, 0.00696364922317138, 
0.00334448472284713, -0.00143198115161919, -0.000238863013198387, 
-0.00287081536879889, 0.00930017434187035, 0.00662410749557415, 
-0.00141576238843574, 0.00893282855786994, 0, 0.000467945726835847, 
-0.000702000730829893, 0.000234055003993947, -0.00422139463655039, 
-0.00117577908901195, -0.00353565483230764, 0.00259403517704675, 
-0.00188590343495702, -0.00330891344214702, 0.00448801981622758, 
-0.00662097476636909, -0.00356506616449603, 0.000476077133486095, 
0.00308898903101011, 0, -0.00953523315099994, 0.000478927212219359, 
-0.00479962524453776, -0.00168532602789342)

I have no idea how to begin.

You can get the Brownian motion as in the following question: Brownian motion simulation using R

Once you have tha data, your problem turns into a optimization problem. Rewrinting your SDE (let me know if you didn't get the notation):

p * Beta^2 - dB * Beta + 2 * dp * Alpha + (d2p - d2B) = 0

Your goal here is minimize the following function:

fobj = function(x){
  Alpha = x[1]
  Beta = x[2]

  if (Alpha<0 | Beta<0) return 1e6
 
  out = (p*Beta^2 - dB*Beta + 2*dp*Alpha + (d2p - d2B)) |>
   mean() |> abs()

  out
}

You can achive that with optim , but you might need to replace mean with another aggregation function (such as RMSE or even sum ) in order to get convergence; the idea is to find a way to summarize the vector p*Beta^2 - dB*Beta + 2*dp*Alpha + (d2p - d2B) .

Step by step:

  1. Generate Brownian data (same length of p )
  2. Calculate dp , d2p , dB and d2B as follows (given a vector x ):
dx = diff(x)
d2x = diff(x, differences = 2)
  1. Apply optim (doc: https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/optim )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM