Métodos computacionais / 2019

Davi C. Rodrigues
Aula 12

As notas desta aula 12 não seguem nenhum livro em especial.

Recomendações gerais sobre programação & performance

(HyperlinkTemplate["What are the most common pitfalls awaiting new users?", https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users, https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users, Hyperlink, HyperlinkActive, ]) -- Vasta lista de recomendações a serem consideradas.

As 10 dicas abaixo são sucintas e relevantes.

10 Tips for Writing Fast Mathematica Code --- Não é de minha autoria, o autor está abaixo.

November 17, 2011

Jon McLoone, International Business & Strategic Development

When people tell me that Mathematica isn’t fast enough, I usually ask to see the offending code and often find that the problem isn’t a lack in Mathematica’s performance, but sub-optimal use of Mathematica. I thought I would share the list of things that I look for first when trying to optimize Mathematica code.

1. Use floating-point numbers if you can, and use them early.

Of the most common issues that I see when I review slow code is that the programmer has inadvertently asked Mathematica to do things more carefully than needed. Unnecessary use of exact arithmetic is the most common case. 

In most numerical software, there is no such thing as exact arithmetic. 1/3 is the same thing as 0.33333333333333. That difference can be pretty important when you hit nasty, numerically unstable problems, but in the majority of tasks, floating-point numbers are good enough and, importantly, much faster. In Mathematica any number with a decimal point and less than 16 digits of input is automatically treated as a machine float, so always use the decimal point if you want speed ahead of accuracy (e.g. enter a third as 1./3.). Here is a simple example where working with floating-point numbers is nearly 40 times faster than doing the computation exactly and then converting the result to a decimal afterward. And in this case it gets the same result.

N[Det[Table[1/(1+Abs[i-j]),{i,1,150},{j,1,150}]]]//AbsoluteTiming

Det[Table[1/(1.+Abs[i-j]),{i,1.,150.},{j,1.,150.}]]//AbsoluteTiming

The same is true for symbolic computation. If you don’t care about the symbolic answer and are not worried about stability, then substitute numerical values as soon as you can. For example, solving this polynomial symbolically before substituting the values in causes Mathematica to produce a five-page-long intermediate symbolic expression.

Solve[a x^4+b x^3+c x+d==0,x]/.{a->2.,b->4.,c->7.,d->11.}//AbsoluteTiming

But do the substitution first, and Solve will use fast numerical methods.

Solve[a x^4+b x^3+c x+d==0/.{a->2.,b->4.,c->7.,d->11.},x]//AbsoluteTiming

When working with lists of data, be consistent in your use of reals. It only takes one exact value to cause the whole dataset to have to be held in a more flexible but less efficient form.

data=RandomReal[1,{1000000}];
ByteCount[Append[data,1.]]

ByteCount[Append[data,1]]

2. Learn about Compile...

The Compile function takes Mathematica code and allows you to pre-declare the types (real, complex, etc.) and structures (value, list, matrix, etc.) of input arguments. This takes away some of the flexibility of the Mathematica language, but freed from having to worry about “What if the argument was symbolic?” and the like, Mathematica can optimize the program and create a byte code to run on its own virtual machine. Not everything can be compiled, and very simple code might not benefit, but complex low-level numerical code can get a really big speedup.

Here is an example:

arg=Range[ -50.,50, 0.25];

fn=Function[{x},
Block[{sum=1.0,inc=1.0},Do[inc=inc*x/i;sum=sum+inc,{i,10000}];sum]];

Map[fn,arg];//AbsoluteTiming

Using Compile instead of Function makes the execution over 10 times faster.

cfn=Compile[{x},
Block[{sum=1.0,inc=1.0},Do[inc=inc*x/i;sum=sum+inc,{i,10000}];sum]];

Map[cfn,arg];//AbsoluteTiming

But we can go further by giving Compile some hints about the parallelizable nature of the code, getting an even better result.

