Community RSS Feed https://community.wolfram.com RSS Feed for Wolfram Community showing any discussions in tag Staff Picks sorted by active Squeezing Pi from a Menger Sponge https://community.wolfram.com/groups/-/m/t/822984 In the [Menger sponge](http://mathworld.wolfram.com/MengerSponge.html), a step divides a cube into 27 cubes, then the center and six touching cubes are removed. Here&#039;s what it looks like after three steps. Graphics3D[ Cuboid /@ ((FromDigits[#, 3] &amp; /@ Transpose[#]) &amp; /@ Select[Partition[#, 3] &amp; /@ Tuples[Range[0, 2], {9}], Max[Count[#, 1] &amp; /@ #] &lt; 2 &amp;]), Boxed -&gt; False] ![enter image description here] You can also take slices, as shown in the Wolfram Demonstration [Menger Sponge Slices](http://demonstrations.wolfram.com/MengerSpongeSlices/). ![enter image description here] A string rewriting system gives a related fractal known as the [Sierpiński Carpet](http://mathworld.wolfram.com/SierpinskiCarpet.html). It is a face of the Menger sponge. Here are two different pieces of code to generate it. Row[{ArrayPlot[ Table[If[ Not[MemberQ[ Transpose[(IntegerDigits[#, 3, 5] &amp; /@ {a, b})], {1, 1}]], 1, 0], {a, 0, 3^5 - 1}, {b, 0, 3^5 - 1}], PixelConstrained -&gt; True, ImageSize -&gt; 250], Spacer, ArrayPlot[ Nest[ArrayFlatten[# /. {0 -&gt; {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, 1 -&gt; {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}}, 2] &amp;, {{1}}, 5], Frame -&gt; False, PixelConstrained -&gt; True, ImageSize -&gt; 250]}] ![enter image description here] Taking a horizontal line through the center gives the [Cantor set](http://mathworld.wolfram.com/CantorSet.html) (also known as the middle-thirds set), an infinite closed perfect set of zero measure. ![enter image description here] You might recognize the ternary (base 3) numbers in each set of code. For the 1D Cantor set, remove any ternary number with a 1. For the 2D Sierpinski carpet, if the *n*th ternary digits are both 1, remove that pair of numbers. For the 3D Menger sponge, if two or three of *n*th ternary digits are1, remove that triplet of numbers. All of these objects tend to a set of measure zero, whether the measure is length, area, or volume. So those are ways to get measure 0 with a fractal. How about making Pi with a fractal? These two infinite products look promising for 1D. ![enter image description here] Transforming the fractions into fractals requires adding a segment when the fraction is greater than 1. Here&#039;s what the fractals look like after a few steps, followed by a check to make sure the segment lengths are adding up correctly. ![enter image description here] I shall call the first of those the happy Pi fractal. To be more bold about it, here are those formulas again. ![enter image description here] Plotting the values given by these fractals gives something that looks like the [Minkowski ? function](http://mathworld.wolfram.com/MinkowskisQuestionMarkFunction.html), which is based on the [Farey sequence](http://mathworld.wolfram.com/FareySequence.html) of irreducible fractions. Build circles of radii twice the denominator squared above each fraction to obtain the [Ford circles](http://demonstrations.wolfram.com/FordCircles/), a fractal of tangent circles. ![enter image description here] An irreducible fraction a/b in the Farey sequence has gcd(a,b)=1 and a less than b. It happens that the probability of random integers being relatively prime with *a* less than *b* is 3/Pi^2. We can use that to approximate the length of F1422, all the irreducible fractions with denominator of 1422 or less. Out of a 1422*1422 grid of pixels, about 3/Pi^2percent of them should be black. ![enter image description here] By squaring 1422 and multiplying we should be able to get a reasonable approximation for the size of F1422. Then we can check to find the actual size. ![enter image description here] Fairly accurate, but enough with 1D. For a 2D fractal for Pi, we can use the Wallis product again. ![enter image description here] The corresponding fractal is the [Wallis sieve](http://demonstrations.wolfram.com/WallisSievePiApproximation/). Start with a 2*2 square and divide it into 4 squares. Divide each new subsquare by 3, and remove the middle square (8/9). Divide each new subsquare by 5, and remove the middle square (24/25). Divide each new subsquare by 7, and remove the middle square (48/49). Divide each new subsquare by 9, and remove the middle square (80/81). And so on. Here&#039;s what it looks like after a few steps. ![enter image description here] As mentioned earlier, this fractal has an area equal to Pi. ![enter image description here] Is there a Menger sponge equivalent of the Wallis sieve? At each step, we&#039;d divide the remaining cubes into the next largest odd cube, then take away the cubes extending out from the center. ![enter image description here] That&#039;s the same volume as a sphere of radius 1. What would one of these fractals look like? The code is similar: getting the digits 1, 2, or 3 in bases 3, 5, or 7 more than once causes a triplet to get tossed out. wallissponge = Select[Tuples[Select[Tuples[Range[0, 6], {3}], Sign[{3, 5, 7} - #] == {1, 1, 1} &amp;], {3}], Max[MapIndexed[Count[#1 - #2[], 0] &amp;, Transpose[#]]] &lt; 2 &amp;]; Graphics3D[{EdgeForm[None], Cuboid /@ ((5 7 #[] + 7 #[] + #[] &amp; /@ # ) &amp; /@ wallissponge)}, Boxed -&gt; False, ImageSize -&gt; 600] ![enter image description here] Use eight of these to get a fractal object with the same volume as the unit sphere. We&#039;ve taken some fractals of measure zero and tweaked them a bit to get Pi. If anyone can break this fractal into pieces and make a sphere, please let me know. : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng1.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng2.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng3.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng4.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng5.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng6.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng7.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng8.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng9.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng15.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng10.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng11.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng12.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng13.gif&amp;userId=21530 : http://community.wolfram.com//c/portal/getImageAttachment?filename=meng14.gif&amp;userId=21530 Ed Pegg 2016-03-14T14:42:30Z The Most Convenient Keyboard Shortcuts Ever https://community.wolfram.com/groups/-/m/t/1855244 Here are two simple but extremely convenient keyboard shortcuts that I&#039;ve used for years. - This post is a favor to all my friends who type slowly (like me) - These shortcuts simply create a pair of brackets containing a template placeholder as the current new selection - Essentially, it&#039;s just a stripped-down version of the bulky (Command + Shift + K + Up/Down Arrow + Enter) completion **Favorite Shortcut #1:** Autocomplete single brackets by pressing CommandKey + [ ![enter image description here] **Favorite Shortcut #2:** Autocomplete part brackets by pressing CommandKey + ] ![enter image description here] ## How to set them up ## On MacOS, first make a backup of the following file, in case you break it: FileNameJoin[{\$InstallationDirectory, &#034;SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr&#034;}]; On my machine this path resolves to: *&#034;/Applications/Mathematica.app/Contents/SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr&#034;* Then, preferably in a non-Mathematica editor (e.g. Sublime), open the above file and copy-and-paste these following code into the list inside EventTranslations. For the first shortcut: Item[KeyEvent[&#034;[&#034;,Modifiers-&gt;{Command}],FrontEndExecute[{FrontEndNotebookWrite[FrontEndInputNotebook[],&#034;[$SelectionPlaceholder]]&#034;],FrontEndToken[&#034;MovePreviousPlaceHolder&#034;]}]] And for the second (of course making sure they are separated by a comma): Item[KeyEvent[&#034;]&#034;,Modifiers-&gt;{Command}],FrontEndExecute[{FrontEndNotebookWrite[FrontEndInputNotebook[],&#034;\[LeftDoubleBracket]\[SelectionPlaceholder]\[RightDoubleBracket]&#034;],FrontEndToken[&#034;MovePreviousPlaceHolder&#034;],FrontEndToken[&#034;MovePreviousPlaceHolder&#034;]}]] Finally, restart Mathematica, and presto, they should both be working. And to remove the shortcuts, simply remove those items or replace the .tr file with the backup. : https://community.wolfram.com//c/portal/getImageAttachment?filename=own.gif&amp;userId=900170 : https://community.wolfram.com//c/portal/getImageAttachment?filename=part.gif&amp;userId=900170 Mike Sollami 2020-01-08T20:42:08Z [GIF] Rotation Redux (Inverse Cayley transform of rotating grid of circles) https://community.wolfram.com/groups/-/m/t/1857532 ![Inverse Cayley transform of rotating grid of circles] **Rotation Redux** A [recent post by Dave Whyte (a.k.a. @beesandbombs)] on Twitter reminded me of my old GIF [_Rotation_], which is almost 5 years old (and dates back to before I was posting on the Wolfram Community). I figured it would be fun to do a new twist on the same basic animation. The basic idea is fairly simple: start with a rotating grid of circles: ![Rotating grid of circles] Apply the map z \mapsto 1/z to the circles to invert them inside the unit circle: ![Rotating grid of circles, inverted inside unit circle] (Notice that the inside circles are rotating the other way; not surprising since 1/z is proportional to \bar{z}.) Now just apply the inverse of the Cayley transform which maps the upper half-plane to the interior of the unit circle (and the lower half-plane to the exterior) and you get the animation from the top of the post. InverseCayley[z_] := I (1 + z)/(1 - z); Here&#039;s the rest of the code: With[{r = 12., cols = RGBColor /@ {&#034;#381460&#034;, &#034;#f4f4f4&#034;}}, Manipulate[ Graphics[ {cols[], Thickness[.003], Line[{{-8, 0}, {8, 0}}], Table[ Polygon /@ Transpose[ Table[ {ReIm[InverseCayley[E^(I t) + #]], ReIm[InverseCayley[1/(E^(I t) + #)]]} &amp;[E^(I θ)*(a + I b)], {t, 0., 2 π, If[Norm[a + I b] &lt; 8, 2 π/400, 2 π/20]} ] ], {a, -r, r, 2}, {b, DeleteCases[Range[-r, r, 2], If[a == 0., 0., 100]]}], If[Abs[θ - π/2] &lt; .01, {Polygon[{{-8, 2.1}, {8, 2.1}, {8, 10}, {-8, 10}}], Polygon[{{-8, -2.1}, {8, -2.1}, {8, -10}, {-8, -10}}]}] }, PlotRange -&gt; 4.5, Axes -&gt; False, ImageSize -&gt; {540, 540}, Background -&gt; cols[[-1]]], {θ, π/4, 3 π/4}] ] : https://community.wolfram.com//c/portal/getImageAttachment?filename=HRot12c.gif&amp;userId=610054 : https://twitter.com/beesandbombs/status/1215997000791011328 : https://shonkwiler.org/featured/rotation : https://community.wolfram.com//c/portal/getImageAttachment?filename=ER2c.gif&amp;userId=610054 : https://community.wolfram.com//c/portal/getImageAttachment?filename=ER3c.gif&amp;userId=610054 Clayton Shonkwiler 2020-01-13T00:43:19Z How-To-Guide: External GPU on OSX - how to use CUDA on your Mac https://community.wolfram.com/groups/-/m/t/1085633 The neural network and machine learning framework has become one of the key features of the latest releases of the Wolfram Language. Training neural networks can be very time consuming on a standard CPU. Luckily the Wolfram Language offers an incredible easy way to use a GPU to train networks - and do lots of other cool stuff. The problem with this was/is that most current Macs do not have an NVIDIA graphics card, which is necessary to access this framework within the Wolfram Language. Therefore, Wolfram Inc. had decided to drop support for GPUs on Macs. There is however a way to use GPUs on Macs. For example you can use an [external GPU like the one offered by Bizon]. ![enter image description here] Apart from the BizonBox there a couple of cables and a power supply. You can buy/configure different versions of the BizonBox: there is a range of different graphics cards available and you can buy a the BizonBox 2s which basically connects via Thunderbolt and the BizonBox 3 which connects to USB-C. Luckily, Wolfram have decided to reintroduce support for GPUs in Mathematica 11.1.1 - see [the discussion here]. I have a variety of these BizonBoxes (both 2s and 3) and a range of Macs. I thought it would be a good idea to post a how-to. The essence of what I will be describing in this post should work for most Macs. I ran Sierra on all of them. Here is the recipe to get the thing to work: Installation of the BizonBox, the required drivers, and compilers ----------------------------------------------------------------- 0. I will assume that you have Sierra installed and that Xcode is running. One of the really important steps if you want to use compilers is to ***downgrade*** the command line tools to version 7.3 You will have to log into your Apple Developer account and download the Command Line Tools version 7.3. Install the tools and run the terminal command (not in Mathematica!): sudo xcode-select --switch /Library/Developer/CommandLineTools 1. Reboot your Mac into safe mode, i.e. hold CMD+R while rebooting. 2. Open a terminal (under item Utilities at the top of the screen). 3. Enter csrutil disable 4. Shut the computer down. 5. Connect your BizonBox to the mains and to either the thunderbolt or USB-C port of your Mac. 6. Restart your Mac. 7. Click on the Apple symbol in the top left. Then &#034;About this Mac&#034; and &#034;System Report&#034;. In the Thunderbolt section you should see something like this: ![enter image description here] 8. In the documentation of the BizonBox you will find a link to a program called bizonboxmac.zip. Download that file and unzip it. 9. Open the folder and click on &#034;bizonbox.prefPane&#034; to install. (If prompted to, do update!) 10. You should see this window: ![enter image description here] 11. Click on Activate. Type in password if required to do so. It should give something like this: ![enter image description here] Then restart. 12. Install the CUDA Toolkit: [https://developer.nvidia.com/cuda-downloads]. You&#039;ll have to click through some questions for the download. ![enter image description here] what you download should be something like cuda_8.0.61_mac.dmg and it should be more or less 1.44 GB worth. 13. Install the toolkit with all its elements. ![enter image description here] 14. Restart your computer. First tests ----------- Now you should be good to go. Open Mathematica 11.1.1. Execute Needs[&#034;CUDALink&#034;] Needs[&#034;CCompilerDriver&#034;] CUDAResourcesInstall[] Then try: CUDAResourcesInformation[] which should look somewhat like this: ![enter image description here] Then you should check SystemInformation[] Head to Links and then CUDA.This should look similar to this: ![enter image description here] So far so good. Next is the really crucial thing: CUDAQ[] should give TRUE. If that&#039;s what you see you are good to go. Be more daring and try CUDAImageConvolve[ExampleData[{&#034;TestImage&#034;,&#034;Lena&#034;}], N[BoxMatrix/9]] // AbsoluteTiming ![enter image description here] You might notice that the non-GPU version of this command runs faster: ImageConvolve[ExampleData[{&#034;TestImage&#034;,&#034;Lena&#034;}], N[BoxMatrix/9]] // AbsoluteTiming runs in something like 0.0824 seconds, but that&#039;s ok. Benchmarking (training neural networks) --------------------------------------- Let&#039;s do some Benchmarking. Download some example data: obj = ResourceObject[&#034;CIFAR-10&#034;]; trainingData = ResourceData[obj, &#034;TrainingData&#034;]; RandomSample[trainingData, 5] You can check whether it worked: RandomSample[trainingData, 5] should give something like this: ![enter image description here] These are the classes of the 50000 images: classes = Union@Values[trainingData] ![enter image description here] Let&#039;s build a network module = NetChain[{ConvolutionLayer[100, {3, 3}], BatchNormalizationLayer[], ElementwiseLayer[Ramp], PoolingLayer[{3, 3}, &#034;PaddingSize&#034; -&gt; 1]}] net = NetChain[{module, module, module, module, FlattenLayer[], 500, Ramp, 10, SoftmaxLayer[]}, &#034;Input&#034; -&gt; NetEncoder[{&#034;Image&#034;, {32, 32}}], &#034;Output&#034; -&gt; NetDecoder[{&#034;Class&#034;, classes}]] When you train the network: {time, trained} = AbsoluteTiming@NetTrain[net, trainingData, Automatic, &#034;TargetDevice&#034; -&gt; &#034;GPU&#034;]; you should see something like this: ![enter image description here] So the thing started 45 secs ago and it supposed to finish in 2m54s. In fact, it finished after 3m30s. If we run the same on the CPU we get: ![enter image description here] The estimate kept changing a bit, but it settled down at about 18h20m.That is slower by a factor of about 315, which is quite substantial. Use of compiler --------------- Up to now we have not needed the actual compiler. Let&#039;s try this, too. Let&#039;s grow a Mandelbulb: width = 4*640; height = 4*480; iconfig = {width, height, 1, 0, 1, 6}; config = {0.001, 0.0, 0.0, 0.0, 8.0, 15.0, 10.0, 5.0}; camera = {{2.0, 2.0, 2.0}, {0.0, 0.0, 0.0}}; AppendTo[camera, Normalize[camera[] - camera[]]]; AppendTo[camera, 0.75*Normalize[Cross[camera[], {0.0, 1.0, 0.0}]]]; AppendTo[camera, 0.75*Normalize[Cross[camera[], camera[]]]]; config = Join[{config, Flatten[camera]}]; pixelsMem = CUDAMemoryAllocate[&#034;Float&#034;, {height, width, 3}] srcf = FileNameJoin[{CUDALinkPath, &#034;SupportFiles&#034;, &#034;mandelbulb.cu&#034;}] Now this should work: mandelbulb = CUDAFunctionLoad[File[srcf], &#034;MandelbulbGPU&#034;, {{&#034;Float&#034;, _, &#034;Output&#034;}, {&#034;Float&#034;, _, &#034;Input&#034;}, {&#034;Integer32&#034;, _, &#034;Input&#034;}, &#034;Integer32&#034;, &#034;Float&#034;, &#034;Float&#034;}, {16}, &#034;UnmangleCode&#034; -&gt; False, &#034;CompileOptions&#034; -&gt; &#034;--Wno-deprecated-gpu-targets &#034;, &#034;ShellOutputFunction&#034; -&gt; Print] Under certain circumstances you might want to specify the location of the compiler like so: mandelbulb = CUDAFunctionLoad[File[srcf], &#034;MandelbulbGPU&#034;, {{&#034;Float&#034;, _, &#034;Output&#034;}, {&#034;Float&#034;, _, &#034;Input&#034;}, {&#034;Integer32&#034;, _, &#034;Input&#034;}, &#034;Integer32&#034;, &#034;Float&#034;, &#034;Float&#034;}, {16}, &#034;UnmangleCode&#034; -&gt; False, &#034;CompileOptions&#034; -&gt; &#034;--Wno-deprecated-gpu-targets &#034;, &#034;ShellOutputFunction&#034; -&gt; Print, &#034;CompilerInstallation&#034; -&gt; &#034;/Developer/NVIDIA/CUDA-8.0/bin/&#034;] This should give: ![enter image description here] Now mandelbulb[pixelsMem, Flatten[config], iconfig, 0, 0.0, 0.0, {width*height*3}]; pixels = CUDAMemoryGet[pixelsMem]; Image[pixels] gives ![enter image description here] So it appears that all is working fine. Problems -------- I did come up with some problems though. There is quite a number of CUDA functions: Names[&#034;CUDALink*&#034;] ![enter image description here] Many work just fine. res = RandomReal[1, 5000]; ListLinePlot[res] ![enter image description here] ListLinePlot[First@CUDAImageConvolve[{res}, {GaussianMatrix[{{10}, 10}]}]] ![enter image description here] The thing is that some don&#039;t and I am not sure why (I have a hypothesis though). Here are some functions that do **not** appear to work: CUDAColorNegate CUDAClamp CUDAFold CUDAVolumetricRender CUDAFluidDynamics and some more. I would be very grateful if someone could check these on OSX (and perhaps Windows?). I am not sure if the this is due to some particularity of my systems or something that could be flagged up to Wolfram Inc for checking. When I wanted to try that systematically I wanted to use the function WolframLanguageData to look for the first example in the documentation of the CUDA functions, but it appears that no CUDA function is in the WolframLanguageData. I think tit would be great to have them there, too, and am not sure why they wouldn&#039;t be there. In spite of these problems I hope that this post will help some Mac users to get CUDA going. It is a great framework and simple to use in the Wolfram Language. With the BizonBox and Mathematica 11.1.1 Mac users are no longer excluded from accessing this feature. Cheers, Marco PS: Note, that there is anecdotal evidence that one can even use the BizonBox under Windows running in a virtual box under OSX. I don&#039;t have Windows, but I&#039;d like to hear if anyone get this running. : https://bizon-tech.com : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at22.09.10.png&amp;userId=48754 : http://community.wolfram.com/groups/-/m/t/902394 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot1.png&amp;userId=48754 : http://bizon-tech.com/bizonboxmac.zip : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot3.png&amp;userId=48754 : https://developer.nvidia.com/cuda-downloads : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot6.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot7.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at22.38.22.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-06at18.46.46.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at22.49.15.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at22.52.43.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at22.53.30.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at20.37.20.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at20.39.02.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at23.04.38.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at21.50.42.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at23.10.05.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at23.15.59.png&amp;userId=48754 : http://community.wolfram.com//c/portal/getImageAttachment?filename=ScreenShot2017-05-07at23.16.36.png&amp;userId=48754 Marco Thiel 2017-05-07T22:21:42Z Using SPARQL to construct a timeline of the Supreme Court Chief Justices https://community.wolfram.com/groups/-/m/t/1857773 The intent of this post is to show those inexpert in SPARQL but curious about capabilities how much can be done with it within the Wolfram Language. My specific quest is to generate a timeline of chief justices of the United States Supreme Court but, in the course of doing so, to share some learning about SPARQL and Wikidata. The main insight is to exploit the many examples of SPARQL code at the Wikidata query site, find one that is relevant, and then use ImportString[#,&#034;SPARQLQuery&#034;]&amp; to generate symbolic SPARQL within the Wolfram Language. Once that&#039;s done, we can use core Wolfram Language tools to modify the code for the specific task at hand and then run it against the Wikidata &#034;endpoint.&#034; Once the results come back from Wikidata, some basic Wolfram Language code generates a nice timeline with relative ease. The notebook finishes with a little section on how to run Python from within a Wolfram notebook and get the same kind of results. Again, one doesn&#039;t need to know much about Python to do this (trust me!). Wikidata actually itself provides the basic code. And, as version 12.1 and beyond permit the Wolfram ecosystem to expand its capabilities with other languages, it may be possible to use Wikidata&#039;s fluency in multiple computer languages (Ruby, JavaScript, etc.) to diversify the set of tools with which to attack Wikidata while remaining in the comfortable and powerful Wolfram system. &amp;[Wolfram Notebook] : https://www.wolframcloud.com/obj/wolfram-community/Published/Chief%20Justice%20Timeline%20from%20Wikidata.nb Seth Chandler 2020-01-13T16:50:45Z [Notebook] How to make a Christmas card on the cloud https://community.wolfram.com/groups/-/m/t/1849339 [![enter image description here]](https://www.wolframcloud.com/env/silviah/Published/xmas.html) **POSTCARD:** https://www.wolframcloud.com/env/silviah/Published/xmas.html &amp;[embedded notebook] : https://community.wolfram.com//c/portal/getImageAttachment?filename=Xmas_postcard.jpg&amp;userId=20103 : https://www.wolframcloud.com/obj/wolfram-community/Published/How_to_make_a_Christmas_card_on_the_cloud.nb Silvia Hao 2019-12-29T14:07:04Z Finding all structural breaks in time series https://community.wolfram.com/groups/-/m/t/1749226 ## Introduction In this document we show how to find the so called &#034;structural breaks&#034;, [[Wk1](https://en.wikipedia.org/wiki/Structural_break)], in a given time series. The algorithm is based in on a systematic application of Chow Test, [[Wk2](https://en.wikipedia.org/wiki/Chow_test)], combined with an algorithm for local extrema finding in noisy time series, [[AA1](https://mathematicaforprediction.wordpress.com/2015/09/27/finding-local-extrema-in-noisy-data-using-quantile-regression/)]. The algorithm implementation is based on the packages [&#034;MonadicQuantileRegression.m&#034;](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicQuantileRegression.m), [[AAp1](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicQuantileRegression.m)], and [&#034;MonadicStructuralBreaksFinder.m&#034;](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicStructuralBreaksFinder.m), [[AAp2](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicStructuralBreaksFinder.m)]. The package [[AAp1](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicQuantileRegression.m)] provides the software monad QRMon that allows rapid and concise specification of [Quantile Regression](https://en.wikipedia.org/wiki/Quantile_regression) workflows. The package [[AAp2](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicStructuralBreaksFinder.m)] extends QRMon with functionalities related to structural breaks finding. ### What is a structural break? It looks like at least one type of &#034;structural breaks&#034; are defined through regression models, [[Wk1](https://en.wikipedia.org/wiki/Structural_break)]. Roughly speaking a structural break point of time series is a regressor point that splits the time series in such way that the obtained two parts have very different regression parameters. One way to test such a point is to use Chow test, [[Wk2](https://en.wikipedia.org/wiki/Chow_test)]. From [[Wk2](https://en.wikipedia.org/wiki/Chow_test)] we have the definition: The Chow test, proposed by econometrician Gregory Chow in 1960, is a test of whether the true coefficients in two linear regressions on different data sets are equal. In econometrics, it is most commonly used in time series analysis to test for the presence of a structural break at a period which can be assumed to be known a priori (for instance, a major historical event such as a war). ### Example Here is an example of the described algorithm application to the data from [[Wk2](https://en.wikipedia.org/wiki/Chow_test#/media/File:Chowtest4.svg)]. QRMonUnit[data]⟹QRMonPlotStructuralBreakSplits[ImageSize -&gt; Small]; ![IntroductionsExample](https://github.com/antononcube/MathematicaForPrediction/raw/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Introductions-example.png) ## Load packages Here we load the packages [AAp1] and [AAp2]. Import[&#034;https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicQuantileRegression.m&#034;] Import[&#034;https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicStructuralBreaksFinder.m&#034;] ## Data used In this section we assign the data used in this document. ### Illustration data from Wikipedia Here is the data used in the Wikipedia article &#034;Chow test&#034;, [[Wk2](https://en.wikipedia.org/wiki/Chow_test#/media/File:Chowtest4.svg)]. data = {{0.08, 0.34}, {0.16, 0.55}, {0.24, 0.54}, {0.32, 0.77}, {0.4, 0.77}, {0.48, 1.2}, {0.56, 0.57}, {0.64, 1.3}, {0.72, 1.}, {0.8, 1.3}, {0.88, 1.2}, {0.96, 0.88}, {1., 1.2}, {1.1, 1.3}, {1.2, 1.3}, {1.3, 1.4}, {1.4, 1.5}, {1.4, 1.5}, {1.5, 1.5}, {1.6, 1.6}, {1.7, 1.1}, {1.8, 0.98}, {1.8, 1.1}, {1.9, 1.4}, {2., 1.3}, {2.1, 1.5}, {2.2, 1.3}, {2.2, 1.3}, {2.3, 1.2}, {2.4, 1.1}, {2.5, 1.1}, {2.6, 1.2}, {2.6, 1.4}, {2.7, 1.3}, {2.8, 1.6}, {2.9, 1.5}, {3., 1.4}, {3., 1.8}, {3.1, 1.4}, {3.2, 1.4}, {3.3, 1.4}, {3.4, 2.}, {3.4, 2.}, {3.5, 1.5}, {3.6, 1.8}, {3.7, 2.1}, {3.8, 1.6}, {3.8, 1.8}, {3.9, 1.9}, {4., 2.1}}; ListPlot[data] ![DataUsedWk2](https://github.com/antononcube/MathematicaForPrediction/raw/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Data-used-Wk2.png) ### S&amp;P 500 Index Here we get the time series corresponding to [S&amp;P 500 Index](https://en.wikipedia.org/wiki/S%26P_500_Index). tsSP500 = FinancialData[Entity[&#034;Financial&#034;, &#034;^SPX&#034;], {{2015, 1, 1}, Date[]}] DateListPlot[tsSP500, ImageSize -&gt; Medium] ![DataUsedSP500](https://github.com/antononcube/MathematicaForPrediction/raw/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Data-used-SP500.png) ## Application of Chow Test The Chow Test statistic is implemented in [AAp1]. In this document we rely on the relative comparison of the Chow Test statistic values: the larger the value of the Chow test statistic, the more likely we have a structural break. Here is how we can apply the Chow Test with a QRMon pipeline to the [Wk2] data given above. chowStats = QRMonUnit[data]⟹ QRMonChowTestStatistic[Range[1, 3, 0.05], {1, x}]⟹ QRMonTakeValue; We see that the regressor points \text{\Failed} and 1.7 have the largest Chow Test statistic values. Block[{chPoint = TakeLargestBy[chowStats, Part[#, 2]&amp; , 1]}, ListPlot[{chowStats, chPoint}, Filling -&gt; Axis, PlotLabel -&gt; Row[{&#034;Point with largest Chow Test statistic:&#034;, Spacer, chPoint}]]] ![ApplicationOfChowTestchowStats](https://github.com/antononcube/MathematicaForPrediction/raw/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Application-of-Chow-Test-chowStats.png) The first argument of QRMonChowTestStatistic is a list of regressor points or Automatic. The second argument is a list of functions to be used for the regressions. Here is an example of an automatic values call. chowStats2 = QRMonUnit[data]⟹QRMonChowTestStatistic⟹QRMonTakeValue; ListPlot[chowStats2, GridLines -&gt; { Part[ Part[chowStats2, All, 1], OutlierIdentifiersOutlierPosition[ Part[chowStats2, All, 2], OutlierIdentifiers`SPLUSQuartileIdentifierParameters]], None}, GridLinesStyle -&gt; Directive[{Orange, Dashed}], Filling -&gt; Axis] ![ApplicationOfChowTestchowStats2](https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Application-of-Chow-Test-chowStats2.png) For the set of values displayed above we can apply simple 1D outlier identification methods, [[AAp3](https://github.com/antononcube/MathematicaForPrediction/blob/master/OutlierIdentifiers.m)], to automatically find the structural break point. chowStats2[[All, 1]][[OutlierPosition[chowStats2[[All, 2]], SPLUSQuartileIdentifierParameters]]] (* {1.7} *) OutlierPosition[chowStats2[[All, 2]], SPLUSQuartileIdentifierParameters] (* {20} *) We cannot use that approach for finding all structural breaks in the general time series cases though as exemplified with the following code using the time series S&amp;P 500 Index. chowStats3 = QRMonUnit[tsSP500]⟹QRMonChowTestStatistic⟹QRMonTakeValue; DateListPlot[chowStats3, Joined -&gt; False, Filling -&gt; Axis] ![ApplicationOfChowTestSP500](https://github.com/antononcube/MathematicaForPrediction/raw/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Application-of-Chow-Test-SP500.png) OutlierPosition[chowStats3[[All, 2]], SPLUSQuartileIdentifierParameters] (* {} *) OutlierPosition[chowStats3[[All, 2]], HampelIdentifierParameters] (* {} *) In the rest of the document we provide an algorithm that works for general time series. ## Finding all structural break points Consider the problem of finding of **all** structural breaks in a given time series. That can be done (reasonably well) with the following procedure. 1. Chose functions for testing for structural breaks (usually linear.) 2. Apply Chow Test over dense enough set of regressor points. 3. Make a time series of the obtained Chow Test statistics. 4. Find the local maxima of the Chow Test statistics time series. 5. Determine the most significant break point. 6. Plot the splits corresponding to the found structural breaks. QRMon has a function, QRMonFindLocalExtrema, for finding local extrema; see [AAp1, AA1]. For the goal of finding all structural breaks, that semi-symbolic algorithm is the crucial part in the steps above. ## Computation ### Chose fitting functions fitFuncs = {1, x}; ### Find Chow test statistics local maxima The computation below combines steps 2,3, and 4. qrObj = QRMonUnit[tsSP500]⟹ QRMonFindChowTestLocalMaxima[&#034;Knots&#034; -&gt; 20, &#034;NearestWithOutliers&#034; -&gt; True, &#034;NumberOfProximityPoints&#034; -&gt; 5, &#034;EchoPlots&#034; -&gt; True, &#034;DateListPlot&#034; -&gt; True, ImageSize -&gt; Medium]⟹ QRMonEchoValue; ![ComputationLocalMaxima](https://github.com/antononcube/MathematicaForPrediction/raw/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Computation-local-maxima.png) ### Find most significant structural break point splitPoint = TakeLargestBy[qrObj⟹QRMonTakeValue, #[] &amp;, 1][[1, 1]] ### Plot structural breaks splits and corresponding fittings Here we just make the plots without showing them. sbPlots = QRMonUnit[tsSP500]⟹ QRMonPlotStructuralBreakSplits[(qrObj⟹ QRMonTakeValue)[[All, 1]], &#034;LeftPartColor&#034; -&gt; Gray, &#034;DateListPlot&#034; -&gt; True, &#034;Echo&#034; -&gt; False, ImageSize -&gt; Medium]⟹ QRMonTakeValue; The function QRMonPlotStructuralBreakSplits returns an association that has as keys paired split points and Chow Test statistics; the plots are association&#039;s values. Here we tabulate the plots with plots with most significant breaks shown first. Multicolumn[ KeyValueMap[ Show[#2, PlotLabel -&gt; Grid[{{&#034;Point:&#034;, #1[]}, {&#034;Chow Test statistic:&#034;, #1[]}}, Alignment -&gt; Left]] &amp;, KeySortBy[sbPlots, -#[] &amp;]], 2] ![ComputationStructuralBreaksPlots](https://github.com/antononcube/MathematicaForPrediction/raw/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Computation-structural-breaks-plots.png) ## Future plans We can further apply the algorithm explained above to identifying time series states or components. The structural break points are used as knots in appropriate Quantile Regression fitting. Here is an example. The plan is to develop such an identifier of time series states in the near future. (And present it at [WTC-2019](https://www.wolfram.com/events/technology-conference/2019/).) ![FuturePlansTimeSeriesStates](https://github.com/antononcube/MathematicaForPrediction/raw/master/MarkdownDocuments/Diagrams/Finding-all-structural-breaks-in-time-series/Future-plans-time-series-states.png) ## References ### Articles \[Wk1$ Wikipedia entry, [Structural breaks](https://en.wikipedia.org/wiki/Structural_break). $Wk2$ Wikipedia entry, [Chow test](https://en.wikipedia.org/wiki/Chow_test). $AA1$ Anton Antonov, [&#034;Finding local extrema in noisy data using Quantile Regression&#034;](https://mathematicaforprediction.wordpress.com/2015/09/27/finding-local-extrema-in-noisy-data-using-quantile-regression/), (2019), [MathematicaForPrediction at WordPress](https://mathematicaforprediction.wordpress.com/2015/09/27/finding-local-extrema-in-noisy-data-using-quantile-regression/). $AA2$ Anton Antonov, [&#034;A monad for Quantile Regression workflows&#034;](https://github.com/antononcube/MathematicaForPrediction/blob/master/MarkdownDocuments/A-monad-for-Quantile-Regression-workflows.md), (2018), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction/). ### Packages $AAp1$ Anton Antonov, [Monadic Quantile Regression Mathematica package](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicQuantileRegression.m), (2018), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction/). $AAp2$ Anton Antonov, [Monadic Structural Breaks Finder Mathematica package](https://github.com/antononcube/MathematicaForPrediction/blob/master/MonadicProgramming/MonadicStructuralBreaksFinder.m), (2019), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction/). $AAp3$ Anton Antonov, [Implementation of one dimensional outlier identifying algorithms in Mathematica](https://github.com/antononcube/MathematicaForPrediction/blob/master/OutlierIdentifiers.m), (2013), [MathematicaForPrediction at GitHub](https://github.com/antononcube/MathematicaForPrediction/). ### Videos $AAv1$ Anton Antonov, Structural Breaks with QRMon, (2019), YouTube. Anton Antonov 2019-07-31T19:26:28Z [Notebook] Summable Areas https://community.wolfram.com/groups/-/m/t/1849304 ##### *This is an interesting geometry problem posted by Dr. Shan Zun, ex-IMO head coach, PRC* ---------- ![illustration] &amp;[embedded notebook] : https://community.wolfram.com//c/portal/getImageAttachment?filename=illustration.png&amp;userId=23928 : https://www.wolframcloud.com/obj/wolfram-community/Published/EqualSumArea_Publish.nb Shenghui Yang 2019-12-28T21:53:45Z