# Numerically solved PDE of Ornstein–Uhlenbeck process on simplex violates conservation of probability

**URL:** <https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265>\
**Category:** Questions\
**Tags:** finance, physics, calculus, equation-solving, numerical-computation\
**Created:** [July 11, 2024, 4:45pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265 "2024-07-11T16:45:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ct2797](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ct2797](https://community.wolfram.com/u/ct2797)\
**Post date:** [July 11, 2024, 4:45pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/1 "2024-07-11T16:45:27Z")

</div>

Thanks for your consideration.

I’m working to create a solution of an Ornstein-Uhlenbeck process with a force that takes mass towards the centre of a Simplex. I’m assuming absorbing boundaries.

The Mathematica code below quickly provides a solution. However, the probability mass within the domain grows significantly early, but it should only ever diminish, due to mass being absorbed.

I don’t think the error lies in my formulation of the forward Kolmogorov (Fokker-Plank) equation.

If it doesn’t lie there, I suppose it could lie in the numerical approximation, with errors growing? I greatly appreciate any insight into this problem.

 ![timevmass.png](https://global.discourse-cdn.com/wolfram/original/3X/4/3/43b6ad627edb84a568f411ab9aaa07b67194a061.png)

 ![simplex.png](https://global.discourse-cdn.com/wolfram/original/2X/c/c479398e621ad79f2db4d45f5fba246d2f486fa3.png)

```
ClearAll["Global`*"]

\[Eta] = 5.; (*side length*)
xopt = {\[Eta]/2, \[Eta]/(2 Sqrt[3])}; (*centroid*)
\[Kappa] = .75; (*rate of reversion to centroid,diffusion constant=1*)
Tmax = 5.; (*length of time*)

\[CapitalOmega] = 
  Polygon[Rationalize[{{0, 0}, {\[Eta], 
      0}, {\[Eta]/2, (\[Eta] Sqrt[3])/2}}, 
    0]]; (*domain is equilateral triangle*)

bC = Rationalize[DirichletCondition[P[x1, x2, t] == 0, True], 
   0]; (*absobing boundary condition*)
iC = Rationalize[
   P[x1, x2, 0] == 
    Piecewise[{{1/((Sqrt[3] \[Eta]^2)/4), 
       RegionMember[\[CapitalOmega], {x1, x2}]}}, 0], 
   0]; (*uniform initial condition*)

(*forward Kolmogorov equation*)
fwrdKol = 
  Rationalize[
   D[P[x1, x2, t], 
     t] == -D[\[Kappa] (xopt[[1]] - x1)*P[x1, x2, t], {x1, 1}] - 
     D[\[Kappa] (xopt[[2]] - x2)*P[x1, x2, t], {x2, 1}] + 
     1/2 D[P[x1, x2, t], {x1, 2}] + 1/2 D[P[x1, x2, t], {x2, 2}], 0];

(*numerical solution*)
Psol = NDSolveValue[{fwrdKol, iC, bC}, 
   P, {x1, x2} \[Element] \[CapitalOmega], {t, 0, Tmax}];

(*visualise solution at a t=Tmax/2*)
ContourPlot[Psol[x1, x2, Tmax/2], {x1, x2} \[Element] \[CapitalOmega]]

(*probability mass within domain*)
domP[t_] := 
 NIntegrate[
  Rationalize[Psol[x1, x2, t], 
   0], {x1, x2} \[Element] \[CapitalOmega], AccuracyGoal -> 4]

(*visualise*)
Plot[domP[t], {t, 0, 5}, PlotTheme -> "Scientific", PlotRange -> All, 
 FrameLabel -> {"t", "Prob. Mass Domain"}]  

```

---

<div class="post-metadata">

**Author:** ![ct2797](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ct2797](https://community.wolfram.com/u/ct2797)\
**Post date:** [July 12, 2024, 6:03pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/2 "2024-07-12T18:03:03Z")

</div>

Update: This is my first foray into numerically solving PDEs, I am learning as I’m troubleshooting. I have tried drastically refining the mesh, accuracy, and step size. This hasn’t lessened the violation of conservation, perhaps indicating a problem in formulation. But I’ve been fastidious with that as well, and can’t find a problem.

---

<div class="post-metadata">

**Author:** ![ct2797](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ct2797](https://community.wolfram.com/u/ct2797)\
**Post date:** [July 12, 2024, 11:48pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/3 "2024-07-12T23:48:14Z")

</div>

Update 2: My formulation of the differential equation matches that produced automatically by Mathematica

```
eqn = ItoProcess[{\[DifferentialD]x1[
      t] == \[Kappa] (x1opt - 
        x1[t]) \[DifferentialD]t + \[DifferentialD]w1[
       t], \[DifferentialD]x2[
      t] == \[Kappa] (x2opt - 
        x2[t]) \[DifferentialD]t + \[DifferentialD]w2[t]}, {x1[t], 
   x2[t]}, {{x1, x2}, {x10, x20}}, 
  t, {w1 \[Distributed] WienerProcess[], 
   w2 \[Distributed] WienerProcess[]}]

eqn["KolmogorovForwardEquation"] // TraditionalForm

```

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [July 13, 2024, 11:34am UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/4 "2024-07-13T11:34:16Z")

</div>

I know nothing about Kolmogorov equation. Your initial condition for `P` is a uniform value of `1/((Sqrt[3] \[Eta]^2)/4)` over the triangle. Hence at `t==0` all the spacial derivatives vanish. Now the initial time derivative of `P` is positive everywhere on the triangle:

```
fwrdKol /. t -> 0 /.
 {P[x1, x2, 0] -> 1/((Sqrt[3] \[Eta]^2)/4),
  Derivative[_, _, 0][P][__] :> 0}

```

No surprise that the integral of `P` increases at the start.

An unrelated question: what is the purpose of all those `Rationalize`? Why don’t you start with exact values for the parameters?

---

<div class="post-metadata">

**Author:** ![ct2797](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ct2797](https://community.wolfram.com/u/ct2797)\
**Post date:** [July 13, 2024, 3:18pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/5 "2024-07-13T15:18:50Z")

</div>

The initial condition was inconsistent and needed to exclude the boundary:

```auto
iC = Rationalize[
   P[x1, x2,0] == 
    Piecewise[{{1/
        Area[\[CapitalOmega]], (RegionMember[
           RegionBoundary[\[CapitalOmega]], {x1, x2}] == False \[And] 
         RegionMember[\[CapitalOmega], {x1, x2}])}}, 0], 0];

```

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [July 13, 2024, 6:08pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/6 "2024-07-13T18:08:45Z")

</div>

I am totally ignorant on numerical methods for PDEs, but I wonder what accuracy you can expect with discontinuous initial data.

---

<div class="post-metadata">

**Author:** ![ct2797](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ct2797](https://community.wolfram.com/u/ct2797)\
**Post date:** [July 14, 2024, 11:44am UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/7 "2024-07-14T11:44:38Z")

</div>

Hi Gianluca,

1. I’m still figuring out the accuracy of the solution, but for the 1D case the FEM and Method of Lines numerical solutions closely agree. If you make the mesh very fine (.001) it is generating reasonable results (e.g. 1\> total probability \>.99).

2. The purpose of Rationalize is to make the input into the numerical solver infinite precision rather than machine precision. It’s recommended in some videos on WolframU (i’m doing it pretty ritualistically at the moment).

3. After having correctly specified the problem the integral does always decrease as expected. No matter the domain, the Kolmogorov (Fokker-Plank) equation describes a conservative system, so there should never be an increase.

 ![followup.jpg](https://global.discourse-cdn.com/wolfram/original/2X/6/6103a6c233242ccf599b5b9e4ae985581737e6bd.jpeg)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [July 14, 2024, 12:21pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/8 "2024-07-14T12:21:35Z")

</div>

It seems reasonable that the mesh needs to be very fine only near the boundary, where the initial datum is discontinuous. You may try the option `MeshRefinementFunction`.

The purpose to make the input exact instead of floating-point is good. In your case it could be done more easily by giving exact parameters at the start. Instead of floating point values, as in

```
\[Eta] = 5.; 
\[Kappa] = .75; 
Tmax = 5.;

```

I would suggest

```
\[Eta] = 5; 
\[Kappa] = 3/4; 
Tmax = 5;

```

This way there is no need for `Rationalize`. Even more: with your original floating-point ` \[Eta] = 5.` the triangle definition

```
\[CapitalOmega] = 
  Polygon[Rationalize[{{0, 0}, {\[Eta], 
      0}, {\[Eta]/2, (\[Eta] Sqrt[3])/2}}, 
    0]]

```

actually decreases the precision, because `\[Eta] Sqrt[3])/2` becomes first floating-point, and then `Rationalize` freezes the approximation instead of the exact value.

---

<div class="post-metadata">

**Author:** ![ct2797](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ct2797](https://community.wolfram.com/u/ct2797)\
**Post date:** [July 15, 2024, 12:19pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/9 "2024-07-15T12:19:55Z")

</div>

That’s a great point regarding rationalising the parameters only, I’ll be instituting that.

I’ll have to think a bit more about how to square your point about spatial derivatives with the fact that mass evidently does decrease within the domain. I’ll get back when I have a decisive answer, but one thing to note is that the flux of probability is in the opposite direction to the gradient of p

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [July 15, 2024, 3:27pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/10 "2024-07-15T15:27:43Z")

</div>

I would suggest to discretize the triangle so that the elements are smaller near the boundary:

```
\[CapitalOmega]Discretized = 
 DiscretizeRegion[\[CapitalOmega], 
  MeshRefinementFunction -> 
   Function[{vertices, area}, 
    area > 0.1 (RegionDistance[RegionBoundary[\[CapitalOmega]], 
        Mean[vertices]])]]

```

Instead of 0.1 you can use a smaller parameter, with longer computing time and hopefully better precision. Try using ` \[CapitalOmega]Discretized` instead of ` \[CapitalOmega]`.

The time derivative of `P` is strongly negative near the boundary, enough to counteract the positive value inside:

```
Plot3D[Derivative[0, 0, 1][Psol][x1, x2, .01],
 Element[{x1, x2}, \[CapitalOmega]Discretized]]

```

---

<div class="post-metadata">

**Author:** ![dmoore101](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dmoore101/32/38622_2.png) [@dmoore101](https://community.wolfram.com/u/dmoore101)\
**Post date:** [July 17, 2024, 9:04pm UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/11 "2024-07-17T21:04:32Z")

</div>

Hi Cameron,  
I agree with you that your formulation of the problem and your PDEs look correct.  
If you plot your solution over time at one of the boundary points such as P(0,0,t), it looks like Mathematica is setting the boundary conditions to be `P(0,0,t)*e^(-t)` with the coefficient (-1) being exact. This is quite annoying, but you can remedy it by explicitly setting the boundary condition to `P(0,0,t)*e^(-10000*t)`.

```
bC = Rationalize[
   DirichletCondition[
    P[x1, x2, t] == Exp[-10000 t] 1/((Sqrt[3] \[Eta]^2)/4), True], 
   0]; 

```

In the study of linear PDEs this discontinuity (setting P(x,y,0)=const. on the domain and P(x,y,t)=0 on the boundary) isn’t much of an issue and it’s standard to do, so it’s frustrating that Mathematica makes such a big deal about it! At least with this fix applied you can see that the derivative of the total probability mass approaches -infinity near t=0, so that hints at some potential numerical difficulties.

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/823e21b5-ac55-4708-8624-db5a8ff97f59)

---

<div class="post-metadata">

**Author:** ![ct2797](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ct2797](https://community.wolfram.com/u/ct2797)\
**Post date:** [July 18, 2024, 11:53am UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/12 "2024-07-18T11:53:15Z")

</div>

Thanks Gianluca!

---

<div class="post-metadata">

**Author:** ![ct2797](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ct2797](https://community.wolfram.com/u/ct2797)\
**Post date:** [July 18, 2024, 11:56am UTC](https://community.wolfram.com/t/numerically-solved-pde-of-ornstein-uhlenbeck-process-on-simplex-violates-conservation-of-probability/24265/13 "2024-07-18T11:56:24Z")

</div>

Thanks David, this is really great input! I think this along with the comments above practically solves the problem!