cfn2=Compile[{x},
Block[{sum=1.0,inc=1.0},Do[inc=inc*x/i;sum=sum+inc,{i,10000}];sum],
RuntimeAttributes->{Listable},Parallelization->True];

cfn2[arg];//AbsoluteTiming

On my dual-core machine I get a result 150 times faster than the original; the benefit would be even greater with more cores.

Be aware though that many Mathematica functions like Table, Plot, NIntegrate, and so on automatically compile their arguments, so you won’t see any improvement when passing them compiled versions of your code.

2.5. ...and use Compile to generate C code.

Furthermore, if your code is compilable, then you can also use the option CompilationTarget->”C” to generate C code, call your C compiler to compile it to a DLL, and link the DLL back into Mathematica, all automatically. There is more overhead in the compilation stage, but the DLL runs directly on your CPU, not on the Mathematica virtual machine, so the results can be even faster.

cfn2C=Compile[{x},
Block[{sum=1.0,inc=1.0},Do[inc=inc*x/i;sum=sum+inc,{i,10000}];sum],
RuntimeAttributes->{Listable},Parallelization->True,CompilationTarget->"C"];

cfn2C[arg];//AbsoluteTiming

3. Use built-in functions.

Mathematica has a lot of functions. More than the average person would care to sit down and learn in one go. So it is not surprising that I often see code where someone has implemented some operation without having realized that Mathematica already knows how to do it. Not only is it a waste of time re-implementing work that is already done, but our guys are paid to worry about what the best algorithms are for different kinds of input and how to implement them efficiently, so most built-in functions are really fast.

If you find something close-but-not-quite-right, then check the options and optional arguments; often they generalize functions to cover many specialized uses or abstracted applications.

Here is such an example. If I have a list of a million 2×2 matrices that I want to turn into a list of a million flat lists of 4 elements, the conceptually easiest way might be to Map the basic Flatten operation down the list of them.

data=RandomReal[1,{1000000,2,2}];

Map[Flatten,data];//AbsoluteTiming

But Flatten knows how to do this whole task on its own when you specify that levels 2 and 3 of the data structure should be merged and level 1 be left alone. Specifying such details might be comparatively fiddly, but staying within Flatten to do the whole flattening job turns out to be nearly 4 times faster than re-implementing that sub-feature yourself.

Flatten[data,{{1},{2,3}}];//AbsoluteTiming

So remember—do a search in the Help menu before you implement anything.

4. Use Wolfram Workbench.

Mathematica can be quite forgiving of some kinds of programming mistakes—it will proceed happily in symbolic mode if you forget to initialize a variable at the right point and doesn’t care about recursion or unexpected data types. That’s great when you just need to get a quick answer, but it will also let you get away with less than optimal solutions without realizing it. 

Workbench helps in several ways. First it lets you debug and organize large code projects better, and having clean, organized code should make it easier to write good code. But the key feature in this context is the profiler that lets you see which lines of code used up the time, and how many times they were called.

Take this example, a truly horrible way (computationally speaking) to implement Fibonacci numbers. If you didn’t think about the consequences of the double recursion, you might be surprised by the 22 seconds it takes to evaluate fib[35] (about the same time it takes the built-in function to calculate all 208,987,639 digits of Fibonacci[1000000000] [see tip 3]).

fib[n_]:=fib[n]= fib[n-1]+fib[n-2];
fib[1]=1;
fib[2]=1;
fib[35];//AbsoluteTiming

Running the code in the profiler reveals the reason. The main rule is invoked 9,227,464 times, and the fib[1] value is requested 18,454,929 times. 

Being told what your code really does, rather than what you thought it would do, can be a real eye-opener.

5. Remember values that you will need in the future.

This is good programming advice in any language. The Mathematica construct that you will want to know is this:

f[x_]:=f[x]=(*What the function does*)

It saves the result of calling f on any value, so that if it is called again on the same value, Mathematica will not need to work it out again. You are trading speed for memory here, so it isn’t appropriate if your function is likely to be called for a huge number of values, but rarely the same ones twice. But if the possible input set is constrained, this can really help. Here it is rescuing the program that I used to illustrate tip 3. Change the first rule to this:

