# Minimizes a function (variance over 1mln data-points)?

**URL:** <https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505>\
**Category:** Questions\
**Tags:** calculus, optimization\
**Created:** [October 24, 2016, 10:53am UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505 "2016-10-24T10:53:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![saur12ab](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/saur12ab/32/15904_2.png) [@saur12ab](https://community.wolfram.com/u/saur12ab)\
**Post date:** [October 24, 2016, 10:53am UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505/1 "2016-10-24T10:53:30Z")

</div>

I have attached below one Mathematica notebook and a MATLAB file where I try to find the \theta which minimizes a function (variance over 1mln data-points).

The MATLAB code runs pretty fast, while the execution of the Mathematica code freezes my machine.

Any suggestions how to make the Mathematica code executable (at least)?

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [October 24, 2016, 12:07pm UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505/2 "2016-10-24T12:07:11Z")

</div>

The problem is that:

```
$HistoryLength=1;
S=100;K=70;\[Sigma]=0.2;T=1;r=0.01;\[Alpha]=r-\[Sigma]^2/2;
n=100;
Z=RandomVariate[NormalDistribution[],n];
fVarPut[\[Theta]_]:=Variance[E^(-r T) Ramp[K-S E^(\[Alpha] T+\[Sigma] Sqrt[T](Z+\[Theta]))]E^(-\[Theta](Z+\[Theta])+\[Theta]^2/2)];

LogPlot[fVarPut[x],{x,-2,4},PlotRange->All]

```

looks like something like:

 ![ScreenShot2016-10-24at14.04.50.png](https://global.discourse-cdn.com/wolfram/original/2X/7/7e146a01a69fdf87174b7dddb37d00ce3cd8ff94.png)

so

fVarPut[100]

will always give 0. it is ‘flat’ on the right (equal to 0, because of the Ramp function), so there is not _one_ minimum…

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [October 24, 2016, 12:11pm UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505/3 "2016-10-24T12:11:36Z")

</div>

The definition of pVarPut needs to signal that the function only works with a numeric argument.

In[1]:= S = 100; K = 70; [Sigma] = 0.2; T = 1; r = 0.01; [Alpha] =  
r - [Sigma]^2/2;

```
In[2]:= n = 100;
Z = RandomVariate[NormalDistribution[], n];

In[4]:= fVarPut[\[Theta]_?NumberQ] := 
  Variance[E^(-r T)
     Ramp[K - 
      S E^(\[Alpha] T + \[Sigma] Sqrt[
         T] (Z + \[Theta]))] E^(-\[Theta] (Z + \[Theta]) + \[Theta]^2/
      2)];

In[5]:= (* freezes even with WorkingPrecision\[Rule]5 *)
NMinimize[fVarPut[x], x, WorkingPrecision -> 5]

Out[5]= {0, {x -> 3.3686}}

In[6]:= FindMinimum[fVarPut[x], {x, -2}, WorkingPrecision -> 5]

Out[6]= {1.0248*10^-382, {x -> -32.000}}

```

---

<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:** [October 24, 2016, 12:43pm UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505/4 "2016-10-24T12:43:01Z")

</div>

I don’t get any freeze with your code. However, it may run faster if you use the immediate assignment

```
fVarPut[\[Theta]_?NumberQ] = 
  Variance[E^(-r T) Ramp[
     K - S E^(\[Alpha] T + \[Sigma] Sqrt[
            T] (Z + \[Theta]))] E^(-\[Theta] (Z + \[Theta]) + \
\[Theta]^2/2)];

```

instead of the delayed assignment :=, because with delayed assignment Mathematica has to recalculate the `Variance` formula every time `fVarPut` is called.

---

<div class="post-metadata">

**Author:** ![saur12ab](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/saur12ab/32/15904_2.png) [@saur12ab](https://community.wolfram.com/u/saur12ab)\
**Post date:** [October 24, 2016, 12:49pm UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505/5 "2016-10-24T12:49:43Z")

</div>

Thank you very much all of you!

I was about to write that it freezes either way even if I ask for the minimum for the interval (-4,-1), when I read Frank’s paramount remark.

So much better now! 🙂

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [October 24, 2016, 12:49pm UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505/6 "2016-10-24T12:49:51Z")

</div>

This will calculate the variance in a symbolic way once; that might be or not be a smart idea depending on the number of points. Definitely not a smart move if n is large, it will do a lot of (comparatively) slow symbolic processing.

[Frank Kampas](http://community.wolfram.com/web/fkampas)’s solution is a good idea; to turn it into a numeric computation, which is very fast. It will avoid Mathematica trying to do any symbolic manipulation on it.

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [October 24, 2016, 12:52pm UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505/7 "2016-10-24T12:52:37Z")

</div>

You will need to constrain it using something like:

```
FindMinimum[f,{x,x0,xmin,xmax}]

```

As this problem will have a minimum for x → -infinity and for x\>a for some ‘a’ it is constant (kind of a minimum) (a depends on the random values).

---

<div class="post-metadata">

**Author:** ![saur12ab](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/saur12ab/32/15904_2.png) [@saur12ab](https://community.wolfram.com/u/saur12ab)\
**Post date:** [October 24, 2016, 1:14pm UTC](https://community.wolfram.com/t/minimizes-a-function-variance-over-1mln-data-points/9505/8 "2016-10-24T13:14:47Z")

</div>

I have a poor machine, Gianluca.. and a bit weird I guess, because it gives me that the implementation with “=” is slower than the one with “:=”
