├── .dockerignore ├── .gitignore ├── Data Analysis ├── Analyzing baby names │ ├── Use Case - Daru for analyzing baby names data.ipynb │ └── data │ │ ├── yob1951.txt │ │ ├── yob1952.txt │ │ ├── yob1953.txt │ │ ├── yob1954.txt │ │ ├── yob1955.txt │ │ ├── yob1956.txt │ │ ├── yob1957.txt │ │ ├── yob1958.txt │ │ ├── yob1959.txt │ │ ├── yob1960.txt │ │ ├── yob1961.txt │ │ ├── yob1962.txt │ │ ├── yob1963.txt │ │ ├── yob1964.txt │ │ ├── yob1965.txt │ │ ├── yob1966.txt │ │ ├── yob1967.txt │ │ ├── yob1968.txt │ │ ├── yob1969.txt │ │ ├── yob1970.txt │ │ ├── yob1971.txt │ │ ├── yob1972.txt │ │ ├── yob1973.txt │ │ ├── yob1974.txt │ │ ├── yob1975.txt │ │ ├── yob1976.txt │ │ ├── yob1977.txt │ │ ├── yob1978.txt │ │ ├── yob1979.txt │ │ ├── yob1980.txt │ │ ├── yob1981.txt │ │ ├── yob1982.txt │ │ ├── yob1983.txt │ │ ├── yob1984.txt │ │ ├── yob1985.txt │ │ ├── yob1986.txt │ │ ├── yob1987.txt │ │ ├── yob1988.txt │ │ ├── yob1989.txt │ │ ├── yob1990.txt │ │ ├── yob1991.txt │ │ ├── yob1992.txt │ │ ├── yob1993.txt │ │ ├── yob1994.txt │ │ ├── yob1995.txt │ │ ├── yob1996.txt │ │ ├── yob1997.txt │ │ ├── yob1998.txt │ │ ├── yob1999.txt │ │ ├── yob2000.txt │ │ ├── yob2001.txt │ │ ├── yob2002.txt │ │ ├── yob2003.txt │ │ ├── yob2004.txt │ │ ├── yob2005.txt │ │ ├── yob2006.txt │ │ ├── yob2007.txt │ │ ├── yob2008.txt │ │ ├── yob2009.txt │ │ ├── yob2010.txt │ │ ├── yob2011.txt │ │ ├── yob2012.txt │ │ ├── yob2013.txt │ │ └── yob2014.txt ├── Basic Time Series.ipynb ├── Categorical Data │ ├── Categorical Data.ipynb │ ├── Indexing in DataFrame.ipynb │ ├── Indexing in Vector.ipynb │ ├── data │ │ └── animal_shelter_train.csv │ └── examples │ │ ├── [Example] Categorical Data.ipynb │ │ ├── [Example] Categorical Index.ipynb │ │ └── [Example] Formula language in Statsample-GLM.ipynb ├── Creation of Vector and DataFrame.ipynb ├── Daru Demo.ipynb ├── Finding and plotting the most heard artists on last fm.ipynb ├── Grouping, Splitting and Pivoting.ipynb ├── Logistic Regression with daru and statsample-glm.ipynb ├── Plotting │ ├── Gnuplotrb.ipynb │ ├── Gruff DataFrame.ipynb │ ├── Gruff Vector.ipynb │ └── Visualization.ipynb ├── Searching and Combining Data.ipynb ├── Time Series Functions.ipynb ├── Usage of DataFrame.ipynb ├── Usage of Vector.ipynb └── data │ ├── sales-funnel.csv │ └── test_xls.xls ├── Dockerfile ├── IRuby Examples ├── demo.ipynb ├── display.ipynb ├── history.ipynb ├── input.ipynb ├── ruby.png ├── ruby.svg ├── stdout.ipynb └── table.ipynb ├── Makefile ├── README.md ├── Statistics ├── Correlation Matrix with daru and statsample.ipynb ├── Dominance Analysis with statsample.ipynb ├── Levene Test.ipynb ├── Mann Whitney U Test.ipynb ├── Multiple Regression.ipynb ├── Parallel Analysis on PCA.ipynb ├── Polychoric Correlation.ipynb ├── Principal Axis Analysis.ipynb ├── Reliability ICC with statsample.ipynb ├── Reliability Scale Analysis.ipynb ├── T Test.ipynb ├── Tetrachoric Correlation.ipynb └── Velicer MAP test.ipynb ├── Symbolic Mathematics ├── beginner.ipynb └── differentiation_example.ipynb ├── Visualization ├── Boxplot with daru and statsample.ipynb ├── Creating a Histogram.ipynb ├── Scatterplot with statsample.ipynb └── Visualizing data with daru DataFrame.ipynb └── getting_started.ipynb /.dockerignore: -------------------------------------------------------------------------------- 1 | Dockerfile 2 | .dockerignore 3 | .gitignore 4 | README.md 5 | Makefile 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints/ 2 | -------------------------------------------------------------------------------- /Data Analysis/Creation of Vector and DataFrame.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Demostrate basic creation of Vector and DataFrame" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "application/javascript": [ 20 | "if(window['d3'] === undefined ||\n", 21 | " window['Nyaplot'] === undefined){\n", 22 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 23 | "\n", 24 | "\n", 25 | "\n", 26 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 27 | "\n", 28 | " require.config({paths: path, shim:shim});\n", 29 | "\n", 30 | "\n", 31 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 32 | "\n", 33 | "\tvar script = d3.select(\"head\")\n", 34 | "\t .append(\"script\")\n", 35 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 36 | "\t .attr(\"async\", true);\n", 37 | "\n", 38 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 39 | "\n", 40 | "\n", 41 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 42 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 43 | "\t window.dispatchEvent(event);\n", 44 | "\t console.log('Finished loading Nyaplotjs');\n", 45 | "\n", 46 | "\t};\n", 47 | "\n", 48 | "\n", 49 | "});});\n", 50 | "}\n" 51 | ], 52 | "text/plain": [ 53 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 54 | ] 55 | }, 56 | "metadata": {}, 57 | "output_type": "display_data" 58 | }, 59 | { 60 | "data": { 61 | "text/html": [ 62 | "
Daru::DataFrame:19365600 rows: 1000 cols: 2
ba
02
100
22
323
432
52
630
721
83
921
1033
1123
1213
132
1420
150
1613
1712
180
1932
2021
2100
223
2301
2401
253
26
270
2801
2931
3023
3121
.........
99931
" 63 | ], 64 | "text/plain": [ 65 | "\n", 66 | "#\n", 67 | " b a \n", 68 | " 0 2 nil \n", 69 | " 1 0 0 \n", 70 | " 2 2 nil \n", 71 | " 3 2 3 \n", 72 | " 4 3 2 \n", 73 | " 5 nil 2 \n", 74 | " 6 3 0 \n", 75 | " 7 2 1 \n", 76 | " 8 3 nil \n", 77 | " 9 2 1 \n", 78 | " 10 3 3 \n", 79 | " 11 2 3 \n", 80 | " 12 1 3 \n", 81 | " 13 nil 2 \n", 82 | " 14 2 0 \n", 83 | " ... ... ... \n" 84 | ] 85 | }, 86 | "execution_count": 1, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "require 'daru'\n", 93 | "samples = 1000\n", 94 | "\n", 95 | "# We set lazy_update to *true* so that time is not wasted in updating\n", 96 | "# metdata every time an assignment happens.\n", 97 | "Daru.lazy_update = true\n", 98 | "# The 'new_with_size' function lets you specify the size of the \n", 99 | "# vector as the argument and the block specifies how each element\n", 100 | "# of the vector will be created.\n", 101 | "a = Daru::Vector.new_with_size(samples) {r=rand(5); r==4 ? nil: r}\n", 102 | "b = Daru::Vector.new_with_size(samples) {r=rand(5); r==4 ? nil: r}\n", 103 | "\n", 104 | "# Call `#update` for updating the metadata of each vector, once \n", 105 | "# creation from the block is complete.\n", 106 | "a.update\n", 107 | "b.update\n", 108 | "\n", 109 | "# Pass the Daru::Vector objects in a Hash to the DataFrame constructor\n", 110 | "# to make a DataFrame.\n", 111 | "# \n", 112 | "# The *order* option lets you specify the way the vectors in the Hash \n", 113 | "# will be ordered. Not specifyin this will order vectors in alphabetical\n", 114 | "# order by default.\n", 115 | "ds = Daru::DataFrame.new({:a=>a,:b=>b}, order: [:b, :a])\n", 116 | "\n", 117 | "# Reset lazy_update to *false* to prevent other code from breaking.\n", 118 | "Daru.lazy_update = false\n", 119 | "\n", 120 | "ds" 121 | ] 122 | } 123 | ], 124 | "metadata": { 125 | "kernelspec": { 126 | "display_name": "Ruby 2.2.1", 127 | "language": "ruby", 128 | "name": "ruby" 129 | }, 130 | "language_info": { 131 | "file_extension": ".rb", 132 | "mimetype": "application/x-ruby", 133 | "name": "ruby", 134 | "version": "2.2.1" 135 | } 136 | }, 137 | "nbformat": 4, 138 | "nbformat_minor": 0 139 | } 140 | -------------------------------------------------------------------------------- /Data Analysis/Finding and plotting the most heard artists on last fm.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Finding and plotting the most heard artists on last.fm\n", 8 | "\n", 9 | "This notebook is a super basic use case of daru for finding a plotting the most heard artists from a dataset from last.fm.\n", 10 | "\n", 11 | "For detailed and in-depth examples see the other notebooks." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "application/javascript": [ 24 | "if(window['d3'] === undefined ||\n", 25 | " window['Nyaplot'] === undefined){\n", 26 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 27 | "\n", 28 | "\n", 29 | "\n", 30 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 31 | "\n", 32 | " require.config({paths: path, shim:shim});\n", 33 | "\n", 34 | "\n", 35 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 36 | "\n", 37 | "\tvar script = d3.select(\"head\")\n", 38 | "\t .append(\"script\")\n", 39 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 40 | "\t .attr(\"async\", true);\n", 41 | "\n", 42 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 43 | "\n", 44 | "\n", 45 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 46 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 47 | "\t window.dispatchEvent(event);\n", 48 | "\t console.log('Finished loading Nyaplotjs');\n", 49 | "\n", 50 | "\t};\n", 51 | "\n", 52 | "\n", 53 | "});});\n", 54 | "}\n" 55 | ], 56 | "text/plain": [ 57 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 58 | ] 59 | }, 60 | "metadata": {}, 61 | "output_type": "display_data" 62 | }, 63 | { 64 | "data": { 65 | "text/plain": [ 66 | "true" 67 | ] 68 | }, 69 | "execution_count": 1, 70 | "metadata": {}, 71 | "output_type": "execute_result" 72 | } 73 | ], 74 | "source": [ 75 | "require 'daru'" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 2, 81 | "metadata": { 82 | "collapsed": false 83 | }, 84 | "outputs": [ 85 | { 86 | "data": { 87 | "text/html": [ 88 | "
Daru::DataFrame:24929480 rows: 2500 cols: 6
artidartnametimestamptraidtranameuserid
0e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:55:00Z0138547f-374d-49d4-8f1c-9b2c8b5c65c0X-Ray Eyesuser_000705
1e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:51:28Z22ce9a32-3510-4c8e-8151-c044a62eb4fdHard Timesuser_000705
2e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:46:55Z22ce9a32-3510-4c8e-8151-c044a62eb4fdHard Timesuser_000705
3e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:42:17Zc386b85f-1bfe-4038-b323-cb8229ab2f8bMagic Touchuser_000705
4e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:37:53Z8ec9b4f8-2d1b-4616-9ae4-4711ca328fc3Charismauser_000705
5e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:33:37Zff47d82e-313d-4bd0-8e61-fe61124a361bDirty Livin'user_000705
6e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:29:39Z3621800b-87b7-43c6-b6a3-47ea4b658407Sure Know Somethinguser_000705
7e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:24:46Z91cc5010-e65b-4f49-94b5-1deed61cfcc72,000 Manuser_000705
8e1f1e33e-2e4c-4d43-b91b-7064068d3283Kiss2009-05-03T12:20:17Zdaa13e72-9056-476f-b431-335b2c26842dI Was Made For Lovin' Youuser_000705
933b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:16:20Z1895c79d-ae5f-40bf-bc18-83fdcd8d2842Burning Of The Midnight Lampuser_000705
1033b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:12:35Z1ae3c250-7b16-49d4-9c71-0928724eecceGypsy Eyesuser_000705
1133b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:08:26Zfca63fe0-c9e6-4347-92b7-21f6cf79226cCome On, Part 1user_000705
1233b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:04:59Z36beb31d-ed74-4a49-832f-a4a38dc96babLong Hot Summer Nightuser_000705
1333b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T12:02:08Z4ad6064e-7d54-49a2-a134-320a1612ee27Little Miss Strangeuser_000705
1433b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T11:47:07Z40ffef8e-b851-473a-a239-08685eab22b0Voodoo Chileuser_000705
1533b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T11:44:43Zfeb64d01-6e05-4974-bb44-0046deca1bdbCrosstown Trafficuser_000705
1633b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T11:42:35Z7f9b462a-8ac5-472d-a5aa-716b3b3d169cHave You Ever Been (To Electric Ladyland)user_000705
1733b3c323-77c2-417c-a5b4-af7e6a111cc9The Jimi Hendrix Experience2009-05-03T11:41:13Z88b57468-8d58-4310-b135-c94620fcfbc1...And The Gods Made Loveuser_000705
180039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:24:28Z788878e0-8965-489e-86e3-add4c1f3d134Line Of Best Fituser_000705
190039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:19:58Z7cfc403d-18e7-4b8e-b0c4-1864ca393fbbFake Frownsuser_000705
200039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:15:03Z34a2d7ad-6b3a-4cfc-b4ca-edfc55a6685bAmputationsuser_000705
210039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:11:21Z99db2ed6-4493-481c-ac20-54ad323cb04bThe Face That Launched 1000 Shitsuser_000705
220039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:07:44Z269084ba-3065-4b43-a733-f491e618b26aSleep Spentuser_000705
230039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T09:03:54Z96fac269-5308-4aee-827f-d01a66674694Pictures In An Exhibitionuser_000705
240039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T08:59:34Zc29440d9-03e6-4e18-91d6-ef8bf1e1ca32Your Bruiseuser_000705
250039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T08:56:55Zed55b695-976e-4898-8fde-51a8e3b0e262Champagne From A Paper Cupuser_000705
260039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T08:49:07Zc5238d6d-9b04-496c-8674-4ea7a07aecd6President Of What?user_000705
270039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-02T08:44:34Zbf59dfef-03a8-4270-9456-50df79da56c2Bend To Squaresuser_000705
280039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-01T19:08:37Z788878e0-8965-489e-86e3-add4c1f3d134Line Of Best Fituser_000705
290039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-01T19:04:07Z7cfc403d-18e7-4b8e-b0c4-1864ca393fbbFake Frownsuser_000705
300039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-01T18:59:12Z34a2d7ad-6b3a-4cfc-b4ca-edfc55a6685bAmputationsuser_000705
310039c7ae-e1a7-4a7d-9b49-0cbc716821a6Death Cab For Cutie2009-05-01T18:55:30Z99db2ed6-4493-481c-ac20-54ad323cb04bThe Face That Launched 1000 Shitsuser_000705
.....................
2499e0bba708-bdd3-478d-84ea-c706413bedabA. R. Rahman2009-01-12T15:26:51ZPaper Planes (Dfa Remix)user_000705
" 89 | ], 90 | "text/plain": [ 91 | "\n", 92 | "#\n", 93 | " artid artname timestamp traid traname userid \n", 94 | " 0 e1f1e33e-2 Kiss 2009-05-03 0138547f-3 X-Ray Eyes user_00070 \n", 95 | " 1 e1f1e33e-2 Kiss 2009-05-03 22ce9a32-3 Hard Times user_00070 \n", 96 | " 2 e1f1e33e-2 Kiss 2009-05-03 22ce9a32-3 Hard Times user_00070 \n", 97 | " 3 e1f1e33e-2 Kiss 2009-05-03 c386b85f-1 Magic Touc user_00070 \n", 98 | " 4 e1f1e33e-2 Kiss 2009-05-03 8ec9b4f8-2 Charisma user_00070 \n", 99 | " 5 e1f1e33e-2 Kiss 2009-05-03 ff47d82e-3 Dirty Livi user_00070 \n", 100 | " 6 e1f1e33e-2 Kiss 2009-05-03 3621800b-8 Sure Know user_00070 \n", 101 | " 7 e1f1e33e-2 Kiss 2009-05-03 91cc5010-e 2,000 Man user_00070 \n", 102 | " 8 e1f1e33e-2 Kiss 2009-05-03 daa13e72-9 I Was Made user_00070 \n", 103 | " 9 33b3c323-7 The Jimi H 2009-05-03 1895c79d-a Burning Of user_00070 \n", 104 | " 10 33b3c323-7 The Jimi H 2009-05-03 1ae3c250-7 Gypsy Eyes user_00070 \n", 105 | " 11 33b3c323-7 The Jimi H 2009-05-03 fca63fe0-c Come On, P user_00070 \n", 106 | " 12 33b3c323-7 The Jimi H 2009-05-03 36beb31d-e Long Hot S user_00070 \n", 107 | " 13 33b3c323-7 The Jimi H 2009-05-03 4ad6064e-7 Little Mis user_00070 \n", 108 | " 14 33b3c323-7 The Jimi H 2009-05-03 40ffef8e-b Voodoo Chi user_00070 \n", 109 | " ... ... ... ... ... ... ... \n" 110 | ] 111 | }, 112 | "execution_count": 2, 113 | "metadata": {}, 114 | "output_type": "execute_result" 115 | } 116 | ], 117 | "source": [ 118 | "require 'open-uri'\n", 119 | "content = open('https://raw.githubusercontent.com/v0dro/daru/master/spec/fixtures/music_data.tsv')\n", 120 | "df = Daru::DataFrame.from_csv content, col_sep: \"\\t\"\n", 121 | "df.vectors = Daru::Index.new(df.vectors.to_a.map(&:to_sym))\n", 122 | "df" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 3, 128 | "metadata": { 129 | "collapsed": false 130 | }, 131 | "outputs": [ 132 | { 133 | "data": { 134 | "text/html": [ 135 | "
Daru::Vector:35332940 size: 376
size
3 Doors Down3
A Perfect Circle1
A. R. Rahman231
Abba1
Aditi Singh Sharma3
Aerosmith1
Air1
Akurat1
Alanis Morissette5
Alice Cooper2
Alice In Chains1
Alison Krauss1
Altamiro Carrilho1
Alvin Youngblood Hart3
America1
Amit Trivedi20
Amon Amarth1
Anathema4
Andrea Bocelli16
Andrea Bocelli;Christina Aquilera2
Andrea Bocelli;Kenny G2
Andrea Bocelli;Mario Reyes1
Andrew Lloyd Webber1
Anton Maiden2
Antsy Pants2
Apocalyptica9
Archive1
Arctic Monkeys1
Aretha Franklin4
Asia1
Audioslave1
Aurelio Fierro1
......
シートベルツ32
" 136 | ], 137 | "text/plain": [ 138 | "\n", 139 | "#\n", 140 | " size\n", 141 | " 3 Doors Down 3\n", 142 | " A Perfect Circle 1\n", 143 | " A. R. Rahman 231\n", 144 | " Abba 1\n", 145 | " Aditi Singh Sharma 3\n", 146 | " Aerosmith 1\n", 147 | " Air 1\n", 148 | " Akurat 1\n", 149 | " Alanis Morissette 5\n", 150 | " Alice Cooper 2\n", 151 | " Alice In Chains 1\n", 152 | " Alison Krauss 1\n", 153 | " Altamiro Carrilho 1\n", 154 | "Alvin Youngblood Har 3\n", 155 | " America 1\n", 156 | " Amit Trivedi 20\n", 157 | " Amon Amarth 1\n", 158 | " ... ...\n" 159 | ] 160 | }, 161 | "execution_count": 3, 162 | "metadata": {}, 163 | "output_type": "execute_result" 164 | } 165 | ], 166 | "source": [ 167 | "# Group by artist name and call 'size' to see the number of rows each artist populates.\n", 168 | "artists = df.group_by(:artname).size" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 4, 174 | "metadata": { 175 | "collapsed": false 176 | }, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/html": [ 181 | "
Daru::Vector:35427380 size: 1
size
A. R. Rahman231
" 182 | ], 183 | "text/plain": [ 184 | "\n", 185 | "#\n", 186 | " size\n", 187 | "A. R. Rahman 231\n" 188 | ] 189 | }, 190 | "execution_count": 4, 191 | "metadata": {}, 192 | "output_type": "execute_result" 193 | } 194 | ], 195 | "source": [ 196 | "artists.max_index" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 5, 202 | "metadata": { 203 | "collapsed": false 204 | }, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/html": [ 209 | "
\n", 210 | "\n" 231 | ], 232 | "text/plain": [ 233 | "#[#[#:bar, :options=>{:x=>\"data0\", :y=>\"data1\"}, :data=>\"f0aed652-9d85-4036-b707-e7033477bfd7\"}, @xrange=[\"A. R. Rahman\", \"Death Cab For Cutie\", \"Pink Floyd\", \"The Rolling Stones\", \"Opeth\", \"The Beatles\", \"The Cranberries\", \"Cat Stevens\", \"Howard Shore\", \"Nick Drake\"], @yrange=[0, 231]>], :options=>{:yrange=>[0, 300], :width=>1120, :height=>800, :legend=>true, :rotate_x_label=>45, :xrange=>[\"A. R. Rahman\", \"Death Cab For Cutie\", \"Pink Floyd\", \"The Rolling Stones\", \"Opeth\", \"The Beatles\", \"The Cranberries\", \"Cat Stevens\", \"Howard Shore\", \"Nick Drake\"]}}>], :data=>{\"f0aed652-9d85-4036-b707-e7033477bfd7\"=>#\"A. R. Rahman\", :data1=>231}, {:data0=>\"Death Cab For Cutie\", :data1=>206}, {:data0=>\"Pink Floyd\", :data1=>158}, {:data0=>\"The Rolling Stones\", :data1=>91}, {:data0=>\"Opeth\", :data1=>89}, {:data0=>\"The Beatles\", :data1=>67}, {:data0=>\"The Cranberries\", :data1=>61}, {:data0=>\"Cat Stevens\", :data1=>57}, {:data0=>\"Howard Shore\", :data1=>56}, {:data0=>\"Nick Drake\", :data1=>51}]>}, :extension=>[]}>" 234 | ] 235 | }, 236 | "metadata": {}, 237 | "output_type": "display_data" 238 | } 239 | ], 240 | "source": [ 241 | "top_ten = artists.sort(ascending: false).head\n", 242 | "\n", 243 | "top_ten.plot type: :bar do |plt|\n", 244 | " plt.yrange [0,300]\n", 245 | " plt.width 1120 \n", 246 | " plt.height 800\n", 247 | " plt.legend true\n", 248 | " plt.rotate_x_label 45\n", 249 | "end" 250 | ] 251 | } 252 | ], 253 | "metadata": { 254 | "kernelspec": { 255 | "display_name": "Ruby 2.2.1", 256 | "language": "ruby", 257 | "name": "ruby" 258 | }, 259 | "language_info": { 260 | "file_extension": ".rb", 261 | "mimetype": "application/x-ruby", 262 | "name": "ruby", 263 | "version": "2.2.1" 264 | } 265 | }, 266 | "nbformat": 4, 267 | "nbformat_minor": 0 268 | } 269 | -------------------------------------------------------------------------------- /Data Analysis/Searching and Combining Data.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Searching and Combining data in daru\n", 8 | "\n", 9 | "## Arel-like query syntax\n", 10 | "\n", 11 | "[Arel](https://github.com/rails/arel) is a very popular ruby gem that is one of the major components of the most popular ruby frameworks, [Rails](https://github.com/rails/rails). It is an ORM-helper of sorts that exposes a beautiful and intuitive syntax for creating SQL strings by chaining Ruby methods.\n", 12 | "\n", 13 | "In daru, we have successfully adopted this syntax and the result is a very intuitive and readable syntax for obtaining any sort of data from a DataFrame or Vector.\n", 14 | "\n", 15 | "Lets see how this syntax works with some examples:" 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": { 22 | "collapsed": false 23 | }, 24 | "outputs": [ 25 | { 26 | "data": { 27 | "application/javascript": [ 28 | "if(window['d3'] === undefined ||\n", 29 | " window['Nyaplot'] === undefined){\n", 30 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 31 | "\n", 32 | "\n", 33 | "\n", 34 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 35 | "\n", 36 | " require.config({paths: path, shim:shim});\n", 37 | "\n", 38 | "\n", 39 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 40 | "\n", 41 | "\tvar script = d3.select(\"head\")\n", 42 | "\t .append(\"script\")\n", 43 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 44 | "\t .attr(\"async\", true);\n", 45 | "\n", 46 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 47 | "\n", 48 | "\n", 49 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 50 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 51 | "\t window.dispatchEvent(event);\n", 52 | "\t console.log('Finished loading Nyaplotjs');\n", 53 | "\n", 54 | "\t};\n", 55 | "\n", 56 | "\n", 57 | "});});\n", 58 | "}\n" 59 | ], 60 | "text/plain": [ 61 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 62 | ] 63 | }, 64 | "metadata": {}, 65 | "output_type": "display_data" 66 | }, 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "true" 71 | ] 72 | }, 73 | "execution_count": 1, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "require 'daru'" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "To use this syntax we call any of the comparator methods defined on `Daru::Vector` and pass the results from these to the `#where` method available for both `DataFrame` and `Vector`.\n", 87 | "\n", 88 | "To demonstrate with a quick example:" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 2, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/html": [ 101 | "
Daru::Vector:21491980 size: 5
nil
25
45
75
115
135
" 102 | ], 103 | "text/plain": [ 104 | "\n", 105 | "#\n", 106 | " nil\n", 107 | " 2 5\n", 108 | " 4 5\n", 109 | " 7 5\n", 110 | " 11 5\n", 111 | " 13 5\n" 112 | ] 113 | }, 114 | "execution_count": 2, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "vector = Daru::Vector.new([2,4,5,51,5,16,2,5,3,2,1,5,2,5,2,1,56,234,6,21])\n", 121 | "vector.where((vector.eq(5) | vector.eq(1)) & vector.mt(2))" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "** Note that we use union OR (`|`) and union AND (`&`) and not logical OR (`||`) or logical AND (`&&`) **\n", 129 | "\n", 130 | "Thus the results returned by the `Vector#eq` method are evaluated by `Vector#where` and the generated Vector is returned. The index is also preserved.\n", 131 | "\n", 132 | "The `where` clause can also be used with DataFrame, with similar results." 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 3, 138 | "metadata": { 139 | "collapsed": false 140 | }, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/html": [ 145 | "
Daru::DataFrame:19261300 rows: 200 cols: 3
abc
2632b22
2485e55
3392b22
1605e55
5052b22
3115e55
1992b22
2325e55
4102b22
1255e55
3502b22
5725e55
292b22
1665e55
682b22
2705e55
2732b22
4165e55
112b22
4925e55
852b22
1985e55
3062b22
3645e55
5892b22
2205e55
4672b22
3415e55
2692b22
1905e55
1582b22
3545e55
............
5145e55
" 146 | ], 147 | "text/plain": [ 148 | "\n", 149 | "#\n", 150 | " a b c \n", 151 | " 263 2 b 22 \n", 152 | " 248 5 e 55 \n", 153 | " 339 2 b 22 \n", 154 | " 160 5 e 55 \n", 155 | " 505 2 b 22 \n", 156 | " 311 5 e 55 \n", 157 | " 199 2 b 22 \n", 158 | " 232 5 e 55 \n", 159 | " 410 2 b 22 \n", 160 | " 125 5 e 55 \n", 161 | " 350 2 b 22 \n", 162 | " 572 5 e 55 \n", 163 | " 29 2 b 22 \n", 164 | " 166 5 e 55 \n", 165 | " 68 2 b 22 \n", 166 | " ... ... ... ... \n" 167 | ] 168 | }, 169 | "execution_count": 3, 170 | "metadata": {}, 171 | "output_type": "execute_result" 172 | } 173 | ], 174 | "source": [ 175 | "df = Daru::DataFrame.new({\n", 176 | " a: [1,2,3,4,5,6]*100,\n", 177 | " b: ['a','b','c','d','e','f']*100,\n", 178 | " c: [11,22,33,44,55,66]*100\n", 179 | "}, index: (1..600).to_a.shuffle)\n", 180 | "\n", 181 | "df.where(df[:a].eq(2) | df[:c].eq(55))" 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "The comparator methods on Vector return an object of type `Daru::Core::Query::BoolArray` which lets us perform **OR** and **AND** operations on it. See [this blog post]() for more information on BoolArray and other comparator methods.\n", 189 | "\n", 190 | "As a convenience for readability, you can also you the `#and` or `#or` methods instead of `#&` and `#|`." 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 4, 196 | "metadata": { 197 | "collapsed": false 198 | }, 199 | "outputs": [ 200 | { 201 | "data": { 202 | "text/html": [ 203 | "
Daru::Vector:18369620 size: 5
nil
25
45
75
115
135
" 204 | ], 205 | "text/plain": [ 206 | "\n", 207 | "#\n", 208 | " nil\n", 209 | " 2 5\n", 210 | " 4 5\n", 211 | " 7 5\n", 212 | " 11 5\n", 213 | " 13 5\n" 214 | ] 215 | }, 216 | "execution_count": 4, 217 | "metadata": {}, 218 | "output_type": "execute_result" 219 | } 220 | ], 221 | "source": [ 222 | "vector.where(vector.eq(2).or(vector.eq(5)).and(vector.mt(2)))" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "One of the major advantages of using the `where` clause over other more robust methods like `DataFrame#filter` or `Vector#keep_if` is that it is much faster (though not destructive). [These benchmarks](https://github.com/v0dro/daru/blob/master/benchmarks/where_vs_filter.rb) prove my point." 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": {}, 235 | "source": [ 236 | "## Performing joins between DataFrames\n", 237 | "\n", 238 | "\n", 239 | "Daru::DataFrame offers the `#join` method for performing SQL style joins between two DataFrames. Currently #join supports inner, left outer, right outer and full outer joins between DataFrames.\n", 240 | "\n", 241 | "To demonstrate:" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 5, 247 | "metadata": { 248 | "collapsed": false 249 | }, 250 | "outputs": [], 251 | "source": [ 252 | "left = Daru::DataFrame.new({\n", 253 | " :id => [1,2,3,4],\n", 254 | " :name => ['Pirate', 'Monkey', 'Ninja', 'Spaghetti']\n", 255 | "})\n", 256 | "right = Daru::DataFrame.new({\n", 257 | " :id => [1,2,3,4],\n", 258 | " :name => ['Rutabaga', 'Pirate', 'Darth Vader', 'Ninja']\n", 259 | "})\n", 260 | "nil" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "To perform an inner join on the `:name` column:" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 6, 273 | "metadata": { 274 | "collapsed": false 275 | }, 276 | "outputs": [ 277 | { 278 | "data": { 279 | "text/html": [ 280 | "
Daru::DataFrame:12529040 rows: 2 cols: 3
id_1nameid_2
01Pirate2
13Ninja4
" 281 | ], 282 | "text/plain": [ 283 | "\n", 284 | "#\n", 285 | " id_1 name id_2 \n", 286 | " 0 1 Pirate 2 \n", 287 | " 1 3 Ninja 4 \n" 288 | ] 289 | }, 290 | "execution_count": 6, 291 | "metadata": {}, 292 | "output_type": "execute_result" 293 | } 294 | ], 295 | "source": [ 296 | "left.join(right, on: [:name], how: :inner)" 297 | ] 298 | }, 299 | { 300 | "cell_type": "markdown", 301 | "metadata": {}, 302 | "source": [ 303 | "An outer left join can be done with:" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 7, 309 | "metadata": { 310 | "collapsed": false 311 | }, 312 | "outputs": [ 313 | { 314 | "data": { 315 | "text/html": [ 316 | "
Daru::DataFrame:12260160 rows: 4 cols: 3
id_1nameid_2
01Pirate2
12Monkey
23Ninja4
34Spaghetti
" 317 | ], 318 | "text/plain": [ 319 | "\n", 320 | "#\n", 321 | " id_1 name id_2 \n", 322 | " 0 1 Pirate 2 \n", 323 | " 1 2 Monkey nil \n", 324 | " 2 3 Ninja 4 \n", 325 | " 3 4 Spaghetti nil \n" 326 | ] 327 | }, 328 | "execution_count": 7, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "left.join(right, on: [:name], how: :left)" 335 | ] 336 | }, 337 | { 338 | "cell_type": "markdown", 339 | "metadata": {}, 340 | "source": [ 341 | "An outer right join can be done like so:" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 8, 347 | "metadata": { 348 | "collapsed": false 349 | }, 350 | "outputs": [ 351 | { 352 | "data": { 353 | "text/html": [ 354 | "
Daru::DataFrame:11720640 rows: 4 cols: 3
id_1nameid_2
0Rutabaga1
11Pirate2
2Darth Vader3
33Ninja4
" 355 | ], 356 | "text/plain": [ 357 | "\n", 358 | "#\n", 359 | " id_1 name id_2 \n", 360 | " 0 nil Rutabaga 1 \n", 361 | " 1 1 Pirate 2 \n", 362 | " 2 nil Darth Vade 3 \n", 363 | " 3 3 Ninja 4 \n" 364 | ] 365 | }, 366 | "execution_count": 8, 367 | "metadata": {}, 368 | "output_type": "execute_result" 369 | } 370 | ], 371 | "source": [ 372 | "left.join(right, on: [:name], how: :right)" 373 | ] 374 | }, 375 | { 376 | "cell_type": "markdown", 377 | "metadata": {}, 378 | "source": [ 379 | "And finally, a full outer join:" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": 9, 385 | "metadata": { 386 | "collapsed": false 387 | }, 388 | "outputs": [ 389 | { 390 | "data": { 391 | "text/html": [ 392 | "
Daru::DataFrame:10555680 rows: 6 cols: 3
id_1nameid_2
01Pirate2
12Monkey
23Ninja4
34Spaghetti
4Rutabaga1
5Darth Vader3
" 393 | ], 394 | "text/plain": [ 395 | "\n", 396 | "#\n", 397 | " id_1 name id_2 \n", 398 | " 0 1 Pirate 2 \n", 399 | " 1 2 Monkey nil \n", 400 | " 2 3 Ninja 4 \n", 401 | " 3 4 Spaghetti nil \n", 402 | " 4 nil Rutabaga 1 \n", 403 | " 5 nil Darth Vade 3 \n" 404 | ] 405 | }, 406 | "execution_count": 9, 407 | "metadata": {}, 408 | "output_type": "execute_result" 409 | } 410 | ], 411 | "source": [ 412 | "left.join(right, on: [:name], how: :outer)" 413 | ] 414 | } 415 | ], 416 | "metadata": { 417 | "kernelspec": { 418 | "display_name": "Ruby 2.2.1", 419 | "language": "ruby", 420 | "name": "ruby" 421 | }, 422 | "language_info": { 423 | "file_extension": ".rb", 424 | "mimetype": "application/x-ruby", 425 | "name": "ruby", 426 | "version": "2.2.1" 427 | } 428 | }, 429 | "nbformat": 4, 430 | "nbformat_minor": 0 431 | } 432 | -------------------------------------------------------------------------------- /Data Analysis/data/sales-funnel.csv: -------------------------------------------------------------------------------- 1 | Account,Name,Rep,Manager,Product,Quantity,Price,Status 2 | 714466,Trantow-Barrows,Craig Booker,Debra Henley,CPU,1,30000,presented 3 | 714466,Trantow-Barrows,Craig Booker,Debra Henley,Software,1,10000,presented 4 | 714466,Trantow-Barrows,Craig Booker,Debra Henley,Maintenance,2,5000,pending 5 | 737550,"Fritsch, Russel and Anderson",Craig Booker,Debra Henley,CPU,1,35000,declined 6 | 146832,Kiehn-Spinka,Daniel Hilton,Debra Henley,CPU,2,65000,won 7 | 218895,Kulas Inc,Daniel Hilton,Debra Henley,CPU,2,40000,pending 8 | 218895,Kulas Inc,Daniel Hilton,Debra Henley,Software,1,10000,presented 9 | 412290,Jerde-Hilpert,John Smith,Debra Henley,Maintenance,2,5000,pending 10 | 740150,Barton LLC,John Smith,Debra Henley,CPU,1,35000,declined 11 | 141962,Herman LLC,Cedric Moss,Fred Anderson,CPU,2,65000,won 12 | 163416,Purdy-Kunde,Cedric Moss,Fred Anderson,CPU,1,30000,presented 13 | 239344,Stokes LLC,Cedric Moss,Fred Anderson,Maintenance,1,5000,pending 14 | 239344,Stokes LLC,Cedric Moss,Fred Anderson,Software,1,10000,presented 15 | 307599,"Kassulke, Ondricka and Metz",Wendy Yule,Fred Anderson,Maintenance,3,7000,won 16 | 688981,Keeling LLC,Wendy Yule,Fred Anderson,CPU,5,100000,won 17 | 729833,Koepp Ltd,Wendy Yule,Fred Anderson,CPU,2,65000,declined 18 | 729833,Koepp Ltd,Wendy Yule,Fred Anderson,Monitor,2,5000,presented 19 | -------------------------------------------------------------------------------- /Data Analysis/data/test_xls.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SciRuby/sciruby-notebooks/a6755db27e005b108700c2641a909b697ed0b56f/Data Analysis/data/test_xls.xls -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM andrewosh/binder-base 2 | 3 | MAINTAINER Kozo Nishida 4 | 5 | USER root 6 | 7 | # Add ecell4 dependencies 8 | RUN apt-get update 9 | RUN apt-get install -y build-essential ruby ruby-dev libzmq3 libzmq3-dev gnuplot-nox libgsl0-dev libtool autoconf automake zlib1g-dev libsqlite3-dev libmagick++-dev imagemagick libatlas-base-dev && apt-get clean 10 | RUN ln -s /usr/bin/libtoolize /usr/bin/libtool # See https://github.com/zeromq/libzmq/issues/1385 11 | 12 | RUN gem update --no-document --system && gem install --no-document sciruby-full 13 | 14 | USER main 15 | 16 | RUN iruby register -------------------------------------------------------------------------------- /IRuby Examples/display.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/latex": [ 13 | "$1+2\\imath$" 14 | ], 15 | "text/plain": [ 16 | "(1+2i)" 17 | ] 18 | }, 19 | "metadata": {}, 20 | "output_type": "display_data" 21 | } 22 | ], 23 | "source": [ 24 | "IRuby.display(Complex(1,2))" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 2, 30 | "metadata": { 31 | "collapsed": false 32 | }, 33 | "outputs": [ 34 | { 35 | "data": { 36 | "text/latex": [ 37 | "$$\\left(\\begin{array}{cc}\n", 38 | " 1&2\\\\\n", 39 | " 3&4\\\\\n", 40 | "\\end{array}\\right)$$" 41 | ], 42 | "text/plain": [ 43 | "Matrix[[1, 2], [3, 4]]" 44 | ] 45 | }, 46 | "metadata": {}, 47 | "output_type": "display_data" 48 | }, 49 | { 50 | "data": { 51 | "text/latex": [ 52 | "$$\\left(\\begin{array}{cc}\n", 53 | " 1&2\\\\\n", 54 | " 3&4\\\\\n", 55 | "\\end{array}\\right)$$" 56 | ], 57 | "text/plain": [ 58 | "Matrix[[1, 2], [3, 4]]" 59 | ] 60 | }, 61 | "metadata": {}, 62 | "output_type": "display_data" 63 | }, 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "Matrix[[1, 2], [3, 4]]" 68 | ] 69 | }, 70 | "metadata": {}, 71 | "output_type": "display_data" 72 | }, 73 | { 74 | "data": { 75 | "text/plain": [ 76 | "Matrix[[1, 2], [3, 4]]" 77 | ] 78 | }, 79 | "metadata": {}, 80 | "output_type": "display_data" 81 | }, 82 | { 83 | "data": { 84 | "text/html": [ 85 | "
12
34
" 86 | ], 87 | "text/plain": [ 88 | "Matrix[[1, 2], [3, 4]]" 89 | ] 90 | }, 91 | "metadata": {}, 92 | "output_type": "display_data" 93 | } 94 | ], 95 | "source": [ 96 | "require 'matrix'\n", 97 | "IRuby.display(Matrix[[1,2],[3,4]])\n", 98 | "IRuby.display(Matrix[[1,2],[3,4]],format:'latex')\n", 99 | "IRuby.display(Matrix[[1,2],[3,4]],'text/plain')\n", 100 | "IRuby.display(Matrix[[1,2],[3,4]],mime:'text/plain')\n", 101 | "IRuby.display(Matrix[[1,2],[3,4]],'html')" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": { 108 | "collapsed": false 109 | }, 110 | "outputs": [], 111 | "source": [] 112 | } 113 | ], 114 | "metadata": { 115 | "kernelspec": { 116 | "display_name": "Ruby 2.2.1", 117 | "language": "ruby", 118 | "name": "ruby" 119 | }, 120 | "language_info": { 121 | "file_extension": ".rb", 122 | "mimetype": "application/x-ruby", 123 | "name": "ruby", 124 | "version": "2.2.1" 125 | } 126 | }, 127 | "nbformat": 4, 128 | "nbformat_minor": 0 129 | } 130 | -------------------------------------------------------------------------------- /IRuby Examples/history.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/plain": [ 13 | ":out1" 14 | ] 15 | }, 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "output_type": "execute_result" 19 | } 20 | ], 21 | "source": [ 22 | ":out1" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": { 29 | "collapsed": false 30 | }, 31 | "outputs": [ 32 | { 33 | "data": { 34 | "text/plain": [ 35 | ":out2" 36 | ] 37 | }, 38 | "execution_count": 2, 39 | "metadata": {}, 40 | "output_type": "execute_result" 41 | } 42 | ], 43 | "source": [ 44 | ":out2" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 3, 50 | "metadata": { 51 | "collapsed": false 52 | }, 53 | "outputs": [ 54 | { 55 | "data": { 56 | "text/plain": [ 57 | ":out3" 58 | ] 59 | }, 60 | "execution_count": 3, 61 | "metadata": {}, 62 | "output_type": "execute_result" 63 | } 64 | ], 65 | "source": [ 66 | ":out3" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": { 73 | "collapsed": false 74 | }, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | ":out1\n", 81 | ":out2\n", 82 | ":out3\n", 83 | ":out1\n", 84 | ":out2\n", 85 | ":out3\n", 86 | ":out1\n", 87 | ":out2\n", 88 | ":out3\n", 89 | ":out1\n", 90 | ":out2\n", 91 | ":out3\n", 92 | ":out1\n", 93 | ":out2\n", 94 | ":out3\n", 95 | "out1\n", 96 | "out2\n", 97 | "\n", 98 | "out1\n", 99 | "out2\n", 100 | "out3\n", 101 | "out1\n", 102 | "out2\n", 103 | "out3\n", 104 | "out1\n", 105 | "out2\n", 106 | "out3\n", 107 | "out1\n", 108 | "out2\n", 109 | "out3\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "puts _iii\n", 115 | "puts _ii\n", 116 | "puts _i\n", 117 | "\n", 118 | "puts _i1\n", 119 | "puts _i2\n", 120 | "puts _i3\n", 121 | "\n", 122 | "puts _ih[1]\n", 123 | "puts _ih[2]\n", 124 | "puts _ih[3]\n", 125 | "\n", 126 | "puts In[1]\n", 127 | "puts In[2]\n", 128 | "puts In[3]\n", 129 | "\n", 130 | "puts In[-3]\n", 131 | "puts In[-2]\n", 132 | "puts In[-1]\n", 133 | "\n", 134 | "puts ___\n", 135 | "puts __\n", 136 | "puts _\n", 137 | "\n", 138 | "puts _1\n", 139 | "puts _2\n", 140 | "puts _3\n", 141 | "\n", 142 | "puts _oh[1]\n", 143 | "puts _oh[2]\n", 144 | "puts _oh[3]\n", 145 | "\n", 146 | "puts Out[1]\n", 147 | "puts Out[2]\n", 148 | "puts Out[3]\n", 149 | "\n", 150 | "puts Out[-3]\n", 151 | "puts Out[-2]\n", 152 | "puts Out[-1]" 153 | ] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Ruby 2.2.1", 159 | "language": "ruby", 160 | "name": "ruby" 161 | }, 162 | "language_info": { 163 | "file_extension": ".rb", 164 | "mimetype": "application/x-ruby", 165 | "name": "ruby", 166 | "version": "2.2.1" 167 | } 168 | }, 169 | "nbformat": 4, 170 | "nbformat_minor": 0 171 | } 172 | -------------------------------------------------------------------------------- /IRuby Examples/input.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# IRuby Input\n", 8 | "\n", 9 | "The `IRuby::Input` class makes it easier for IRuby users to get input from users. For example:" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": { 16 | "collapsed": false 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "name = IRuby.input 'Enter your name'" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "The following input methods are supported on the `IRuby` module:\n", 28 | "\n", 29 | "| method | description |\n", 30 | "| -------- | -------- |\n", 31 | "| `input(prompt)` | Prompts the user for a line of input |\n", 32 | "| `password(prompt)` | Prompts the user for a password |\n", 33 | "| `form(&block)` | Presents a form to the user |\n", 34 | "| `popup(title,&block)` | Displays a form to the user as a popup |" 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "## Forms\n", 42 | "\n", 43 | "Forms are groups of inputs to be collected from the user. For example:" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "metadata": { 50 | "collapsed": false 51 | }, 52 | "outputs": [], 53 | "source": [ 54 | "result = IRuby.form do \n", 55 | " input :username\n", 56 | " password :password\n", 57 | " button\n", 58 | "end" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "The following methods are available to build forms: \n", 66 | "\n", 67 | "| method | description |\n", 68 | "| -------- | -------- |\n", 69 | "| `input(key=:input)` | Prompts the user for a line of input |\n", 70 | "| `textarea(key=:textarea),` | Adds a textarea to the form |\n", 71 | "| `password(key=:password)` | Prompts the user for a password |\n", 72 | "| `button(key=:done, color: :blue)` | Adds a submit button to the form |\n", 73 | "| `cancel(prompt='Cancel')` | Adds a cancel button to the form |\n", 74 | "| `text(string)` | Adds text to the form |\n", 75 | "| `html(&block)` | Inserts HTML from the given [erector block](https://github.com/erector/erector) |\n", 76 | "| `file(key=:file)` | Adds a file input to the form |\n", 77 | "| `date(key=:date)` | Adds a date picker to the form |\n", 78 | "| `select(*options)` | Adds a dropdown select input to the form |\n", 79 | "| `radio(*options)` | Adds a radio select input to the form |\n", 80 | "| `checkbox(*options)` | Adds checkbox inputs to the form |" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "## Popups\n", 88 | " \n", 89 | "Popups are just forms in a bootstrap modal. They are useful when users **Run All** in a notebook with a lot of inputs. The popups always appear in the same spot, so users don't have to scroll down to find the next input. \n", 90 | "\n", 91 | "Popups accept a `title` argument, for example: " 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": null, 97 | "metadata": { 98 | "collapsed": false 99 | }, 100 | "outputs": [], 101 | "source": [ 102 | "result = IRuby.popup 'Please enter your name' do \n", 103 | " input\n", 104 | " button\n", 105 | "end" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "## Submit and cancel\n", 113 | "\n", 114 | "The enter key will submit an input or form and the escape key will cancel it. Canceled inputs are returned as `nil`. Inputs are automatically canceled if destroyed. An input can be destroyed by clearing its cell's output. The `cancel` button will cancel a form and all other buttons will submit it. \n", 115 | "\n", 116 | "After a form destroyed, the cell's output is cleared. Be careful not to prompt for input in a block that has previous output you would like to keep. Output is cleared to prevent forms from interferring with one another and to ensure that inputs are not inadvertently saved to the notebook. " 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": null, 122 | "metadata": { 123 | "collapsed": false 124 | }, 125 | "outputs": [], 126 | "source": [ 127 | "result = IRuby.popup 'Confirm' do \n", 128 | " text 'Are you sure you want to continue?'\n", 129 | " cancel 'No'\n", 130 | " button 'Yes'\n", 131 | "end" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "## Custom keys\n", 139 | "\n", 140 | "Every widget has an entry in the final results hash. A custom key can be passed as the first parameter to the hash. If no key is provided, the widget name is used as the key. The `cancel` widget has no key; it's first parameter is its label. " 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": null, 146 | "metadata": { 147 | "collapsed": false 148 | }, 149 | "outputs": [], 150 | "source": [ 151 | "result = IRuby.form do\n", 152 | " input :username\n", 153 | " password :password\n", 154 | "end" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "## Custom labels\n", 162 | "\n", 163 | "Field labels appear to the left of the field. Button labels appear as the text on the button. `cancel` labels are passed as the first argument. All other widgets' labels are set using the `label` parameter. " 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": null, 169 | "metadata": { 170 | "collapsed": false 171 | }, 172 | "outputs": [], 173 | "source": [ 174 | "result = IRuby.form do \n", 175 | " input :name, label: 'Please enter your name'\n", 176 | " cancel 'None of your business!'\n", 177 | " button :submit, label: 'All done'\n", 178 | "end" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "## Buttons\n", 186 | "\n", 187 | "Buttons do not appear in the final hash unless they are clicked. If clicked, their value is `true`. Here are the various colors a button can be:" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": null, 193 | "metadata": { 194 | "collapsed": false 195 | }, 196 | "outputs": [], 197 | "source": [ 198 | "result = IRuby.form do \n", 199 | " IRuby::Input::Button::COLORS.each_key do |color|\n", 200 | " button color, color: color\n", 201 | " end\n", 202 | "end" 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "## Textareas\n", 210 | "\n", 211 | "Textareas are multiline inputs that are convenient for larger inputs. If you need a line return when typing in a textarea, use shift+enter since enter will submit the form." 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": { 218 | "collapsed": false 219 | }, 220 | "outputs": [], 221 | "source": [ 222 | "result = IRuby.form do \n", 223 | " text 'Enter email addresses, one per line (use shift+enter for newlines)'\n", 224 | " textarea :emails\n", 225 | "end" 226 | ] 227 | }, 228 | { 229 | "cell_type": "markdown", 230 | "metadata": {}, 231 | "source": [ 232 | "## Text and HTML\n", 233 | "\n", 234 | "You can insert lines of text or custom html using their respective helpers:" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": { 241 | "collapsed": false 242 | }, 243 | "outputs": [], 244 | "source": [ 245 | "result = IRuby.form do \n", 246 | " html { h1 'Choose a Stooge' }\n", 247 | " text 'Choose your favorite stooge'\n", 248 | " select :stooge, 'Moe', 'Larry', 'Curly'\n", 249 | " button\n", 250 | "end" 251 | ] 252 | }, 253 | { 254 | "cell_type": "markdown", 255 | "metadata": {}, 256 | "source": [ 257 | "## Dropdowns\n", 258 | "\n", 259 | "Selects are dropdowns of options. By default, the first supplied option is selected. If you don't want any default value, use `nil` as the first parameter. " 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "metadata": { 266 | "collapsed": false 267 | }, 268 | "outputs": [], 269 | "source": [ 270 | "result = IRuby.form do \n", 271 | " text 'Do you have a favorite Stooge?' \n", 272 | " select :stooge, nil, 'Moe', 'Larry', 'Curly'\n", 273 | "end" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "## Radio selects and checkboxes\n", 281 | "\n", 282 | "Like selects, radio selects and checkboxes take multiple arguments, each one an option. If the first argument is a symbol, it is used as the key. \n", 283 | "\n", 284 | "Note that the `checkbox` widget will always return `nil` or an array. " 285 | ] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "metadata": { 291 | "collapsed": false 292 | }, 293 | "outputs": [], 294 | "source": [ 295 | "result = IRuby.form do \n", 296 | " radio :children, *(0..12), label: 'How many children do you have?'\n", 297 | " checkbox :gender, 'Male', 'Female', 'Intersex', label: 'Select the genders of your children'\n", 298 | " button\n", 299 | "end" 300 | ] 301 | }, 302 | { 303 | "cell_type": "markdown", 304 | "metadata": {}, 305 | "source": [ 306 | "## Files\n", 307 | "\n", 308 | "Since file widgets capture the enter key, you should include a button when creating forms that contain only a file input:" 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": null, 314 | "metadata": { 315 | "collapsed": true 316 | }, 317 | "outputs": [], 318 | "source": [ 319 | "IRuby.form do \n", 320 | " file :avatar, label: 'Choose an Avatar'\n", 321 | " button :submit\n", 322 | "end" 323 | ] 324 | }, 325 | { 326 | "cell_type": "markdown", 327 | "metadata": {}, 328 | "source": [ 329 | "File widgets return a hash with three keys: \n", 330 | "\n", 331 | "* `data`: The contents of the file as a string\n", 332 | "* `content_type`: The type of file, such as `text/plain` or `image/jpeg`\n", 333 | "* `name`: The name of the uploaded file" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "## Example\n", 341 | "\n", 342 | "Here is an example form that uses every built-in widget. " 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": null, 348 | "metadata": { 349 | "collapsed": false, 350 | "scrolled": false 351 | }, 352 | "outputs": [], 353 | "source": [ 354 | "result = IRuby.form do \n", 355 | " html { h1 'The Everything Form' }\n", 356 | " text 'Marvel at the strange and varied inputs!'\n", 357 | " date\n", 358 | " file\n", 359 | " input :username\n", 360 | " password\n", 361 | " textarea\n", 362 | " radio *(1..10)\n", 363 | " checkbox 'Fish', 'Cat', 'Dog', label: 'Animals'\n", 364 | " select :color, *IRuby::Input::Button::COLORS.keys\n", 365 | " cancel \n", 366 | " button \n", 367 | "end" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": {}, 373 | "source": [ 374 | "## Writing your own widget\n", 375 | "\n", 376 | "Most form methods are `IRuby::Input::Widget` instances. A `Widget` is an [`Erector::Widget`](https://github.com/erector/erector) with some additional helpers. Here is the `cancel` widget:\n", 377 | "\n", 378 | "```ruby\n", 379 | "module IRuby\n", 380 | " module Input\n", 381 | " class Cancel < Widget\n", 382 | " needs :label\n", 383 | "\n", 384 | " builder :cancel do |label='Cancel'|\n", 385 | " add_button Cancel.new(label: label)\n", 386 | " end\n", 387 | "\n", 388 | " def widget_css\n", 389 | " \".iruby-cancel { margin-left: 5px; }\"\n", 390 | " end\n", 391 | "\n", 392 | " def widget_js\n", 393 | " <<-JS\n", 394 | " $('.iruby-cancel').click(function(){\n", 395 | " $('#iruby-form').remove();\n", 396 | " });\n", 397 | " JS\n", 398 | " end\n", 399 | "\n", 400 | " def widget_html\n", 401 | " button(\n", 402 | " @label,\n", 403 | " type: 'button', \n", 404 | " :'data-dismiss' => 'modal',\n", 405 | " class: \"btn btn-danger pull-right iruby-cancel\"\n", 406 | " )\n", 407 | " end\n", 408 | " end\n", 409 | " end\n", 410 | "end\n", 411 | "```\n", 412 | "\n", 413 | "The following methods are available for widgets to use or override: \n", 414 | "\n", 415 | "| method | description |\n", 416 | "| -------- | -------- |\n", 417 | "| `widget_js` | Returns the widget's Javascript |\n", 418 | "| `widget_css` | Returns the widget's CSS |\n", 419 | "| `widget_html` | Returns the widget's |\n", 420 | "| `builder(method,&block)` | Class method to add form building helpers. |\n", 421 | "\n", 422 | "The following methods are available in the `builder` block:\n", 423 | "\n", 424 | "| method | description |\n", 425 | "| -------- | -------- |\n", 426 | "| `add_field(field)` | Adds a widget to the form's field area |\n", 427 | "| `add_button(button)` | Adds a button to the form's button area |\n", 428 | "| `process(key,&block)` | Register a custom processing block for the given key in the results hash |\n", 429 | "| `unique_key(key)` | Returns a unique key for the given key. Use this to make sure that there are no key collisions in the final results hash. |\n", 430 | "\n", 431 | "A canceled form always returns `nil`. Otherwise, the form collects any element with a `data-iruby-key` and non-falsey `data-iruby-value` and passes those to the processor proc registered for the key. See the `File` widget for a more involved example of processing results." 432 | ] 433 | } 434 | ], 435 | "metadata": { 436 | "kernelspec": { 437 | "display_name": "Ruby 2.2.1", 438 | "language": "ruby", 439 | "name": "ruby" 440 | }, 441 | "language_info": { 442 | "file_extension": ".rb", 443 | "mimetype": "application/x-ruby", 444 | "name": "ruby", 445 | "version": "2.2.3" 446 | } 447 | }, 448 | "nbformat": 4, 449 | "nbformat_minor": 0 450 | } 451 | -------------------------------------------------------------------------------- /IRuby Examples/ruby.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SciRuby/sciruby-notebooks/a6755db27e005b108700c2641a909b697ed0b56f/IRuby Examples/ruby.png -------------------------------------------------------------------------------- /IRuby Examples/stdout.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Standard input and output" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Hello, world!\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "puts 'Hello, world!'" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 2, 32 | "metadata": { 33 | "collapsed": false 34 | }, 35 | "outputs": [ 36 | { 37 | "name": "stdout", 38 | "output_type": "stream", 39 | "text": [ 40 | "stdout\n" 41 | ] 42 | }, 43 | { 44 | "name": "stderr", 45 | "output_type": "stream", 46 | "text": [ 47 | "stderr\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "$stdout.puts 'stdout'\n", 53 | "$stderr.puts 'stderr'" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "metadata": { 60 | "collapsed": false 61 | }, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "print\n", 68 | "write\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "print \"print\\n\"\n", 74 | "$stdout.write \"write\\n\"" 75 | ] 76 | } 77 | ], 78 | "metadata": { 79 | "kernelspec": { 80 | "display_name": "Ruby 2.2.1", 81 | "language": "ruby", 82 | "name": "ruby" 83 | }, 84 | "language_info": { 85 | "file_extension": ".rb", 86 | "mimetype": "application/x-ruby", 87 | "name": "ruby", 88 | "version": "2.2.1" 89 | } 90 | }, 91 | "nbformat": 4, 92 | "nbformat_minor": 0 93 | } 94 | -------------------------------------------------------------------------------- /IRuby Examples/table.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "data": { 12 | "text/html": [ 13 | "
1
2
3
" 14 | ], 15 | "text/plain": [ 16 | "\"
1
2
3
\"" 17 | ] 18 | }, 19 | "metadata": {}, 20 | "output_type": "display_data" 21 | }, 22 | { 23 | "data": { 24 | "text/html": [ 25 | "
1
2
123
23
" 26 | ], 27 | "text/plain": [ 28 | "\"
1
2
123
23
\"" 29 | ] 30 | }, 31 | "metadata": {}, 32 | "output_type": "display_data" 33 | }, 34 | { 35 | "data": { 36 | "text/html": [ 37 | "
abc
123
" 38 | ], 39 | "text/plain": [ 40 | "\"
abc
123
\"" 41 | ] 42 | }, 43 | "metadata": {}, 44 | "output_type": "display_data" 45 | }, 46 | { 47 | "data": { 48 | "text/html": [ 49 | "
11121314
212223
not an Array
31323334
" 50 | ], 51 | "text/plain": [ 52 | "\"
11121314
212223
not an Array
31323334
\"" 53 | ] 54 | }, 55 | "metadata": {}, 56 | "output_type": "display_data" 57 | }, 58 | { 59 | "data": { 60 | "text/html": [ 61 | "
abc
112131
122232
132333
1434
" 62 | ], 63 | "text/plain": [ 64 | "\"
abc
112131
122232
132333
1434
\"" 65 | ] 66 | }, 67 | "metadata": {}, 68 | "output_type": "display_data" 69 | }, 70 | { 71 | "data": { 72 | "text/html": [ 73 | "
abce
123
not an Array
2345
" 74 | ], 75 | "text/plain": [ 76 | "\"
abce
123
not an Array
2345
\"" 77 | ] 78 | }, 79 | "metadata": {}, 80 | "output_type": "display_data" 81 | }, 82 | { 83 | "data": { 84 | "text/html": [ 85 | "
abc0d12
1230
2345
xy
abc
" 86 | ], 87 | "text/plain": [ 88 | "\"
abc0d12
1230
2345
xy
abc
\"" 89 | ] 90 | }, 91 | "metadata": {}, 92 | "output_type": "display_data" 93 | } 94 | ], 95 | "source": [ 96 | "IRuby.display IRuby.table([1,2,[],3])\n", 97 | "IRuby.display IRuby.table([1,2,[1,2,3],[2,3]])\n", 98 | "IRuby.display IRuby.table({a:1,b:2,c:3})\n", 99 | "IRuby.display IRuby.table([[11,12,13,14],[21,22,23],'not an Array',[31,32,33,34]])\n", 100 | "IRuby.display IRuby.table({a:[11,12,13,14],b:[21,22,23],c:[31,32,33,34]})\n", 101 | "IRuby.display IRuby.table([{a:1,b:2,c:3},'not an Array',{a:2,b:3,c:4,e:5}])\n", 102 | "IRuby.display IRuby.table([{a:1,b:2,c:3, 0 => 0},{a:2,b:3,c:4,d:5},{0=>:x,1=>:y},[:a,:b,:c]])" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 2, 108 | "metadata": { 109 | "collapsed": false 110 | }, 111 | "outputs": [ 112 | { 113 | "data": { 114 | "text/html": [ 115 | "
1234567993994995996997998999
24681012141986198819901992199419961998
369121518212979298229852988299129942997
4812162024283972397639803984398839923996
51015202530354965497049754980498549904995
61218243036425958596459705976598259885994
71421283542496951695869656972697969866993
993198629793972496559586951986049987042988035989028990021991014992007
994198829823976497059646958987042988036989030990024991018992012993006
995199029853980497559706965988035989030990025991020992015993010994005
996199229883984498059766972989028990024991020992016993012994008995004
997199429913988498559826979990021991018992015993012994009995006996003
998199629943992499059886986991014992012993010994008995006996004997002
999199829973996499559946993992007993006994005995004996003997002998001
" 116 | ], 117 | "text/plain": [ 118 | "\"
1234567993994995996997998999
24681012141986198819901992199419961998
369121518212979298229852988299129942997
4812162024283972397639803984398839923996
51015202530354965497049754980498549904995
61218243036425958596459705976598259885994
71421283542496951695869656972697969866993
993198629793972496559586951986049987042988035989028990021991014992007
994198829823976497059646958987042988036989030990024991018992012993006
995199029853980497559706965988035989030990025991020992015993010994005
996199229883984498059766972989028990024991020992016993012994008995004
997199429913988498559826979990021991018992015993012994009995006996003
998199629943992499059886986991014992012993010994008995006996004997002
999199829973996499559946993992007993006994005995004996003997002998001
\"" 119 | ] 120 | }, 121 | "execution_count": 2, 122 | "metadata": {}, 123 | "output_type": "execute_result" 124 | } 125 | ], 126 | "source": [ 127 | "IRuby.table (1..1000).map {|i| (1..1000).map {|j| i * j } }" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 3, 133 | "metadata": { 134 | "collapsed": false 135 | }, 136 | "outputs": [ 137 | { 138 | "data": { 139 | "text/html": [ 140 | "
1234567993994995996997998999
1234567993994995996997998999
24681012141986198819901992199419961998
369121518212979298229852988299129942997
4812162024283972397639803984398839923996
51015202530354965497049754980498549904995
61218243036425958596459705976598259885994
71421283542496951695869656972697969866993
993198629793972496559586951986049987042988035989028990021991014992007
994198829823976497059646958987042988036989030990024991018992012993006
995199029853980497559706965988035989030990025991020992015993010994005
996199229883984498059766972989028990024991020992016993012994008995004
997199429913988498559826979990021991018992015993012994009995006996003
998199629943992499059886986991014992012993010994008995006996004997002
999199829973996499559946993992007993006994005995004996003997002998001
" 141 | ], 142 | "text/plain": [ 143 | "\"
1234567993994995996997998999
1234567993994995996997998999
24681012141986198819901992199419961998
369121518212979298229852988299129942997
4812162024283972397639803984398839923996
51015202530354965497049754980498549904995
61218243036425958596459705976598259885994
71421283542496951695869656972697969866993
993198629793972496559586951986049987042988035989028990021991014992007
994198829823976497059646958987042988036989030990024991018992012993006
995199029853980497559706965988035989030990025991020992015993010994005
996199229883984498059766972989028990024991020992016993012994008995004
997199429913988498559826979990021991018992015993012994009995006996003
998199629943992499059886986991014992012993010994008995006996004997002
999199829973996499559946993992007993006994005995004996003997002998001
\"" 144 | ] 145 | }, 146 | "execution_count": 3, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "IRuby.table Hash[(1..1000).map {|i| [i, (1..1000).map {|j| i * j }] }]" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 4, 158 | "metadata": { 159 | "collapsed": false 160 | }, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/html": [ 165 | "
01
12
34
" 166 | ], 167 | "text/plain": [ 168 | "\"
01
12
34
\"" 169 | ] 170 | }, 171 | "metadata": {}, 172 | "output_type": "display_data" 173 | }, 174 | { 175 | "data": { 176 | "text/html": [ 177 | "
12
34
" 178 | ], 179 | "text/plain": [ 180 | "\"
12
34
\"" 181 | ] 182 | }, 183 | "metadata": {}, 184 | "output_type": "display_data" 185 | }, 186 | { 187 | "data": { 188 | "text/html": [ 189 | "
12
34
" 190 | ], 191 | "text/plain": [ 192 | "\"
12
34
\"" 193 | ] 194 | }, 195 | "metadata": {}, 196 | "output_type": "display_data" 197 | }, 198 | { 199 | "data": { 200 | "text/html": [ 201 | "
ab
12
34
" 202 | ], 203 | "text/plain": [ 204 | "\"
ab
12
34
\"" 205 | ] 206 | }, 207 | "metadata": {}, 208 | "output_type": "display_data" 209 | }, 210 | { 211 | "data": { 212 | "text/html": [ 213 | "
12
34
" 214 | ], 215 | "text/plain": [ 216 | "\"
12
34
\"" 217 | ] 218 | }, 219 | "metadata": {}, 220 | "output_type": "display_data" 221 | }, 222 | { 223 | "data": { 224 | "text/html": [ 225 | "
ab
12
34
" 226 | ], 227 | "text/plain": [ 228 | "\"
ab
12
34
\"" 229 | ] 230 | }, 231 | "metadata": {}, 232 | "output_type": "display_data" 233 | }, 234 | { 235 | "data": { 236 | "text/html": [ 237 | "
ab
12
34
" 238 | ], 239 | "text/plain": [ 240 | "\"
ab
12
34
\"" 241 | ] 242 | }, 243 | "metadata": {}, 244 | "output_type": "display_data" 245 | }, 246 | { 247 | "data": { 248 | "text/html": [ 249 | "
12
34
" 250 | ], 251 | "text/plain": [ 252 | "\"
12
34
\"" 253 | ] 254 | }, 255 | "metadata": {}, 256 | "output_type": "display_data" 257 | }, 258 | { 259 | "data": { 260 | "text/html": [ 261 | "
ab
12
34
" 262 | ], 263 | "text/plain": [ 264 | "\"
ab
12
34
\"" 265 | ] 266 | }, 267 | "metadata": {}, 268 | "output_type": "display_data" 269 | } 270 | ], 271 | "source": [ 272 | "IRuby.display IRuby.table [[1,2],[3,4]], header: true\n", 273 | "IRuby.display IRuby.table [[1,2],[3,4]], header: false\n", 274 | "IRuby.display IRuby.table [[1,2],[3,4]]\n", 275 | "\n", 276 | "IRuby.display IRuby.table [{a:1,b:2},{a:3,b:4}], header: true\n", 277 | "IRuby.display IRuby.table [{a:1,b:2},{a:3,b:4}], header: false\n", 278 | "IRuby.display IRuby.table [{a:1,b:2},{a:3,b:4}]\n", 279 | "\n", 280 | "\n", 281 | "IRuby.display IRuby.table({a:[1,3],b:[2,4]}, header: true)\n", 282 | "IRuby.display IRuby.table({a:[1,3],b:[2,4]}, header: false)\n", 283 | "IRuby.display IRuby.table({a:[1,3],b:[2,4]})" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 5, 289 | "metadata": { 290 | "collapsed": false 291 | }, 292 | "outputs": [ 293 | { 294 | "data": { 295 | "text/html": [ 296 | "
abc
123
4
" 297 | ], 298 | "text/plain": [ 299 | "\"
abc
123
4
\"" 300 | ] 301 | }, 302 | "metadata": {}, 303 | "output_type": "display_data" 304 | } 305 | ], 306 | "source": [ 307 | "IRuby.display IRuby.table({a:1,b:2,c:[3,4]})" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 6, 313 | "metadata": { 314 | "collapsed": false 315 | }, 316 | "outputs": [ 317 | { 318 | "data": { 319 | "text/html": [ 320 | "
abc
123
4
" 321 | ], 322 | "text/plain": [ 323 | "\"
abc
123
4
\"" 324 | ] 325 | }, 326 | "metadata": {}, 327 | "output_type": "display_data" 328 | } 329 | ], 330 | "source": [ 331 | "class Red < String\n", 332 | " def to_html\n", 333 | " \"#{self}\"\n", 334 | " end\n", 335 | "end\n", 336 | "\n", 337 | "IRuby.display IRuby.table({Red.new(\"a\") => 1, b:2, c: [3, Red.new(\"4\")]})" 338 | ] 339 | } 340 | ], 341 | "metadata": { 342 | "kernelspec": { 343 | "display_name": "Ruby 2.2.1", 344 | "language": "ruby", 345 | "name": "ruby" 346 | }, 347 | "language_info": { 348 | "file_extension": ".rb", 349 | "mimetype": "application/x-ruby", 350 | "name": "ruby", 351 | "version": "2.2.1" 352 | } 353 | }, 354 | "nbformat": 4, 355 | "nbformat_minor": 0 356 | } 357 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: image 2 | image: 3 | docker build -t sciruby . 4 | 5 | .PHONY: run 6 | run: 7 | docker run -d -p 8888:8888 sciruby 8 | 9 | .PHONY: nuke 10 | nuke: 11 | -docker stop `docker ps -aq` 12 | -docker rm -fv `docker ps -aq` 13 | -docker images -q --filter "dangling=true" | xargs docker rmi 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SciRuby example notebooks 2 | [![Binder](http://mybinder.org/badge.svg)](http://mybinder.org/repo/SciRuby/sciruby-notebooks) 3 | 4 | You can try SciRuby example notebooks without installing IRuby. Please click *launch binder* badge. 5 | 6 | This repository includes a bunch of notebooks showing the functionality of iruby and scientific ruby gems. 7 | 8 | Furthermore there is a Dockerfile which allows the creation of a Docker container with everything preinstalled what is needed. 9 | A prebuild container is available at [Docker Hub](https://registry.hub.docker.com/u/minad/sciruby-notebooks/). 10 | 11 | A lot of Python examples can be found at [the IPython Gallery](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks). 12 | -------------------------------------------------------------------------------- /Statistics/Correlation Matrix with daru and statsample.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Creating and summarizing a correlation matrix with daru and statsample\n", 8 | "\n", 9 | "## This notebook also serves as a demostration of Daru.lazy_update and " 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": false 17 | }, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "application/javascript": [ 22 | "if(window['d3'] === undefined ||\n", 23 | " window['Nyaplot'] === undefined){\n", 24 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 25 | "\n", 26 | "\n", 27 | "\n", 28 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 29 | "\n", 30 | " require.config({paths: path, shim:shim});\n", 31 | "\n", 32 | "\n", 33 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 34 | "\n", 35 | "\tvar script = d3.select(\"head\")\n", 36 | "\t .append(\"script\")\n", 37 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 38 | "\t .attr(\"async\", true);\n", 39 | "\n", 40 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 41 | "\n", 42 | "\n", 43 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 44 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 45 | "\t window.dispatchEvent(event);\n", 46 | "\t console.log('Finished loading Nyaplotjs');\n", 47 | "\n", 48 | "\t};\n", 49 | "\n", 50 | "\n", 51 | "});});\n", 52 | "}\n" 53 | ], 54 | "text/plain": [ 55 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 56 | ] 57 | }, 58 | "metadata": {}, 59 | "output_type": "display_data" 60 | }, 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "== DataFrame ==\n", 66 | "\n" 67 | ] 68 | }, 69 | { 70 | "data": { 71 | "text/html": [ 72 | "
Daru::DataFrame:13085860 rows: 10 cols: 4
abcd
00.9011467513657144-2.0698264996309637-0.05424188567538422-0.21497530674467752
10.34773905293396651.96937999883251650.64382100003735290.2691070769303308
21.169337960514499-1.2003500655322563-1.8876472885285303-0.21250262336698017
3-2.01492422282253520.37771960932499420.2339215509793050.39979121524058253
4-0.8464428241042591-1.47821823047429350.22458711725118236-0.33396263618551913
51.2471920549543476-0.887490499184695-1.10419913624543151.932482592873003
6-0.8529853403070782-0.4909368945208435-0.73064239758412050.8578541353085531
7-0.384705453247700170.48128789967429710.565122377348464-1.277608770535443
81.2856180977412772-1.288566320945822-1.78687461456970051.0500431060289053
90.51827387639413760.36345933022878907-0.463010770932286760.4544283438001362
" 73 | ], 74 | "text/plain": [ 75 | "\n", 76 | "#\n", 77 | " a b c d \n", 78 | " 0 0.90114675 -2.0698264 -0.0542418 -0.2149753 \n", 79 | " 1 0.34773905 1.96937999 0.64382100 0.26910707 \n", 80 | " 2 1.16933796 -1.2003500 -1.8876472 -0.2125026 \n", 81 | " 3 -2.0149242 0.37771960 0.23392155 0.39979121 \n", 82 | " 4 -0.8464428 -1.4782182 0.22458711 -0.3339626 \n", 83 | " 5 1.24719205 -0.8874904 -1.1041991 1.93248259 \n", 84 | " 6 -0.8529853 -0.4909368 -0.7306423 0.85785413 \n", 85 | " 7 -0.3847054 0.48128789 0.56512237 -1.2776087 \n", 86 | " 8 1.28561809 -1.2885663 -1.7868746 1.05004310 \n", 87 | " 9 0.51827387 0.36345933 -0.4630107 0.45442834 \n" 88 | ] 89 | }, 90 | "metadata": {}, 91 | "output_type": "display_data" 92 | }, 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "\n", 98 | "== Correlation Matrix ==\n", 99 | "\n" 100 | ] 101 | }, 102 | { 103 | "data": { 104 | "text/latex": [ 105 | "$$\\left(\\begin{array}{cccc}\n", 106 | " 1.0&-0.01467367745077135&0.011913969445518504&-0.0034212506073735397\\\\\n", 107 | " -0.01467367745077135&1.0&-0.04653177857574501&0.022525025891610012\\\\\n", 108 | " 0.011913969445518504&-0.04653177857574501&1.0&0.031949296919919545\\\\\n", 109 | " -0.0034212506073735397&0.022525025891610012&0.031949296919919545&1.0\\\\\n", 110 | "\\end{array}\\right)$$" 111 | ], 112 | "text/plain": [ 113 | "Matrix[[1.0, -0.01467367745077135, 0.011913969445518504, -0.0034212506073735397], [-0.01467367745077135, 1.0, -0.04653177857574501, 0.022525025891610012], [0.011913969445518504, -0.04653177857574501, 1.0, 0.031949296919919545], [-0.0034212506073735397, 0.022525025891610012, 0.031949296919919545, 1.0]]" 114 | ] 115 | }, 116 | "metadata": {}, 117 | "output_type": "display_data" 118 | }, 119 | { 120 | "name": "stdout", 121 | "output_type": "stream", 122 | "text": [ 123 | "Analysis 2016-03-24 11:58:04 +0000\n", 124 | "= Statsample::Bivariate.correlation_matrix\n", 125 | "\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "require 'statsample'\n", 131 | "\n", 132 | "Statsample::Analysis.store(\"Statsample::Bivariate.correlation_matrix\") do\n", 133 | " \n", 134 | " # It so happens that Daru::Vector and Daru::DataFrame must update metadata\n", 135 | " # like positions of missing values every time they are created. \n", 136 | " #\n", 137 | " # Since we dont have any missing values in the data that we are creating, \n", 138 | " # we set Daru.lazy_update = true so that missing data is not updated every\n", 139 | " # time and things happen much faster.\n", 140 | " #\n", 141 | " # In case you do have missing data and lazy_update has been set to *true*, \n", 142 | " # you _SHOULD_ called `#update` on the concerned Vector or DataFrame object\n", 143 | " # everytime an assingment or deletion cycle is complete.\n", 144 | " Daru.lazy_update = true\n", 145 | " \n", 146 | " # Create a Daru::DataFrame containing 4 vectors a, b, c and d.\n", 147 | " #\n", 148 | " # Notice that the `clone` option has been set to *false*. This tells Daru\n", 149 | " # to not clone the Daru::Vectors being supplied by `rnorm`, since it would\n", 150 | " # be unnecessarily counter productive to clone the vectors once they have\n", 151 | " # been assigned to the dataframe.\n", 152 | " samples = 1000\n", 153 | " ds = Daru::DataFrame.new({\n", 154 | " :a => rnorm(samples),\n", 155 | " :b => rnorm(samples),\n", 156 | " :c => rnorm(samples),\n", 157 | " :d => rnorm(samples)\n", 158 | " }, clone: false)\n", 159 | " \n", 160 | " \n", 161 | " puts \"== DataFrame ==\\n\"\n", 162 | " IRuby.display ds.head\n", 163 | " \n", 164 | " # Calculate correlation matrix by calling the `cor` shorthand.\n", 165 | " cm = Statsample::Bivariate.correlation_matrix(ds)\n", 166 | " \n", 167 | " puts \"\\n== Correlation Matrix ==\\n\"\n", 168 | " IRuby.display cm\n", 169 | " \n", 170 | " # Set lazy_update to *false* once our job is done so that this analysis does\n", 171 | " # not accidentally affect code elsewhere.\n", 172 | " Daru.lazy_update = false\n", 173 | "end\n", 174 | "\n", 175 | "Statsample::Analysis.run_batch" 176 | ] 177 | } 178 | ], 179 | "metadata": { 180 | "kernelspec": { 181 | "display_name": "Ruby 2.2.1", 182 | "language": "ruby", 183 | "name": "ruby" 184 | }, 185 | "language_info": { 186 | "file_extension": ".rb", 187 | "mimetype": "application/x-ruby", 188 | "name": "ruby", 189 | "version": "2.2.1" 190 | } 191 | }, 192 | "nbformat": 4, 193 | "nbformat_minor": 0 194 | } 195 | -------------------------------------------------------------------------------- /Statistics/Levene Test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Levene's Test\n", 8 | "\n", 9 | "This example demonstrates how a levene test can be performed by using \n", 10 | "Daru::Vector and the Statsample::Test::Levene class.\n", 11 | " \n", 12 | "Levene's test is an inferential statistic used to assess the\n", 13 | "equality of variances for a variable calculated for two or more groups.\n", 14 | " \n", 15 | "#### References\n", 16 | " \n", 17 | "http://en.wikipedia.org/wiki/Levene%27s_test" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 1, 23 | "metadata": { 24 | "collapsed": false 25 | }, 26 | "outputs": [ 27 | { 28 | "data": { 29 | "application/javascript": [ 30 | "if(window['d3'] === undefined ||\n", 31 | " window['Nyaplot'] === undefined){\n", 32 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 33 | "\n", 34 | "\n", 35 | "\n", 36 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 37 | "\n", 38 | " require.config({paths: path, shim:shim});\n", 39 | "\n", 40 | "\n", 41 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 42 | "\n", 43 | "\tvar script = d3.select(\"head\")\n", 44 | "\t .append(\"script\")\n", 45 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 46 | "\t .attr(\"async\", true);\n", 47 | "\n", 48 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 49 | "\n", 50 | "\n", 51 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 52 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 53 | "\t window.dispatchEvent(event);\n", 54 | "\t console.log('Finished loading Nyaplotjs');\n", 55 | "\n", 56 | "\t};\n", 57 | "\n", 58 | "\n", 59 | "});});\n", 60 | "}\n" 61 | ], 62 | "text/plain": [ 63 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 64 | ] 65 | }, 66 | "metadata": {}, 67 | "output_type": "display_data" 68 | }, 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "variance a : 908.0444444444445\n", 74 | "variance b : 916.6666666666666\n", 75 | "\n", 76 | "\n", 77 | "Analysis 2016-03-24 16:01:57 +0000\n", 78 | "= Statsample::Test::Levene\n", 79 | " Levene Test : F(1, 18) = 0.7781 , p = 0.3893\n", 80 | "\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "require 'statsample'\n", 86 | "\n", 87 | "Statsample::Analysis.store(Statsample::Test::Levene) do\n", 88 | "\n", 89 | " a = Daru::Vector.new([1,2,3,4,5,6,7,8,100,10])\n", 90 | " puts \"variance a : #{a.variance}\"\n", 91 | " b = Daru::Vector.new([30,40,50,60,70,80,90,100,110,120])\n", 92 | " puts \"variance b : #{b.variance}\\n\\n\"\n", 93 | " summary(levene([a,b]))\n", 94 | "end\n", 95 | " Statsample::Analysis.run_batch" 96 | ] 97 | } 98 | ], 99 | "metadata": { 100 | "kernelspec": { 101 | "display_name": "Ruby 2.2.1", 102 | "language": "ruby", 103 | "name": "ruby" 104 | }, 105 | "language_info": { 106 | "file_extension": ".rb", 107 | "mimetype": "application/x-ruby", 108 | "name": "ruby", 109 | "version": "2.2.1" 110 | } 111 | }, 112 | "nbformat": 4, 113 | "nbformat_minor": 0 114 | } 115 | -------------------------------------------------------------------------------- /Statistics/Mann Whitney U Test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Mann-Whitney U test\n", 8 | "\n", 9 | "Example illustrating Mann-Whitney U test with statsample.\n", 10 | "\n", 11 | "#### References\n", 12 | "\n", 13 | "http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "application/javascript": [ 26 | "if(window['d3'] === undefined ||\n", 27 | " window['Nyaplot'] === undefined){\n", 28 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 29 | "\n", 30 | "\n", 31 | "\n", 32 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 33 | "\n", 34 | " require.config({paths: path, shim:shim});\n", 35 | "\n", 36 | "\n", 37 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 38 | "\n", 39 | "\tvar script = d3.select(\"head\")\n", 40 | "\t .append(\"script\")\n", 41 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 42 | "\t .attr(\"async\", true);\n", 43 | "\n", 44 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 45 | "\n", 46 | "\n", 47 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 48 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 49 | "\t window.dispatchEvent(event);\n", 50 | "\t console.log('Finished loading Nyaplotjs');\n", 51 | "\n", 52 | "\t};\n", 53 | "\n", 54 | "\n", 55 | "});});\n", 56 | "}\n" 57 | ], 58 | "text/plain": [ 59 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 60 | ] 61 | }, 62 | "metadata": {}, 63 | "output_type": "display_data" 64 | }, 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Analysis 2016-03-26 02:37:49 +0000\n", 70 | "= Statsample::Test::UMannWhitney\n", 71 | " == Mann-Whitney's U\n", 72 | " Mann-Whitney's U results\n", 73 | "+--------------------------------------+-------------------+\n", 74 | "| Sum of ranks | 81.000 |\n", 75 | "| Sum of ranks | 384.000 |\n", 76 | "| U Value | 26.000 |\n", 77 | "| Z | -3.258 (p: 0.001) |\n", 78 | "| Exact p (Dinneen & Blakesley, 1973): | 0.001 |\n", 79 | "+--------------------------------------+-------------------+\n", 80 | "\n", 81 | "\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "require 'statsample'\n", 87 | "\n", 88 | "Statsample::Analysis.store(Statsample::Test::UMannWhitney) do\n", 89 | "\n", 90 | " a = Daru::Vector.new(10.times.map {rand(100)})\n", 91 | " b = Daru::Vector.new(20.times.map {(rand(20))**2+50})\n", 92 | "\n", 93 | " u=Statsample::Test::UMannWhitney.new(a,b)\n", 94 | " summary u\n", 95 | "end\n", 96 | " Statsample::Analysis.run_batch" 97 | ] 98 | } 99 | ], 100 | "metadata": { 101 | "kernelspec": { 102 | "display_name": "Ruby 2.2.1", 103 | "language": "ruby", 104 | "name": "ruby" 105 | }, 106 | "language_info": { 107 | "file_extension": ".rb", 108 | "mimetype": "application/x-ruby", 109 | "name": "ruby", 110 | "version": "2.2.1" 111 | } 112 | }, 113 | "nbformat": 4, 114 | "nbformat_minor": 0 115 | } 116 | -------------------------------------------------------------------------------- /Statistics/Multiple Regression.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Multiple Regression\n", 8 | "\n", 9 | "This example shows how multiple regression can be performed using statsample and daru.\n", 10 | "\n", 11 | "The `lr()` shorthand will call the function Statsample::Regression.multiple. It should be\n", 12 | "noted that internally statsample implements multiple regression using either Ruby methods\n", 13 | "or GSL methods. This lets statsample run even in the absence of gsl. But ruby implementations\n", 14 | "of functions are much much slower than those from GSL, and hence it is recomended that you\n", 15 | "install the [rb-gsl](https://github.com/blackwinter/rb-gsl) or [gsl-nmatrix](https://github.com/v0dro/gsl-nmatrix) gems before proceeding (these will work only on MRI).\n", 16 | "\n", 17 | "Rb-gsl can be installed from rubygems directly with `gem install rb-gsl`. To see how to install\n", 18 | "gsl-nmatrix, see [this blog post](http://v0dro.github.io/blog/2015/05/12/making-statsample-work-with-rb-gsl-and-gsl-nmatrix/)." 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "metadata": { 25 | "collapsed": false 26 | }, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "application/javascript": [ 31 | "if(window['d3'] === undefined ||\n", 32 | " window['Nyaplot'] === undefined){\n", 33 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 34 | "\n", 35 | "\n", 36 | "\n", 37 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 38 | "\n", 39 | " require.config({paths: path, shim:shim});\n", 40 | "\n", 41 | "\n", 42 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 43 | "\n", 44 | "\tvar script = d3.select(\"head\")\n", 45 | "\t .append(\"script\")\n", 46 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 47 | "\t .attr(\"async\", true);\n", 48 | "\n", 49 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 50 | "\n", 51 | "\n", 52 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 53 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 54 | "\t window.dispatchEvent(event);\n", 55 | "\t console.log('Finished loading Nyaplotjs');\n", 56 | "\n", 57 | "\t};\n", 58 | "\n", 59 | "\n", 60 | "});});\n", 61 | "}\n" 62 | ], 63 | "text/plain": [ 64 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 65 | ] 66 | }, 67 | "metadata": {}, 68 | "output_type": "display_data" 69 | }, 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "Analysis 2016-03-26 02:39:03 +0000\n", 75 | "= Statsample::Regression::Multiple\n", 76 | " == Multiple reggresion of a,b,cc,d on y\n", 77 | " Engine: Statsample::Regression::Multiple::RubyEngine\n", 78 | " Cases(listwise)=2000(2000)\n", 79 | " R=0.987\n", 80 | " R^2=0.975\n", 81 | " R^2 Adj=0.975\n", 82 | " Std.Error R=0.992\n", 83 | " Equation=-0.004 + 4.990a + 2.965b + 1.986cc + 0.991d\n", 84 | " === ANOVA\n", 85 | " ANOVA Table\n", 86 | "+------------+-----------+------+-----------+-----------+-------+\n", 87 | "| source | ss | df | ms | f | p |\n", 88 | "+------------+-----------+------+-----------+-----------+-------+\n", 89 | "| Regression | 76290.828 | 4 | 19072.707 | 19373.613 | 0.000 |\n", 90 | "| Error | 1964.014 | 1995 | 0.984 | | |\n", 91 | "| Total | 78254.842 | 1999 | 19073.691 | | |\n", 92 | "+------------+-----------+------+-----------+-----------+-------+\n", 93 | "\n", 94 | " Beta coefficients\n", 95 | "+----------+--------+-------+-------+---------+\n", 96 | "| coeff | b | beta | se | t |\n", 97 | "+----------+--------+-------+-------+---------+\n", 98 | "| Constant | -0.004 | - | 0.022 | -0.170 |\n", 99 | "| a | 4.990 | 0.805 | 0.022 | 226.825 |\n", 100 | "| b | 2.965 | 0.460 | 0.023 | 129.376 |\n", 101 | "| cc | 1.986 | 0.317 | 0.022 | 89.238 |\n", 102 | "| d | 0.991 | 0.160 | 0.022 | 44.963 |\n", 103 | "+----------+--------+-------+-------+---------+\n", 104 | "\n", 105 | "\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "require 'statsample'\n", 111 | "\n", 112 | "Statsample::Analysis.store(Statsample::Regression::Multiple) do\n", 113 | " Daru.lazy_update = true\n", 114 | " \n", 115 | " samples=2000\n", 116 | " ds = Daru::DataFrame.new({\n", 117 | " :a => rnorm(samples),\n", 118 | " :b => rnorm(samples),\n", 119 | " :cc => rnorm(samples),\n", 120 | " :d => rnorm(samples)}, clone: false)\n", 121 | " attach(ds)\n", 122 | " ds[:y] = a*5+b*3+cc*2+d+rnorm(samples)\n", 123 | " \n", 124 | " # REMEMBER: It is _mandatory_ to call #update after assingnment cycles if your \n", 125 | " # operations to be performed as expected.\n", 126 | " ds.update\n", 127 | " summary lr(ds,:y)\n", 128 | " \n", 129 | " Daru.lazy_update = false\n", 130 | "end\n", 131 | "Statsample::Analysis.run_batch" 132 | ] 133 | } 134 | ], 135 | "metadata": { 136 | "kernelspec": { 137 | "display_name": "Ruby 2.2.1", 138 | "language": "ruby", 139 | "name": "ruby" 140 | }, 141 | "language_info": { 142 | "file_extension": ".rb", 143 | "mimetype": "application/x-ruby", 144 | "name": "ruby", 145 | "version": "2.2.1" 146 | } 147 | }, 148 | "nbformat": 4, 149 | "nbformat_minor": 0 150 | } 151 | -------------------------------------------------------------------------------- /Statistics/Parallel Analysis on PCA.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Parallel Analysis on PCA" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false, 15 | "scrolled": false 16 | }, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "application/javascript": [ 21 | "if(window['d3'] === undefined ||\n", 22 | " window['Nyaplot'] === undefined){\n", 23 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 24 | "\n", 25 | "\n", 26 | "\n", 27 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 28 | "\n", 29 | " require.config({paths: path, shim:shim});\n", 30 | "\n", 31 | "\n", 32 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 33 | "\n", 34 | "\tvar script = d3.select(\"head\")\n", 35 | "\t .append(\"script\")\n", 36 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 37 | "\t .attr(\"async\", true);\n", 38 | "\n", 39 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 40 | "\n", 41 | "\n", 42 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 43 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 44 | "\t window.dispatchEvent(event);\n", 45 | "\t console.log('Finished loading Nyaplotjs');\n", 46 | "\n", 47 | "\t};\n", 48 | "\n", 49 | "\n", 50 | "});});\n", 51 | "}\n" 52 | ], 53 | "text/plain": [ 54 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 55 | ] 56 | }, 57 | "metadata": {}, 58 | "output_type": "display_data" 59 | }, 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "Parallel Analysis: Iteration 0\n", 65 | "Parallel Analysis: Iteration 1\n", 66 | "Parallel Analysis: Iteration 2\n", 67 | "Parallel Analysis: Iteration 3\n", 68 | "Parallel Analysis: Iteration 4\n", 69 | "Parallel Analysis: Iteration 5\n", 70 | "Parallel Analysis: Iteration 6\n", 71 | "Parallel Analysis: Iteration 7\n", 72 | "Parallel Analysis: Iteration 8\n", 73 | "Parallel Analysis: Iteration 9\n", 74 | "Parallel Analysis: Iteration 10\n", 75 | "Parallel Analysis: Iteration 11\n", 76 | "Parallel Analysis: Iteration 12\n", 77 | "Parallel Analysis: Iteration 13\n", 78 | "Parallel Analysis: Iteration 14\n", 79 | "Parallel Analysis: Iteration 15\n", 80 | "Parallel Analysis: Iteration 16\n", 81 | "Parallel Analysis: Iteration 17\n", 82 | "Parallel Analysis: Iteration 18\n", 83 | "Parallel Analysis: Iteration 19\n", 84 | "Parallel Analysis: Iteration 20\n", 85 | "Parallel Analysis: Iteration 21\n", 86 | "Parallel Analysis: Iteration 22\n", 87 | "Parallel Analysis: Iteration 23\n", 88 | "Parallel Analysis: Iteration 24\n", 89 | "Parallel Analysis: Iteration 25\n", 90 | "Parallel Analysis: Iteration 26\n", 91 | "Parallel Analysis: Iteration 27\n", 92 | "Parallel Analysis: Iteration 28\n", 93 | "Parallel Analysis: Iteration 29\n", 94 | "Parallel Analysis: Iteration 30\n", 95 | "Parallel Analysis: Iteration 31\n", 96 | "Parallel Analysis: Iteration 32\n", 97 | "Parallel Analysis: Iteration 33\n", 98 | "Parallel Analysis: Iteration 34\n", 99 | "Parallel Analysis: Iteration 35\n", 100 | "Parallel Analysis: Iteration 36\n", 101 | "Parallel Analysis: Iteration 37\n", 102 | "Parallel Analysis: Iteration 38\n", 103 | "Parallel Analysis: Iteration 39\n", 104 | "Parallel Analysis: Iteration 40\n", 105 | "Parallel Analysis: Iteration 41\n", 106 | "Parallel Analysis: Iteration 42\n", 107 | "Parallel Analysis: Iteration 43\n", 108 | "Parallel Analysis: Iteration 44\n", 109 | "Parallel Analysis: Iteration 45\n", 110 | "Parallel Analysis: Iteration 46\n", 111 | "Parallel Analysis: Iteration 47\n", 112 | "Parallel Analysis: Iteration 48\n", 113 | "Parallel Analysis: Iteration 49\n", 114 | "Analysis 2016-03-26 02:42:37 +0000\n", 115 | "= Statsample::Factor::ParallelAnalysis\n", 116 | " There are 3 real factors on data\n", 117 | " == Principal Component Analysis\n", 118 | " Number of factors: 8\n", 119 | " Communalities\n", 120 | "+----------+---------+------------+--------+\n", 121 | "| Variable | Initial | Extraction | % |\n", 122 | "+----------+---------+------------+--------+\n", 123 | "| v0 | 1.000 | 0.718 | 71.814 |\n", 124 | "| v1 | 1.000 | 0.806 | 80.592 |\n", 125 | "| v10 | 1.000 | 0.742 | 74.220 |\n", 126 | "| v11 | 1.000 | 0.672 | 67.184 |\n", 127 | "| v12 | 1.000 | 0.767 | 76.672 |\n", 128 | "| v13 | 1.000 | 0.525 | 52.483 |\n", 129 | "| v14 | 1.000 | 0.613 | 61.319 |\n", 130 | "| v15 | 1.000 | 0.767 | 76.689 |\n", 131 | "| v16 | 1.000 | 0.580 | 58.006 |\n", 132 | "| v17 | 1.000 | 0.614 | 61.435 |\n", 133 | "| v18 | 1.000 | 0.571 | 57.060 |\n", 134 | "| v19 | 1.000 | 0.606 | 60.624 |\n", 135 | "| v2 | 1.000 | 0.745 | 74.486 |\n", 136 | "| v20 | 1.000 | 0.735 | 73.461 |\n", 137 | "| v21 | 1.000 | 0.835 | 83.501 |\n", 138 | "| v22 | 1.000 | 0.874 | 87.361 |\n", 139 | "| v23 | 1.000 | 0.830 | 82.958 |\n", 140 | "| v24 | 1.000 | 0.900 | 90.029 |\n", 141 | "| v25 | 1.000 | 0.930 | 93.029 |\n", 142 | "| v26 | 1.000 | 0.940 | 94.045 |\n", 143 | "| v27 | 1.000 | 0.957 | 95.748 |\n", 144 | "| v28 | 1.000 | 0.975 | 97.489 |\n", 145 | "| v29 | 1.000 | 0.979 | 97.931 |\n", 146 | "| v3 | 1.000 | 0.675 | 67.468 |\n", 147 | "| v4 | 1.000 | 0.676 | 67.614 |\n", 148 | "| v5 | 1.000 | 0.639 | 63.899 |\n", 149 | "| v6 | 1.000 | 0.707 | 70.699 |\n", 150 | "| v7 | 1.000 | 0.702 | 70.152 |\n", 151 | "| v8 | 1.000 | 0.593 | 59.331 |\n", 152 | "| v9 | 1.000 | 0.774 | 77.365 |\n", 153 | "+----------+---------+------------+--------+\n", 154 | "\n", 155 | " Total Variance Explained\n", 156 | "+--------------+---------+---------+---------+\n", 157 | "| Component | E.Total | % | Cum. % |\n", 158 | "+--------------+---------+---------+---------+\n", 159 | "| Component 1 | 11.635 | 38.784% | 38.784 |\n", 160 | "| Component 2 | 2.228 | 7.425% | 46.209 |\n", 161 | "| Component 3 | 1.868 | 6.225% | 52.434 |\n", 162 | "| Component 4 | 1.781 | 5.936% | 58.371 |\n", 163 | "| Component 5 | 1.503 | 5.009% | 63.380 |\n", 164 | "| Component 6 | 1.275 | 4.250% | 67.630 |\n", 165 | "| Component 7 | 1.149 | 3.830% | 71.460 |\n", 166 | "| Component 8 | 1.009 | 3.362% | 74.822 |\n", 167 | "| Component 9 | 0.948 | 3.162% | 77.984 |\n", 168 | "| Component 10 | 0.813 | 2.709% | 80.692 |\n", 169 | "| Component 11 | 0.776 | 2.585% | 83.278 |\n", 170 | "| Component 12 | 0.688 | 2.292% | 85.570 |\n", 171 | "| Component 13 | 0.584 | 1.945% | 87.515 |\n", 172 | "| Component 14 | 0.516 | 1.719% | 89.235 |\n", 173 | "| Component 15 | 0.490 | 1.633% | 90.868 |\n", 174 | "| Component 16 | 0.454 | 1.512% | 92.380 |\n", 175 | "| Component 17 | 0.416 | 1.388% | 93.768 |\n", 176 | "| Component 18 | 0.345 | 1.149% | 94.916 |\n", 177 | "| Component 19 | 0.322 | 1.073% | 95.990 |\n", 178 | "| Component 20 | 0.276 | 0.919% | 96.908 |\n", 179 | "| Component 21 | 0.240 | 0.800% | 97.708 |\n", 180 | "| Component 22 | 0.206 | 0.685% | 98.394 |\n", 181 | "| Component 23 | 0.132 | 0.439% | 98.832 |\n", 182 | "| Component 24 | 0.098 | 0.327% | 99.159 |\n", 183 | "| Component 25 | 0.095 | 0.315% | 99.475 |\n", 184 | "| Component 26 | 0.064 | 0.215% | 99.690 |\n", 185 | "| Component 27 | 0.050 | 0.167% | 99.857 |\n", 186 | "| Component 28 | 0.030 | 0.100% | 99.957 |\n", 187 | "| Component 29 | 0.010 | 0.034% | 99.990 |\n", 188 | "| Component 30 | 0.003 | 0.010% | 100.000 |\n", 189 | "+--------------+---------+---------+---------+\n", 190 | "\n", 191 | " Component matrix\n", 192 | "+-----+-------+-------+-------+-------+-------+-------+-------+-------+\n", 193 | "| | PC_1 | PC_2 | PC_3 | PC_4 | PC_5 | PC_6 | PC_7 | PC_8 |\n", 194 | "+-----+-------+-------+-------+-------+-------+-------+-------+-------+\n", 195 | "| v0 | .029 | .599 | .211 | -.365 | -.017 | -.128 | -.379 | .145 |\n", 196 | "| v1 | -.011 | .431 | .139 | .104 | -.148 | .729 | -.163 | -.096 |\n", 197 | "| v10 | -.309 | .211 | -.302 | -.367 | -.453 | .220 | .102 | .335 |\n", 198 | "| v11 | -.386 | .373 | .258 | .394 | -.326 | -.091 | -.001 | .217 |\n", 199 | "| v12 | -.351 | .063 | .544 | -.128 | .016 | .019 | .567 | -.070 |\n", 200 | "| v13 | -.450 | .432 | .243 | .120 | .114 | -.130 | -.172 | -.046 |\n", 201 | "| v14 | -.539 | -.069 | .166 | .273 | .024 | .395 | .018 | .242 |\n", 202 | "| v15 | -.576 | -.032 | -.210 | .192 | -.494 | .202 | .215 | -.147 |\n", 203 | "| v16 | -.546 | -.378 | .264 | -.110 | .074 | -.006 | .060 | .218 |\n", 204 | "| v17 | -.627 | .258 | .005 | .151 | .093 | .131 | -.271 | -.181 |\n", 205 | "| v18 | -.716 | -.171 | -.084 | .120 | .020 | .049 | -.050 | .045 |\n", 206 | "| v19 | -.718 | .088 | -.063 | .054 | -.249 | .111 | .007 | .047 |\n", 207 | "| v2 | -.064 | -.591 | .400 | .197 | -.086 | .126 | -.361 | -.199 |\n", 208 | "| v20 | -.826 | -.133 | .017 | .069 | .132 | .099 | -.008 | -.053 |\n", 209 | "| v21 | -.882 | -.147 | .001 | -.034 | .014 | .091 | -.141 | -.081 |\n", 210 | "| v22 | -.904 | .033 | -.012 | -.147 | .040 | -.149 | -.059 | .078 |\n", 211 | "| v23 | -.890 | .023 | .109 | -.112 | .085 | -.012 | .041 | -.056 |\n", 212 | "| v24 | -.924 | .125 | -.011 | -.025 | .097 | -.089 | .101 | .054 |\n", 213 | "| v25 | -.937 | .054 | -.084 | -.060 | .083 | -.176 | -.006 | -.025 |\n", 214 | "| v26 | -.961 | -.054 | -.052 | -.028 | .076 | -.061 | .006 | -.022 |\n", 215 | "| v27 | -.965 | -.016 | -.070 | .006 | .122 | -.048 | .016 | -.058 |\n", 216 | "| v28 | -.970 | .015 | -.106 | -.009 | .126 | -.070 | .029 | -.012 |\n", 217 | "| v29 | -.974 | -.007 | -.108 | .002 | .117 | -.057 | .004 | -.035 |\n", 218 | "| v3 | -.036 | .422 | .209 | -.062 | -.345 | -.231 | .368 | -.374 |\n", 219 | "| v4 | -.107 | -.546 | .428 | -.164 | -.320 | .069 | .046 | -.217 |\n", 220 | "| v5 | .010 | .055 | .656 | .203 | .004 | -.067 | .015 | .400 |\n", 221 | "| v6 | .078 | .131 | .503 | -.612 | .130 | .076 | -.140 | -.115 |\n", 222 | "| v7 | -.091 | -.236 | .063 | .227 | -.528 | -.439 | -.230 | .240 |\n", 223 | "| v8 | -.318 | .010 | -.048 | -.248 | -.445 | -.162 | -.337 | -.300 |\n", 224 | "| v9 | -.203 | -.302 | -.115 | -.713 | -.142 | .164 | .011 | .269 |\n", 225 | "+-----+-------+-------+-------+-------+-------+-------+-------+-------+\n", 226 | "\n", 227 | " Traditional Kaiser criterion (k>1) returns 8 factors\n", 228 | " == Parallel Analysis\n", 229 | " Bootstrap Method: random\n", 230 | " Uses SMC: No\n", 231 | " Correlation Matrix type : correlation_matrix\n", 232 | " Number of variables: 30\n", 233 | " Number of cases: 150\n", 234 | " Number of iterations: 50\n", 235 | " Number or factors to preserve: 4\n", 236 | " Eigenvalues\n", 237 | "+----+-----------------+----------------------+--------+-----------+\n", 238 | "| n | data eigenvalue | generated eigenvalue | p.95 | preserve? |\n", 239 | "+----+-----------------+----------------------+--------+-----------+\n", 240 | "| 1 | 11.6353 | 1.9397 | 2.0744 | Yes |\n", 241 | "| 2 | 2.2275 | 1.7961 | 1.8770 | Yes |\n", 242 | "| 3 | 1.8675 | 1.6885 | 1.7637 | Yes |\n", 243 | "| 4 | 1.7809 | 1.6032 | 1.6780 | Yes |\n", 244 | "| 5 | 1.5027 | 1.5281 | 1.5856 | |\n", 245 | "| 6 | 1.2750 | 1.4573 | 1.5253 | |\n", 246 | "| 7 | 1.1491 | 1.3892 | 1.4417 | |\n", 247 | "| 8 | 1.0086 | 1.3263 | 1.3981 | |\n", 248 | "| 9 | 0.9485 | 1.2711 | 1.3060 | |\n", 249 | "| 10 | 0.8126 | 1.2174 | 1.2501 | |\n", 250 | "| 11 | 0.7756 | 1.1585 | 1.2080 | |\n", 251 | "| 12 | 0.6877 | 1.1053 | 1.1429 | |\n", 252 | "| 13 | 0.5836 | 1.0553 | 1.0955 | |\n", 253 | "| 14 | 0.5158 | 1.0083 | 1.0468 | |\n", 254 | "| 15 | 0.4899 | 0.9635 | 1.0054 | |\n", 255 | "| 16 | 0.4537 | 0.9161 | 0.9573 | |\n", 256 | "| 17 | 0.4164 | 0.8739 | 0.9153 | |\n", 257 | "| 18 | 0.3446 | 0.8270 | 0.8561 | |\n", 258 | "| 19 | 0.3220 | 0.7909 | 0.8310 | |\n", 259 | "| 20 | 0.2756 | 0.7524 | 0.7944 | |\n", 260 | "| 21 | 0.2400 | 0.7086 | 0.7465 | |\n", 261 | "| 22 | 0.2056 | 0.6678 | 0.7227 | |\n", 262 | "| 23 | 0.1316 | 0.6309 | 0.6700 | |\n", 263 | "| 24 | 0.0982 | 0.5889 | 0.6263 | |\n", 264 | "| 25 | 0.0946 | 0.5534 | 0.5885 | |\n", 265 | "| 26 | 0.0645 | 0.5173 | 0.5562 | |\n", 266 | "| 27 | 0.0501 | 0.4795 | 0.5195 | |\n", 267 | "| 28 | 0.0300 | 0.4405 | 0.4791 | |\n", 268 | "| 29 | 0.0101 | 0.3978 | 0.4305 | |\n", 269 | "| 30 | 0.0029 | 0.3470 | 0.3938 | |\n", 270 | "+----+-----------------+----------------------+--------+-----------+\n", 271 | "\n", 272 | " Parallel Analysis returns 4 factors to preserve\n", 273 | "\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "require 'statsample'\n", 279 | "samples=150\n", 280 | "variables=30\n", 281 | "iterations=50\n", 282 | "Statsample::Analysis.store(Statsample::Factor::ParallelAnalysis) do\n", 283 | " Daru.lazy_update = true\n", 284 | " \n", 285 | " rng = Distribution::Normal.rng()\n", 286 | " f1 = rnorm(samples)\n", 287 | " f2 = rnorm(samples)\n", 288 | " f3 = rnorm(samples)\n", 289 | "\n", 290 | " vectors={}\n", 291 | "\n", 292 | " variables.times do |i|\n", 293 | " vectors[\"v#{i}\".to_sym] = Daru::Vector.new(samples.times.collect {|nv| f1[nv]*i+(f2[nv]*(15-i))+((f3[nv]*(30-i))*1.5)*rng.call})\n", 294 | " vectors[\"v#{i}\".to_sym].rename \"Vector #{i}\"\n", 295 | " end\n", 296 | "\n", 297 | " ds = Daru::DataFrame.new(vectors)\n", 298 | "\n", 299 | " pa=Statsample::Factor::ParallelAnalysis.new(ds, :iterations=>iterations, :debug=>true)\n", 300 | " pca=pca(cor(ds))\n", 301 | " echo \"There are 3 real factors on data\"\n", 302 | " summary pca\n", 303 | " echo \"Traditional Kaiser criterion (k>1) returns #{pca.m} factors\"\n", 304 | " summary pa\n", 305 | " echo \"Parallel Analysis returns #{pa.number_of_factors} factors to preserve\"\n", 306 | " Daru.lazy_update = false\n", 307 | "end\n", 308 | "\n", 309 | "Statsample::Analysis.run_batch\n" 310 | ] 311 | } 312 | ], 313 | "metadata": { 314 | "kernelspec": { 315 | "display_name": "Ruby 2.2.1", 316 | "language": "ruby", 317 | "name": "ruby" 318 | }, 319 | "language_info": { 320 | "file_extension": ".rb", 321 | "mimetype": "application/x-ruby", 322 | "name": "ruby", 323 | "version": "2.2.1" 324 | } 325 | }, 326 | "nbformat": 4, 327 | "nbformat_minor": 0 328 | } 329 | -------------------------------------------------------------------------------- /Statistics/Polychoric Correlation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Polychoric Correlation using two-step and joint method\n", 8 | "\n", 9 | "Polychoric correlation in statsample requires installation of the [statsample-bivariate-extension](https://rubygems.org/gems/statsample-bivariate-extension) gem. This gem extends the Statsample::Bivariate class with useful algorithms for polychoric and tetrachoric correlation.\n", 10 | "\n", 11 | "Statsample will automatically detect presence of polychoric/tetrachoric algorithms so there is no need to explicitly require the gem.\n", 12 | "\n", 13 | "In this example we'll see how polychoric correlation can be performed using statsample." 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 3, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [ 23 | { 24 | "name": "stdout", 25 | "output_type": "stream", 26 | "text": [ 27 | "Analysis 2015-06-04 12:48:32 +0530\n", 28 | "= Statsample::Bivariate::Polychoric\n", 29 | " == Polychoric with two-step\n", 30 | " Contingence Table\n", 31 | "+-------+-----+-----+-----+-------+\n", 32 | "| | Y=0 | Y=1 | Y=2 | Total |\n", 33 | "+-------+-----+-----+-----+-------+\n", 34 | "| X = 0 | 52 | 58 | 8 | 118 |\n", 35 | "| X = 1 | 18 | 15 | 7 | 40 |\n", 36 | "| X = 2 | 5 | 11 | 3 | 19 |\n", 37 | "+-------+-----+-----+-----+-------+\n", 38 | "| T | 75 | 84 | 18 | 177 |\n", 39 | "+-------+-----+-----+-----+-------+\n", 40 | "\n", 41 | " r: 0.1683\n", 42 | " Thresholds\n", 43 | "+---------------+---------+\n", 44 | "| | Value |\n", 45 | "+---------------+---------+\n", 46 | "| Threshold X 0 | 0.4307 |\n", 47 | "| Threshold X 1 | 1.2408 |\n", 48 | "| Threshold Y 0 | -0.1924 |\n", 49 | "| Threshold Y 1 | 1.2720 |\n", 50 | "+---------------+---------+\n", 51 | "\n", 52 | " Iterations: 10\n", 53 | " Test of bivariate normality: X^2 = 3.941, df = 3, p= 0.26795\n", 54 | " == Polychoric with joint\n", 55 | " Contingence Table\n", 56 | "+-------+-----+-----+-----+-------+\n", 57 | "| | Y=0 | Y=1 | Y=2 | Total |\n", 58 | "+-------+-----+-----+-----+-------+\n", 59 | "| X = 0 | 52 | 58 | 8 | 118 |\n", 60 | "| X = 1 | 18 | 15 | 7 | 40 |\n", 61 | "| X = 2 | 5 | 11 | 3 | 19 |\n", 62 | "+-------+-----+-----+-----+-------+\n", 63 | "| T | 75 | 84 | 18 | 177 |\n", 64 | "+-------+-----+-----+-----+-------+\n", 65 | "\n", 66 | " r: 0.1682\n", 67 | " Thresholds\n", 68 | "+---------------+---------+\n", 69 | "| | Value |\n", 70 | "+---------------+---------+\n", 71 | "| Threshold X 0 | 0.4296 |\n", 72 | "| Threshold X 1 | 1.2411 |\n", 73 | "| Threshold Y 0 | -0.1936 |\n", 74 | "| Threshold Y 1 | 1.2731 |\n", 75 | "+---------------+---------+\n", 76 | "\n", 77 | " Iterations: 1\n", 78 | " Test of bivariate normality: X^2 = 3.940, df = 3, p= 0.26801\n", 79 | " == Polychoric with polychoric series\n", 80 | " Contingence Table\n", 81 | "+-------+------+------+------+-------+\n", 82 | "| | Y=0 | Y=1 | Y=2 | Total |\n", 83 | "+-------+------+------+------+-------+\n", 84 | "| X = 0 | 52 | 58 | 8 | 118.0 |\n", 85 | "| X = 1 | 18 | 15 | 7 | 40.0 |\n", 86 | "| X = 2 | 5 | 11 | 3 | 19.0 |\n", 87 | "+-------+------+------+------+-------+\n", 88 | "| T | 75.0 | 84.0 | 18.0 | 177.0 |\n", 89 | "+-------+------+------+------+-------+\n", 90 | "\n", 91 | " r: 0.1701\n", 92 | " Thresholds\n", 93 | "+---------------+---------+\n", 94 | "| | Value |\n", 95 | "+---------------+---------+\n", 96 | "| Threshold X 0 | 0.4307 |\n", 97 | "| Threshold X 1 | 1.2408 |\n", 98 | "| Threshold Y 0 | -0.1924 |\n", 99 | "| Threshold Y 1 | 1.2720 |\n", 100 | "+---------------+---------+\n", 101 | "\n", 102 | " Test of bivariate normality: X^2 = 3.941, df = 3, p= 0.26791\n", 103 | "\n" 104 | ] 105 | } 106 | ], 107 | "source": [ 108 | "require 'statsample'\n", 109 | "\n", 110 | "Statsample::Analysis.store(Statsample::Bivariate::Polychoric) do \n", 111 | " ct=Matrix[[rand(10)+50, rand(10)+50, rand(10)+1],\n", 112 | " [rand(20)+5, rand(50)+4, rand(10)+1],\n", 113 | " [rand(8)+1, rand(12)+1, rand(10)+1]]\n", 114 | "\n", 115 | " # Estimation of polychoric correlation using two-step (default)\n", 116 | " poly=polychoric(ct, :name=>\"Polychoric with two-step\", :debug=>false)\n", 117 | " summary poly\n", 118 | "\n", 119 | " # Estimation of polychoric correlation using joint method (slow)\n", 120 | " poly=polychoric(ct, :method=>:joint, :name=>\"Polychoric with joint\")\n", 121 | " summary poly\n", 122 | "\n", 123 | " # Uses polychoric series (not recomended)\n", 124 | "\n", 125 | " poly=polychoric(ct, :method=>:polychoric_series, :name=>\"Polychoric with polychoric series\")\n", 126 | " summary poly\n", 127 | "end\n", 128 | "\n", 129 | "Statsample::Analysis.run_batch" 130 | ] 131 | } 132 | ], 133 | "metadata": { 134 | "kernelspec": { 135 | "display_name": "Ruby 2.2.1", 136 | "language": "ruby", 137 | "name": "ruby" 138 | }, 139 | "language_info": { 140 | "file_extension": "rb", 141 | "mimetype": "application/x-ruby", 142 | "name": "ruby", 143 | "version": "2.2.1" 144 | } 145 | }, 146 | "nbformat": 4, 147 | "nbformat_minor": 0 148 | } 149 | -------------------------------------------------------------------------------- /Statistics/Principal Axis Analysis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Principal Axis Analysis\n", 8 | "\n", 9 | "Here we use the Statsample::Factor::PrincipalAnalysis class for principal axis analysis for a correlation or covariance matrix." 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": false 17 | }, 18 | "outputs": [ 19 | { 20 | "data": { 21 | "application/javascript": [ 22 | "if(window['d3'] === undefined ||\n", 23 | " window['Nyaplot'] === undefined){\n", 24 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 25 | "\n", 26 | "\n", 27 | "\n", 28 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 29 | "\n", 30 | " require.config({paths: path, shim:shim});\n", 31 | "\n", 32 | "\n", 33 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 34 | "\n", 35 | "\tvar script = d3.select(\"head\")\n", 36 | "\t .append(\"script\")\n", 37 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 38 | "\t .attr(\"async\", true);\n", 39 | "\n", 40 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 41 | "\n", 42 | "\n", 43 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 44 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 45 | "\t window.dispatchEvent(event);\n", 46 | "\t console.log('Finished loading Nyaplotjs');\n", 47 | "\n", 48 | "\t};\n", 49 | "\n", 50 | "\n", 51 | "});});\n", 52 | "}\n" 53 | ], 54 | "text/plain": [ 55 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 56 | ] 57 | }, 58 | "metadata": {}, 59 | "output_type": "display_data" 60 | }, 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "Analysis 2016-03-26 10:21:02 +0000\n", 66 | "= Statsample::Factor::PrincipalAxis\n", 67 | " == \n", 68 | " Number of factors: 1\n", 69 | " Iterations: 5\n", 70 | " Communalities\n", 71 | "+----------+---------+------------+\n", 72 | "| Variable | Initial | Extraction |\n", 73 | "+----------+---------+------------+\n", 74 | "| X0 | 1.0000 | 0.854 |\n", 75 | "| X1 | 1.0000 | 0.830 |\n", 76 | "| X2 | 1.0000 | 0.258 |\n", 77 | "| X3 | 1.0000 | 0.233 |\n", 78 | "+----------+---------+------------+\n", 79 | "\n", 80 | " Total Variance\n", 81 | "+----------+-----------+---------+------------+-----------+---------+------------+\n", 82 | "| Factor | I.E.Total | I.E. % | I.E.Cum. % | S.L.Total | S.L. % | S.L.Cum. % |\n", 83 | "+----------+-----------+---------+------------+-----------+---------+------------+\n", 84 | "| Factor 1 | 2.493 | 62.328% | 62.328 | 2.175 | 54.382% | 54.382 |\n", 85 | "| Factor 2 | 1.503 | 37.572% | 99.900 | | | |\n", 86 | "| Factor 3 | 0.004 | 0.099% | 99.998 | | | |\n", 87 | "| Factor 4 | 0.000 | 0.002% | 100.000 | | | |\n", 88 | "+----------+-----------+---------+------------+-----------+---------+------------+\n", 89 | "\n", 90 | " Factor Matrix\n", 91 | "+----+----------+\n", 92 | "| | factor_1 |\n", 93 | "+----+----------+\n", 94 | "| X0 | .924 |\n", 95 | "| X1 | .911 |\n", 96 | "| X2 | .508 |\n", 97 | "| X3 | .483 |\n", 98 | "+----+----------+\n", 99 | "\n", 100 | "\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "require 'statsample'\n", 106 | "\n", 107 | "Statsample::Analysis.store(Statsample::Factor::PrincipalAxis) do\n", 108 | " matrix = Matrix[\n", 109 | " [1.0, 0.709501601093587, 0.877596585880047, 0.272219316266807], \n", 110 | " [0.709501601093587, 1.0, 0.291633797330304, 0.871141831433844], \n", 111 | " [0.877596585880047, 0.291633797330304, 1.0, -0.213373722977167], \n", 112 | " [0.272219316266807, 0.871141831433844, -0.213373722977167, 1.0]\n", 113 | " ]\n", 114 | " \n", 115 | " matrix.extend Statsample::CovariateMatrix\n", 116 | " fa = principal_axis(matrix,:m=>1,:smc=>false)\n", 117 | " \n", 118 | " summary fa\n", 119 | "end\n", 120 | "\n", 121 | "Statsample::Analysis.run_batch" 122 | ] 123 | } 124 | ], 125 | "metadata": { 126 | "kernelspec": { 127 | "display_name": "Ruby 2.2.1", 128 | "language": "ruby", 129 | "name": "ruby" 130 | }, 131 | "language_info": { 132 | "file_extension": ".rb", 133 | "mimetype": "application/x-ruby", 134 | "name": "ruby", 135 | "version": "2.2.1" 136 | } 137 | }, 138 | "nbformat": 4, 139 | "nbformat_minor": 0 140 | } 141 | -------------------------------------------------------------------------------- /Statistics/Reliability ICC with statsample.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Reliability ICC analysis with statsample" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "application/javascript": [ 20 | "if(window['d3'] === undefined ||\n", 21 | " window['Nyaplot'] === undefined){\n", 22 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 23 | "\n", 24 | "\n", 25 | "\n", 26 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 27 | "\n", 28 | " require.config({paths: path, shim:shim});\n", 29 | "\n", 30 | "\n", 31 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 32 | "\n", 33 | "\tvar script = d3.select(\"head\")\n", 34 | "\t .append(\"script\")\n", 35 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 36 | "\t .attr(\"async\", true);\n", 37 | "\n", 38 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 39 | "\n", 40 | "\n", 41 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 42 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 43 | "\t window.dispatchEvent(event);\n", 44 | "\t console.log('Finished loading Nyaplotjs');\n", 45 | "\n", 46 | "\t};\n", 47 | "\n", 48 | "\n", 49 | "});});\n", 50 | "}\n" 51 | ], 52 | "text/plain": [ 53 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 54 | ] 55 | }, 56 | "metadata": {}, 57 | "output_type": "display_data" 58 | }, 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "Analysis 2016-03-26 10:21:47 +0000\n", 64 | "= Statsample::Reliability::ICC\n", 65 | " == Intra-class correlation\n", 66 | " McGraw & Wong ICC(A,K)\n", 67 | " ICC: 0.9712\n", 68 | " F Test : F(999.00, 534.39) = 37.1753 , p = 0.0000\n", 69 | " CI (95.00): [0.9665 - 0.9751]\n", 70 | " == Intra-class correlation\n", 71 | " McGraw & Wong ICC(A,K)\n", 72 | " ICC: 0.9712\n", 73 | " F Test : F(999.00, 534.39) = 37.1753 , p = 0.0000\n", 74 | " CI (95.00): [0.9665 - 0.9751]\n", 75 | " == Intra-class correlation\n", 76 | " McGraw & Wong ICC(A,K)\n", 77 | " ICC: 0.9712\n", 78 | " F Test : F(999.00, 534.39) = 37.1753 , p = 0.0000\n", 79 | " CI (95.00): [0.9665 - 0.9751]\n", 80 | "\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "require 'statsample'\n", 86 | "\n", 87 | "Statsample::Analysis.store(Statsample::Reliability::ICC) do\n", 88 | " Daru.lazy_update = true\n", 89 | " size=1000\n", 90 | " a = Daru::Vector.new_with_size(size) {rand(10)}\n", 91 | " \n", 92 | " # The `recode` method acts similarly to `map`, the difference\n", 93 | " # being that `recode` returns a Daru::Vector using the value\n", 94 | " # returned by each run of the block\n", 95 | " b = a.recode{|i|i+rand(4)-2}\n", 96 | " c = a.recode{|i|i+rand(4)-2}\n", 97 | " d = a.recode{|i|i+rand(4)-2}\n", 98 | " @ds = Daru::DataFrame.new({:a => a,:b => b,:c => c,:d => d})\n", 99 | " @icc=Statsample::Reliability::ICC.new(@ds)\n", 100 | " summary(@icc)\n", 101 | " @icc.type=:icc_3_1\n", 102 | " summary(@icc)\n", 103 | " @icc.type=:icc_a_k\n", 104 | " summary(@icc)\n", 105 | " \n", 106 | " Daru.lazy_update = false\n", 107 | "end\n", 108 | "\n", 109 | "Statsample::Analysis.run_batch" 110 | ] 111 | } 112 | ], 113 | "metadata": { 114 | "kernelspec": { 115 | "display_name": "Ruby 2.2.1", 116 | "language": "ruby", 117 | "name": "ruby" 118 | }, 119 | "language_info": { 120 | "file_extension": ".rb", 121 | "mimetype": "application/x-ruby", 122 | "name": "ruby", 123 | "version": "2.2.1" 124 | } 125 | }, 126 | "nbformat": 4, 127 | "nbformat_minor": 0 128 | } 129 | -------------------------------------------------------------------------------- /Statistics/Reliability Scale Analysis.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Reliability Scale analysis" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "application/javascript": [ 20 | "if(window['d3'] === undefined ||\n", 21 | " window['Nyaplot'] === undefined){\n", 22 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 23 | "\n", 24 | "\n", 25 | "\n", 26 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 27 | "\n", 28 | " require.config({paths: path, shim:shim});\n", 29 | "\n", 30 | "\n", 31 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 32 | "\n", 33 | "\tvar script = d3.select(\"head\")\n", 34 | "\t .append(\"script\")\n", 35 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 36 | "\t .attr(\"async\", true);\n", 37 | "\n", 38 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 39 | "\n", 40 | "\n", 41 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 42 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 43 | "\t window.dispatchEvent(event);\n", 44 | "\t console.log('Finished loading Nyaplotjs');\n", 45 | "\n", 46 | "\t};\n", 47 | "\n", 48 | "\n", 49 | "});});\n", 50 | "}\n" 51 | ], 52 | "text/plain": [ 53 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 54 | ] 55 | }, 56 | "metadata": {}, 57 | "output_type": "display_data" 58 | }, 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "Analysis 2016-03-26 10:22:36 +0000\n", 64 | "= Statsample::Reliability\n", 65 | " == Reliability Analysis\n", 66 | " Summary for Reliability Analysis\n", 67 | "+------------------------------+-------------------+\n", 68 | "| Valid Items | 20 |\n", 69 | "| Valid cases | 100 |\n", 70 | "| Sum mean | 1.0691 |\n", 71 | "| Sum sd | 19.4551 |\n", 72 | "| Sum median | 4.063620925282448 |\n", 73 | "+------------------------------+-------------------+\n", 74 | "| Item mean | 0.0535 |\n", 75 | "| Item sd | 0.9728 |\n", 76 | "+------------------------------+-------------------+\n", 77 | "| Skewness | -0.3883 |\n", 78 | "| Kurtosis | -0.2671 |\n", 79 | "+------------------------------+-------------------+\n", 80 | "| Cronbach's alpha | 0.9979 |\n", 81 | "| Standarized Cronbach's alpha | 0.9979 |\n", 82 | "| Mean rpb | 0.9788 |\n", 83 | "| Variances mean | 0.983765 |\n", 84 | "| Covariances mean | 0.944276 |\n", 85 | "+------------------------------+-------------------+\n", 86 | "\n", 87 | " Items for obtain alpha(0.8) : 0\n", 88 | " Items for obtain alpha(0.9) : 0\n", 89 | " Items report for Reliability Analysis\n", 90 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 91 | "| item | mean | sd | mean if deleted | var if deleted | sd if deleted | item-total correl. | alpha if deleted |\n", 92 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 93 | "| (v0) | 0.07570 | 1.02435 | 0.99337 | 340.44186 | 18.45107 | 0.97906 | 0.99780 |\n", 94 | "| (v1) | 0.04737 | 1.00360 | 1.02171 | 341.17282 | 18.47086 | 0.97966 | 0.99780 |\n", 95 | "| (v2) | 0.09366 | 0.96842 | 0.97541 | 342.58944 | 18.50917 | 0.97556 | 0.99782 |\n", 96 | "| (v3) | 0.05185 | 0.97455 | 1.01723 | 342.14884 | 18.49727 | 0.98194 | 0.99779 |\n", 97 | "| (v4) | 0.04570 | 0.99565 | 1.02337 | 341.51265 | 18.48006 | 0.97818 | 0.99781 |\n", 98 | "| (v5) | 0.06053 | 0.99252 | 1.00855 | 341.53079 | 18.48055 | 0.98092 | 0.99779 |\n", 99 | "| (v6) | 0.02392 | 0.98476 | 1.04516 | 341.71873 | 18.48564 | 0.98363 | 0.99777 |\n", 100 | "| (v7) | 0.02845 | 0.97928 | 1.04063 | 342.06366 | 18.49496 | 0.97941 | 0.99780 |\n", 101 | "| (v8) | 0.06049 | 1.00672 | 1.00859 | 340.94454 | 18.46468 | 0.98291 | 0.99778 |\n", 102 | "| (v9) | 0.04812 | 0.96548 | 1.02095 | 342.91228 | 18.51789 | 0.96919 | 0.99786 |\n", 103 | "| (v10) | 0.03464 | 1.02432 | 1.03444 | 340.43920 | 18.45099 | 0.97916 | 0.99780 |\n", 104 | "| (v11) | 0.03204 | 0.97632 | 1.03704 | 342.19005 | 18.49838 | 0.97886 | 0.99780 |\n", 105 | "| (v12) | 0.08685 | 1.01969 | 0.98223 | 340.52967 | 18.45345 | 0.98133 | 0.99779 |\n", 106 | "| (v13) | 0.05757 | 0.98015 | 1.01150 | 341.99716 | 18.49317 | 0.98042 | 0.99779 |\n", 107 | "| (v14) | 0.04636 | 0.99898 | 1.02272 | 341.48719 | 18.47937 | 0.97547 | 0.99782 |\n", 108 | "| (v15) | 0.05206 | 1.01824 | 1.01702 | 340.61883 | 18.45586 | 0.98030 | 0.99780 |\n", 109 | "| (v16) | 0.08545 | 0.98659 | 0.98362 | 341.94768 | 18.49183 | 0.97511 | 0.99782 |\n", 110 | "| (v17) | 0.02525 | 0.97607 | 1.04382 | 342.22581 | 18.49935 | 0.97808 | 0.99781 |\n", 111 | "| (v18) | 0.06765 | 0.95923 | 1.00142 | 342.77599 | 18.51421 | 0.97989 | 0.99780 |\n", 112 | "| (v19) | 0.04539 | 0.99829 | 1.02368 | 341.43479 | 18.47795 | 0.97767 | 0.99781 |\n", 113 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 114 | "\n", 115 | " -50.00 : ***\n", 116 | " -40.00 : *****\n", 117 | " -30.00 : *******\n", 118 | " -20.00 : ************\n", 119 | " -10.00 : ******************\n", 120 | " 0.00 : ********************\n", 121 | " 10.00 : *********************\n", 122 | " 20.00 : *********\n", 123 | " 30.00 : ****\n", 124 | " 40.00 : *\n", 125 | " == Multi Scale analyss\n", 126 | " === Reliability analysis of scales\n", 127 | " ==== Scale Scale 1\n", 128 | " Summary for Scale Scale 1\n", 129 | "+------------------------------+-------------------+\n", 130 | "| Valid Items | 10 |\n", 131 | "| Valid cases | 100 |\n", 132 | "| Sum mean | 0.4947 |\n", 133 | "| Sum sd | 9.7206 |\n", 134 | "| Sum median | 1.745914154683958 |\n", 135 | "+------------------------------+-------------------+\n", 136 | "| Item mean | 0.0495 |\n", 137 | "| Item sd | 0.9721 |\n", 138 | "+------------------------------+-------------------+\n", 139 | "| Skewness | -0.3931 |\n", 140 | "| Kurtosis | -0.2427 |\n", 141 | "+------------------------------+-------------------+\n", 142 | "| Cronbach's alpha | 0.9959 |\n", 143 | "| Standarized Cronbach's alpha | 0.9960 |\n", 144 | "| Mean rpb | 0.9782 |\n", 145 | "| Variances mean | 0.979484 |\n", 146 | "| Covariances mean | 0.941068 |\n", 147 | "+------------------------------+-------------------+\n", 148 | "\n", 149 | " Items for obtain alpha(0.8) : 0\n", 150 | " Items for obtain alpha(0.9) : 0\n", 151 | " Items report for Scale Scale 1\n", 152 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 153 | "| item | mean | sd | mean if deleted | var if deleted | sd if deleted | item-total correl. | alpha if deleted |\n", 154 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 155 | "| (v1) | 0.04737 | 1.00360 | 0.44736 | 76.33585 | 8.73704 | 0.97782 | 0.99549 |\n", 156 | "| (v10) | 0.09366 | 0.96842 | 0.40107 | 76.94542 | 8.77185 | 0.97752 | 0.99550 |\n", 157 | "| (v2) | 0.05185 | 0.97455 | 0.44289 | 76.77019 | 8.76186 | 0.98204 | 0.99538 |\n", 158 | "| (v3) | 0.04570 | 0.99565 | 0.44903 | 76.48009 | 8.74529 | 0.97732 | 0.99550 |\n", 159 | "| (v4) | 0.06053 | 0.99252 | 0.43421 | 76.47154 | 8.74480 | 0.98131 | 0.99540 |\n", 160 | "| (v5) | 0.02392 | 0.98476 | 0.47081 | 76.59234 | 8.75170 | 0.98214 | 0.99538 |\n", 161 | "| (v6) | 0.02845 | 0.97928 | 0.46629 | 76.74124 | 8.76021 | 0.97863 | 0.99547 |\n", 162 | "| (v7) | 0.06049 | 1.00672 | 0.43425 | 76.25645 | 8.73249 | 0.97945 | 0.99545 |\n", 163 | "| (v8) | 0.04812 | 0.96548 | 0.44661 | 77.16046 | 8.78410 | 0.96678 | 0.99578 |\n", 164 | "| (v9) | 0.03464 | 1.02432 | 0.46010 | 75.96873 | 8.71600 | 0.97855 | 0.99549 |\n", 165 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 166 | "\n", 167 | " -30.00 : *\n", 168 | " -24.00 : ***\n", 169 | " -18.00 : *******\n", 170 | " -12.00 : ***********\n", 171 | " -6.00 : ***********************\n", 172 | " 0.00 : **************************\n", 173 | " 6.00 : ********************\n", 174 | " 12.00 : ******\n", 175 | " 18.00 : ***\n", 176 | " 24.00 : \n", 177 | " ==== Scale Scale 2\n", 178 | " Summary for Scale Scale 2\n", 179 | "+------------------------------+--------------------+\n", 180 | "| Valid Items | 9 |\n", 181 | "| Valid cases | 100 |\n", 182 | "| Sum mean | 0.4986 |\n", 183 | "| Sum sd | 8.7550 |\n", 184 | "| Sum median | 1.5885071766348218 |\n", 185 | "+------------------------------+--------------------+\n", 186 | "| Item mean | 0.0554 |\n", 187 | "| Item sd | 0.9728 |\n", 188 | "+------------------------------+--------------------+\n", 189 | "| Skewness | -0.3662 |\n", 190 | "| Kurtosis | -0.3053 |\n", 191 | "+------------------------------+--------------------+\n", 192 | "| Cronbach's alpha | 0.9954 |\n", 193 | "| Standarized Cronbach's alpha | 0.9954 |\n", 194 | "| Mean rpb | 0.9774 |\n", 195 | "| Variances mean | 0.981242 |\n", 196 | "| Covariances mean | 0.941926 |\n", 197 | "+------------------------------+--------------------+\n", 198 | "\n", 199 | " Items for obtain alpha(0.8) : 0\n", 200 | " Items for obtain alpha(0.9) : 0\n", 201 | " Items report for Scale Scale 2\n", 202 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 203 | "| item | mean | sd | mean if deleted | var if deleted | sd if deleted | item-total correl. | alpha if deleted |\n", 204 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 205 | "| (v11) | 0.03204 | 0.97632 | 0.46660 | 60.84026 | 7.80002 | 0.97543 | 0.99487 |\n", 206 | "| (v12) | 0.08685 | 1.01969 | 0.41179 | 60.12024 | 7.75372 | 0.97958 | 0.99475 |\n", 207 | "| (v13) | 0.05757 | 0.98015 | 0.44107 | 60.71265 | 7.79183 | 0.98050 | 0.99470 |\n", 208 | "| (v14) | 0.04636 | 0.99898 | 0.45228 | 60.48544 | 7.77724 | 0.97605 | 0.99485 |\n", 209 | "| (v15) | 0.05206 | 1.01824 | 0.44658 | 60.15395 | 7.75590 | 0.97875 | 0.99477 |\n", 210 | "| (v16) | 0.08545 | 0.98659 | 0.41318 | 60.71287 | 7.79185 | 0.97326 | 0.99494 |\n", 211 | "| (v17) | 0.02525 | 0.97607 | 0.47338 | 60.81200 | 7.79820 | 0.97779 | 0.99479 |\n", 212 | "| (v18) | 0.06765 | 0.95923 | 0.43099 | 61.06099 | 7.81415 | 0.97850 | 0.99479 |\n", 213 | "| (v19) | 0.04539 | 0.99829 | 0.45325 | 60.48152 | 7.77699 | 0.97709 | 0.99481 |\n", 214 | "+-------+---------+---------+-----------------+----------------+---------------+---------------------+------------------+\n", 215 | "\n", 216 | " -30.00 : \n", 217 | " -25.00 : **\n", 218 | " -20.00 : ****\n", 219 | " -15.00 : ******\n", 220 | " -10.00 : *************\n", 221 | " -5.00 : ********************\n", 222 | " 0.00 : **********************\n", 223 | " 5.00 : ********************\n", 224 | " 10.00 : **********\n", 225 | " 15.00 : ***\n", 226 | "\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "require 'statsample'\n", 232 | "\n", 233 | "Statsample::Analysis.store(Statsample::Reliability) do \n", 234 | " Daru.lazy_update = true\n", 235 | " samples=100\n", 236 | " a=rnorm(samples)\n", 237 | " \n", 238 | " ds = Daru::DataFrame.new({})\n", 239 | " \n", 240 | " 20.times do |i|\n", 241 | " ds[\"v#{i}\".to_sym]= a + rnorm(samples,0,0.2)\n", 242 | " end \n", 243 | " ds.update\n", 244 | " \n", 245 | " rel=Statsample::Reliability::ScaleAnalysis.new(ds)\n", 246 | " summary rel\n", 247 | " \n", 248 | " ms=Statsample::Reliability::MultiScaleAnalysis.new(:name=>\"Multi Scale analyss\") do |m|\n", 249 | " m.scale \"Scale 1\", ds.clone([:v1, :v2, :v3, :v4, :v5, :v6, :v7, :v8, :v9, :v10])\n", 250 | " m.scale \"Scale 2\", ds.clone([:v11, :v12, :v13, :v14, :v15, :v16, :v17, :v18, :v19])\n", 251 | " end\n", 252 | " \n", 253 | " summary ms\n", 254 | " \n", 255 | " Daru.lazy_update = false\n", 256 | "end\n", 257 | "\n", 258 | "Statsample::Analysis.run_batch" 259 | ] 260 | } 261 | ], 262 | "metadata": { 263 | "kernelspec": { 264 | "display_name": "Ruby 2.2.1", 265 | "language": "ruby", 266 | "name": "ruby" 267 | }, 268 | "language_info": { 269 | "file_extension": ".rb", 270 | "mimetype": "application/x-ruby", 271 | "name": "ruby", 272 | "version": "2.2.1" 273 | } 274 | }, 275 | "nbformat": 4, 276 | "nbformat_minor": 0 277 | } 278 | -------------------------------------------------------------------------------- /Statistics/T Test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# T Test\n", 8 | "\n", 9 | "An illustratio of how a T Test can be performed and summarized with statsample.\n", 10 | "\n", 11 | "#### References\n", 12 | "\n", 13 | "http://en.wikipedia.org/wiki/Student%27s_t-test" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "application/javascript": [ 26 | "if(window['d3'] === undefined ||\n", 27 | " window['Nyaplot'] === undefined){\n", 28 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 29 | "\n", 30 | "\n", 31 | "\n", 32 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 33 | "\n", 34 | " require.config({paths: path, shim:shim});\n", 35 | "\n", 36 | "\n", 37 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 38 | "\n", 39 | "\tvar script = d3.select(\"head\")\n", 40 | "\t .append(\"script\")\n", 41 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 42 | "\t .attr(\"async\", true);\n", 43 | "\n", 44 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 45 | "\n", 46 | "\n", 47 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 48 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 49 | "\t window.dispatchEvent(event);\n", 50 | "\t console.log('Finished loading Nyaplotjs');\n", 51 | "\n", 52 | "\t};\n", 53 | "\n", 54 | "\n", 55 | "});});\n", 56 | "}\n" 57 | ], 58 | "text/plain": [ 59 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 60 | ] 61 | }, 62 | "metadata": {}, 63 | "output_type": "display_data" 64 | }, 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Analysis 2016-03-26 10:23:17 +0000\n", 70 | "= Statsample::Test::T\n", 71 | " == One Sample T Test\n", 72 | " Sample mean: -0.4698 | Sample sd: 1.1807 | se : 0.3734\n", 73 | " Population mean: 50.0000\n", 74 | " t(9) = -135.1683, p=0.0000 (both tails)\n", 75 | " CI(95%): -51.3144 - -49.6251\n", 76 | " == Two Sample T Test\n", 77 | " Mean and standard deviation\n", 78 | "+----------+---------+--------+----+\n", 79 | "| Variable | mean | sd | n |\n", 80 | "+----------+---------+--------+----+\n", 81 | "| | -0.4698 | 1.1807 | 10 |\n", 82 | "| | 1.8296 | 0.8102 | 10 |\n", 83 | "+----------+---------+--------+----+\n", 84 | "\n", 85 | " Levene test for equality of variances : F(1, 18) = 1.4575 , p = 0.2430\n", 86 | " T statistics\n", 87 | "+--------------------+---------+---------+----------------+\n", 88 | "| Type | t | df | p (both tails) |\n", 89 | "+--------------------+---------+---------+----------------+\n", 90 | "| Equal variance | -5.0777 | 18 | 0.0001 |\n", 91 | "| Non equal variance | -5.0777 | 15.9373 | 0.0001 |\n", 92 | "+--------------------+---------+---------+----------------+\n", 93 | "\n", 94 | " Effect size\n", 95 | "+-------+---------+\n", 96 | "| x1-x2 | -2.2994 |\n", 97 | "| d | -2.4293 |\n", 98 | "+-------+---------+\n", 99 | "\n", 100 | "\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "require 'statsample'\n", 106 | "\n", 107 | "Statsample::Analysis.store(Statsample::Test::T) do\n", 108 | " a = rnorm(10)\n", 109 | " t_1 = Statsample::Test.t_one_sample(a,{:u=>50})\n", 110 | " \n", 111 | " summary t_1\n", 112 | " \n", 113 | " b = rnorm(10,2)\n", 114 | " t_2 = Statsample::Test.t_two_samples_independent(a,b)\n", 115 | " \n", 116 | " summary t_2\n", 117 | "end\n", 118 | "\n", 119 | "Statsample::Analysis.run_batch" 120 | ] 121 | } 122 | ], 123 | "metadata": { 124 | "kernelspec": { 125 | "display_name": "Ruby 2.2.1", 126 | "language": "ruby", 127 | "name": "ruby" 128 | }, 129 | "language_info": { 130 | "file_extension": ".rb", 131 | "mimetype": "application/x-ruby", 132 | "name": "ruby", 133 | "version": "2.2.1" 134 | } 135 | }, 136 | "nbformat": 4, 137 | "nbformat_minor": 0 138 | } 139 | -------------------------------------------------------------------------------- /Statistics/Tetrachoric Correlation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Tetrachoric Correlation\n", 8 | "\n", 9 | "Tetrachoric correlation in statsample requires installation of the statsample-bivariate-extension gem. This gem extends the Statsample::Bivariate class with useful algorithms for polychoric and tetrachoric correlation.\n", 10 | "\n", 11 | "Statsample will automatically detect presence of polychoric/tetrachoric algorithms so there is no need to explicitly require the gem.\n", 12 | "\n", 13 | "In this example we'll see how tetrachoric correlation can be performed using statsampl" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 1, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [ 23 | { 24 | "data": { 25 | "application/javascript": [ 26 | "if(window['d3'] === undefined ||\n", 27 | " window['Nyaplot'] === undefined){\n", 28 | " var path = {\"d3\":\"http://d3js.org/d3.v3.min\",\"downloadable\":\"http://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 29 | "\n", 30 | "\n", 31 | "\n", 32 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 33 | "\n", 34 | " require.config({paths: path, shim:shim});\n", 35 | "\n", 36 | "\n", 37 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 38 | "\n", 39 | "\tvar script = d3.select(\"head\")\n", 40 | "\t .append(\"script\")\n", 41 | "\t .attr(\"src\", \"http://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 42 | "\t .attr(\"async\", true);\n", 43 | "\n", 44 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 45 | "\n", 46 | "\n", 47 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 48 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 49 | "\t window.dispatchEvent(event);\n", 50 | "\t console.log('Finished loading Nyaplotjs');\n", 51 | "\n", 52 | "\t};\n", 53 | "\n", 54 | "\n", 55 | "});});\n", 56 | "}\n" 57 | ], 58 | "text/plain": [ 59 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"http://d3js.org/d3.v3.min\\\",\\\"downloadable\\\":\\\"http://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"http://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 60 | ] 61 | }, 62 | "metadata": {}, 63 | "output_type": "display_data" 64 | }, 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "Analysis 2015-06-04 16:06:58 +0530\n", 70 | "= Statsample::Bivariate::Tetrachoric\n", 71 | " == Tetrachoric correlation\n", 72 | " Contingence Table\n", 73 | "+-----+-----+-----+-----+\n", 74 | "| | Y=0 | Y=1 | T |\n", 75 | "+-----+-----+-----+-----+\n", 76 | "| X=0 | 40 | 10 | 50 |\n", 77 | "| X=1 | 20 | 30 | 50 |\n", 78 | "+-----+-----+-----+-----+\n", 79 | "| T | 60 | 40 | 100 |\n", 80 | "+-----+-----+-----+-----+\n", 81 | "\n", 82 | " r: 0.607\n", 83 | " SE: 0.115\n", 84 | " Threshold X: 0.000 \n", 85 | " Threshold Y: 0.253 \n", 86 | "\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "require 'statsample'\n", 92 | "\n", 93 | "Statsample::Analysis.store(Statsample::Bivariate::Tetrachoric) do\n", 94 | " a = 40\n", 95 | " b = 10\n", 96 | " c = 20\n", 97 | " d = 30\n", 98 | " # The 'tetrachoric' shorthand calls the Statsample::Bivariate::Tetrachoric class internally\n", 99 | " summary tetrachoric(a,b,c,d)\n", 100 | "end\n", 101 | "\n", 102 | "Statsample::Analysis.run_batch" 103 | ] 104 | } 105 | ], 106 | "metadata": { 107 | "kernelspec": { 108 | "display_name": "Ruby 2.2.1", 109 | "language": "ruby", 110 | "name": "ruby" 111 | }, 112 | "language_info": { 113 | "file_extension": "rb", 114 | "mimetype": "application/x-ruby", 115 | "name": "ruby", 116 | "version": "2.2.1" 117 | } 118 | }, 119 | "nbformat": 4, 120 | "nbformat_minor": 0 121 | } 122 | -------------------------------------------------------------------------------- /Statistics/Velicer MAP test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Velicer MAP test" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "application/javascript": [ 20 | "if(window['d3'] === undefined ||\n", 21 | " window['Nyaplot'] === undefined){\n", 22 | " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", 23 | "\n", 24 | "\n", 25 | "\n", 26 | " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", 27 | "\n", 28 | " require.config({paths: path, shim:shim});\n", 29 | "\n", 30 | "\n", 31 | "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", 32 | "\n", 33 | "\tvar script = d3.select(\"head\")\n", 34 | "\t .append(\"script\")\n", 35 | "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", 36 | "\t .attr(\"async\", true);\n", 37 | "\n", 38 | "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", 39 | "\n", 40 | "\n", 41 | "\t var event = document.createEvent(\"HTMLEvents\");\n", 42 | "\t event.initEvent(\"load_nyaplot\",false,false);\n", 43 | "\t window.dispatchEvent(event);\n", 44 | "\t console.log('Finished loading Nyaplotjs');\n", 45 | "\n", 46 | "\t};\n", 47 | "\n", 48 | "\n", 49 | "});});\n", 50 | "}\n" 51 | ], 52 | "text/plain": [ 53 | "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" 54 | ] 55 | }, 56 | "metadata": {}, 57 | "output_type": "display_data" 58 | }, 59 | { 60 | "name": "stdout", 61 | "output_type": "stream", 62 | "text": [ 63 | "Analysis 2016-03-26 10:24:24 +0000\n", 64 | "= Statsample::Factor::MAP\n", 65 | " There are 2 real factors on data\n", 66 | " == Principal Component Analysis\n", 67 | " Number of factors: 1\n", 68 | " Communalities\n", 69 | "+----------+---------+------------+--------+\n", 70 | "| Variable | Initial | Extraction | % |\n", 71 | "+----------+---------+------------+--------+\n", 72 | "| v0 | 1.000 | 0.883 | 88.326 |\n", 73 | "| v1 | 1.000 | 0.875 | 87.503 |\n", 74 | "| v2 | 1.000 | 0.856 | 85.559 |\n", 75 | "| v3 | 1.000 | 0.886 | 88.600 |\n", 76 | "| v4 | 1.000 | 0.887 | 88.659 |\n", 77 | "| v5 | 1.000 | 0.819 | 81.855 |\n", 78 | "| v6 | 1.000 | 0.852 | 85.159 |\n", 79 | "| v7 | 1.000 | 0.822 | 82.226 |\n", 80 | "| v8 | 1.000 | 0.861 | 86.081 |\n", 81 | "| v9 | 1.000 | 0.872 | 87.165 |\n", 82 | "+----------+---------+------------+--------+\n", 83 | "\n", 84 | " Total Variance Explained\n", 85 | "+--------------+---------+---------+---------+\n", 86 | "| Component | E.Total | % | Cum. % |\n", 87 | "+--------------+---------+---------+---------+\n", 88 | "| Component 1 | 8.611 | 86.113% | 86.113 |\n", 89 | "| Component 2 | 0.926 | 9.263% | 95.377 |\n", 90 | "| Component 3 | 0.100 | 1.004% | 96.380 |\n", 91 | "| Component 4 | 0.089 | 0.894% | 97.274 |\n", 92 | "| Component 5 | 0.067 | 0.674% | 97.948 |\n", 93 | "| Component 6 | 0.060 | 0.597% | 98.545 |\n", 94 | "| Component 7 | 0.043 | 0.429% | 98.974 |\n", 95 | "| Component 8 | 0.040 | 0.399% | 99.373 |\n", 96 | "| Component 9 | 0.032 | 0.319% | 99.692 |\n", 97 | "| Component 10 | 0.031 | 0.308% | 100.000 |\n", 98 | "+--------------+---------+---------+---------+\n", 99 | "\n", 100 | " Component matrix\n", 101 | "+----+------+\n", 102 | "| | PC_1 |\n", 103 | "+----+------+\n", 104 | "| v0 | .940 |\n", 105 | "| v1 | .935 |\n", 106 | "| v2 | .925 |\n", 107 | "| v3 | .941 |\n", 108 | "| v4 | .942 |\n", 109 | "| v5 | .905 |\n", 110 | "| v6 | .923 |\n", 111 | "| v7 | .907 |\n", 112 | "| v8 | .928 |\n", 113 | "| v9 | .934 |\n", 114 | "+----+------+\n", 115 | "\n", 116 | " Traditional Kaiser criterion (k>1) returns 1 factors\n", 117 | " == Velicer's MAP\n", 118 | " Eigenvalues\n", 119 | "+----------+\n", 120 | "| Value |\n", 121 | "+----------+\n", 122 | "| 8.611330 |\n", 123 | "| 0.926336 |\n", 124 | "| 0.100374 |\n", 125 | "| 0.089363 |\n", 126 | "| 0.067434 |\n", 127 | "| 0.059687 |\n", 128 | "| 0.042917 |\n", 129 | "| 0.039857 |\n", 130 | "| 0.031882 |\n", 131 | "| 0.030820 |\n", 132 | "+----------+\n", 133 | "\n", 134 | " Velicer's Average Squared Correlations\n", 135 | "+----------------------+----------------------------+\n", 136 | "| number of components | average square correlation |\n", 137 | "+----------------------+----------------------------+\n", 138 | "| 0 | 0.722719 |\n", 139 | "| 1 | 0.407274 |\n", 140 | "| 2 | 0.036444 |\n", 141 | "| 3 | 0.060324 |\n", 142 | "| 4 | 0.106041 |\n", 143 | "| 5 | 0.188101 |\n", 144 | "| 6 | 0.225866 |\n", 145 | "| 7 | 0.310435 |\n", 146 | "| 8 | 0.538523 |\n", 147 | "| 9 | 1.000000 |\n", 148 | "+----------------------+----------------------------+\n", 149 | "\n", 150 | " The smallest average squared correlation is : 0.036444\n", 151 | " The number of components is : 2\n", 152 | " Velicer's MAP Test returns 2 factors to preserve\n", 153 | "\n" 154 | ] 155 | } 156 | ], 157 | "source": [ 158 | "require 'statsample'\n", 159 | "\n", 160 | "Statsample::Analysis.store(Statsample::Factor::MAP) do\n", 161 | " \n", 162 | " rng = Distribution::Normal.rng\n", 163 | " samples = 100\n", 164 | " variables = 10\n", 165 | " \n", 166 | " f1 = rnorm(samples)\n", 167 | " f2 = rnorm(samples)\n", 168 | " \n", 169 | " vectors={}\n", 170 | " \n", 171 | " variables.times do |i|\n", 172 | " vectors[\"v#{i}\".to_sym]= Daru::Vector.new(\n", 173 | " samples.times.collect do |nv| \n", 174 | " if i<5\n", 175 | " f1[nv]*5 + f2[nv] *2 +rng.call\n", 176 | " else\n", 177 | " f1[nv]*2 + f2[nv] *3 +rng.call\n", 178 | " end\n", 179 | " end)\n", 180 | " end\n", 181 | " \n", 182 | " \n", 183 | " ds = Daru::DataFrame.new(vectors)\n", 184 | " cor=cor(ds)\n", 185 | " pca=pca(cor)\n", 186 | " \n", 187 | " map=Statsample::Factor::MAP.new(cor)\n", 188 | " \n", 189 | " echo (\"There are 2 real factors on data\")\n", 190 | " summary(pca)\n", 191 | " echo(\"Traditional Kaiser criterion (k>1) returns #{pca.m} factors\")\n", 192 | " summary(map)\n", 193 | " echo(\"Velicer's MAP Test returns #{map.number_of_factors} factors to preserve\")\n", 194 | "end\n", 195 | " Statsample::Analysis.run_batch" 196 | ] 197 | } 198 | ], 199 | "metadata": { 200 | "kernelspec": { 201 | "display_name": "Ruby 2.2.1", 202 | "language": "ruby", 203 | "name": "ruby" 204 | }, 205 | "language_info": { 206 | "file_extension": ".rb", 207 | "mimetype": "application/x-ruby", 208 | "name": "ruby", 209 | "version": "2.2.1" 210 | } 211 | }, 212 | "nbformat": 4, 213 | "nbformat_minor": 0 214 | } 215 | -------------------------------------------------------------------------------- /Symbolic Mathematics/beginner.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Using the SymEngine gem\n", 8 | "---\n", 9 | "SymEngine is a module in the extensions, and the classes are a part of it. So first you fire up the interpreter and load the file\n", 10 | "\n", 11 | "***Note :*** You will need to have `libsymengine`(SymEngine C++ library) installed to be able to use the SymEngine extension with Ruby. `libsymengine` installation instructions and other prerequisite installation instructions can be found [here](https://github.com/symengine/symengine.rb#installation)." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "outputs": [ 21 | { 22 | "data": { 23 | "text/plain": [ 24 | "true" 25 | ] 26 | }, 27 | "execution_count": 1, 28 | "metadata": {}, 29 | "output_type": "execute_result" 30 | } 31 | ], 32 | "source": [ 33 | "require 'symengine'" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "Go ahead and try a function" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 2, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "\n", 55 | " _____ _____ _\n", 56 | "| __|_ _ _____| __|___ ___|_|___ ___\n", 57 | "|__ | | | | __| | . | | | -_|\n", 58 | "|_____|_ |_|_|_|_____|_|_|_ |_|_|_|___|\n", 59 | " |___| |___| \n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "SymEngine.ascii_art" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "or create a variable" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 3, 77 | "metadata": { 78 | "collapsed": false 79 | }, 80 | "outputs": [ 81 | { 82 | "data": { 83 | "text/plain": [ 84 | "#" 85 | ] 86 | }, 87 | "execution_count": 3, 88 | "metadata": {}, 89 | "output_type": "execute_result" 90 | } 91 | ], 92 | "source": [ 93 | "basic = SymEngine::Basic.new" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "This shows that we have successfully loaded the module." 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "## SymEngine::Symbol\n", 108 | "Just like there are variables like x, y, and z in a mathematical expression or equation, we have `SymEngine::Symbol` in SymEngine to represent them. To use a variable, first we need to make a `SymEngine::Symbol` object with the string we are going to represent the variable with." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 25, 114 | "metadata": { 115 | "collapsed": false 116 | }, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "x\n", 123 | "y\n", 124 | "z\n" 125 | ] 126 | } 127 | ], 128 | "source": [ 129 | "puts x = SymEngine::Symbol.new(\"x\")\n", 130 | "puts y = SymEngine::Symbol.new(\"y\")\n", 131 | "puts z = SymEngine::Symbol.new(\"z\")" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "Then we can construct expressions out of them" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 26, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "\"x**y*(x - y)/z\"" 152 | ] 153 | }, 154 | "execution_count": 26, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "e = (x-y)*(x**y/z)\n", 161 | "e.to_s" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "In SymEngine, every object is an instance of Basic or its subclasses. So, even an instance of `SymEngine::Symbol` is a Basic object." 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 5, 174 | "metadata": { 175 | "collapsed": false, 176 | "scrolled": true 177 | }, 178 | "outputs": [ 179 | { 180 | "data": { 181 | "text/plain": [ 182 | "SymEngine::Symbol" 183 | ] 184 | }, 185 | "execution_count": 5, 186 | "metadata": {}, 187 | "output_type": "execute_result" 188 | } 189 | ], 190 | "source": [ 191 | "x.class" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 6, 197 | "metadata": { 198 | "collapsed": false 199 | }, 200 | "outputs": [ 201 | { 202 | "data": { 203 | "text/plain": [ 204 | "true" 205 | ] 206 | }, 207 | "execution_count": 6, 208 | "metadata": {}, 209 | "output_type": "execute_result" 210 | } 211 | ], 212 | "source": [ 213 | "x.is_a? SymEngine::Basic" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "Now that we have an expression, we would like to see it's expanded form using `#expand`" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 27, 226 | "metadata": { 227 | "collapsed": false 228 | }, 229 | "outputs": [ 230 | { 231 | "data": { 232 | "text/plain": [ 233 | "\"x**(1 + y)/z - x**y*y/z\"" 234 | ] 235 | }, 236 | "execution_count": 27, 237 | "metadata": {}, 238 | "output_type": "execute_result" 239 | } 240 | ], 241 | "source": [ 242 | "f = e.expand()\n", 243 | "f.to_s" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "Or check if two expressions are same" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 8, 256 | "metadata": { 257 | "collapsed": false 258 | }, 259 | "outputs": [ 260 | { 261 | "data": { 262 | "text/plain": [ 263 | "true" 264 | ] 265 | }, 266 | "execution_count": 8, 267 | "metadata": {}, 268 | "output_type": "execute_result" 269 | } 270 | ], 271 | "source": [ 272 | "f == - (x**y*y/z) + (x**y*x/z)" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "But `e` and `f` are not equal since they are only mathematically equal, not structurally" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": 9, 285 | "metadata": { 286 | "collapsed": false 287 | }, 288 | "outputs": [ 289 | { 290 | "data": { 291 | "text/plain": [ 292 | "false" 293 | ] 294 | }, 295 | "execution_count": 9, 296 | "metadata": {}, 297 | "output_type": "execute_result" 298 | } 299 | ], 300 | "source": [ 301 | "e == f" 302 | ] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": {}, 307 | "source": [ 308 | "Let us suppose you want to know **what variables/symbols your expression has**. You can do that with the `#free_symbols` method. The method `#free_symbols` returns a `Set` of the symbols that are in an expression." 309 | ] 310 | }, 311 | { 312 | "cell_type": "code", 313 | "execution_count": 10, 314 | "metadata": { 315 | "collapsed": false 316 | }, 317 | "outputs": [ 318 | { 319 | "data": { 320 | "text/plain": [ 321 | "#, #, #}>" 322 | ] 323 | }, 324 | "execution_count": 10, 325 | "metadata": {}, 326 | "output_type": "execute_result" 327 | } 328 | ], 329 | "source": [ 330 | "f.free_symbols" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "Let us use `#map` method to see the elements of the `Set`. " 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 11, 343 | "metadata": { 344 | "collapsed": false, 345 | "scrolled": true 346 | }, 347 | "outputs": [ 348 | { 349 | "data": { 350 | "text/plain": [ 351 | "[\"x\", \"y\", \"z\"]" 352 | ] 353 | }, 354 | "execution_count": 11, 355 | "metadata": {}, 356 | "output_type": "execute_result" 357 | } 358 | ], 359 | "source": [ 360 | "f.free_symbols.map { |x| x.to_s }" 361 | ] 362 | }, 363 | { 364 | "cell_type": "markdown", 365 | "metadata": {}, 366 | "source": [ 367 | "`#args` returns the terms of the expression," 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 12, 373 | "metadata": { 374 | "collapsed": false 375 | }, 376 | "outputs": [ 377 | { 378 | "data": { 379 | "text/plain": [ 380 | "[\"-x**y*y/z\", \"x**(1 + y)/z\"]" 381 | ] 382 | }, 383 | "execution_count": 12, 384 | "metadata": {}, 385 | "output_type": "execute_result" 386 | } 387 | ], 388 | "source": [ 389 | "f.args.map { |x| x.to_s }" 390 | ] 391 | }, 392 | { 393 | "cell_type": "markdown", 394 | "metadata": {}, 395 | "source": [ 396 | "or if it is a single term it breaks down the elements" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 13, 402 | "metadata": { 403 | "collapsed": false 404 | }, 405 | "outputs": [ 406 | { 407 | "data": { 408 | "text/plain": [ 409 | "[\"-1\", \"x**y\", \"y\", \"z**(-1)\"]" 410 | ] 411 | }, 412 | "execution_count": 13, 413 | "metadata": {}, 414 | "output_type": "execute_result" 415 | } 416 | ], 417 | "source": [ 418 | "f.args[0].args.map { |k| k.to_s }" 419 | ] 420 | }, 421 | { 422 | "cell_type": "markdown", 423 | "metadata": {}, 424 | "source": [ 425 | "## SymEngine::Integer" 426 | ] 427 | }, 428 | { 429 | "cell_type": "markdown", 430 | "metadata": {}, 431 | "source": [ 432 | "You can make objects of class `SymEngine::Integer`. It's like regular `Integer` in ruby kernel, except it can do all the operations a `Basic` object can like arithmetic operations, etc." 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": 14, 438 | "metadata": { 439 | "collapsed": false 440 | }, 441 | "outputs": [ 442 | { 443 | "data": { 444 | "text/html": [ 445 | "1168422057627266461843148138873451659428421700563161428957815831003136" 446 | ], 447 | "text/plain": [ 448 | "#" 449 | ] 450 | }, 451 | "execution_count": 14, 452 | "metadata": {}, 453 | "output_type": "execute_result" 454 | } 455 | ], 456 | "source": [ 457 | "a = SymEngine::Integer.new(12)\n", 458 | "b = SymEngine::Integer.new(64)\n", 459 | "a**b" 460 | ] 461 | }, 462 | { 463 | "cell_type": "markdown", 464 | "metadata": {}, 465 | "source": [ 466 | "And yes it can support numbers of arbitrarily large length." 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 15, 472 | "metadata": { 473 | "collapsed": false 474 | }, 475 | "outputs": [ 476 | { 477 | "data": { 478 | "text/plain": [ 479 | "\"12**x\"" 480 | ] 481 | }, 482 | "execution_count": 15, 483 | "metadata": {}, 484 | "output_type": "execute_result" 485 | } 486 | ], 487 | "source": [ 488 | "(a**x).to_s" 489 | ] 490 | }, 491 | { 492 | "cell_type": "markdown", 493 | "metadata": {}, 494 | "source": [ 495 | "## SymEngine::Rational" 496 | ] 497 | }, 498 | { 499 | "cell_type": "markdown", 500 | "metadata": {}, 501 | "source": [ 502 | "You can also make objects of class `SymEngine::Rational` that is the SymEngine counterpart for `Rationals` in Ruby." 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 16, 508 | "metadata": { 509 | "collapsed": false 510 | }, 511 | "outputs": [ 512 | { 513 | "data": { 514 | "text/html": [ 515 | "2/3" 516 | ], 517 | "text/plain": [ 518 | "#" 519 | ] 520 | }, 521 | "execution_count": 16, 522 | "metadata": {}, 523 | "output_type": "execute_result" 524 | } 525 | ], 526 | "source": [ 527 | "c = Rational('2/3')\n", 528 | "d = SymEngine::Rational.new(c)" 529 | ] 530 | }, 531 | { 532 | "cell_type": "markdown", 533 | "metadata": {}, 534 | "source": [ 535 | "Like any other `Basic` object arithmetic operations can be done on this one too." 536 | ] 537 | }, 538 | { 539 | "cell_type": "code", 540 | "execution_count": 17, 541 | "metadata": { 542 | "collapsed": false 543 | }, 544 | "outputs": [ 545 | { 546 | "data": { 547 | "text/plain": [ 548 | "\"34/3\"" 549 | ] 550 | }, 551 | "execution_count": 17, 552 | "metadata": {}, 553 | "output_type": "execute_result" 554 | } 555 | ], 556 | "source": [ 557 | "(a-d).to_s" 558 | ] 559 | }, 560 | { 561 | "cell_type": "markdown", 562 | "metadata": {}, 563 | "source": [ 564 | "---\n", 565 | "You **need not create** an instance of `SymEngine::Integer` or `SymEngine::Rational`, every time you want to use them in an expression that uses many `Integer`s. Let us say you already have `Integer`/`Rational` object. Even then you can use them without having to create a new `SymEngine` object. " 566 | ] 567 | }, 568 | { 569 | "cell_type": "code", 570 | "execution_count": 18, 571 | "metadata": { 572 | "collapsed": false 573 | }, 574 | "outputs": [ 575 | { 576 | "data": { 577 | "text/plain": [ 578 | "\"(2/3 + x*y)*(2 + 1/(x*y) - x*y)\"" 579 | ] 580 | }, 581 | "execution_count": 18, 582 | "metadata": {}, 583 | "output_type": "execute_result" 584 | } 585 | ], 586 | "source": [ 587 | "k = (1 / (x * y) - x * y + 2) * (c + x * y) # c is a Rational object, not SymEngine::Rational\n", 588 | "k.to_s" 589 | ] 590 | }, 591 | { 592 | "cell_type": "markdown", 593 | "metadata": {}, 594 | "source": [ 595 | "As you can see, ruby kernel `Integer`s and `Rational`s interoperate seamlessly with the `SymEngine` objects." 596 | ] 597 | }, 598 | { 599 | "cell_type": "code", 600 | "execution_count": 19, 601 | "metadata": { 602 | "collapsed": false 603 | }, 604 | "outputs": [ 605 | { 606 | "data": { 607 | "text/plain": [ 608 | "\"7/3 + (2/3)*1/(x*y) + (4/3)*x*y - x**2*y**2\"" 609 | ] 610 | }, 611 | "execution_count": 19, 612 | "metadata": {}, 613 | "output_type": "execute_result" 614 | } 615 | ], 616 | "source": [ 617 | "k.expand.to_s" 618 | ] 619 | } 620 | ], 621 | "metadata": { 622 | "kernelspec": { 623 | "display_name": "Ruby 2.2.0", 624 | "language": "ruby", 625 | "name": "ruby" 626 | }, 627 | "language_info": { 628 | "file_extension": ".rb", 629 | "mimetype": "application/x-ruby", 630 | "name": "ruby", 631 | "version": "2.2.0" 632 | } 633 | }, 634 | "nbformat": 4, 635 | "nbformat_minor": 0 636 | } 637 | -------------------------------------------------------------------------------- /Symbolic Mathematics/differentiation_example.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Differentiation example\n", 8 | "This demonstrates solving the problem I found [here](http://math.stackexchange.com/questions/221197/a-tough-differential-calculus-problem), using the *SymEngine* gem.\n", 9 | "\n", 10 | "---" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "The function is described as \n", 18 | "$$f_p(x) = \\dfrac{9\\sqrt{x^2+p}}{x^2+2}$$\n", 19 | "We need to differentiate w.r.t. $x$" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "One solution is:\n", 27 | "$$f_p^2(x) (x^2 + 2)^2 = 81(x^2 + p) \\quad\\Longrightarrow$$\n", 28 | "\n", 29 | "$$2 f_p(x) f_p'(x)(x^2 + 2)^2 + 4x f_p^2(x^2 +2) = 162x \\quad\\Longrightarrow$$\n", 30 | "\n", 31 | "\\begin{align}f_p'(x) &= \\frac{162x - 4x f_p^2(x)(x^2+2)}{2f_p(x)(x^2+2)^2}\\\\ \\\\ &= \\frac{162x - 324x\\frac{x^2 + p}{x^2 +2}}{18(x^2 +2)\\sqrt{x^2+p}}\\\\ \\\\ &= \\frac{9 x (x^2+2) - 18x(x^2+p)}{(x^2 + 2)^2 \\sqrt{x^2 + p}}\\\\ \\\\ &= \\frac{9x(2-2p-x^2)}{(x^2 + 2)^2\\sqrt{x^2 + p}}\\end{align}" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": {}, 37 | "source": [ 38 | "We will attempt to solve this using features in *SymEngine*\n", 39 | "\n", 40 | "---" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 1, 46 | "metadata": { 47 | "collapsed": false 48 | }, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "true" 54 | ] 55 | }, 56 | "execution_count": 1, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "require 'symengine'" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "Declare the symbols" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 3, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "(1/2)" 83 | ] 84 | }, 85 | "execution_count": 3, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "x = SymEngine::Symbol.new('x')\n", 92 | "p = SymEngine::Symbol.new('p')\n", 93 | "half = Rational('1/2')" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Create the expression" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 5, 106 | "metadata": { 107 | "collapsed": false 108 | }, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "\"9*(p + x**2)**(1/2)/(2 + x**2)\"" 114 | ] 115 | }, 116 | "execution_count": 5, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "fp = 9*((x**2 + p)**half)/((x**2)+2)\n", 123 | "fp.to_s" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "Differentiate wrt $x$" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 8, 136 | "metadata": { 137 | "collapsed": false 138 | }, 139 | "outputs": [ 140 | { 141 | "data": { 142 | "text/plain": [ 143 | "\"-18*x*(p + x**2)**(1/2)/(2 + x**2)**2 + 9*x/((2 + x**2)*(p + x**2)**(1/2))\"" 144 | ] 145 | }, 146 | "execution_count": 8, 147 | "metadata": {}, 148 | "output_type": "execute_result" 149 | } 150 | ], 151 | "source": [ 152 | "answer = fp.diff(x)\n", 153 | "answer.to_s" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "Which is indeed correct! If you simplify the second last answer that we got from our solution. You will find the exact same answer. Using *SymEngine* for solving problems is as simple as that." 161 | ] 162 | } 163 | ], 164 | "metadata": { 165 | "kernelspec": { 166 | "display_name": "Ruby 2.2.0", 167 | "language": "ruby", 168 | "name": "ruby" 169 | }, 170 | "language_info": { 171 | "file_extension": ".rb", 172 | "mimetype": "application/x-ruby", 173 | "name": "ruby", 174 | "version": "2.2.0" 175 | } 176 | }, 177 | "nbformat": 4, 178 | "nbformat_minor": 0 179 | } 180 | --------------------------------------------------------------------------------