fib[n_]:=fib[n]=fib[n-1]+fib[n-2];

And it becomes immeasurably fast, since fib[35] now only requires the main rule to be evaluated 33 times. Looking up previous results prevents the need to repeatedly recurse down to fib[1].

6. Parallelize.

An increasing number of Mathematica operations will automatically parallelize over local cores (most linear algebra, image processing, and statistics), and, as we have seen, so does Compile if manually requested. But for other operations, or if you want to parallelize over remote hardware, you can use the built-in parallel programming constructs.

There is a collection of tools for this, but for very independent tasks, you can get quite a long way with just ParallelTable, ParallelMap, and ParallelTry. Each of these automatically takes care of communication, worker management, and collection of results. There is some overhead for sending the task and retrieving the result, so there is a trade-off of time gained versus time lost. Your Mathematica comes with four compute kernels, and you can scale up with gridMathematica if you have access to additional CPU power. Here, ParallelTable gives me double the performance, since it is running on my dual-core machine. More CPUs would give a better speedup.

Table[PrimeQ[x],{x,10^1000,10^1000+5000}];//AbsoluteTiming

ParallelTable[PrimeQ[x],{x,10^1000,10^1000+5000}];//AbsoluteTiming

Anything that Mathematica can do, it can also do in parallel. For example, you could send a set of parallel tasks to remote hardware, each of which compiles and runs in C or on a GPU.

6.5. Think about CUDALink and OpenCLLink.

If you have GPU hardware, there are some really fast things you can do with massive parallelization. Unless one of the built-in CUDA-optimized functions happens to do what you want, you will need to do a little work, but the CUDALink and OpenCLLink tools automate a lot of the messy details for you.

7. Use Sow and Reap to accumulate large amounts of data (not AppendTo).

Because of the flexibility of Mathematica data structures, AppendTo can’t assume that you will be appending a number, because you might equally append a document or a sound or an image. As a result, AppendTo must create a fresh copy of all of the data, restructured to accommodate the appended information. This makes it progressively slower as the data accumulates. (And the construct data=Append[data,value] is the same as AppendTo.)

Instead use Sow and Reap. Sow throws out the values that you want to accumulate, and Reap collects them and builds a data object once at the end. The following are equivalent:

data={};Do[AppendTo[data,RandomReal[x]],{x,0,40000}];//AbsoluteTiming

data=Reap[Do[Sow[RandomReal[x]],{x,0,40000}]][[2]];//AbsoluteTiming

8. Use Block or With rather than Module.

Block, With, and Module are all localization constructs with slightly different properties. In my experience, Block and Module are interchangeable in at least 95% of code that I write, but Block is usually faster, and in some cases With (effectively Block with the variables in a read-only state) is faster still.

Do[Module[{x=2.},1/x],{1000000}];//AbsoluteTiming

Do[Block[{x=2.},1/x],{1000000}];//AbsoluteTiming

9. Go easy on pattern matching.

Pattern matching is great. It can make complicated tasks easy to program. But it isn’t always fast, especially the fuzzier patterns like BlankNullSequence (usually written as “___”), which can search long and hard through your data for patterns that you, as a programmer, might already know will never be there. If execution speed matters, use tighter patterns, or none at all.

As an example, here is a rather neat way to implement a bubble sort in a single line of code using patterns:

data=RandomReal[1,{200}];
data//.{a___,b_,c_,d___}/;b>c->{a,c,b,d};//AbsoluteTiming

Conceptually neat, but slow compared to this procedural approach that I was taught when I first learned programming:

(flag=True;
While[TrueQ[flag],
flag=False;
Do[If[
data[[i]]>data[[i+1]],
temp=data[[i]];
data[[i]]=data[[i+1]];
data[[i+1]]=temp;
flag=True],{i,1,Length[data]-1}]];
data);//AbsoluteTiming

Of course in this case you should use the built-in function (see tip 3), which will use better sorting algorithms than bubble sort.

Sort[RandomReal[1,{200}]];//AbsoluteTiming

10. Try doing things differently.

One of Mathematica’s great strengths is that it can tackle the same problem in different ways. It allows you to program the way you think, as opposed to reconceptualizing the problem for the style of the programming language. However, conceptual simplicity is not always the same as computational efficiency. Sometimes the easy-to-understand idea does more work than is necessary.

But another issue is that because special optimizations and smart algorithms are applied automatically in Mathematica, it is often hard to predict when something clever is going to happen. For example, here are two ways of calculating factorial, but the second is over 10 times faster.

temp=1;Do[temp=temp i,{i,2^16}];//AbsoluteTiming

Apply[Times,Range[2^16]];//AbsoluteTiming

Why? You might guess that the Do loop is slow, or all those assignments to temp take time, or that there is something else “wrong” with the first implementation, but the real reason is probably quite unexpected. Times knows a clever binary splitting trick that can be used when you have a large number of integer arguments. It is faster to recursively split the arguments into two smaller products, (1×2×...×32767)×(32768×...×65536), rather than working through the arguments from first to last. It still has to do the same number of multiplications, but fewer of them involve very big integers, and so, on average, are quicker to do. There are lots of such pieces of hidden magic in Mathematica, and more get added with each release.

Of course the best way here is to use the built-in function (tip 3 again):

AbsoluteTiming[65536!;]

Mathematica is capable of superb computational performance, and also superb robustness and accuracy, but not always both at the same time. I hope that these tips will help you to balance the sometimes conflicting needs for rapid programming, rapid execution, and accurate results. 

All timings use a Windows 7 64-bit PC with 2.66 GHz Intel Core 2 Duo and 6 GB RAM.

MCMC - Uma introdução a diferentes formas de implementação

Introdução: Links...

(HyperlinkTemplate["MCMC - introdução", https://en.wikipedia.org/wiki/Malaysian_Communications_and_Multimedia_Commission, https://en.wikipedia.org/wiki/Malaysian_Communications_and_Multimedia_Commission, Hyperlink, HyperlinkActive, ])
(HyperlinkTemplate["MCMC - mais introdução", https://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo, https://en.wikipedia.org/wiki/Markov_chain_Monte_Carlo, Hyperlink, HyperlinkActive, ])

(HyperlinkTemplate["MCMC for dummies", https://twiecki.io/blog/2015/11/10/mcmc-sampling/, https://twiecki.io/blog/2015/11/10/mcmc-sampling/, Hyperlink, HyperlinkActive, ])

(HyperlinkTemplate["Interactive MCMC: Metropolis vs HMC/NUTS", http://elevanth.org/blog/2017/11/28/build-a-better-markov-chain/, http://elevanth.org/blog/2017/11/28/build-a-better-markov-chain/, Hyperlink, HyperlinkActive, ])

(HyperlinkURL["List of various samplers.", https://m-clark.github.io/docs/ld_mcmc/index_onepage.html#differential_evolution_markov_chain])

(HyperlinkURL["Comparison of emcee, PyMC and Stan", http://jakevdp.github.io/blog/2014/06/14/frequentism-and-bayesianism-4-bayesian-in-python/])

(HyperlinkTemplate["Stan main site and documentation", https://mc-stan.org, https://mc-stan.org, Hyperlink, HyperlinkActive, ])  &  (HyperlinkURL["MathematicaStan", https://mc-stan.org/users/interfaces/mathematica-stan.html])

Uma realização do “Metropolis Sampler”

Versão obtida a partir do stackexchange ( https://mathematica.stackexchange.com/a/143668/53261 )

Clear[pdf0,pdf1,pdf2,pdf,pdfN];

pdf0=MultinormalDistribution[{1,0,-1,1},1/8 {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}];
pdf1=MultinormalDistribution[{1,1,0,1},1/8 {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}];
pdf2=MultinormalDistribution[{0,-1,1,0},1/8 {{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}}];

pdf[x_,y_,z_,t_]=PDF[pdf0,{x,y,z,t}]+1/2. PDF[pdf1,{x,y,z,t}]+1/4. PDF[pdf2,{x,y,z,t}];

pdfN=Compile[{{x,_Real},{y,_Real},{z,_Real},{t,_Real}},pdf[x,y,z,t]//Evaluate,Parallelization->True,CompilationTarget->"C"];

mcmcfun=Compile[{{s,_Real,1}},Module[{xm,ym,zm,tm,proposal,xp,yp,zp,tp,p2,p1,proposalSigma=1},{xm,ym,zm,tm}=s;
xp=RandomReal[NormalDistribution[xm,proposalSigma]];
yp=RandomReal[NormalDistribution[ym,proposalSigma]];
zp=RandomReal[NormalDistribution[zm,proposalSigma]];
tp=RandomReal[NormalDistribution[tm,proposalSigma]];
p2=pdfN[xp,yp,zp,tp];
p1=pdfN[xm,ym,zm,tm];
proposal=p2/p1;
If[RandomReal[]≤proposal,{xp,yp,zp,tp},{xm,ym,zm,tm}]],CompilationOptions->{"InlineExternalDefinitions"->True},Parallelization->True,CompilationTarget->"C"];

sim=Drop[NestList[mcmcfun[#]&,{0,0,0,0},1000000],{1,1000}];

{{{First[#],Last[#]}&/@sim//DensityHistogram//Image,{First[#],#[[2]]}&/@sim//DensityHistogram//Image,{First[#],#[[3]]}&/@sim//DensityHistogram//Image},{{#[[2]],#[[3]]}&/@sim//DensityHistogram//Image,{#[[4]],#[[2]]}&/@sim//DensityHistogram//Image,{#[[3]],#[[4]]}&/@sim//DensityHistogram//Image}}//GraphicsGrid

?MultinormalDistribution

NestList[k, {0,0,0,0}, 10]

Tutorial MathematicaStan --- Pequena variação em torno do tutorial oficial do MathematicaStan (https://mc-stan.org/users/interfaces/mathematica-stan.html)

Apresentação de como instalar o Stan.

Comentar sobre resolução do trivial problema de diretórios que são dependentes do sistema operacional (i.e., usar FileNameJoin, que por algum motivo não foi usado na versão oficial).

CmdStan package

<<CmdStan`

FileNames["*",FileNameJoin[{$HomeDirectory, "ExternalPrograms", "cmdstan-2.19.1"}]]

(*  SetCmdStanDirectory[FileNameJoin[{$HomeDirectory, "ExternalPrograms", "cmdstan-2.19.1"}]] *) (*PRECISA SER EXECUTADO UMA ÚNICA VEZ*)

?CmdStan`*

Linear regression

Stan code

SetDirectory[$TemporaryDirectory]

stanCode="data
{
  int<lower = 0> N;
  vector[N] x;
  vector[N] y;
}
parameters
{
  real alpha;
  real beta;
  real<lower = 0> sigma;
}
model {
  y ~normal(alpha + beta * x, sigma);
}";

stanCodeFile=ExportStanCode["linear_regression.stan",stanCode]

Stan exe

(stanExeFile = CompileStanCode[stanCodeFile] )// Timing

stanExeFile

Linear regression data

σ=3;α=1;β=2;
n=20;
X=Range[n];
Y=α+β*X+RandomVariate[NormalDistribution[0,σ],n];
Show[Plot[α+β*x,{x,Min[X],Max[X]}],ListPlot[Transpose@{X,Y},PlotStyle->Red]]
Export[FileNameJoin[{$HomeDirectory,"lineRegData.png"}],%] (*Why it exports this plot?*)

X

Y

Stan data.R file

stanData=<|"N"->n,"x"->X,"y"->Y|>;
stanDataFile=ExportStanData[stanExeFile,stanData]

Run Stan: “method=Optimize”

? OptimizeDefaultOptions

? RunStan

stanExeFile

stanResultFile=RunStan[stanExeFile, OptimizeDefaultOptions]

?RunStan

OptimizeDefaultOptions

Load result

stanResult=ImportStanResult[stanResultFile]

GetStanResultMeta[stanResult,"lp__"]
αe=GetStanResult[stanResult,"alpha"]
βe=GetStanResult[stanResult,"beta"]
σe=GetStanResult[stanResult,"sigma"]

Show[Plot[{αe+βe*x,α+β*x},{x,Min[X],Max[X]},PlotLegends->"Expressions"],ListPlot[Transpose@{X,Y},PlotStyle->Red]]

stanResult

Import["/private/var/folders/yz/v13sgmf13lq9_mdfpyvq0jw00000gn/T/linear_regression.csv"] // TableForm

GetStanResult[Histogram,"sigma"]

Run Stan: “method=Variational”

myOpt=VariationalDefaultOptions

myOpt=SetStanOption[myOpt,"output.file",FileNameJoin[{Directory[],"myOutputFile.csv"}]]

myOpt=SetStanOption[myOpt,"method.adapt.iter",123]

GetStanOption[myOpt,"method.adapt.iter"]

myOpt=RemoveStanOption[myOpt,"method.adapt.iter"]

myOutputFile=RunStan[stanExeFile,myOpt]

myResult=ImportStanResult[myOutputFile]

GetStanResult[Mean,myResult,"alpha"]
GetStanResult[Variance,myResult,"alpha"]
GetStanResult[Histogram,myResult,"alpha"]

Linear regression, more than one predictor

Stan code

stanCode="data {
  int<lower=0> N;   // number of data items
  int<lower=0> K;   // number of predictors
  matrix[N, K] x;   // predictor matrix
  vector[N] y;      // outcome vector
}
parameters {
  real alpha;           // intercept
  vector[K] beta;       // coefficients for predictors
  real<lower=0> sigma;  // error scale
}
model {
  y ~ normal(x * beta + alpha, sigma);  // likelihood
}";

stanCodeFile=ExportStanCode["linear_regression_vect.stan",stanCode];
stanExeFile = CompileStanCode[stanCodeFile,StanVerbose->False];

Generate data

σ=3;α=1;β1=2;β2=0.1;β3=0.01;
n=20;
X=Range[n];
Y=α+β1*X+β2*X^2+β3*X^3+RandomVariate[NormalDistribution[0,σ],n];
Show[Plot[α+β1*x+β2*x^2+β3*x^3,{x,Min[X],Max[X]}],ListPlot[Transpose@{X,Y},PlotStyle->Red]]

Export["~/GitHub/MathematicaStan/figures/linReg2Data.png",%]

Export data

stanData=<|"N"->n,"K"->3,"x"->Transpose[{X,X^2,X^3}],"y"->Y|>;
stanDataFile=ExportStanData[stanExeFile,stanData]

Run Stan: “method=Sample”

stanResultFile=RunStan[stanExeFile,SampleDefaultOptions]

Load the CSV file

stanResult=ImportStanResult[stanResultFile]

{β1e,β2e,β3e}=GetStanResult[Mean,stanResult,"beta"]

{αe,{β1e,β2e,β3e}}={GetStanResult[Mean,stanResult,"alpha"],GetStanResult[Mean,stanResult,"beta"]}

p=GraphicsRow[GetStanResult[Histogram[#,ImageSize->200]&,stanResult,"beta"]]

Export["~/GitHub/MathematicaStan/figures/linReg2Histo.png",p]

StanResultKeys[stanResult]
StanResultMetaKeys[stanResult]

StanResultReducedKeys[stanResult]
StanResultReducedMetaKeys[stanResult]

GetStanResult[Mean,stanResult,"beta.2"]
GetStanResult[Mean,stanResult,"beta"]

Map[#->GetStanResult[stanResult,#]&,StanResultKeys[stanResult]];
Map[ListLinePlot[Values[#],PlotLabel->Keys[#]]&,%];
GraphicsGrid[Partition[%,3,3,{1,1},Null]]