├── LICENSE ├── README.md ├── pom.xml ├── project.clj ├── resources └── datasets │ ├── boston_house_prices.csv │ ├── dataset_descriptions │ ├── boston_house_prices.rst │ └── iris.rst │ └── iris.csv ├── src ├── clj │ └── clojurewerkz │ │ └── statistiker │ │ ├── classification │ │ ├── k_nearest_neighbours.clj │ │ ├── naive_bayes.clj │ │ └── svm.clj │ │ ├── clustering │ │ ├── dbscan.clj │ │ └── kmeans.clj │ │ ├── correlation.clj │ │ ├── datasets.clj │ │ ├── distance.clj │ │ ├── distribution.clj │ │ ├── entropy.clj │ │ ├── fast_math.clj │ │ ├── fitting.clj │ │ ├── functions.clj │ │ ├── histograms.clj │ │ ├── inference.clj │ │ ├── metrics.clj │ │ ├── optimization.clj │ │ ├── regression.clj │ │ ├── scaling.clj │ │ ├── statistics.clj │ │ ├── time_series │ │ └── smoothing.clj │ │ ├── transform │ │ └── fft.clj │ │ └── utils.clj └── java │ └── clojurewerkz │ └── statistiker │ ├── DoublePointWithMeta.java │ └── libsvm │ ├── NuSVMSolver.java │ ├── SVM.java │ ├── Solver.java │ ├── data │ ├── KernelType.java │ ├── QMatrix.java │ ├── SvmModel.java │ ├── SvmNode.java │ ├── SvmParameter.java │ ├── SvmProblem.java │ └── SvmType.java │ └── kernel │ ├── Kernel.java │ ├── KernelCache.java │ ├── OneClassQ.java │ ├── SvcQ.java │ └── SvrQ.java └── test └── clj └── clojurewerkz └── statistiker ├── classification ├── k_nearest_neighbours_test.clj ├── naive_bayes_test.clj └── svm_test.clj ├── clustering ├── dbscan_test.clj └── kmeans_test.clj ├── correlation_test.clj ├── distribution_test.clj ├── entropy_test.clj ├── fitting_test.clj ├── metrics_test.clj ├── optimization_test.clj ├── scaling_test.clj ├── statistics_test.clj ├── time_series └── smoothing_test.clj └── transform └── fft_test.clj /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of Washington and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Statistiker 2 | 3 | Statistiker is a simple way to do Statistics in Clojure. 4 | The goal is to have an implementation of all popular algorithms 5 | for mining datasets. 6 | 7 | ## Project Goals 8 | 9 | * Provide a Clojure-friendly, easy way for statistical analysis, inference and machine learning 10 | * Be reasonably feature-complete (provide majority of things an advanced user may demand) 11 | * Be well maintained. 12 | * Be well documented. 13 | * Be well tested. 14 | 15 | 16 | ## Project Maturity 17 | 18 | Statistiker is a young and incomplete project. There may be major API 19 | changes (conforming to semantic versioning) inbetween releases. 20 | 21 | The Most Recent Release 22 | 23 | With Leiningen: 24 | 25 | ```clj 26 | [clojurewerkz/statistiker "0.1.0-SNAPSHOT"] 27 | ``` 28 | 29 | With Maven: 30 | 31 | ```xml 32 | 33 | clojurewerkz 34 | statistiker 35 | 0.1.0-SNAPSHOT 36 | 37 | ``` 38 | 39 | ## Documentation and Examples 40 | 41 | ### Descriptive statistics 42 | 43 | 44 | 45 | ## Correlation 46 | 47 | ## License 48 | 49 | Copyright © 2014 Alex Petrov & ClojureWerkz Team 50 | 51 | Double licensed under the Eclipse Public License (the same as Clojure) or the Apache Public License 2.0. 52 | 53 | ## Credits 54 | 55 | LibSVM sources belong to their respected owners, except for the changes our team made to the source code. 56 | 57 | The project repository contains several public datasets, added to provide best available examples for 58 | the users to start exploring the project with realistic data. The Data sets belong to their respective owners, 59 | specified in the description. The data sets are same as in Python's `scikit-learn`. 60 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | clojurewerkz 4 | statistiker 5 | jar 6 | 0.1.0-SNAPSHOT 7 | statistiker 8 | FIXME: write description 9 | http://example.com/FIXME 10 | 11 | 12 | Eclipse Public License 13 | http://www.eclipse.org/legal/epl-v10.html 14 | 15 | 16 | 17 | scm:git:git://github.com/clojurewerkz/statistiker.git 18 | scm:git:ssh://git@github.com/clojurewerkz/statistiker.git 19 | 77c9342c703d05d621d7a9cf57a17616ac685cd5 20 | 21 | https://github.com/clojurewerkz/statistiker 22 | 23 | 24 | src/clj 25 | test/clj 26 | 27 | 28 | resources 29 | 30 | 31 | 32 | 33 | dev-resources 34 | 35 | 36 | resources 37 | 38 | 39 | target 40 | target/classes 41 | 42 | 43 | org.codehaus.mojo 44 | build-helper-maven-plugin 45 | 1.7 46 | 47 | 48 | add-source 49 | generate-sources 50 | 51 | add-source 52 | 53 | 54 | 55 | src/java 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | central 66 | https://repo1.maven.org/maven2/ 67 | 68 | false 69 | 70 | 71 | true 72 | 73 | 74 | 75 | clojars 76 | https://clojars.org/repo/ 77 | 78 | true 79 | 80 | 81 | true 82 | 83 | 84 | 85 | 86 | 87 | org.clojure 88 | clojure 89 | 1.5.1 90 | 91 | 92 | org.clojure 93 | math.combinatorics 94 | 0.0.7 95 | 96 | 97 | org.apache.commons 98 | commons-math3 99 | 3.3 100 | 101 | 102 | net.mikera 103 | core.matrix 104 | 0.20.0 105 | 106 | 107 | prismatic 108 | schema 109 | 0.2.2 110 | 111 | 112 | org.clojure 113 | tools.nrepl 114 | 0.2.6 115 | 116 | 117 | org.clojure 118 | clojure 119 | 120 | 121 | test 122 | 123 | 124 | clojure-complete 125 | clojure-complete 126 | 0.2.3 127 | 128 | 129 | org.clojure 130 | clojure 131 | 132 | 133 | test 134 | 135 | 136 | 137 | 138 | 142 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clojurewerkz/statistiker "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | 4 | :url "http://example.com/FIXME" 5 | :license {:name "Eclipse Public License" 6 | :url "http://www.eclipse.org/legal/epl-v10.html"} 7 | :dependencies [[org.clojure/clojure "1.6.0"] 8 | [org.clojure/math.combinatorics "0.1.1"] 9 | [org.apache.commons/commons-math3 "3.4.1"] 10 | [net.mikera/core.matrix "0.33.2"] 11 | [prismatic/schema "0.2.2"]] 12 | :core.typed {:check [clojurewerkz.statistiker.optimization]} 13 | :source-paths ["src/clj"] 14 | :java-source-paths ["src/java"] 15 | :test-paths ["test/clj"]) 16 | -------------------------------------------------------------------------------- /resources/datasets/boston_house_prices.csv: -------------------------------------------------------------------------------- 1 | 506,13,,,,,,,,,,,, 2 | "CRIM","ZN","INDUS","CHAS","NOX","RM","AGE","DIS","RAD","TAX","PTRATIO","B","LSTAT","MEDV" 3 | 0.00632,18,2.31,0,0.538,6.575,65.2,4.09,1,296,15.3,396.9,4.98,24 4 | 0.02731,0,7.07,0,0.469,6.421,78.9,4.9671,2,242,17.8,396.9,9.14,21.6 5 | 0.02729,0,7.07,0,0.469,7.185,61.1,4.9671,2,242,17.8,392.83,4.03,34.7 6 | 0.03237,0,2.18,0,0.458,6.998,45.8,6.0622,3,222,18.7,394.63,2.94,33.4 7 | 0.06905,0,2.18,0,0.458,7.147,54.2,6.0622,3,222,18.7,396.9,5.33,36.2 8 | 0.02985,0,2.18,0,0.458,6.43,58.7,6.0622,3,222,18.7,394.12,5.21,28.7 9 | 0.08829,12.5,7.87,0,0.524,6.012,66.6,5.5605,5,311,15.2,395.6,12.43,22.9 10 | 0.14455,12.5,7.87,0,0.524,6.172,96.1,5.9505,5,311,15.2,396.9,19.15,27.1 11 | 0.21124,12.5,7.87,0,0.524,5.631,100,6.0821,5,311,15.2,386.63,29.93,16.5 12 | 0.17004,12.5,7.87,0,0.524,6.004,85.9,6.5921,5,311,15.2,386.71,17.1,18.9 13 | 0.22489,12.5,7.87,0,0.524,6.377,94.3,6.3467,5,311,15.2,392.52,20.45,15 14 | 0.11747,12.5,7.87,0,0.524,6.009,82.9,6.2267,5,311,15.2,396.9,13.27,18.9 15 | 0.09378,12.5,7.87,0,0.524,5.889,39,5.4509,5,311,15.2,390.5,15.71,21.7 16 | 0.62976,0,8.14,0,0.538,5.949,61.8,4.7075,4,307,21,396.9,8.26,20.4 17 | 0.63796,0,8.14,0,0.538,6.096,84.5,4.4619,4,307,21,380.02,10.26,18.2 18 | 0.62739,0,8.14,0,0.538,5.834,56.5,4.4986,4,307,21,395.62,8.47,19.9 19 | 1.05393,0,8.14,0,0.538,5.935,29.3,4.4986,4,307,21,386.85,6.58,23.1 20 | 0.7842,0,8.14,0,0.538,5.99,81.7,4.2579,4,307,21,386.75,14.67,17.5 21 | 0.80271,0,8.14,0,0.538,5.456,36.6,3.7965,4,307,21,288.99,11.69,20.2 22 | 0.7258,0,8.14,0,0.538,5.727,69.5,3.7965,4,307,21,390.95,11.28,18.2 23 | 1.25179,0,8.14,0,0.538,5.57,98.1,3.7979,4,307,21,376.57,21.02,13.6 24 | 0.85204,0,8.14,0,0.538,5.965,89.2,4.0123,4,307,21,392.53,13.83,19.6 25 | 1.23247,0,8.14,0,0.538,6.142,91.7,3.9769,4,307,21,396.9,18.72,15.2 26 | 0.98843,0,8.14,0,0.538,5.813,100,4.0952,4,307,21,394.54,19.88,14.5 27 | 0.75026,0,8.14,0,0.538,5.924,94.1,4.3996,4,307,21,394.33,16.3,15.6 28 | 0.84054,0,8.14,0,0.538,5.599,85.7,4.4546,4,307,21,303.42,16.51,13.9 29 | 0.67191,0,8.14,0,0.538,5.813,90.3,4.682,4,307,21,376.88,14.81,16.6 30 | 0.95577,0,8.14,0,0.538,6.047,88.8,4.4534,4,307,21,306.38,17.28,14.8 31 | 0.77299,0,8.14,0,0.538,6.495,94.4,4.4547,4,307,21,387.94,12.8,18.4 32 | 1.00245,0,8.14,0,0.538,6.674,87.3,4.239,4,307,21,380.23,11.98,21 33 | 1.13081,0,8.14,0,0.538,5.713,94.1,4.233,4,307,21,360.17,22.6,12.7 34 | 1.35472,0,8.14,0,0.538,6.072,100,4.175,4,307,21,376.73,13.04,14.5 35 | 1.38799,0,8.14,0,0.538,5.95,82,3.99,4,307,21,232.6,27.71,13.2 36 | 1.15172,0,8.14,0,0.538,5.701,95,3.7872,4,307,21,358.77,18.35,13.1 37 | 1.61282,0,8.14,0,0.538,6.096,96.9,3.7598,4,307,21,248.31,20.34,13.5 38 | 0.06417,0,5.96,0,0.499,5.933,68.2,3.3603,5,279,19.2,396.9,9.68,18.9 39 | 0.09744,0,5.96,0,0.499,5.841,61.4,3.3779,5,279,19.2,377.56,11.41,20 40 | 0.08014,0,5.96,0,0.499,5.85,41.5,3.9342,5,279,19.2,396.9,8.77,21 41 | 0.17505,0,5.96,0,0.499,5.966,30.2,3.8473,5,279,19.2,393.43,10.13,24.7 42 | 0.02763,75,2.95,0,0.428,6.595,21.8,5.4011,3,252,18.3,395.63,4.32,30.8 43 | 0.03359,75,2.95,0,0.428,7.024,15.8,5.4011,3,252,18.3,395.62,1.98,34.9 44 | 0.12744,0,6.91,0,0.448,6.77,2.9,5.7209,3,233,17.9,385.41,4.84,26.6 45 | 0.1415,0,6.91,0,0.448,6.169,6.6,5.7209,3,233,17.9,383.37,5.81,25.3 46 | 0.15936,0,6.91,0,0.448,6.211,6.5,5.7209,3,233,17.9,394.46,7.44,24.7 47 | 0.12269,0,6.91,0,0.448,6.069,40,5.7209,3,233,17.9,389.39,9.55,21.2 48 | 0.17142,0,6.91,0,0.448,5.682,33.8,5.1004,3,233,17.9,396.9,10.21,19.3 49 | 0.18836,0,6.91,0,0.448,5.786,33.3,5.1004,3,233,17.9,396.9,14.15,20 50 | 0.22927,0,6.91,0,0.448,6.03,85.5,5.6894,3,233,17.9,392.74,18.8,16.6 51 | 0.25387,0,6.91,0,0.448,5.399,95.3,5.87,3,233,17.9,396.9,30.81,14.4 52 | 0.21977,0,6.91,0,0.448,5.602,62,6.0877,3,233,17.9,396.9,16.2,19.4 53 | 0.08873,21,5.64,0,0.439,5.963,45.7,6.8147,4,243,16.8,395.56,13.45,19.7 54 | 0.04337,21,5.64,0,0.439,6.115,63,6.8147,4,243,16.8,393.97,9.43,20.5 55 | 0.0536,21,5.64,0,0.439,6.511,21.1,6.8147,4,243,16.8,396.9,5.28,25 56 | 0.04981,21,5.64,0,0.439,5.998,21.4,6.8147,4,243,16.8,396.9,8.43,23.4 57 | 0.0136,75,4,0,0.41,5.888,47.6,7.3197,3,469,21.1,396.9,14.8,18.9 58 | 0.01311,90,1.22,0,0.403,7.249,21.9,8.6966,5,226,17.9,395.93,4.81,35.4 59 | 0.02055,85,0.74,0,0.41,6.383,35.7,9.1876,2,313,17.3,396.9,5.77,24.7 60 | 0.01432,100,1.32,0,0.411,6.816,40.5,8.3248,5,256,15.1,392.9,3.95,31.6 61 | 0.15445,25,5.13,0,0.453,6.145,29.2,7.8148,8,284,19.7,390.68,6.86,23.3 62 | 0.10328,25,5.13,0,0.453,5.927,47.2,6.932,8,284,19.7,396.9,9.22,19.6 63 | 0.14932,25,5.13,0,0.453,5.741,66.2,7.2254,8,284,19.7,395.11,13.15,18.7 64 | 0.17171,25,5.13,0,0.453,5.966,93.4,6.8185,8,284,19.7,378.08,14.44,16 65 | 0.11027,25,5.13,0,0.453,6.456,67.8,7.2255,8,284,19.7,396.9,6.73,22.2 66 | 0.1265,25,5.13,0,0.453,6.762,43.4,7.9809,8,284,19.7,395.58,9.5,25 67 | 0.01951,17.5,1.38,0,0.4161,7.104,59.5,9.2229,3,216,18.6,393.24,8.05,33 68 | 0.03584,80,3.37,0,0.398,6.29,17.8,6.6115,4,337,16.1,396.9,4.67,23.5 69 | 0.04379,80,3.37,0,0.398,5.787,31.1,6.6115,4,337,16.1,396.9,10.24,19.4 70 | 0.05789,12.5,6.07,0,0.409,5.878,21.4,6.498,4,345,18.9,396.21,8.1,22 71 | 0.13554,12.5,6.07,0,0.409,5.594,36.8,6.498,4,345,18.9,396.9,13.09,17.4 72 | 0.12816,12.5,6.07,0,0.409,5.885,33,6.498,4,345,18.9,396.9,8.79,20.9 73 | 0.08826,0,10.81,0,0.413,6.417,6.6,5.2873,4,305,19.2,383.73,6.72,24.2 74 | 0.15876,0,10.81,0,0.413,5.961,17.5,5.2873,4,305,19.2,376.94,9.88,21.7 75 | 0.09164,0,10.81,0,0.413,6.065,7.8,5.2873,4,305,19.2,390.91,5.52,22.8 76 | 0.19539,0,10.81,0,0.413,6.245,6.2,5.2873,4,305,19.2,377.17,7.54,23.4 77 | 0.07896,0,12.83,0,0.437,6.273,6,4.2515,5,398,18.7,394.92,6.78,24.1 78 | 0.09512,0,12.83,0,0.437,6.286,45,4.5026,5,398,18.7,383.23,8.94,21.4 79 | 0.10153,0,12.83,0,0.437,6.279,74.5,4.0522,5,398,18.7,373.66,11.97,20 80 | 0.08707,0,12.83,0,0.437,6.14,45.8,4.0905,5,398,18.7,386.96,10.27,20.8 81 | 0.05646,0,12.83,0,0.437,6.232,53.7,5.0141,5,398,18.7,386.4,12.34,21.2 82 | 0.08387,0,12.83,0,0.437,5.874,36.6,4.5026,5,398,18.7,396.06,9.1,20.3 83 | 0.04113,25,4.86,0,0.426,6.727,33.5,5.4007,4,281,19,396.9,5.29,28 84 | 0.04462,25,4.86,0,0.426,6.619,70.4,5.4007,4,281,19,395.63,7.22,23.9 85 | 0.03659,25,4.86,0,0.426,6.302,32.2,5.4007,4,281,19,396.9,6.72,24.8 86 | 0.03551,25,4.86,0,0.426,6.167,46.7,5.4007,4,281,19,390.64,7.51,22.9 87 | 0.05059,0,4.49,0,0.449,6.389,48,4.7794,3,247,18.5,396.9,9.62,23.9 88 | 0.05735,0,4.49,0,0.449,6.63,56.1,4.4377,3,247,18.5,392.3,6.53,26.6 89 | 0.05188,0,4.49,0,0.449,6.015,45.1,4.4272,3,247,18.5,395.99,12.86,22.5 90 | 0.07151,0,4.49,0,0.449,6.121,56.8,3.7476,3,247,18.5,395.15,8.44,22.2 91 | 0.0566,0,3.41,0,0.489,7.007,86.3,3.4217,2,270,17.8,396.9,5.5,23.6 92 | 0.05302,0,3.41,0,0.489,7.079,63.1,3.4145,2,270,17.8,396.06,5.7,28.7 93 | 0.04684,0,3.41,0,0.489,6.417,66.1,3.0923,2,270,17.8,392.18,8.81,22.6 94 | 0.03932,0,3.41,0,0.489,6.405,73.9,3.0921,2,270,17.8,393.55,8.2,22 95 | 0.04203,28,15.04,0,0.464,6.442,53.6,3.6659,4,270,18.2,395.01,8.16,22.9 96 | 0.02875,28,15.04,0,0.464,6.211,28.9,3.6659,4,270,18.2,396.33,6.21,25 97 | 0.04294,28,15.04,0,0.464,6.249,77.3,3.615,4,270,18.2,396.9,10.59,20.6 98 | 0.12204,0,2.89,0,0.445,6.625,57.8,3.4952,2,276,18,357.98,6.65,28.4 99 | 0.11504,0,2.89,0,0.445,6.163,69.6,3.4952,2,276,18,391.83,11.34,21.4 100 | 0.12083,0,2.89,0,0.445,8.069,76,3.4952,2,276,18,396.9,4.21,38.7 101 | 0.08187,0,2.89,0,0.445,7.82,36.9,3.4952,2,276,18,393.53,3.57,43.8 102 | 0.0686,0,2.89,0,0.445,7.416,62.5,3.4952,2,276,18,396.9,6.19,33.2 103 | 0.14866,0,8.56,0,0.52,6.727,79.9,2.7778,5,384,20.9,394.76,9.42,27.5 104 | 0.11432,0,8.56,0,0.52,6.781,71.3,2.8561,5,384,20.9,395.58,7.67,26.5 105 | 0.22876,0,8.56,0,0.52,6.405,85.4,2.7147,5,384,20.9,70.8,10.63,18.6 106 | 0.21161,0,8.56,0,0.52,6.137,87.4,2.7147,5,384,20.9,394.47,13.44,19.3 107 | 0.1396,0,8.56,0,0.52,6.167,90,2.421,5,384,20.9,392.69,12.33,20.1 108 | 0.13262,0,8.56,0,0.52,5.851,96.7,2.1069,5,384,20.9,394.05,16.47,19.5 109 | 0.1712,0,8.56,0,0.52,5.836,91.9,2.211,5,384,20.9,395.67,18.66,19.5 110 | 0.13117,0,8.56,0,0.52,6.127,85.2,2.1224,5,384,20.9,387.69,14.09,20.4 111 | 0.12802,0,8.56,0,0.52,6.474,97.1,2.4329,5,384,20.9,395.24,12.27,19.8 112 | 0.26363,0,8.56,0,0.52,6.229,91.2,2.5451,5,384,20.9,391.23,15.55,19.4 113 | 0.10793,0,8.56,0,0.52,6.195,54.4,2.7778,5,384,20.9,393.49,13,21.7 114 | 0.10084,0,10.01,0,0.547,6.715,81.6,2.6775,6,432,17.8,395.59,10.16,22.8 115 | 0.12329,0,10.01,0,0.547,5.913,92.9,2.3534,6,432,17.8,394.95,16.21,18.8 116 | 0.22212,0,10.01,0,0.547,6.092,95.4,2.548,6,432,17.8,396.9,17.09,18.7 117 | 0.14231,0,10.01,0,0.547,6.254,84.2,2.2565,6,432,17.8,388.74,10.45,18.5 118 | 0.17134,0,10.01,0,0.547,5.928,88.2,2.4631,6,432,17.8,344.91,15.76,18.3 119 | 0.13158,0,10.01,0,0.547,6.176,72.5,2.7301,6,432,17.8,393.3,12.04,21.2 120 | 0.15098,0,10.01,0,0.547,6.021,82.6,2.7474,6,432,17.8,394.51,10.3,19.2 121 | 0.13058,0,10.01,0,0.547,5.872,73.1,2.4775,6,432,17.8,338.63,15.37,20.4 122 | 0.14476,0,10.01,0,0.547,5.731,65.2,2.7592,6,432,17.8,391.5,13.61,19.3 123 | 0.06899,0,25.65,0,0.581,5.87,69.7,2.2577,2,188,19.1,389.15,14.37,22 124 | 0.07165,0,25.65,0,0.581,6.004,84.1,2.1974,2,188,19.1,377.67,14.27,20.3 125 | 0.09299,0,25.65,0,0.581,5.961,92.9,2.0869,2,188,19.1,378.09,17.93,20.5 126 | 0.15038,0,25.65,0,0.581,5.856,97,1.9444,2,188,19.1,370.31,25.41,17.3 127 | 0.09849,0,25.65,0,0.581,5.879,95.8,2.0063,2,188,19.1,379.38,17.58,18.8 128 | 0.16902,0,25.65,0,0.581,5.986,88.4,1.9929,2,188,19.1,385.02,14.81,21.4 129 | 0.38735,0,25.65,0,0.581,5.613,95.6,1.7572,2,188,19.1,359.29,27.26,15.7 130 | 0.25915,0,21.89,0,0.624,5.693,96,1.7883,4,437,21.2,392.11,17.19,16.2 131 | 0.32543,0,21.89,0,0.624,6.431,98.8,1.8125,4,437,21.2,396.9,15.39,18 132 | 0.88125,0,21.89,0,0.624,5.637,94.7,1.9799,4,437,21.2,396.9,18.34,14.3 133 | 0.34006,0,21.89,0,0.624,6.458,98.9,2.1185,4,437,21.2,395.04,12.6,19.2 134 | 1.19294,0,21.89,0,0.624,6.326,97.7,2.271,4,437,21.2,396.9,12.26,19.6 135 | 0.59005,0,21.89,0,0.624,6.372,97.9,2.3274,4,437,21.2,385.76,11.12,23 136 | 0.32982,0,21.89,0,0.624,5.822,95.4,2.4699,4,437,21.2,388.69,15.03,18.4 137 | 0.97617,0,21.89,0,0.624,5.757,98.4,2.346,4,437,21.2,262.76,17.31,15.6 138 | 0.55778,0,21.89,0,0.624,6.335,98.2,2.1107,4,437,21.2,394.67,16.96,18.1 139 | 0.32264,0,21.89,0,0.624,5.942,93.5,1.9669,4,437,21.2,378.25,16.9,17.4 140 | 0.35233,0,21.89,0,0.624,6.454,98.4,1.8498,4,437,21.2,394.08,14.59,17.1 141 | 0.2498,0,21.89,0,0.624,5.857,98.2,1.6686,4,437,21.2,392.04,21.32,13.3 142 | 0.54452,0,21.89,0,0.624,6.151,97.9,1.6687,4,437,21.2,396.9,18.46,17.8 143 | 0.2909,0,21.89,0,0.624,6.174,93.6,1.6119,4,437,21.2,388.08,24.16,14 144 | 1.62864,0,21.89,0,0.624,5.019,100,1.4394,4,437,21.2,396.9,34.41,14.4 145 | 3.32105,0,19.58,1,0.871,5.403,100,1.3216,5,403,14.7,396.9,26.82,13.4 146 | 4.0974,0,19.58,0,0.871,5.468,100,1.4118,5,403,14.7,396.9,26.42,15.6 147 | 2.77974,0,19.58,0,0.871,4.903,97.8,1.3459,5,403,14.7,396.9,29.29,11.8 148 | 2.37934,0,19.58,0,0.871,6.13,100,1.4191,5,403,14.7,172.91,27.8,13.8 149 | 2.15505,0,19.58,0,0.871,5.628,100,1.5166,5,403,14.7,169.27,16.65,15.6 150 | 2.36862,0,19.58,0,0.871,4.926,95.7,1.4608,5,403,14.7,391.71,29.53,14.6 151 | 2.33099,0,19.58,0,0.871,5.186,93.8,1.5296,5,403,14.7,356.99,28.32,17.8 152 | 2.73397,0,19.58,0,0.871,5.597,94.9,1.5257,5,403,14.7,351.85,21.45,15.4 153 | 1.6566,0,19.58,0,0.871,6.122,97.3,1.618,5,403,14.7,372.8,14.1,21.5 154 | 1.49632,0,19.58,0,0.871,5.404,100,1.5916,5,403,14.7,341.6,13.28,19.6 155 | 1.12658,0,19.58,1,0.871,5.012,88,1.6102,5,403,14.7,343.28,12.12,15.3 156 | 2.14918,0,19.58,0,0.871,5.709,98.5,1.6232,5,403,14.7,261.95,15.79,19.4 157 | 1.41385,0,19.58,1,0.871,6.129,96,1.7494,5,403,14.7,321.02,15.12,17 158 | 3.53501,0,19.58,1,0.871,6.152,82.6,1.7455,5,403,14.7,88.01,15.02,15.6 159 | 2.44668,0,19.58,0,0.871,5.272,94,1.7364,5,403,14.7,88.63,16.14,13.1 160 | 1.22358,0,19.58,0,0.605,6.943,97.4,1.8773,5,403,14.7,363.43,4.59,41.3 161 | 1.34284,0,19.58,0,0.605,6.066,100,1.7573,5,403,14.7,353.89,6.43,24.3 162 | 1.42502,0,19.58,0,0.871,6.51,100,1.7659,5,403,14.7,364.31,7.39,23.3 163 | 1.27346,0,19.58,1,0.605,6.25,92.6,1.7984,5,403,14.7,338.92,5.5,27 164 | 1.46336,0,19.58,0,0.605,7.489,90.8,1.9709,5,403,14.7,374.43,1.73,50 165 | 1.83377,0,19.58,1,0.605,7.802,98.2,2.0407,5,403,14.7,389.61,1.92,50 166 | 1.51902,0,19.58,1,0.605,8.375,93.9,2.162,5,403,14.7,388.45,3.32,50 167 | 2.24236,0,19.58,0,0.605,5.854,91.8,2.422,5,403,14.7,395.11,11.64,22.7 168 | 2.924,0,19.58,0,0.605,6.101,93,2.2834,5,403,14.7,240.16,9.81,25 169 | 2.01019,0,19.58,0,0.605,7.929,96.2,2.0459,5,403,14.7,369.3,3.7,50 170 | 1.80028,0,19.58,0,0.605,5.877,79.2,2.4259,5,403,14.7,227.61,12.14,23.8 171 | 2.3004,0,19.58,0,0.605,6.319,96.1,2.1,5,403,14.7,297.09,11.1,23.8 172 | 2.44953,0,19.58,0,0.605,6.402,95.2,2.2625,5,403,14.7,330.04,11.32,22.3 173 | 1.20742,0,19.58,0,0.605,5.875,94.6,2.4259,5,403,14.7,292.29,14.43,17.4 174 | 2.3139,0,19.58,0,0.605,5.88,97.3,2.3887,5,403,14.7,348.13,12.03,19.1 175 | 0.13914,0,4.05,0,0.51,5.572,88.5,2.5961,5,296,16.6,396.9,14.69,23.1 176 | 0.09178,0,4.05,0,0.51,6.416,84.1,2.6463,5,296,16.6,395.5,9.04,23.6 177 | 0.08447,0,4.05,0,0.51,5.859,68.7,2.7019,5,296,16.6,393.23,9.64,22.6 178 | 0.06664,0,4.05,0,0.51,6.546,33.1,3.1323,5,296,16.6,390.96,5.33,29.4 179 | 0.07022,0,4.05,0,0.51,6.02,47.2,3.5549,5,296,16.6,393.23,10.11,23.2 180 | 0.05425,0,4.05,0,0.51,6.315,73.4,3.3175,5,296,16.6,395.6,6.29,24.6 181 | 0.06642,0,4.05,0,0.51,6.86,74.4,2.9153,5,296,16.6,391.27,6.92,29.9 182 | 0.0578,0,2.46,0,0.488,6.98,58.4,2.829,3,193,17.8,396.9,5.04,37.2 183 | 0.06588,0,2.46,0,0.488,7.765,83.3,2.741,3,193,17.8,395.56,7.56,39.8 184 | 0.06888,0,2.46,0,0.488,6.144,62.2,2.5979,3,193,17.8,396.9,9.45,36.2 185 | 0.09103,0,2.46,0,0.488,7.155,92.2,2.7006,3,193,17.8,394.12,4.82,37.9 186 | 0.10008,0,2.46,0,0.488,6.563,95.6,2.847,3,193,17.8,396.9,5.68,32.5 187 | 0.08308,0,2.46,0,0.488,5.604,89.8,2.9879,3,193,17.8,391,13.98,26.4 188 | 0.06047,0,2.46,0,0.488,6.153,68.8,3.2797,3,193,17.8,387.11,13.15,29.6 189 | 0.05602,0,2.46,0,0.488,7.831,53.6,3.1992,3,193,17.8,392.63,4.45,50 190 | 0.07875,45,3.44,0,0.437,6.782,41.1,3.7886,5,398,15.2,393.87,6.68,32 191 | 0.12579,45,3.44,0,0.437,6.556,29.1,4.5667,5,398,15.2,382.84,4.56,29.8 192 | 0.0837,45,3.44,0,0.437,7.185,38.9,4.5667,5,398,15.2,396.9,5.39,34.9 193 | 0.09068,45,3.44,0,0.437,6.951,21.5,6.4798,5,398,15.2,377.68,5.1,37 194 | 0.06911,45,3.44,0,0.437,6.739,30.8,6.4798,5,398,15.2,389.71,4.69,30.5 195 | 0.08664,45,3.44,0,0.437,7.178,26.3,6.4798,5,398,15.2,390.49,2.87,36.4 196 | 0.02187,60,2.93,0,0.401,6.8,9.9,6.2196,1,265,15.6,393.37,5.03,31.1 197 | 0.01439,60,2.93,0,0.401,6.604,18.8,6.2196,1,265,15.6,376.7,4.38,29.1 198 | 0.01381,80,0.46,0,0.422,7.875,32,5.6484,4,255,14.4,394.23,2.97,50 199 | 0.04011,80,1.52,0,0.404,7.287,34.1,7.309,2,329,12.6,396.9,4.08,33.3 200 | 0.04666,80,1.52,0,0.404,7.107,36.6,7.309,2,329,12.6,354.31,8.61,30.3 201 | 0.03768,80,1.52,0,0.404,7.274,38.3,7.309,2,329,12.6,392.2,6.62,34.6 202 | 0.0315,95,1.47,0,0.403,6.975,15.3,7.6534,3,402,17,396.9,4.56,34.9 203 | 0.01778,95,1.47,0,0.403,7.135,13.9,7.6534,3,402,17,384.3,4.45,32.9 204 | 0.03445,82.5,2.03,0,0.415,6.162,38.4,6.27,2,348,14.7,393.77,7.43,24.1 205 | 0.02177,82.5,2.03,0,0.415,7.61,15.7,6.27,2,348,14.7,395.38,3.11,42.3 206 | 0.0351,95,2.68,0,0.4161,7.853,33.2,5.118,4,224,14.7,392.78,3.81,48.5 207 | 0.02009,95,2.68,0,0.4161,8.034,31.9,5.118,4,224,14.7,390.55,2.88,50 208 | 0.13642,0,10.59,0,0.489,5.891,22.3,3.9454,4,277,18.6,396.9,10.87,22.6 209 | 0.22969,0,10.59,0,0.489,6.326,52.5,4.3549,4,277,18.6,394.87,10.97,24.4 210 | 0.25199,0,10.59,0,0.489,5.783,72.7,4.3549,4,277,18.6,389.43,18.06,22.5 211 | 0.13587,0,10.59,1,0.489,6.064,59.1,4.2392,4,277,18.6,381.32,14.66,24.4 212 | 0.43571,0,10.59,1,0.489,5.344,100,3.875,4,277,18.6,396.9,23.09,20 213 | 0.17446,0,10.59,1,0.489,5.96,92.1,3.8771,4,277,18.6,393.25,17.27,21.7 214 | 0.37578,0,10.59,1,0.489,5.404,88.6,3.665,4,277,18.6,395.24,23.98,19.3 215 | 0.21719,0,10.59,1,0.489,5.807,53.8,3.6526,4,277,18.6,390.94,16.03,22.4 216 | 0.14052,0,10.59,0,0.489,6.375,32.3,3.9454,4,277,18.6,385.81,9.38,28.1 217 | 0.28955,0,10.59,0,0.489,5.412,9.8,3.5875,4,277,18.6,348.93,29.55,23.7 218 | 0.19802,0,10.59,0,0.489,6.182,42.4,3.9454,4,277,18.6,393.63,9.47,25 219 | 0.0456,0,13.89,1,0.55,5.888,56,3.1121,5,276,16.4,392.8,13.51,23.3 220 | 0.07013,0,13.89,0,0.55,6.642,85.1,3.4211,5,276,16.4,392.78,9.69,28.7 221 | 0.11069,0,13.89,1,0.55,5.951,93.8,2.8893,5,276,16.4,396.9,17.92,21.5 222 | 0.11425,0,13.89,1,0.55,6.373,92.4,3.3633,5,276,16.4,393.74,10.5,23 223 | 0.35809,0,6.2,1,0.507,6.951,88.5,2.8617,8,307,17.4,391.7,9.71,26.7 224 | 0.40771,0,6.2,1,0.507,6.164,91.3,3.048,8,307,17.4,395.24,21.46,21.7 225 | 0.62356,0,6.2,1,0.507,6.879,77.7,3.2721,8,307,17.4,390.39,9.93,27.5 226 | 0.6147,0,6.2,0,0.507,6.618,80.8,3.2721,8,307,17.4,396.9,7.6,30.1 227 | 0.31533,0,6.2,0,0.504,8.266,78.3,2.8944,8,307,17.4,385.05,4.14,44.8 228 | 0.52693,0,6.2,0,0.504,8.725,83,2.8944,8,307,17.4,382,4.63,50 229 | 0.38214,0,6.2,0,0.504,8.04,86.5,3.2157,8,307,17.4,387.38,3.13,37.6 230 | 0.41238,0,6.2,0,0.504,7.163,79.9,3.2157,8,307,17.4,372.08,6.36,31.6 231 | 0.29819,0,6.2,0,0.504,7.686,17,3.3751,8,307,17.4,377.51,3.92,46.7 232 | 0.44178,0,6.2,0,0.504,6.552,21.4,3.3751,8,307,17.4,380.34,3.76,31.5 233 | 0.537,0,6.2,0,0.504,5.981,68.1,3.6715,8,307,17.4,378.35,11.65,24.3 234 | 0.46296,0,6.2,0,0.504,7.412,76.9,3.6715,8,307,17.4,376.14,5.25,31.7 235 | 0.57529,0,6.2,0,0.507,8.337,73.3,3.8384,8,307,17.4,385.91,2.47,41.7 236 | 0.33147,0,6.2,0,0.507,8.247,70.4,3.6519,8,307,17.4,378.95,3.95,48.3 237 | 0.44791,0,6.2,1,0.507,6.726,66.5,3.6519,8,307,17.4,360.2,8.05,29 238 | 0.33045,0,6.2,0,0.507,6.086,61.5,3.6519,8,307,17.4,376.75,10.88,24 239 | 0.52058,0,6.2,1,0.507,6.631,76.5,4.148,8,307,17.4,388.45,9.54,25.1 240 | 0.51183,0,6.2,0,0.507,7.358,71.6,4.148,8,307,17.4,390.07,4.73,31.5 241 | 0.08244,30,4.93,0,0.428,6.481,18.5,6.1899,6,300,16.6,379.41,6.36,23.7 242 | 0.09252,30,4.93,0,0.428,6.606,42.2,6.1899,6,300,16.6,383.78,7.37,23.3 243 | 0.11329,30,4.93,0,0.428,6.897,54.3,6.3361,6,300,16.6,391.25,11.38,22 244 | 0.10612,30,4.93,0,0.428,6.095,65.1,6.3361,6,300,16.6,394.62,12.4,20.1 245 | 0.1029,30,4.93,0,0.428,6.358,52.9,7.0355,6,300,16.6,372.75,11.22,22.2 246 | 0.12757,30,4.93,0,0.428,6.393,7.8,7.0355,6,300,16.6,374.71,5.19,23.7 247 | 0.20608,22,5.86,0,0.431,5.593,76.5,7.9549,7,330,19.1,372.49,12.5,17.6 248 | 0.19133,22,5.86,0,0.431,5.605,70.2,7.9549,7,330,19.1,389.13,18.46,18.5 249 | 0.33983,22,5.86,0,0.431,6.108,34.9,8.0555,7,330,19.1,390.18,9.16,24.3 250 | 0.19657,22,5.86,0,0.431,6.226,79.2,8.0555,7,330,19.1,376.14,10.15,20.5 251 | 0.16439,22,5.86,0,0.431,6.433,49.1,7.8265,7,330,19.1,374.71,9.52,24.5 252 | 0.19073,22,5.86,0,0.431,6.718,17.5,7.8265,7,330,19.1,393.74,6.56,26.2 253 | 0.1403,22,5.86,0,0.431,6.487,13,7.3967,7,330,19.1,396.28,5.9,24.4 254 | 0.21409,22,5.86,0,0.431,6.438,8.9,7.3967,7,330,19.1,377.07,3.59,24.8 255 | 0.08221,22,5.86,0,0.431,6.957,6.8,8.9067,7,330,19.1,386.09,3.53,29.6 256 | 0.36894,22,5.86,0,0.431,8.259,8.4,8.9067,7,330,19.1,396.9,3.54,42.8 257 | 0.04819,80,3.64,0,0.392,6.108,32,9.2203,1,315,16.4,392.89,6.57,21.9 258 | 0.03548,80,3.64,0,0.392,5.876,19.1,9.2203,1,315,16.4,395.18,9.25,20.9 259 | 0.01538,90,3.75,0,0.394,7.454,34.2,6.3361,3,244,15.9,386.34,3.11,44 260 | 0.61154,20,3.97,0,0.647,8.704,86.9,1.801,5,264,13,389.7,5.12,50 261 | 0.66351,20,3.97,0,0.647,7.333,100,1.8946,5,264,13,383.29,7.79,36 262 | 0.65665,20,3.97,0,0.647,6.842,100,2.0107,5,264,13,391.93,6.9,30.1 263 | 0.54011,20,3.97,0,0.647,7.203,81.8,2.1121,5,264,13,392.8,9.59,33.8 264 | 0.53412,20,3.97,0,0.647,7.52,89.4,2.1398,5,264,13,388.37,7.26,43.1 265 | 0.52014,20,3.97,0,0.647,8.398,91.5,2.2885,5,264,13,386.86,5.91,48.8 266 | 0.82526,20,3.97,0,0.647,7.327,94.5,2.0788,5,264,13,393.42,11.25,31 267 | 0.55007,20,3.97,0,0.647,7.206,91.6,1.9301,5,264,13,387.89,8.1,36.5 268 | 0.76162,20,3.97,0,0.647,5.56,62.8,1.9865,5,264,13,392.4,10.45,22.8 269 | 0.7857,20,3.97,0,0.647,7.014,84.6,2.1329,5,264,13,384.07,14.79,30.7 270 | 0.57834,20,3.97,0,0.575,8.297,67,2.4216,5,264,13,384.54,7.44,50 271 | 0.5405,20,3.97,0,0.575,7.47,52.6,2.872,5,264,13,390.3,3.16,43.5 272 | 0.09065,20,6.96,1,0.464,5.92,61.5,3.9175,3,223,18.6,391.34,13.65,20.7 273 | 0.29916,20,6.96,0,0.464,5.856,42.1,4.429,3,223,18.6,388.65,13,21.1 274 | 0.16211,20,6.96,0,0.464,6.24,16.3,4.429,3,223,18.6,396.9,6.59,25.2 275 | 0.1146,20,6.96,0,0.464,6.538,58.7,3.9175,3,223,18.6,394.96,7.73,24.4 276 | 0.22188,20,6.96,1,0.464,7.691,51.8,4.3665,3,223,18.6,390.77,6.58,35.2 277 | 0.05644,40,6.41,1,0.447,6.758,32.9,4.0776,4,254,17.6,396.9,3.53,32.4 278 | 0.09604,40,6.41,0,0.447,6.854,42.8,4.2673,4,254,17.6,396.9,2.98,32 279 | 0.10469,40,6.41,1,0.447,7.267,49,4.7872,4,254,17.6,389.25,6.05,33.2 280 | 0.06127,40,6.41,1,0.447,6.826,27.6,4.8628,4,254,17.6,393.45,4.16,33.1 281 | 0.07978,40,6.41,0,0.447,6.482,32.1,4.1403,4,254,17.6,396.9,7.19,29.1 282 | 0.21038,20,3.33,0,0.4429,6.812,32.2,4.1007,5,216,14.9,396.9,4.85,35.1 283 | 0.03578,20,3.33,0,0.4429,7.82,64.5,4.6947,5,216,14.9,387.31,3.76,45.4 284 | 0.03705,20,3.33,0,0.4429,6.968,37.2,5.2447,5,216,14.9,392.23,4.59,35.4 285 | 0.06129,20,3.33,1,0.4429,7.645,49.7,5.2119,5,216,14.9,377.07,3.01,46 286 | 0.01501,90,1.21,1,0.401,7.923,24.8,5.885,1,198,13.6,395.52,3.16,50 287 | 0.00906,90,2.97,0,0.4,7.088,20.8,7.3073,1,285,15.3,394.72,7.85,32.2 288 | 0.01096,55,2.25,0,0.389,6.453,31.9,7.3073,1,300,15.3,394.72,8.23,22 289 | 0.01965,80,1.76,0,0.385,6.23,31.5,9.0892,1,241,18.2,341.6,12.93,20.1 290 | 0.03871,52.5,5.32,0,0.405,6.209,31.3,7.3172,6,293,16.6,396.9,7.14,23.2 291 | 0.0459,52.5,5.32,0,0.405,6.315,45.6,7.3172,6,293,16.6,396.9,7.6,22.3 292 | 0.04297,52.5,5.32,0,0.405,6.565,22.9,7.3172,6,293,16.6,371.72,9.51,24.8 293 | 0.03502,80,4.95,0,0.411,6.861,27.9,5.1167,4,245,19.2,396.9,3.33,28.5 294 | 0.07886,80,4.95,0,0.411,7.148,27.7,5.1167,4,245,19.2,396.9,3.56,37.3 295 | 0.03615,80,4.95,0,0.411,6.63,23.4,5.1167,4,245,19.2,396.9,4.7,27.9 296 | 0.08265,0,13.92,0,0.437,6.127,18.4,5.5027,4,289,16,396.9,8.58,23.9 297 | 0.08199,0,13.92,0,0.437,6.009,42.3,5.5027,4,289,16,396.9,10.4,21.7 298 | 0.12932,0,13.92,0,0.437,6.678,31.1,5.9604,4,289,16,396.9,6.27,28.6 299 | 0.05372,0,13.92,0,0.437,6.549,51,5.9604,4,289,16,392.85,7.39,27.1 300 | 0.14103,0,13.92,0,0.437,5.79,58,6.32,4,289,16,396.9,15.84,20.3 301 | 0.06466,70,2.24,0,0.4,6.345,20.1,7.8278,5,358,14.8,368.24,4.97,22.5 302 | 0.05561,70,2.24,0,0.4,7.041,10,7.8278,5,358,14.8,371.58,4.74,29 303 | 0.04417,70,2.24,0,0.4,6.871,47.4,7.8278,5,358,14.8,390.86,6.07,24.8 304 | 0.03537,34,6.09,0,0.433,6.59,40.4,5.4917,7,329,16.1,395.75,9.5,22 305 | 0.09266,34,6.09,0,0.433,6.495,18.4,5.4917,7,329,16.1,383.61,8.67,26.4 306 | 0.1,34,6.09,0,0.433,6.982,17.7,5.4917,7,329,16.1,390.43,4.86,33.1 307 | 0.05515,33,2.18,0,0.472,7.236,41.1,4.022,7,222,18.4,393.68,6.93,36.1 308 | 0.05479,33,2.18,0,0.472,6.616,58.1,3.37,7,222,18.4,393.36,8.93,28.4 309 | 0.07503,33,2.18,0,0.472,7.42,71.9,3.0992,7,222,18.4,396.9,6.47,33.4 310 | 0.04932,33,2.18,0,0.472,6.849,70.3,3.1827,7,222,18.4,396.9,7.53,28.2 311 | 0.49298,0,9.9,0,0.544,6.635,82.5,3.3175,4,304,18.4,396.9,4.54,22.8 312 | 0.3494,0,9.9,0,0.544,5.972,76.7,3.1025,4,304,18.4,396.24,9.97,20.3 313 | 2.63548,0,9.9,0,0.544,4.973,37.8,2.5194,4,304,18.4,350.45,12.64,16.1 314 | 0.79041,0,9.9,0,0.544,6.122,52.8,2.6403,4,304,18.4,396.9,5.98,22.1 315 | 0.26169,0,9.9,0,0.544,6.023,90.4,2.834,4,304,18.4,396.3,11.72,19.4 316 | 0.26938,0,9.9,0,0.544,6.266,82.8,3.2628,4,304,18.4,393.39,7.9,21.6 317 | 0.3692,0,9.9,0,0.544,6.567,87.3,3.6023,4,304,18.4,395.69,9.28,23.8 318 | 0.25356,0,9.9,0,0.544,5.705,77.7,3.945,4,304,18.4,396.42,11.5,16.2 319 | 0.31827,0,9.9,0,0.544,5.914,83.2,3.9986,4,304,18.4,390.7,18.33,17.8 320 | 0.24522,0,9.9,0,0.544,5.782,71.7,4.0317,4,304,18.4,396.9,15.94,19.8 321 | 0.40202,0,9.9,0,0.544,6.382,67.2,3.5325,4,304,18.4,395.21,10.36,23.1 322 | 0.47547,0,9.9,0,0.544,6.113,58.8,4.0019,4,304,18.4,396.23,12.73,21 323 | 0.1676,0,7.38,0,0.493,6.426,52.3,4.5404,5,287,19.6,396.9,7.2,23.8 324 | 0.18159,0,7.38,0,0.493,6.376,54.3,4.5404,5,287,19.6,396.9,6.87,23.1 325 | 0.35114,0,7.38,0,0.493,6.041,49.9,4.7211,5,287,19.6,396.9,7.7,20.4 326 | 0.28392,0,7.38,0,0.493,5.708,74.3,4.7211,5,287,19.6,391.13,11.74,18.5 327 | 0.34109,0,7.38,0,0.493,6.415,40.1,4.7211,5,287,19.6,396.9,6.12,25 328 | 0.19186,0,7.38,0,0.493,6.431,14.7,5.4159,5,287,19.6,393.68,5.08,24.6 329 | 0.30347,0,7.38,0,0.493,6.312,28.9,5.4159,5,287,19.6,396.9,6.15,23 330 | 0.24103,0,7.38,0,0.493,6.083,43.7,5.4159,5,287,19.6,396.9,12.79,22.2 331 | 0.06617,0,3.24,0,0.46,5.868,25.8,5.2146,4,430,16.9,382.44,9.97,19.3 332 | 0.06724,0,3.24,0,0.46,6.333,17.2,5.2146,4,430,16.9,375.21,7.34,22.6 333 | 0.04544,0,3.24,0,0.46,6.144,32.2,5.8736,4,430,16.9,368.57,9.09,19.8 334 | 0.05023,35,6.06,0,0.4379,5.706,28.4,6.6407,1,304,16.9,394.02,12.43,17.1 335 | 0.03466,35,6.06,0,0.4379,6.031,23.3,6.6407,1,304,16.9,362.25,7.83,19.4 336 | 0.05083,0,5.19,0,0.515,6.316,38.1,6.4584,5,224,20.2,389.71,5.68,22.2 337 | 0.03738,0,5.19,0,0.515,6.31,38.5,6.4584,5,224,20.2,389.4,6.75,20.7 338 | 0.03961,0,5.19,0,0.515,6.037,34.5,5.9853,5,224,20.2,396.9,8.01,21.1 339 | 0.03427,0,5.19,0,0.515,5.869,46.3,5.2311,5,224,20.2,396.9,9.8,19.5 340 | 0.03041,0,5.19,0,0.515,5.895,59.6,5.615,5,224,20.2,394.81,10.56,18.5 341 | 0.03306,0,5.19,0,0.515,6.059,37.3,4.8122,5,224,20.2,396.14,8.51,20.6 342 | 0.05497,0,5.19,0,0.515,5.985,45.4,4.8122,5,224,20.2,396.9,9.74,19 343 | 0.06151,0,5.19,0,0.515,5.968,58.5,4.8122,5,224,20.2,396.9,9.29,18.7 344 | 0.01301,35,1.52,0,0.442,7.241,49.3,7.0379,1,284,15.5,394.74,5.49,32.7 345 | 0.02498,0,1.89,0,0.518,6.54,59.7,6.2669,1,422,15.9,389.96,8.65,16.5 346 | 0.02543,55,3.78,0,0.484,6.696,56.4,5.7321,5,370,17.6,396.9,7.18,23.9 347 | 0.03049,55,3.78,0,0.484,6.874,28.1,6.4654,5,370,17.6,387.97,4.61,31.2 348 | 0.03113,0,4.39,0,0.442,6.014,48.5,8.0136,3,352,18.8,385.64,10.53,17.5 349 | 0.06162,0,4.39,0,0.442,5.898,52.3,8.0136,3,352,18.8,364.61,12.67,17.2 350 | 0.0187,85,4.15,0,0.429,6.516,27.7,8.5353,4,351,17.9,392.43,6.36,23.1 351 | 0.01501,80,2.01,0,0.435,6.635,29.7,8.344,4,280,17,390.94,5.99,24.5 352 | 0.02899,40,1.25,0,0.429,6.939,34.5,8.7921,1,335,19.7,389.85,5.89,26.6 353 | 0.06211,40,1.25,0,0.429,6.49,44.4,8.7921,1,335,19.7,396.9,5.98,22.9 354 | 0.0795,60,1.69,0,0.411,6.579,35.9,10.7103,4,411,18.3,370.78,5.49,24.1 355 | 0.07244,60,1.69,0,0.411,5.884,18.5,10.7103,4,411,18.3,392.33,7.79,18.6 356 | 0.01709,90,2.02,0,0.41,6.728,36.1,12.1265,5,187,17,384.46,4.5,30.1 357 | 0.04301,80,1.91,0,0.413,5.663,21.9,10.5857,4,334,22,382.8,8.05,18.2 358 | 0.10659,80,1.91,0,0.413,5.936,19.5,10.5857,4,334,22,376.04,5.57,20.6 359 | 8.98296,0,18.1,1,0.77,6.212,97.4,2.1222,24,666,20.2,377.73,17.6,17.8 360 | 3.8497,0,18.1,1,0.77,6.395,91,2.5052,24,666,20.2,391.34,13.27,21.7 361 | 5.20177,0,18.1,1,0.77,6.127,83.4,2.7227,24,666,20.2,395.43,11.48,22.7 362 | 4.26131,0,18.1,0,0.77,6.112,81.3,2.5091,24,666,20.2,390.74,12.67,22.6 363 | 4.54192,0,18.1,0,0.77,6.398,88,2.5182,24,666,20.2,374.56,7.79,25 364 | 3.83684,0,18.1,0,0.77,6.251,91.1,2.2955,24,666,20.2,350.65,14.19,19.9 365 | 3.67822,0,18.1,0,0.77,5.362,96.2,2.1036,24,666,20.2,380.79,10.19,20.8 366 | 4.22239,0,18.1,1,0.77,5.803,89,1.9047,24,666,20.2,353.04,14.64,16.8 367 | 3.47428,0,18.1,1,0.718,8.78,82.9,1.9047,24,666,20.2,354.55,5.29,21.9 368 | 4.55587,0,18.1,0,0.718,3.561,87.9,1.6132,24,666,20.2,354.7,7.12,27.5 369 | 3.69695,0,18.1,0,0.718,4.963,91.4,1.7523,24,666,20.2,316.03,14,21.9 370 | 13.5222,0,18.1,0,0.631,3.863,100,1.5106,24,666,20.2,131.42,13.33,23.1 371 | 4.89822,0,18.1,0,0.631,4.97,100,1.3325,24,666,20.2,375.52,3.26,50 372 | 5.66998,0,18.1,1,0.631,6.683,96.8,1.3567,24,666,20.2,375.33,3.73,50 373 | 6.53876,0,18.1,1,0.631,7.016,97.5,1.2024,24,666,20.2,392.05,2.96,50 374 | 9.2323,0,18.1,0,0.631,6.216,100,1.1691,24,666,20.2,366.15,9.53,50 375 | 8.26725,0,18.1,1,0.668,5.875,89.6,1.1296,24,666,20.2,347.88,8.88,50 376 | 11.1081,0,18.1,0,0.668,4.906,100,1.1742,24,666,20.2,396.9,34.77,13.8 377 | 18.4982,0,18.1,0,0.668,4.138,100,1.137,24,666,20.2,396.9,37.97,13.8 378 | 19.6091,0,18.1,0,0.671,7.313,97.9,1.3163,24,666,20.2,396.9,13.44,15 379 | 15.288,0,18.1,0,0.671,6.649,93.3,1.3449,24,666,20.2,363.02,23.24,13.9 380 | 9.82349,0,18.1,0,0.671,6.794,98.8,1.358,24,666,20.2,396.9,21.24,13.3 381 | 23.6482,0,18.1,0,0.671,6.38,96.2,1.3861,24,666,20.2,396.9,23.69,13.1 382 | 17.8667,0,18.1,0,0.671,6.223,100,1.3861,24,666,20.2,393.74,21.78,10.2 383 | 88.9762,0,18.1,0,0.671,6.968,91.9,1.4165,24,666,20.2,396.9,17.21,10.4 384 | 15.8744,0,18.1,0,0.671,6.545,99.1,1.5192,24,666,20.2,396.9,21.08,10.9 385 | 9.18702,0,18.1,0,0.7,5.536,100,1.5804,24,666,20.2,396.9,23.6,11.3 386 | 7.99248,0,18.1,0,0.7,5.52,100,1.5331,24,666,20.2,396.9,24.56,12.3 387 | 20.0849,0,18.1,0,0.7,4.368,91.2,1.4395,24,666,20.2,285.83,30.63,8.8 388 | 16.8118,0,18.1,0,0.7,5.277,98.1,1.4261,24,666,20.2,396.9,30.81,7.2 389 | 24.3938,0,18.1,0,0.7,4.652,100,1.4672,24,666,20.2,396.9,28.28,10.5 390 | 22.5971,0,18.1,0,0.7,5,89.5,1.5184,24,666,20.2,396.9,31.99,7.4 391 | 14.3337,0,18.1,0,0.7,4.88,100,1.5895,24,666,20.2,372.92,30.62,10.2 392 | 8.15174,0,18.1,0,0.7,5.39,98.9,1.7281,24,666,20.2,396.9,20.85,11.5 393 | 6.96215,0,18.1,0,0.7,5.713,97,1.9265,24,666,20.2,394.43,17.11,15.1 394 | 5.29305,0,18.1,0,0.7,6.051,82.5,2.1678,24,666,20.2,378.38,18.76,23.2 395 | 11.5779,0,18.1,0,0.7,5.036,97,1.77,24,666,20.2,396.9,25.68,9.7 396 | 8.64476,0,18.1,0,0.693,6.193,92.6,1.7912,24,666,20.2,396.9,15.17,13.8 397 | 13.3598,0,18.1,0,0.693,5.887,94.7,1.7821,24,666,20.2,396.9,16.35,12.7 398 | 8.71675,0,18.1,0,0.693,6.471,98.8,1.7257,24,666,20.2,391.98,17.12,13.1 399 | 5.87205,0,18.1,0,0.693,6.405,96,1.6768,24,666,20.2,396.9,19.37,12.5 400 | 7.67202,0,18.1,0,0.693,5.747,98.9,1.6334,24,666,20.2,393.1,19.92,8.5 401 | 38.3518,0,18.1,0,0.693,5.453,100,1.4896,24,666,20.2,396.9,30.59,5 402 | 9.91655,0,18.1,0,0.693,5.852,77.8,1.5004,24,666,20.2,338.16,29.97,6.3 403 | 25.0461,0,18.1,0,0.693,5.987,100,1.5888,24,666,20.2,396.9,26.77,5.6 404 | 14.2362,0,18.1,0,0.693,6.343,100,1.5741,24,666,20.2,396.9,20.32,7.2 405 | 9.59571,0,18.1,0,0.693,6.404,100,1.639,24,666,20.2,376.11,20.31,12.1 406 | 24.8017,0,18.1,0,0.693,5.349,96,1.7028,24,666,20.2,396.9,19.77,8.3 407 | 41.5292,0,18.1,0,0.693,5.531,85.4,1.6074,24,666,20.2,329.46,27.38,8.5 408 | 67.9208,0,18.1,0,0.693,5.683,100,1.4254,24,666,20.2,384.97,22.98,5 409 | 20.7162,0,18.1,0,0.659,4.138,100,1.1781,24,666,20.2,370.22,23.34,11.9 410 | 11.9511,0,18.1,0,0.659,5.608,100,1.2852,24,666,20.2,332.09,12.13,27.9 411 | 7.40389,0,18.1,0,0.597,5.617,97.9,1.4547,24,666,20.2,314.64,26.4,17.2 412 | 14.4383,0,18.1,0,0.597,6.852,100,1.4655,24,666,20.2,179.36,19.78,27.5 413 | 51.1358,0,18.1,0,0.597,5.757,100,1.413,24,666,20.2,2.6,10.11,15 414 | 14.0507,0,18.1,0,0.597,6.657,100,1.5275,24,666,20.2,35.05,21.22,17.2 415 | 18.811,0,18.1,0,0.597,4.628,100,1.5539,24,666,20.2,28.79,34.37,17.9 416 | 28.6558,0,18.1,0,0.597,5.155,100,1.5894,24,666,20.2,210.97,20.08,16.3 417 | 45.7461,0,18.1,0,0.693,4.519,100,1.6582,24,666,20.2,88.27,36.98,7 418 | 18.0846,0,18.1,0,0.679,6.434,100,1.8347,24,666,20.2,27.25,29.05,7.2 419 | 10.8342,0,18.1,0,0.679,6.782,90.8,1.8195,24,666,20.2,21.57,25.79,7.5 420 | 25.9406,0,18.1,0,0.679,5.304,89.1,1.6475,24,666,20.2,127.36,26.64,10.4 421 | 73.5341,0,18.1,0,0.679,5.957,100,1.8026,24,666,20.2,16.45,20.62,8.8 422 | 11.8123,0,18.1,0,0.718,6.824,76.5,1.794,24,666,20.2,48.45,22.74,8.4 423 | 11.0874,0,18.1,0,0.718,6.411,100,1.8589,24,666,20.2,318.75,15.02,16.7 424 | 7.02259,0,18.1,0,0.718,6.006,95.3,1.8746,24,666,20.2,319.98,15.7,14.2 425 | 12.0482,0,18.1,0,0.614,5.648,87.6,1.9512,24,666,20.2,291.55,14.1,20.8 426 | 7.05042,0,18.1,0,0.614,6.103,85.1,2.0218,24,666,20.2,2.52,23.29,13.4 427 | 8.79212,0,18.1,0,0.584,5.565,70.6,2.0635,24,666,20.2,3.65,17.16,11.7 428 | 15.8603,0,18.1,0,0.679,5.896,95.4,1.9096,24,666,20.2,7.68,24.39,8.3 429 | 12.2472,0,18.1,0,0.584,5.837,59.7,1.9976,24,666,20.2,24.65,15.69,10.2 430 | 37.6619,0,18.1,0,0.679,6.202,78.7,1.8629,24,666,20.2,18.82,14.52,10.9 431 | 7.36711,0,18.1,0,0.679,6.193,78.1,1.9356,24,666,20.2,96.73,21.52,11 432 | 9.33889,0,18.1,0,0.679,6.38,95.6,1.9682,24,666,20.2,60.72,24.08,9.5 433 | 8.49213,0,18.1,0,0.584,6.348,86.1,2.0527,24,666,20.2,83.45,17.64,14.5 434 | 10.0623,0,18.1,0,0.584,6.833,94.3,2.0882,24,666,20.2,81.33,19.69,14.1 435 | 6.44405,0,18.1,0,0.584,6.425,74.8,2.2004,24,666,20.2,97.95,12.03,16.1 436 | 5.58107,0,18.1,0,0.713,6.436,87.9,2.3158,24,666,20.2,100.19,16.22,14.3 437 | 13.9134,0,18.1,0,0.713,6.208,95,2.2222,24,666,20.2,100.63,15.17,11.7 438 | 11.1604,0,18.1,0,0.74,6.629,94.6,2.1247,24,666,20.2,109.85,23.27,13.4 439 | 14.4208,0,18.1,0,0.74,6.461,93.3,2.0026,24,666,20.2,27.49,18.05,9.6 440 | 15.1772,0,18.1,0,0.74,6.152,100,1.9142,24,666,20.2,9.32,26.45,8.7 441 | 13.6781,0,18.1,0,0.74,5.935,87.9,1.8206,24,666,20.2,68.95,34.02,8.4 442 | 9.39063,0,18.1,0,0.74,5.627,93.9,1.8172,24,666,20.2,396.9,22.88,12.8 443 | 22.0511,0,18.1,0,0.74,5.818,92.4,1.8662,24,666,20.2,391.45,22.11,10.5 444 | 9.72418,0,18.1,0,0.74,6.406,97.2,2.0651,24,666,20.2,385.96,19.52,17.1 445 | 5.66637,0,18.1,0,0.74,6.219,100,2.0048,24,666,20.2,395.69,16.59,18.4 446 | 9.96654,0,18.1,0,0.74,6.485,100,1.9784,24,666,20.2,386.73,18.85,15.4 447 | 12.8023,0,18.1,0,0.74,5.854,96.6,1.8956,24,666,20.2,240.52,23.79,10.8 448 | 0.6718,0,18.1,0,0.74,6.459,94.8,1.9879,24,666,20.2,43.06,23.98,11.8 449 | 6.28807,0,18.1,0,0.74,6.341,96.4,2.072,24,666,20.2,318.01,17.79,14.9 450 | 9.92485,0,18.1,0,0.74,6.251,96.6,2.198,24,666,20.2,388.52,16.44,12.6 451 | 9.32909,0,18.1,0,0.713,6.185,98.7,2.2616,24,666,20.2,396.9,18.13,14.1 452 | 7.52601,0,18.1,0,0.713,6.417,98.3,2.185,24,666,20.2,304.21,19.31,13 453 | 6.71772,0,18.1,0,0.713,6.749,92.6,2.3236,24,666,20.2,0.32,17.44,13.4 454 | 5.44114,0,18.1,0,0.713,6.655,98.2,2.3552,24,666,20.2,355.29,17.73,15.2 455 | 5.09017,0,18.1,0,0.713,6.297,91.8,2.3682,24,666,20.2,385.09,17.27,16.1 456 | 8.24809,0,18.1,0,0.713,7.393,99.3,2.4527,24,666,20.2,375.87,16.74,17.8 457 | 9.51363,0,18.1,0,0.713,6.728,94.1,2.4961,24,666,20.2,6.68,18.71,14.9 458 | 4.75237,0,18.1,0,0.713,6.525,86.5,2.4358,24,666,20.2,50.92,18.13,14.1 459 | 4.66883,0,18.1,0,0.713,5.976,87.9,2.5806,24,666,20.2,10.48,19.01,12.7 460 | 8.20058,0,18.1,0,0.713,5.936,80.3,2.7792,24,666,20.2,3.5,16.94,13.5 461 | 7.75223,0,18.1,0,0.713,6.301,83.7,2.7831,24,666,20.2,272.21,16.23,14.9 462 | 6.80117,0,18.1,0,0.713,6.081,84.4,2.7175,24,666,20.2,396.9,14.7,20 463 | 4.81213,0,18.1,0,0.713,6.701,90,2.5975,24,666,20.2,255.23,16.42,16.4 464 | 3.69311,0,18.1,0,0.713,6.376,88.4,2.5671,24,666,20.2,391.43,14.65,17.7 465 | 6.65492,0,18.1,0,0.713,6.317,83,2.7344,24,666,20.2,396.9,13.99,19.5 466 | 5.82115,0,18.1,0,0.713,6.513,89.9,2.8016,24,666,20.2,393.82,10.29,20.2 467 | 7.83932,0,18.1,0,0.655,6.209,65.4,2.9634,24,666,20.2,396.9,13.22,21.4 468 | 3.1636,0,18.1,0,0.655,5.759,48.2,3.0665,24,666,20.2,334.4,14.13,19.9 469 | 3.77498,0,18.1,0,0.655,5.952,84.7,2.8715,24,666,20.2,22.01,17.15,19 470 | 4.42228,0,18.1,0,0.584,6.003,94.5,2.5403,24,666,20.2,331.29,21.32,19.1 471 | 15.5757,0,18.1,0,0.58,5.926,71,2.9084,24,666,20.2,368.74,18.13,19.1 472 | 13.0751,0,18.1,0,0.58,5.713,56.7,2.8237,24,666,20.2,396.9,14.76,20.1 473 | 4.34879,0,18.1,0,0.58,6.167,84,3.0334,24,666,20.2,396.9,16.29,19.9 474 | 4.03841,0,18.1,0,0.532,6.229,90.7,3.0993,24,666,20.2,395.33,12.87,19.6 475 | 3.56868,0,18.1,0,0.58,6.437,75,2.8965,24,666,20.2,393.37,14.36,23.2 476 | 4.64689,0,18.1,0,0.614,6.98,67.6,2.5329,24,666,20.2,374.68,11.66,29.8 477 | 8.05579,0,18.1,0,0.584,5.427,95.4,2.4298,24,666,20.2,352.58,18.14,13.8 478 | 6.39312,0,18.1,0,0.584,6.162,97.4,2.206,24,666,20.2,302.76,24.1,13.3 479 | 4.87141,0,18.1,0,0.614,6.484,93.6,2.3053,24,666,20.2,396.21,18.68,16.7 480 | 15.0234,0,18.1,0,0.614,5.304,97.3,2.1007,24,666,20.2,349.48,24.91,12 481 | 10.233,0,18.1,0,0.614,6.185,96.7,2.1705,24,666,20.2,379.7,18.03,14.6 482 | 14.3337,0,18.1,0,0.614,6.229,88,1.9512,24,666,20.2,383.32,13.11,21.4 483 | 5.82401,0,18.1,0,0.532,6.242,64.7,3.4242,24,666,20.2,396.9,10.74,23 484 | 5.70818,0,18.1,0,0.532,6.75,74.9,3.3317,24,666,20.2,393.07,7.74,23.7 485 | 5.73116,0,18.1,0,0.532,7.061,77,3.4106,24,666,20.2,395.28,7.01,25 486 | 2.81838,0,18.1,0,0.532,5.762,40.3,4.0983,24,666,20.2,392.92,10.42,21.8 487 | 2.37857,0,18.1,0,0.583,5.871,41.9,3.724,24,666,20.2,370.73,13.34,20.6 488 | 3.67367,0,18.1,0,0.583,6.312,51.9,3.9917,24,666,20.2,388.62,10.58,21.2 489 | 5.69175,0,18.1,0,0.583,6.114,79.8,3.5459,24,666,20.2,392.68,14.98,19.1 490 | 4.83567,0,18.1,0,0.583,5.905,53.2,3.1523,24,666,20.2,388.22,11.45,20.6 491 | 0.15086,0,27.74,0,0.609,5.454,92.7,1.8209,4,711,20.1,395.09,18.06,15.2 492 | 0.18337,0,27.74,0,0.609,5.414,98.3,1.7554,4,711,20.1,344.05,23.97,7 493 | 0.20746,0,27.74,0,0.609,5.093,98,1.8226,4,711,20.1,318.43,29.68,8.1 494 | 0.10574,0,27.74,0,0.609,5.983,98.8,1.8681,4,711,20.1,390.11,18.07,13.6 495 | 0.11132,0,27.74,0,0.609,5.983,83.5,2.1099,4,711,20.1,396.9,13.35,20.1 496 | 0.17331,0,9.69,0,0.585,5.707,54,2.3817,6,391,19.2,396.9,12.01,21.8 497 | 0.27957,0,9.69,0,0.585,5.926,42.6,2.3817,6,391,19.2,396.9,13.59,24.5 498 | 0.17899,0,9.69,0,0.585,5.67,28.8,2.7986,6,391,19.2,393.29,17.6,23.1 499 | 0.2896,0,9.69,0,0.585,5.39,72.9,2.7986,6,391,19.2,396.9,21.14,19.7 500 | 0.26838,0,9.69,0,0.585,5.794,70.6,2.8927,6,391,19.2,396.9,14.1,18.3 501 | 0.23912,0,9.69,0,0.585,6.019,65.3,2.4091,6,391,19.2,396.9,12.92,21.2 502 | 0.17783,0,9.69,0,0.585,5.569,73.5,2.3999,6,391,19.2,395.77,15.1,17.5 503 | 0.22438,0,9.69,0,0.585,6.027,79.7,2.4982,6,391,19.2,396.9,14.33,16.8 504 | 0.06263,0,11.93,0,0.573,6.593,69.1,2.4786,1,273,21,391.99,9.67,22.4 505 | 0.04527,0,11.93,0,0.573,6.12,76.7,2.2875,1,273,21,396.9,9.08,20.6 506 | 0.06076,0,11.93,0,0.573,6.976,91,2.1675,1,273,21,396.9,5.64,23.9 507 | 0.10959,0,11.93,0,0.573,6.794,89.3,2.3889,1,273,21,393.45,6.48,22 508 | 0.04741,0,11.93,0,0.573,6.03,80.8,2.505,1,273,21,396.9,7.88,11.9 509 | -------------------------------------------------------------------------------- /resources/datasets/dataset_descriptions/boston_house_prices.rst: -------------------------------------------------------------------------------- 1 | Boston House Prices dataset 2 | 3 | Notes 4 | ------ 5 | Data Set Characteristics: 6 | 7 | :Number of Instances: 506 8 | 9 | :Number of Attributes: 13 numeric/categorical predictive 10 | 11 | :Median Value (attribute 14) is usually the target 12 | 13 | :Attribute Information (in order): 14 | - CRIM per capita crime rate by town 15 | - ZN proportion of residential land zoned for lots over 25,000 sq.ft. 16 | - INDUS proportion of non-retail business acres per town 17 | - CHAS Charles River dummy variable (= 1 if tract bounds river; 0 otherwise) 18 | - NOX nitric oxides concentration (parts per 10 million) 19 | - RM average number of rooms per dwelling 20 | - AGE proportion of owner-occupied units built prior to 1940 21 | - DIS weighted distances to five Boston employment centres 22 | - RAD index of accessibility to radial highways 23 | - TAX full-value property-tax rate per $10,000 24 | - PTRATIO pupil-teacher ratio by town 25 | - B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town 26 | - LSTAT % lower status of the population 27 | - MEDV Median value of owner-occupied homes in $1000's 28 | 29 | :Missing Attribute Values: None 30 | 31 | :Creator: Harrison, D. and Rubinfeld, D.L. 32 | 33 | This is a copy of UCI ML housing dataset. 34 | http://archive.ics.uci.edu/ml/datasets/Housing 35 | 36 | 37 | This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University. 38 | 39 | The Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic 40 | prices and the demand for clean air', J. Environ. Economics & Management, 41 | vol.5, 81-102, 1978. Used in Belsley, Kuh & Welsch, 'Regression diagnostics 42 | ...', Wiley, 1980. N.B. Various transformations are used in the table on 43 | pages 244-261 of the latter. 44 | 45 | The Boston house-price data has been used in many machine learning papers that address regression 46 | problems. 47 | 48 | **References** 49 | 50 | - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261. 51 | - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann. 52 | - many more! (see http://archive.ics.uci.edu/ml/datasets/Housing) 53 | -------------------------------------------------------------------------------- /resources/datasets/dataset_descriptions/iris.rst: -------------------------------------------------------------------------------- 1 | Iris Plants Database 2 | 3 | Notes 4 | ----- 5 | Data Set Characteristics: 6 | :Number of Instances: 150 (50 in each of three classes) 7 | :Number of Attributes: 4 numeric, predictive attributes and the class 8 | :Attribute Information: 9 | - sepal length in cm 10 | - sepal width in cm 11 | - petal length in cm 12 | - petal width in cm 13 | - class: 14 | - Iris-Setosa 15 | - Iris-Versicolour 16 | - Iris-Virginica 17 | :Summary Statistics: 18 | ============== ==== ==== ======= ===== ==================== 19 | Min Max Mean SD Class Correlation 20 | ============== ==== ==== ======= ===== ==================== 21 | sepal length: 4.3 7.9 5.84 0.83 0.7826 22 | sepal width: 2.0 4.4 3.05 0.43 -0.4194 23 | petal length: 1.0 6.9 3.76 1.76 0.9490 (high!) 24 | petal width: 0.1 2.5 1.20 0.76 0.9565 (high!) 25 | ============== ==== ==== ======= ===== ==================== 26 | :Missing Attribute Values: None 27 | :Class Distribution: 33.3% for each of 3 classes. 28 | :Creator: R.A. Fisher 29 | :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov) 30 | :Date: July, 1988 31 | 32 | This is a copy of UCI ML iris datasets. 33 | http://archive.ics.uci.edu/ml/datasets/Iris 34 | 35 | The famous Iris database, first used by Sir R.A Fisher 36 | 37 | This is perhaps the best known database to be found in the 38 | pattern recognition literature. Fisher's paper is a classic in the field and 39 | is referenced frequently to this day. (See Duda & Hart, for example.) The 40 | data set contains 3 classes of 50 instances each, where each class refers to a 41 | type of iris plant. One class is linearly separable from the other 2; the 42 | latter are NOT linearly separable from each other. 43 | 44 | References 45 | ---------- 46 | - Fisher,R.A. "The use of multiple measurements in taxonomic problems" 47 | Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to 48 | Mathematical Statistics" (John Wiley, NY, 1950). 49 | - Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis. 50 | (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218. 51 | - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System 52 | Structure and Classification Rule for Recognition in Partially Exposed 53 | Environments". IEEE Transactions on Pattern Analysis and Machine 54 | Intelligence, Vol. PAMI-2, No. 1, 67-71. 55 | - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions 56 | on Information Theory, May 1972, 431-433. 57 | - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II 58 | conceptual clustering system finds 3 classes in the data. 59 | - Many, many more ... 60 | -------------------------------------------------------------------------------- /resources/datasets/iris.csv: -------------------------------------------------------------------------------- 1 | 150,4,setosa,versicolor,virginica 2 | 5.1,3.5,1.4,0.2,0 3 | 4.9,3.0,1.4,0.2,0 4 | 4.7,3.2,1.3,0.2,0 5 | 4.6,3.1,1.5,0.2,0 6 | 5.0,3.6,1.4,0.2,0 7 | 5.4,3.9,1.7,0.4,0 8 | 4.6,3.4,1.4,0.3,0 9 | 5.0,3.4,1.5,0.2,0 10 | 4.4,2.9,1.4,0.2,0 11 | 4.9,3.1,1.5,0.1,0 12 | 5.4,3.7,1.5,0.2,0 13 | 4.8,3.4,1.6,0.2,0 14 | 4.8,3.0,1.4,0.1,0 15 | 4.3,3.0,1.1,0.1,0 16 | 5.8,4.0,1.2,0.2,0 17 | 5.7,4.4,1.5,0.4,0 18 | 5.4,3.9,1.3,0.4,0 19 | 5.1,3.5,1.4,0.3,0 20 | 5.7,3.8,1.7,0.3,0 21 | 5.1,3.8,1.5,0.3,0 22 | 5.4,3.4,1.7,0.2,0 23 | 5.1,3.7,1.5,0.4,0 24 | 4.6,3.6,1.0,0.2,0 25 | 5.1,3.3,1.7,0.5,0 26 | 4.8,3.4,1.9,0.2,0 27 | 5.0,3.0,1.6,0.2,0 28 | 5.0,3.4,1.6,0.4,0 29 | 5.2,3.5,1.5,0.2,0 30 | 5.2,3.4,1.4,0.2,0 31 | 4.7,3.2,1.6,0.2,0 32 | 4.8,3.1,1.6,0.2,0 33 | 5.4,3.4,1.5,0.4,0 34 | 5.2,4.1,1.5,0.1,0 35 | 5.5,4.2,1.4,0.2,0 36 | 4.9,3.1,1.5,0.1,0 37 | 5.0,3.2,1.2,0.2,0 38 | 5.5,3.5,1.3,0.2,0 39 | 4.9,3.1,1.5,0.1,0 40 | 4.4,3.0,1.3,0.2,0 41 | 5.1,3.4,1.5,0.2,0 42 | 5.0,3.5,1.3,0.3,0 43 | 4.5,2.3,1.3,0.3,0 44 | 4.4,3.2,1.3,0.2,0 45 | 5.0,3.5,1.6,0.6,0 46 | 5.1,3.8,1.9,0.4,0 47 | 4.8,3.0,1.4,0.3,0 48 | 5.1,3.8,1.6,0.2,0 49 | 4.6,3.2,1.4,0.2,0 50 | 5.3,3.7,1.5,0.2,0 51 | 5.0,3.3,1.4,0.2,0 52 | 7.0,3.2,4.7,1.4,1 53 | 6.4,3.2,4.5,1.5,1 54 | 6.9,3.1,4.9,1.5,1 55 | 5.5,2.3,4.0,1.3,1 56 | 6.5,2.8,4.6,1.5,1 57 | 5.7,2.8,4.5,1.3,1 58 | 6.3,3.3,4.7,1.6,1 59 | 4.9,2.4,3.3,1.0,1 60 | 6.6,2.9,4.6,1.3,1 61 | 5.2,2.7,3.9,1.4,1 62 | 5.0,2.0,3.5,1.0,1 63 | 5.9,3.0,4.2,1.5,1 64 | 6.0,2.2,4.0,1.0,1 65 | 6.1,2.9,4.7,1.4,1 66 | 5.6,2.9,3.6,1.3,1 67 | 6.7,3.1,4.4,1.4,1 68 | 5.6,3.0,4.5,1.5,1 69 | 5.8,2.7,4.1,1.0,1 70 | 6.2,2.2,4.5,1.5,1 71 | 5.6,2.5,3.9,1.1,1 72 | 5.9,3.2,4.8,1.8,1 73 | 6.1,2.8,4.0,1.3,1 74 | 6.3,2.5,4.9,1.5,1 75 | 6.1,2.8,4.7,1.2,1 76 | 6.4,2.9,4.3,1.3,1 77 | 6.6,3.0,4.4,1.4,1 78 | 6.8,2.8,4.8,1.4,1 79 | 6.7,3.0,5.0,1.7,1 80 | 6.0,2.9,4.5,1.5,1 81 | 5.7,2.6,3.5,1.0,1 82 | 5.5,2.4,3.8,1.1,1 83 | 5.5,2.4,3.7,1.0,1 84 | 5.8,2.7,3.9,1.2,1 85 | 6.0,2.7,5.1,1.6,1 86 | 5.4,3.0,4.5,1.5,1 87 | 6.0,3.4,4.5,1.6,1 88 | 6.7,3.1,4.7,1.5,1 89 | 6.3,2.3,4.4,1.3,1 90 | 5.6,3.0,4.1,1.3,1 91 | 5.5,2.5,4.0,1.3,1 92 | 5.5,2.6,4.4,1.2,1 93 | 6.1,3.0,4.6,1.4,1 94 | 5.8,2.6,4.0,1.2,1 95 | 5.0,2.3,3.3,1.0,1 96 | 5.6,2.7,4.2,1.3,1 97 | 5.7,3.0,4.2,1.2,1 98 | 5.7,2.9,4.2,1.3,1 99 | 6.2,2.9,4.3,1.3,1 100 | 5.1,2.5,3.0,1.1,1 101 | 5.7,2.8,4.1,1.3,1 102 | 6.3,3.3,6.0,2.5,2 103 | 5.8,2.7,5.1,1.9,2 104 | 7.1,3.0,5.9,2.1,2 105 | 6.3,2.9,5.6,1.8,2 106 | 6.5,3.0,5.8,2.2,2 107 | 7.6,3.0,6.6,2.1,2 108 | 4.9,2.5,4.5,1.7,2 109 | 7.3,2.9,6.3,1.8,2 110 | 6.7,2.5,5.8,1.8,2 111 | 7.2,3.6,6.1,2.5,2 112 | 6.5,3.2,5.1,2.0,2 113 | 6.4,2.7,5.3,1.9,2 114 | 6.8,3.0,5.5,2.1,2 115 | 5.7,2.5,5.0,2.0,2 116 | 5.8,2.8,5.1,2.4,2 117 | 6.4,3.2,5.3,2.3,2 118 | 6.5,3.0,5.5,1.8,2 119 | 7.7,3.8,6.7,2.2,2 120 | 7.7,2.6,6.9,2.3,2 121 | 6.0,2.2,5.0,1.5,2 122 | 6.9,3.2,5.7,2.3,2 123 | 5.6,2.8,4.9,2.0,2 124 | 7.7,2.8,6.7,2.0,2 125 | 6.3,2.7,4.9,1.8,2 126 | 6.7,3.3,5.7,2.1,2 127 | 7.2,3.2,6.0,1.8,2 128 | 6.2,2.8,4.8,1.8,2 129 | 6.1,3.0,4.9,1.8,2 130 | 6.4,2.8,5.6,2.1,2 131 | 7.2,3.0,5.8,1.6,2 132 | 7.4,2.8,6.1,1.9,2 133 | 7.9,3.8,6.4,2.0,2 134 | 6.4,2.8,5.6,2.2,2 135 | 6.3,2.8,5.1,1.5,2 136 | 6.1,2.6,5.6,1.4,2 137 | 7.7,3.0,6.1,2.3,2 138 | 6.3,3.4,5.6,2.4,2 139 | 6.4,3.1,5.5,1.8,2 140 | 6.0,3.0,4.8,1.8,2 141 | 6.9,3.1,5.4,2.1,2 142 | 6.7,3.1,5.6,2.4,2 143 | 6.9,3.1,5.1,2.3,2 144 | 5.8,2.7,5.1,1.9,2 145 | 6.8,3.2,5.9,2.3,2 146 | 6.7,3.3,5.7,2.5,2 147 | 6.7,3.0,5.2,2.3,2 148 | 6.3,2.5,5.0,1.9,2 149 | 6.5,3.0,5.2,2.0,2 150 | 6.2,3.4,5.4,2.3,2 151 | 5.9,3.0,5.1,1.8,2 152 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/classification/k_nearest_neighbours.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.classification.k-nearest-neighbours 2 | (:require [clojurewerkz.statistiker.distance :as distance] 3 | [clojurewerkz.statistiker.utils :refer [map-groups select-keys-order-dependent]])) 4 | 5 | (defn make-model 6 | [data] 7 | (->> data 8 | (map (fn [[label items]] 9 | (mapv #(vector label %) items))) 10 | (mapcat identity) 11 | vec)) 12 | 13 | (defn maps->model 14 | [maps label features] 15 | (->> maps 16 | vec 17 | (group-by #(get % label)) 18 | (map-groups (fn [items] (mapv #(select-keys-order-dependent % features) items))) 19 | make-model)) 20 | 21 | (defn classify 22 | ([model item k] 23 | (classify model item k :euclidean)) 24 | ([model item k distance-measure] 25 | (->> model 26 | (map (fn [[label model-item]] [label ((get distance/distance-measure-fns distance-measure) item model-item)])) 27 | (sort-by second) 28 | (take k)))) 29 | 30 | (defn best-match 31 | [classified] 32 | (->> classified 33 | (map first) 34 | (frequencies) 35 | vec 36 | (sort-by second >) 37 | ffirst)) 38 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/classification/naive_bayes.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.classification.naive-bayes 2 | (:require [clojure.core.matrix :refer [transpose]] 3 | [clojurewerkz.statistiker.fast-math :refer [sqrt pow exp]] 4 | [clojurewerkz.statistiker.utils :refer [map-groups select-keys-order-dependent]] 5 | [clojurewerkz.statistiker.statistics :refer [variance mean]])) 6 | 7 | (def pi Math/PI) 8 | 9 | (defn make-model 10 | [train-data] 11 | (let [total (reduce + (map count (vals train-data)))] 12 | (into {} 13 | (for [[k v] train-data] 14 | [k {:p (/ (count v) total) 15 | ;; ? Evidence 16 | :classification-data (->> v 17 | transpose 18 | (mapv (fn [v] 19 | (let [var (variance v)] 20 | (assert (not (= 0.0 var)) 21 | (str "Variance of " k " is " var)) 22 | {:mean (mean v) 23 | :variance var}))))}])))) 24 | 25 | (defn maps->model 26 | [maps label features] 27 | (->> maps 28 | vec 29 | (group-by #(get % label)) 30 | (map-groups (fn [items] (mapv #(select-keys-order-dependent % features) items))) 31 | make-model)) 32 | 33 | (defn posterior-prob 34 | "Calculate probability for the single item" 35 | [point variance mean] 36 | (* (/ 1 (sqrt (* 2 pi variance))) 37 | (exp (/ (* -1 (pow (- point mean) 2)) 38 | (* 2 variance))))) 39 | 40 | (defn classify 41 | [model item] 42 | (into {} 43 | (for [[k {:keys [classification-data p]}] model] 44 | [k 45 | (->> classification-data 46 | (mapv (fn [point {:keys [mean variance]}] 47 | (posterior-prob point variance mean)) 48 | item) 49 | ;; TODO: add probability and confidence 50 | (reduce * p))]))) 51 | 52 | (defn best-match 53 | [posterior-probabilities] 54 | (loop [[[k v] & tail] (vec posterior-probabilities) 55 | max Integer/MIN_VALUE 56 | max-key nil] 57 | (let [[max max-key] (if (> max v) 58 | [max max-key] 59 | [v k])] 60 | (if (empty? tail) 61 | [max-key max] 62 | (recur tail v k))))) 63 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/classification/svm.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.classification.svm 2 | (:import [clojurewerkz.statistiker.libsvm.data SvmProblem SvmNode SvmParameter KernelType SvmType] 3 | [clojurewerkz.statistiker.libsvm SVM])) 4 | 5 | (def kernel-types 6 | {:linear KernelType/LINEAR 7 | :poly KernelType/POLY 8 | :pre-computed KernelType/PRECOMPUTED 9 | :rbf KernelType/RBF 10 | :sigmoid KernelType/SIGMOID}) 11 | 12 | (def svm-types 13 | {:c-svc SvmType/C_SVC 14 | :epsilon-svr SvmType/EPSILON_SVR 15 | :nu-svc SvmType/NU_SVC 16 | :nu-svr SvmType/NU_SVR 17 | :one-class SvmType/ONE_CLASS}) 18 | 19 | (def default-params 20 | {:C 1 21 | :cache-size 100 22 | :coef0 0 23 | :degree 3 24 | :eps 1e-3 25 | :gamma 0 26 | :kernel-type (:rbf kernel-types) 27 | :nr-weight 0 28 | :nu 0.5 29 | :p 0.1 30 | :probability 0 31 | :shrinking 1 32 | :svm-type (:c-svc svm-types) 33 | :weight (double-array 0) 34 | :weight-label (int-array 0)}) 35 | 36 | (defn make-node-vec 37 | [vector] 38 | (->> vector 39 | (mapv (fn [index datapoint] 40 | (SvmNode. index (double datapoint))) 41 | (iterate inc 1)) 42 | (into-array))) 43 | 44 | (defn make-problem 45 | [dataset] 46 | (let [converted (->> dataset 47 | vec 48 | (map (fn [[label vectors]] 49 | (->> vectors 50 | (map #(vector label (make-node-vec %)))))) 51 | (mapcat identity)) 52 | labels (mapv first converted) 53 | nodes (mapv second converted)] 54 | (SvmProblem. (double-array labels) 55 | (into-array nodes)))) 56 | 57 | (defn make-params 58 | [dataset & {:as options}] 59 | 60 | (let [params (SvmParameter.)] 61 | (doseq [[key val] (merge default-params options)] 62 | (clojure.lang.Reflector/setInstanceField params (clojure.string/replace (name key) "-" "_") val)) 63 | (set! (.gamma params) (/ 1.0 13)) 64 | params)) 65 | 66 | (defn train-model 67 | [^SvmProblem problem ^SvmParameter params] 68 | (SVM/svm_train problem params)) 69 | 70 | (defn classify 71 | [model point] 72 | (SVM/svm_predict model (make-node-vec point))) 73 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/clustering/dbscan.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.clustering.dbscan 2 | (:import [org.apache.commons.math3.ml.clustering DBSCANClusterer]) 3 | (:require [clojurewerkz.statistiker.distance :as distance] 4 | [clojurewerkz.statistiker.utils :as u])) 5 | 6 | (defn- ^DBSCANClusterer clusterer 7 | ([eps min-points] 8 | (DBSCANClusterer. eps min-points)) 9 | ([eps min-points distance-measure] 10 | (DBSCANClusterer. eps min-points (get distance/distance-measure-instances distance-measure)))) 11 | 12 | (defn cluster 13 | [initial eps min-points] 14 | (let [clusterer (clusterer eps min-points)] 15 | (->> initial 16 | (map u/double-point) 17 | (.cluster clusterer) 18 | (map #(hash-map :points (map (fn [a] 19 | (with-meta (vec (.getPoint a)) 20 | (.getMetadata a))) 21 | (.getPoints %))))))) 22 | 23 | (defn cluster-by 24 | "Clusters the hashmaps by fields. Fields should be given as vector. Field values 25 | should be numerical. 26 | 27 | Resulting hashmap will be returned with :cluster-id field that identifies the cluster" 28 | [data fields eps min-points] 29 | (let [vectors (u/prepare-vectors fields data) 30 | clusters (map :points (cluster vectors eps min-points))] 31 | (u/unmeta-clusters clusters))) 32 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/clustering/kmeans.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.clustering.kmeans 2 | (:import [org.apache.commons.math3.ml.clustering KMeansPlusPlusClusterer]) 3 | 4 | (:require [clojurewerkz.statistiker.utils :as u] 5 | [clojurewerkz.statistiker.distance :as distance])) 6 | 7 | (defn- ^KMeansPlusPlusClusterer clusterer 8 | ([k max-iter] 9 | (KMeansPlusPlusClusterer. k max-iter)) 10 | ([k max-iter distance-measure] 11 | (KMeansPlusPlusClusterer. k max-iter (get distance/distance-measure-instances distance-measure)))) 12 | 13 | (defn cluster 14 | [initial k max-iter] 15 | (let [clusterer (clusterer k max-iter)] 16 | (->> initial 17 | (map u/double-point) 18 | (.cluster clusterer) 19 | (map #(hash-map :center (vec (.getPoint (.getCenter %))) 20 | :points (map (fn [a] (with-meta (vec (.getPoint a)) 21 | (.getMetadata a))) 22 | (.getPoints %))))))) 23 | 24 | (defn cluster-by 25 | "Clusters the hashmaps by fields. Fields should be given as vector. Field values 26 | should be numerical. 27 | 28 | Resulting hashmap will be returned with :cluster-id field that identifies the cluster" 29 | ([data fields k] 30 | (cluster-by data fields k 100)) 31 | ([data fields k iterations] 32 | (let [vectors (u/prepare-vectors fields data) 33 | clusters (map :points (cluster vectors k iterations))] 34 | (u/unmeta-clusters clusters)))) 35 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/correlation.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.correlation 2 | (:import [org.apache.commons.math3.stat.correlation PearsonsCorrelation]) 3 | (:require [clojure.math.combinatorics :as combo] 4 | [clojurewerkz.statistiker.transform.fft :as fft] 5 | [clojurewerkz.statistiker.statistics :as s] 6 | )) 7 | 8 | (defn pearsons-correlation 9 | [v1 v2] 10 | (let [instance (PearsonsCorrelation.)] 11 | (.correlation instance (double-array v1) (double-array v2)))) 12 | 13 | (defn best-repetition-patterns 14 | [v correlation] 15 | (->> (for [i (range 2 (inc (/ (count v) 2)))] 16 | (let [partitions (partition i v) 17 | combinations (combo/combinations partitions 2)] 18 | [i (s/mean (map (fn [[v1 v2]] (pearsons-correlation v1 v2)) combinations))])) 19 | 20 | (remove #(or (.isNaN (last %)) (> 0 (last %)))) 21 | (sort-by last >))) 22 | 23 | (defn detrend 24 | [v cycle] 25 | (->> v 26 | (partition cycle) 27 | (map (fn [s] 28 | (let [m (apply min s)] 29 | (map #(- % m) s)))) 30 | (mapcat identity))) 31 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/datasets.clj: -------------------------------------------------------------------------------- 1 | (ns clj.clojurewerkz.statistiker.datasets) 2 | 3 | (def bell-curve-formed 4 | [{:a 4.0254623, :b 531026.0} 5 | {:a 4.02804905, :b 664002.0} 6 | {:a 4.02934242, :b 787079.0} 7 | {:a 4.03128248, :b 984167.0} 8 | {:a 4.03386923, :b 1294546.0} 9 | {:a 4.03580929, :b 1560230.0} 10 | {:a 4.03839603, :b 1887233.0} 11 | {:a 4.0396894, :b 2113240.0} 12 | {:a 4.04162946, :b 2375211.0} 13 | {:a 4.04421621, :b 2687152.0} 14 | {:a 4.04550958, :b 2862644.0} 15 | {:a 4.04744964, :b 3078898.0} 16 | {:a 4.05003639, :b 3327238.0} 17 | {:a 4.05132976, :b 3461228.0} 18 | {:a 4.05326982, :b 3580526.0} 19 | {:a 4.05585657, :b 3576946.0} 20 | {:a 4.05779662, :b 3439750.0} 21 | {:a 4.06038337, :b 3220296.0} 22 | {:a 4.06167674, :b 3070073.0} 23 | {:a 4.0636168, :b 2877648.0} 24 | {:a 4.06620355, :b 2595848.0} 25 | {:a 4.06749692, :b 2390157.0} 26 | {:a 4.06943698, :b 2175960.0} 27 | {:a 4.07202373, :b 1895104.0} 28 | {:a 4.0733171, :b 1687576.0} 29 | {:a 4.07525716, :b 1447024.0} 30 | {:a 4.0778439, :b 1130879.0} 31 | {:a 4.07978396, :b 904900.0} 32 | {:a 4.08237071, :b 717104.0} 33 | {:a 4.08366408, :b 620014.0}]) 34 | 35 | 36 | (def poly-curve-formed 37 | [{:a 32 :b 99.5} 38 | {:a 64 :b 104.8} 39 | {:a 96 :b 108.5} 40 | {:a 118 :b 100} 41 | {:a 126 :b 86} 42 | {:a 144 :b 64} 43 | {:a 152.5 :b 35.3} 44 | {:a 158 :b 15}]) 45 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/distance.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.distance 2 | (:import [org.apache.commons.math3.ml.distance CanberraDistance ChebyshevDistance EuclideanDistance ManhattanDistance])) 3 | 4 | (def euclidean-distance-instance (EuclideanDistance.)) 5 | (def canberra-distance-instance (CanberraDistance.)) 6 | (def chebyshev-distance-instance (ChebyshevDistance.)) 7 | (def manhattan-distance-instance (ManhattanDistance.)) 8 | 9 | (defn euclidean-distance 10 | [a b] 11 | (.compute euclidean-distance-instance (double-array a) (double-array b))) 12 | 13 | (defn canberra-distance 14 | [a b] 15 | (.compute canberra-distance-instance (double-array a) (double-array b))) 16 | 17 | (defn chebyshev-distance 18 | [a b] 19 | (.compute chebyshev-distance-instance (double-array a) (double-array b))) 20 | 21 | (defn manhattan-distance 22 | [a b] 23 | (.compute manhattan-distance-instance (double-array a) (double-array b))) 24 | 25 | (def distance-measure-instances 26 | {:euclidean euclidean-distance-instance 27 | :canberra canberra-distance-instance 28 | :chebyshev chebyshev-distance-instance 29 | :manhattan manhattan-distance-instance}) 30 | 31 | (def distance-measure-fns 32 | {:euclidean euclidean-distance 33 | :canberra canberra-distance 34 | :chebyshev chebyshev-distance 35 | :manhattan manhattan-distance}) 36 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/distribution.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.distribution 2 | (:import [org.apache.commons.math3.distribution 3 | NormalDistribution 4 | LogNormalDistribution 5 | MultivariateNormalDistribution 6 | PoissonDistribution 7 | BinomialDistribution 8 | ChiSquaredDistribution 9 | ExponentialDistribution 10 | GammaDistribution 11 | HypergeometricDistribution])) 12 | 13 | (defn- distribution 14 | ([gen] 15 | ((fn sample [] 16 | (cons (.sample gen) (lazy-seq (sample)))))) 17 | ([gen conv] 18 | ((fn sample [] 19 | (cons (conv (.sample gen)) (lazy-seq (sample))))))) 20 | 21 | (defn normal-distribution 22 | [mean sd] 23 | (distribution (NormalDistribution. mean sd))) 24 | 25 | (defn poisson-distribution 26 | [mean eps max-iter] 27 | (distribution (PoissonDistribution. (double mean) (double eps) (int max-iter)))) 28 | 29 | (defn log-normal-distribution 30 | [scale shape inverse-accuracy] 31 | (distribution (LogNormalDistribution. (double scale) 32 | (double shape) 33 | (double inverse-accuracy)))) 34 | 35 | (defn binomial-distribution 36 | [trials p] 37 | (distribution (BinomialDistribution. (int trials) 38 | (double p)))) 39 | 40 | (defn multivariate-normal-distribution 41 | [means covariances] 42 | (distribution (MultivariateNormalDistribution. (double-array means) 43 | (into-array (map double-array covariances))) 44 | vec)) 45 | 46 | (defn chi-suqared-distribution 47 | [degrees-of-freedom inverse-cum-accuracy] 48 | (distribution (ChiSquaredDistribution. (double degrees-of-freedom) 49 | (double inverse-cum-accuracy)))) 50 | 51 | (defn exponential-distribution 52 | [mean inverse-cum-accuracy] 53 | (distribution (ExponentialDistribution. (double mean) 54 | (double inverse-cum-accuracy)))) 55 | 56 | (defn gamma-distribution 57 | [shape scale] 58 | (distribution (GammaDistribution. (double shape) 59 | (double scale)))) 60 | 61 | (defn hypergeometric-distribution 62 | [population-size number-success sample-size] 63 | (distribution (HypergeometricDistribution. (int population-size) 64 | (int number-success) 65 | (int sample-size)))) 66 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/entropy.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.entropy) 2 | 3 | (defn shannon-entropy 4 | "Shannon entropy measure. 5 | 6 | Larger " 7 | [v] 8 | (let [sum (reduce + v)] 9 | (->> v 10 | (map (fn shannon-entropy-step [i] 11 | (let [pi (/ i sum)] 12 | (* pi (Math/log pi))))) 13 | (reduce +) 14 | (* -1)))) 15 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/fast_math.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.fast-math 2 | (:import [org.apache.commons.math3.util FastMath Precision])) 3 | 4 | (defn exp 5 | [v] 6 | (FastMath/exp (double v))) 7 | 8 | (defn pow 9 | [v power] 10 | (FastMath/pow (double v) power)) 11 | 12 | (defn sqr 13 | [v] 14 | (FastMath/pow (double v) 2)) 15 | 16 | (defn sqrt 17 | [v] 18 | (FastMath/sqrt v)) 19 | 20 | (defn equals 21 | [x y eps] 22 | (Precision/equals x y eps)) 23 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/fitting.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.fitting 2 | (:require [clojurewerkz.statistiker.optimization :as optim] 3 | [clojurewerkz.statistiker.functions :as funk]) 4 | (:import [org.apache.commons.math3.analysis.function Gaussian] 5 | [org.apache.commons.math3.fitting GaussianFitter CurveFitter HarmonicFitter PolynomialFitter])) 6 | 7 | (defn- make-fitter 8 | [fitter-klass v] 9 | (let [inst (eval `(new ~fitter-klass (optim/make-levenberg-marquardt-optimizer)))] 10 | (doseq [[x y] v] 11 | (.addObservedPoint inst x y)) 12 | inst)) 13 | 14 | (defn gaussian-fitter 15 | [v] 16 | (let [fitter (make-fitter GaussianFitter v)] 17 | (vec (.fit fitter)))) 18 | 19 | (defn curve-fitter 20 | [v f initial-guess] 21 | (let [fitter (make-fitter CurveFitter v)] 22 | (vec (.fit fitter f (double-array initial-guess))))) 23 | 24 | (defn polynomial-fitter 25 | [v max-eval initial-guess] 26 | (let [fitter (make-fitter PolynomialFitter v)] 27 | (vec (.fit fitter (int max-eval) (double-array initial-guess))))) 28 | 29 | (defn harmonic-fitter 30 | [v initial-guess] 31 | (let [fitter (make-fitter HarmonicFitter v)] 32 | (vec (.fit fitter (double-array initial-guess))))) 33 | 34 | (defn fit 35 | "Extract x and y from dataset, and compose an approximated, fitted dataset from interpolated points, taking 36 | `steps` points." 37 | [dss x y fitter-ctor function-ctor steps] 38 | (let [prepared (map (fn [i] [(get i x) 39 | (get i y)]) dss) 40 | min-x (reduce min (map (fn [i] (get i x)) dss)) 41 | max-x (reduce max (map (fn [i] (get i x)) dss)) 42 | step (/ (- max-x min-x) steps) 43 | params (fitter-ctor prepared) 44 | f (function-ctor params)] 45 | (mapv 46 | (fn [x-val] 47 | {x x-val y (.value f (double x-val))}) 48 | (take (inc steps) (iterate #(+ % step) min-x))))) 49 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/functions.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.functions 2 | (:require [clojurewerkz.statistiker.fast-math :as fm] 3 | [clojure.core.matrix :as matrix] 4 | [clojure.core.matrix.operators :as ops] 5 | [schema.core :as s]) 6 | 7 | (:import [clojure.lang IFn] 8 | [org.apache.commons.math3.analysis.function Gaussian Gaussian$Parametric HarmonicOscillator 9 | HarmonicOscillator$Parametric] 10 | [org.apache.commons.math3.analysis.polynomials PolynomialFunction PolynomialFunction$Parametric] 11 | [org.apache.commons.math3.analysis MultivariateFunction MultivariateVectorFunction ParametricUnivariateFunction] 12 | [org.apache.commons.math3.optim.nonlinear.scalar ObjectiveFunction ObjectiveFunctionGradient])) 13 | 14 | (defn ^Number line 15 | "Simple linear funciton: 16 | 17 | f(y) = ax + b" 18 | [^Number intercept ^Number slope] 19 | (fn [x] 20 | (+ intercept (* slope x)))) 21 | 22 | (defn fn->multivariate-function 23 | [^IFn f] 24 | (reify MultivariateFunction 25 | (value [_ v] 26 | (apply f (vec v))))) 27 | 28 | (defn fn->multivariate-vector-function 29 | [^IFn f] 30 | (reify MultivariateVectorFunction 31 | (value [_ v] 32 | (double-array 33 | (apply f (vec v)))))) 34 | 35 | (defn objective-function 36 | [^IFn f] 37 | (ObjectiveFunction. (fn->multivariate-function f))) 38 | 39 | (defn objective-function-gradient 40 | [^IFn f] 41 | (ObjectiveFunctionGradient. (fn->multivariate-vector-function f))) 42 | 43 | ;; 44 | ;; Functions 45 | ;; 46 | 47 | (s/defrecord GradientProblem 48 | [^{:s ObjectiveFunction} objective-fn 49 | ^{:s ObjectiveFunctionGradient} objective-fn-gradient]) 50 | 51 | (defn make-gradient-problem 52 | [^IFn objective ^IFn gradient] 53 | (s/validate GradientProblem 54 | (GradientProblem. (objective-function objective) 55 | (objective-function-gradient)))) 56 | 57 | 58 | (defn ^IFn linear-fn 59 | "Linear function for optimizing least squares for linear regression and so forth" 60 | [data] 61 | (fn [^Number intercept ^Number slope] 62 | (let [f (line intercept slope) 63 | res (->> data 64 | (map (fn [[x y]] 65 | (fm/sqr 66 | (- y (f x))))) 67 | (reduce +))] 68 | res))) 69 | 70 | (defn ^GradientProblem linear-problem 71 | [factors target] 72 | (GradientProblem. (objective-function 73 | (fn [& point] 74 | (->> (matrix/e* factors point) 75 | (map (fn [target-i value] 76 | (- value target-i)) 77 | target) 78 | (reduce +)))) 79 | 80 | (objective-function-gradient 81 | (fn [& point] 82 | (let [r (ops/- (matrix/e* factors point) 83 | target)] 84 | (matrix/e* (matrix/transpose target) 85 | r 86 | 2)))))) 87 | 88 | 89 | (defn least-squares-problem 90 | "Least squares problem: https://en.wikipedia.org/wiki/Linear_least_squares_(mathematics)#The_general_problem" 91 | [points] 92 | (let [factors (->> points (map butlast) (map #(cons 1 %))) 93 | target (map last points)] 94 | (GradientProblem. (objective-function 95 | (fn [intercept slope] 96 | (let [f (line intercept slope) 97 | res (->> points 98 | (map (fn [[x y]] 99 | (fm/sqr (- y (f x))))) 100 | (reduce +))] 101 | res))) 102 | 103 | (objective-function-gradient 104 | (fn [& point] 105 | ;; M = (X ^ T * X) 106 | ;; b = (X ^ T * y) 107 | ;; beta = M^-1 * b 108 | (let [ft (matrix/transpose factors) 109 | m! (matrix/inverse (matrix/dot ft factors)) 110 | b (matrix/dot ft target)] 111 | 112 | (ops/- (matrix/mmul m! b) 113 | point))))))) 114 | 115 | ;; (curve-fitter [[1 1] [2 2] [3 3]] (funk/gaussian-function) [0 0 1 ]) 116 | (defn ^Gaussian gaussian-function 117 | ([] 118 | (Gaussian$Parametric.)) 119 | ([[norm mean sigma]] 120 | (Gaussian. norm mean sigma))) 121 | 122 | (defn ^PolynomialFunction polynomial-function 123 | ([] 124 | (PolynomialFunction$Parametric.)) 125 | ([coefficients] 126 | (PolynomialFunction. (double-array coefficients)))) 127 | 128 | (defn ^HarmonicOscillator harmonic-oscillator 129 | ([] 130 | (HarmonicOscillator$Parametric.)) 131 | ([[amplitude omega phase]] 132 | (HarmonicOscillator. (double amplitude) (double omega) (double phase)))) 133 | 134 | (defn wrap-function 135 | "Returns a clojure Fn that wraps Function" 136 | [funk] 137 | (fn [x] 138 | (.value funk (double x)))) 139 | 140 | (defn to-parametric-fn 141 | [identity] 142 | (reify ParametricUnivariateFunction 143 | (value [this x params] 144 | (.value (apply identity params) (double x))) 145 | (gradient [this x params] 146 | (.gradient (apply identity params) (double x))))) 147 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/histograms.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.histograms 2 | (:import [org.apache.commons.math3.random EmpiricalDistribution])) 3 | 4 | (defprotocol Histogram 5 | (add [_ value]) 6 | (get-counts [_])) 7 | 8 | (defn- get-diff 9 | [[i1 i2]] 10 | (- i2 i1)) 11 | 12 | (defn- merge-closest 13 | [m] 14 | (let [[[q1 q2] _] (->> m 15 | keys 16 | sort 17 | (partition 2 1) 18 | (sort-by get-diff)) 19 | k1 (get m q1) 20 | k2 (get m q2)] 21 | (-> m 22 | (dissoc q1) 23 | (dissoc q2) 24 | (assoc 25 | (/ (+ (* q1 k1) (* q2 k2)) 26 | (+ k1 k2)) 27 | (+ k1 k2))))) 28 | 29 | (defn make-numerical-histogram 30 | [max-bins] 31 | (let [bins (atom {})] 32 | (reify Histogram 33 | (add [_ value] 34 | (swap! bins 35 | (fn [bins] 36 | (if-let [prev (get bins value)] 37 | (assoc bins value (inc prev)) 38 | (loop [bins (assoc bins value 1)] 39 | (if (<= (count bins) max-bins) 40 | bins 41 | (recur (merge-closest bins)))))))) 42 | (get-counts [_] (into {} 43 | (mapv (fn [[k v]] [(double k) (double v)]) @bins)))))) 44 | 45 | (defn numerical-histogram 46 | [max-bins vals] 47 | (let [hist (make-numerical-histogram max-bins)] 48 | (doseq [i vals] 49 | (add hist i)) 50 | (get-counts hist))) 51 | 52 | (defn empirical-distribution 53 | [bins data] 54 | (let [emp-d (EmpiricalDistribution. (int bins))] 55 | (.load emp-d (double-array (mapv double data))) 56 | (zipmap 57 | (.getUpperBounds emp-d) 58 | (map #(.getN %) (.getBinStats emp-d))))) 59 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/inference.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.inference 2 | ) 3 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/metrics.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.metrics 2 | (:require [clojure.math.combinatorics :refer [cartesian-product]] 3 | [clojurewerkz.statistiker.entropy :refer [shannon-entropy]] 4 | [clojurewerkz.statistiker.utils :refer [factorial safe-log safe-shannon-entropy]])) 5 | 6 | (defn mutual-information 7 | "The Mutual Information is a measure of the similarity between two labels of the 8 | same data. 9 | 10 | Where `P(i)` is the probability of a random sample occurring in cluster `U_i` and `P'(j)` 11 | is the probability of a random sample occurring in cluster V_j, the Mutual 12 | Information between clusterings U and V is given as: 13 | 14 | MI(U,V)=\\sum_{i=1}^R \\sum_{j=1}^C P(i,j)\\log\\frac{P(i,j)}{P(i)P'(j)} 15 | 16 | It takes: 17 | * sequence of golden standard cluster labels (e.g,. [1 1 2 2 4 4]) 18 | * and a set of predicted labels (e.g., [1 1 1 2 4 4]) 19 | 20 | This metric is independent of the absolute values of the labels: a permutation of the 21 | class or cluster label values won’t change the score value in any way." 22 | [labels-u labels-v] 23 | {:pre [(= (count labels-u) (count labels-v)) 24 | (not (empty? labels-u)) 25 | (not (empty? labels-v)) 26 | (not-any? coll? labels-u) 27 | (not-any? coll? labels-v)]} 28 | (let [freq-u (frequencies labels-u) 29 | freq-v (frequencies labels-v) 30 | freq-combined (->> (map vector labels-u labels-v) 31 | frequencies) 32 | labels-count (count labels-u) 33 | norm-n (zipmap (keys freq-combined) 34 | (map #(/ % labels-count) (vals freq-combined)))] 35 | (->> norm-n 36 | (map (fn [[[i j] frequency]] 37 | (* frequency 38 | (safe-log (/ frequency 39 | (/ (* (freq-u i) (freq-v j)) 40 | (* labels-count labels-count))))))) 41 | (reduce +)))) 42 | 43 | (defn p-contingency-cell 44 | "Calculate probability for one cell of a permuted contingency matrix." 45 | [labels-count lables-u labels-v [i j nij]] 46 | (* (/ nij labels-count) 47 | (safe-log (/ (* labels-count nij) (* (lables-u i) (labels-v j)))) 48 | (/ 49 | (*' (factorial (lables-u i)) 50 | (factorial (labels-v j)) 51 | (factorial (- labels-count (lables-u i))) 52 | (factorial (- labels-count (labels-v j)))) 53 | (*' (factorial labels-count) 54 | (factorial nij) 55 | (factorial (- (lables-u i) nij)) 56 | (factorial (- (labels-v j) nij)) 57 | (factorial (+ (- labels-count (lables-u i) (labels-v j)) nij)))))) 58 | 59 | (defn- cell-triples 60 | "Generates set of indices for permutation model when calculating expected mutual information." 61 | [labels-count freq-u freq-v i j] 62 | (let [start (max (- (+ (freq-u i) (freq-v j)) labels-count) 0) 63 | end (min (freq-u i) (freq-v j))] 64 | (map #(vector i j %) (range start (inc end))))) 65 | 66 | (defn- expected-mututal-information 67 | [labels-u labels-v] 68 | (let [freq-u (frequencies labels-u) 69 | freq-v (frequencies labels-v) 70 | labels-count (count labels-u) 71 | cells (cartesian-product (distinct labels-u) (distinct labels-v))] 72 | (->> cells 73 | (map (fn[[i j]] 74 | (cell-triples labels-count freq-u freq-v i j))) 75 | (reduce concat) 76 | (map #(p-contingency-cell labels-count freq-u freq-v %)) 77 | (reduce +)))) 78 | 79 | (defn adjusted-mutual-information 80 | "Adjusted Mutual Information (AMI) is an adjustment of the Mutual Information (MI) 81 | score to account for chance. 82 | 83 | AMI(U, V) = [MI(U, V) - E(MI(U, V))] / [max(H(U), H(V)) - E(MI(U, V))] 84 | 85 | It takes 86 | 87 | * a sequence of golden standard cluster labels (e.g,. [1 1 2 2 4 4]) 88 | * and a set of predicted labels (e.g., [1 1 1 2 4 4]) 89 | 90 | This metric is independent of the absolute values of the labels: a permutation of the class 91 | or cluster label values won’t change the score value in any way." 92 | [labels-u labels-v] 93 | {:pre [(= (count labels-u) (count labels-v)) 94 | (not-any? coll? labels-u) 95 | (not-any? coll? labels-v)]} 96 | (if (or (empty? labels-u) 97 | (= 1 98 | (count (distinct labels-u)) 99 | (count (distinct labels-v))) ; only one cluster 100 | (= (count labels-u) 101 | (count (distinct labels-u)) 102 | (count (distinct labels-v)))) ; every point is a singleton cluster 103 | 1.0 ; special limit cases 104 | (let [mutual-info (mutual-information labels-u labels-v) 105 | expected-mutual-info (expected-mututal-information labels-u labels-v) 106 | h-labels-u (safe-shannon-entropy (vals (frequencies labels-u))) 107 | h-labels-v (safe-shannon-entropy (vals (frequencies labels-v)))] 108 | (/ (- mutual-info expected-mutual-info) 109 | (- (max h-labels-u h-labels-v) expected-mutual-info))))) 110 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/optimization.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.optimization 2 | (:require [clojurewerkz.statistiker.fast-math :as fm] 3 | [clojurewerkz.statistiker.functions :as funk] 4 | [schema.macros :as sm]) 5 | 6 | (:import [clojure.lang IFn] 7 | [clojurewerkz.statistiker.functions GradientProblem] 8 | [org.apache.commons.math3.optim InitialGuess MaxEval SimpleBounds 9 | OptimizationData SimpleValueChecker PointValuePair] 10 | [org.apache.commons.math3.optim.nonlinear.vector.jacobian LevenbergMarquardtOptimizer] 11 | [org.apache.commons.math3.optim.nonlinear.scalar ObjectiveFunction 12 | ObjectiveFunctionGradient GoalType MultivariateOptimizer] 13 | [ org.apache.commons.math3.optim.nonlinear.scalar.gradient NonLinearConjugateGradientOptimizer 14 | NonLinearConjugateGradientOptimizer$Formula] 15 | [org.apache.commons.math3.optim.nonlinear.scalar.noderiv BOBYQAOptimizer])) 16 | 17 | ;; 18 | ;; Constants 19 | ;; 20 | 21 | (def ^:private goal-types 22 | {:minimize GoalType/MINIMIZE 23 | :maximize GoalType/MAXIMIZE}) 24 | 25 | (def ^:private non-conjugate-gradient-optimizer-formula 26 | {:fletcher-reeves NonLinearConjugateGradientOptimizer$Formula/FLETCHER_REEVES 27 | :polak-ribiere NonLinearConjugateGradientOptimizer$Formula/POLAK_RIBIERE}) 28 | 29 | ;; 30 | ;; Constructors 31 | ;; 32 | 33 | (defn ^LevenbergMarquardtOptimizer make-levenberg-marquardt-optimizer 34 | [] 35 | (LevenbergMarquardtOptimizer.)) 36 | 37 | (defn ^BOBYQAOptimizer make-bobyqa-optimizer 38 | [iteration-points] 39 | (BOBYQAOptimizer. (int iteration-points))) 40 | 41 | (defn ^BOBYQAOptimizer make-bobyqa-optimizer 42 | [iteration-points] 43 | (BOBYQAOptimizer. (int iteration-points))) 44 | 45 | (defn ^NonLinearConjugateGradientOptimizer make-ncg-optimizer 46 | ([formula] 47 | (make-ncg-optimizer formula 1e-6 1e-6)) 48 | ([formula relative-threshold absolute-threshold] 49 | (NonLinearConjugateGradientOptimizer. (sm/safe-get non-conjugate-gradient-optimizer-formula formula) 50 | (SimpleValueChecker. (double relative-threshold) (double absolute-threshold))))) 51 | 52 | 53 | (defn- point->map 54 | [^PointValuePair point] 55 | {:point (vec (.getPoint point)) 56 | :value (.getValue point)}) 57 | 58 | (defn initial-guess 59 | "Receives a vector of numbers to be used as an initial guess" 60 | [guess] 61 | (InitialGuess. (double-array guess))) 62 | 63 | ;; 64 | ;; Optimization Shortcuts 65 | ;; 66 | 67 | 68 | (defn optimize-bobyqa 69 | "Optimize (multivariate) function `f` with Bound Optimization By Quadratic Approximation" 70 | [interpolation-points max-evaluations ^IFn f goal-type guess] 71 | (let [optimizer (make-bobyqa-optimizer 4) ;; Remove hardcoded variable 72 | res (.optimize optimizer 73 | (into-array OptimizationData 74 | [(MaxEval. (int max-evaluations)) 75 | (funk/objective-function f) 76 | (sm/safe-get goal-types goal-type) 77 | (SimpleBounds/unbounded (count guess)) 78 | (initial-guess guess)]))] 79 | (point->map res))) 80 | 81 | 82 | 83 | (defn optimize-non-conjugate-gradient 84 | ([^IFn objective ^IFn gradient ^Number max-evaluations ^clojure.lang.Keyword formula] 85 | (optimize-non-conjugate-gradient (funk/make-gradient-problem objective 86 | gradient) 87 | max-evaluations 88 | formula)) 89 | 90 | ([^GradientProblem problem ^Number max-evaluations ^clojure.lang.Keyword formula] 91 | (let [optim (make-ncg-optimizer formula) 92 | res (.optimize optim 93 | (into-array OptimizationData 94 | [(MaxEval. (int max-evaluations)) 95 | (.objective-fn problem) 96 | (.objective-fn-gradient problem) 97 | (:minimize goal-types) ;; TODO: use safe-get? 98 | (initial-guess [0 0])]))] 99 | (point->map res)))) 100 | 101 | ;; 102 | ;; Implementaion 103 | ;; 104 | 105 | 106 | (defn bobyqa-interpolation-points-avg 107 | [n] 108 | (int (/ (+ (+ n 2) 109 | (/ (* (+ n 1) (+ n 2)) 2)) 110 | 2))) 111 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/regression.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.regression 2 | (:import [org.apache.commons.math3.stat.regression SimpleRegression]) 3 | (:require [clojurewerkz.statistiker.optimization :as optim] 4 | [clojurewerkz.statistiker.functions :as funk])) 5 | 6 | (defn linear-regression 7 | "Performs a linear regression" 8 | ([data] 9 | (linear-regression data first second)) 10 | ([data field1-extractor field2-extractor] 11 | (let [field1-extractor (if (fn? field1-extractor) 12 | field1-extractor 13 | #(get % field1-extractor)) 14 | field2-extractor (if (fn? field2-extractor) 15 | field2-extractor 16 | #(get % field2-extractor)) 17 | regression (SimpleRegression. true) 18 | matrix (into-array (map double-array (map vector 19 | (map field1-extractor data) 20 | (map field2-extractor data))))] 21 | (.addData regression matrix) 22 | {:intercept (.getIntercept regression) 23 | :slope (.getSlope regression)}))) 24 | 25 | (defn linear-regression2 26 | "Linear Regression through linear BOBYQA optimizer. 27 | 28 | We do not recommend using linear-regression2 or linear-regression3, since they yield same results 29 | as a default implementation, although have much poorer performance. They're given here as reference 30 | implementation of linear and gradient optimisation." 31 | ([data initial-guess max-iterations] 32 | (linear-regression2 data 33 | initial-guess 34 | max-iterations 35 | (optim/bobyqa-interpolation-points-avg (count initial-guess)))) 36 | ([data initial-guess max-iterations interpolation-points ] 37 | (let [{:keys [point]} (optim/optimize-bobyqa interpolation-points 38 | max-iterations 39 | (funk/linear-fn data) 40 | :minimize 41 | (double-array initial-guess)) 42 | [intercept slope] point] 43 | {:intercept intercept 44 | :slope slope}))) 45 | 46 | (defn linear-regression3 47 | "Linear Regression through Non-Conjugate Gradient Descent." 48 | [data max-evaluations formula] 49 | (let [problem (funk/least-squares-problem data) 50 | res (optim/optimize-non-conjugate-gradient problem max-evaluations formula) 51 | {:keys [point]} res 52 | [intercept slope] point] 53 | {:intercept intercept 54 | :slope slope})) 55 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/scaling.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.scaling 2 | (:require [clojurewerkz.statistiker.statistics :refer [mean sd]] 3 | [clojurewerkz.statistiker.fast-math :refer [sqr sqrt pow]])) 4 | 5 | (defn make-rescale-range-fn 6 | [x xmin xmax] 7 | (let [orig-min (apply min x) 8 | orig-max (apply max x) 9 | orig-diff (- orig-max orig-min) 10 | diff (- xmax xmin)] 11 | (fn [x] 12 | (double (+ (/ (* (- x orig-min) 13 | diff) 14 | orig-diff) 15 | xmin 16 | ))))) 17 | 18 | (defn make-rescale-fn 19 | [x] 20 | (let [xmin (apply min x) 21 | xmax (apply max x) 22 | diff (- xmax xmin) ] 23 | (if (= (double diff) (double 0)) 24 | (constantly 0.0) 25 | (fn [x] (double (/ (- x xmin) diff)))))) 26 | 27 | (defn rescale 28 | [x] 29 | (mapv (make-rescale-fn x) x)) 30 | 31 | (defn rescale-range 32 | [x xmin xmax] 33 | (mapv (make-rescale-range-fn x xmin xmax) x)) 34 | 35 | (defn make-standartise-fn 36 | [x] 37 | (let [xmean (mean x) 38 | xsd (sd x)] 39 | (fn [x] (double (/ (- x xmean) xsd))))) 40 | 41 | (defn standartise 42 | [x] 43 | (mapv (make-standartise-fn x) x)) 44 | 45 | (defn make-lp-normalize-fn 46 | [p x] 47 | {:pre [(pos? p) 48 | (integer? p)]} 49 | (let 50 | [sum (->> x 51 | (map (comp #(pow % p) #(Math/abs %))) 52 | (reduce +) 53 | (#(pow % (/ 1.0 p))))] 54 | #(/ % sum))) 55 | 56 | (defn lp-normalize 57 | [p x] 58 | (mapv (make-lp-normalize-fn p x) x)) 59 | 60 | (defn l1-normalize 61 | "L1-normalize (divide each element by sum of absolute values)." 62 | [x] 63 | (mapv (make-lp-normalize-fn 1 x) x)) 64 | 65 | (defn l2-normalize 66 | "L2-normalize (divide each element by the square root of the sum of squares)" 67 | [x] 68 | (mapv (make-lp-normalize-fn 2 x) x)) 69 | 70 | (defn scale-feature 71 | ([maps key scaled-key scale-fn-factory] 72 | (let [extracted (map #(get % key) maps) 73 | scale-f (scale-fn-factory extracted) 74 | scaled (map scale-f extracted)] 75 | (map (fn [v scaled] 76 | (assoc v scaled-key scaled)) maps scaled))) 77 | ([maps key scale-fn-factory] 78 | (scale-feature maps key key scale-fn-factory))) 79 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/statistics.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.statistics 2 | (:import [org.apache.commons.math3.stat.descriptive.rank Percentile] 3 | [org.apache.commons.math3.stat.descriptive DescriptiveStatistics] 4 | 5 | [org.apache.commons.math3.stat StatUtils]) 6 | (:require [clojurewerkz.statistiker.fast-math :refer [sqrt pow]])) 7 | 8 | (defn mean 9 | [values] 10 | (let [c (count values)] 11 | (if (> c 0) 12 | (float (/ (reduce + values) (count values))) 13 | 0))) 14 | 15 | (defn variance 16 | [values] 17 | (StatUtils/variance (double-array values))) 18 | 19 | (defn standard-deviation 20 | [values] 21 | (sqrt (variance values))) 22 | 23 | (def sd standard-deviation) 24 | 25 | (defn geometric-mean 26 | [values] 27 | (StatUtils/geometricMean (double-array values))) 28 | 29 | (defn mode 30 | [values] 31 | (vec (StatUtils/mode (double-array values)))) 32 | 33 | (defn normalize 34 | [values] 35 | (vec (StatUtils/normalize (double-array values)))) 36 | 37 | (defn product 38 | [values] 39 | (StatUtils/product (double-array values))) 40 | 41 | (def ^:private percentile-mappings 42 | {:min 1 43 | :max 100 44 | :median 50 45 | :25 25 46 | :75 75}) 47 | 48 | (defn fivenum 49 | [values] 50 | (let [p (Percentile.)] 51 | (.setData p (double-array values)) 52 | (reduce (fn [acc [k v]] 53 | (assoc acc k (.evaluate p (double v)))) 54 | {} 55 | percentile-mappings))) 56 | 57 | (defn iqr 58 | [values] 59 | (let [p (Percentile.) 60 | _ (.setData p (double-array values)) 61 | first-quartile (.evaluate p (double 25)) 62 | third-quartile (.evaluate p (double 75))] 63 | (- third-quartile first-quartile))) 64 | 65 | (defn median 66 | [values] 67 | (let [p (Percentile.) 68 | _ (.setData p (double-array values))] 69 | (.evaluate p (double 50)))) 70 | 71 | (defn percentiles 72 | [values percentiles] 73 | (let [p (Percentile.) 74 | _ (.setData p (double-array values))] 75 | (mapv #(.evaluate p (double %)) percentiles))) 76 | 77 | (defn percentile 78 | [values percentile] 79 | (let [p (Percentile.) 80 | _ (.setData p (double-array values))] 81 | (.evaluate p (double percentile)))) 82 | 83 | 84 | (defn kurtosis 85 | " 86 | n*(n+1) / (n - 1)*(n - 2)*(n-3)] * sum[(x_i - mean)^4 3 * (n - 1)^2 87 | kurtosis = ------------------------------------------------------------ - ---------------------- 88 | std^4 (n - 2) * (n - 3) 89 | " 90 | 91 | [xs] 92 | (let [mu (mean xs) 93 | sigma (sd xs) 94 | n (count xs)] 95 | 96 | (- (* (/ (* n (+ n 1)) 97 | (* (- n 1) (- n 2) (- n 3))) 98 | 99 | (/ (->> xs 100 | (map #(pow (- % mu) 4)) 101 | (reduce +)) 102 | (pow sigma 4))) 103 | 104 | 105 | (/ (* 3 (pow (- n 1) 2)) 106 | (- n 2) (- n 3))))) 107 | 108 | 109 | (defn- kurtosis-theoretical 110 | "One of the ways to calculate kurtosis (biased on smaller data sets)" 111 | [xs] 112 | (let [mu (mean xs) 113 | sigma (variance xs) 114 | n (count xs)] 115 | (- (/ 116 | (/ 117 | (->> xs 118 | (map #(pow (- % mu) 4)) 119 | (reduce +)) 120 | n) 121 | (pow sigma 2)) 122 | 3))) 123 | 124 | (defn kurtosis-math3 125 | "Slow (and memory consuming but correct) way to claculate Kurtosis" 126 | [data] 127 | (.getKurtosis (DescriptiveStatistics. (double-array data)))) 128 | 129 | (defn skewness 130 | " 131 | [n / (n - 1) (n - 2)] * sum[(x_i - mean)^3] 132 | skewness = --------------------------------------------- 133 | std^3 134 | " 135 | [xs] 136 | (let [mu (mean xs) 137 | sigma (sd xs) 138 | n (count xs)] 139 | (/ (* (/ n 140 | (* (- n 1) (- n 2))) 141 | (->> xs 142 | (map #(pow (- % mu) 3)) 143 | (reduce +))) 144 | (pow sigma 3)))) 145 | 146 | (defn central-moment 147 | [xs i] 148 | (let [mu (mean xs) 149 | sigma (sd xs) 150 | n (count xs)] 151 | 152 | (/ (* (/ n ;; That doesn't appear to be correct??? 153 | (apply * (take (dec i) (iterate dec (dec n))))) 154 | (->> xs 155 | (map #(pow (- % mu) i)) 156 | (reduce +))) 157 | (pow sigma i)))) 158 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/time_series/smoothing.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.time-series.smoothing 2 | (:require [clojurewerkz.statistiker.statistics :as s])) 3 | 4 | (defn fixed-size-conj 5 | [size] 6 | (fn fixed-size-conj-fn [v item] 7 | (let [c (count v)] 8 | (if (>= c size) 9 | (conj (subvec v (- c (- size 1)) c) item) 10 | (conj v item))))) 11 | 12 | (defn linear-smooth-stream 13 | "Linear smoothing makes a fitted line " 14 | ([size consume-fn] 15 | (let [conj-fn (fixed-size-conj size) 16 | window (atom [])] 17 | (fn [i] 18 | (swap! window conj-fn i) 19 | (when (= (count @window) size) 20 | (consume-fn (s/mean @window)))))) 21 | ([size consume-fn field] 22 | (let [conj-fn (fixed-size-conj size) 23 | window (atom [])] 24 | (fn [i] 25 | (swap! window conj-fn (get i field)) 26 | (when (= (count @window) size) 27 | (consume-fn (assoc i field (s/mean @window)))))))) 28 | 29 | (defn linear-smooth-seq 30 | ([window lst] 31 | (map s/mean (partition window 1 lst))) 32 | ;; swap lst and field 33 | ([window lst field] 34 | (map (fn [items] 35 | (let [v (map #(get % field) items) 36 | m (s/mean v)] 37 | (assoc (last items) field m))) 38 | (partition window 1 lst)))) 39 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/transform/fft.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.transform.fft 2 | (:import [org.apache.commons.math3.transform DftNormalization] 3 | [org.apache.commons.math3.transform FastFourierTransformer TransformType])) 4 | 5 | (def dft-normalizations 6 | {:standard DftNormalization/STANDARD 7 | :unitary DftNormalization/UNITARY}) 8 | 9 | (def transform-types 10 | {:forward TransformType/FORWARD 11 | :inverse TransformType/INVERSE}) 12 | 13 | (defn +=bit-shift-right 14 | [i n] 15 | (bit-or i (bit-shift-right i n))) 16 | 17 | (defn adjust-to-p2 18 | [num] 19 | (-> num 20 | dec 21 | (+=bit-shift-right 1) 22 | (+=bit-shift-right 2) 23 | (+=bit-shift-right 4) 24 | (+=bit-shift-right 8) 25 | (+=bit-shift-right 16) 26 | inc)) 27 | 28 | (defn zero-fill 29 | [ds new-size] 30 | (if (> new-size (count ds)) 31 | (concat ds (into [] (repeat (- new-size (count ds)) 0))) 32 | ds)) 33 | 34 | (defn transform 35 | [data transform-type dft-normalization] 36 | (let [transformer (FastFourierTransformer. (get dft-normalizations dft-normalization)) 37 | data (zero-fill data (adjust-to-p2 (count data)))] 38 | (map (fn [complex idx] 39 | {:real (.getReal complex) :imaginary (.getImaginary complex) :abs (.abs complex) :idx idx}) 40 | (.transform transformer (double-array (map double data)) (get transform-types transform-type)) 41 | (iterate inc 0)))) 42 | -------------------------------------------------------------------------------- /src/clj/clojurewerkz/statistiker/utils.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.utils 2 | (:import [java.util TreeMap] 3 | [clojurewerkz.statistiker DoublePointWithMeta])) 4 | 5 | (defn- add-unique-id-single 6 | [a b] 7 | (assoc a :unique-id b)) 8 | 9 | (defn add-unique-id 10 | "Adds unique 0-based incremented id field to each map in `coll`. Field 11 | is `:unique-id`." 12 | [coll] 13 | (map add-unique-id-single coll (iterate inc 0))) 14 | 15 | (defn to-sorted-map 16 | [m] 17 | (TreeMap. m)) 18 | 19 | (defn map-groups 20 | [f groups] 21 | (into {} 22 | (for [[group-id items] groups] 23 | [group-id (f items)]))) 24 | 25 | (defn select-keys-order-dependent 26 | [m keys] 27 | (reduce (fn [acc key] (conj acc (get m key))) [] keys)) 28 | 29 | 30 | ;; 31 | ;; Clustering 32 | ;; 33 | 34 | (defn double-point 35 | [nums] 36 | (DoublePointWithMeta. (meta nums) (double-array nums))) 37 | 38 | (defn prepare-vectors 39 | [fields data] 40 | (map #(with-meta (select-keys-order-dependent % fields) %) data)) 41 | 42 | (defn unmeta-clusters 43 | [clusters] 44 | (->> clusters 45 | (map vector (iterate inc (int 0))) 46 | (map (fn [[cluster points]] 47 | (map #(assoc (meta %) :cluster-id cluster) points))) 48 | (mapcat identity))) 49 | 50 | ;; 51 | ;; 52 | ;; 53 | 54 | (defn almost= 55 | "Non-strict equality" 56 | [wat center tolerance] 57 | (and (>= wat (- center tolerance)) 58 | (<= wat (+ center tolerance)))) 59 | 60 | 61 | 62 | ;; 63 | ;; Used by metrics 64 | ;; 65 | 66 | (defn factorial 67 | "Safe factorial. Special case: returns 1 if x is zero" 68 | [x] 69 | {:pre [(>= x 0)]} 70 | (loop [n x f 1] 71 | (if (<= n 1) ; It assigns a value of 1.0 for "(factorial 0)"" 72 | f 73 | (recur (dec n) (*' f n))))) 74 | 75 | (defn safe-log 76 | "Safe-log function. Special case: returns 0 if x is equal to 0, instead of indetermination." 77 | [x] 78 | (if (zero? x) 79 | 0 80 | (Math/log x))) 81 | 82 | (defn safe-shannon-entropy 83 | "Safe Shannon entropy measure (inc. protected-logarithms)." 84 | [v] 85 | (let [sum (reduce + v)] 86 | (->> v 87 | (map (fn shannon-entropy-step [i] 88 | (let [pi (/ i sum)] 89 | (* pi (safe-log pi))))) 90 | (reduce +) 91 | (* -1)))) 92 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/DoublePointWithMeta.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker; 2 | 3 | import org.apache.commons.math3.ml.clustering.DoublePoint; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class DoublePointWithMeta extends DoublePoint { 9 | 10 | private final Map metadata; 11 | 12 | public DoublePointWithMeta(Map metadata, double[] point) { 13 | super(point); 14 | this.metadata = metadata; 15 | } 16 | 17 | public DoublePointWithMeta(Map metadata, int[] point) { 18 | super(point); 19 | this.metadata = metadata; 20 | } 21 | 22 | public Map getMetadata() { 23 | return this.metadata; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/NuSVMSolver.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm; 2 | 3 | import clojurewerkz.statistiker.libsvm.data.QMatrix; 4 | 5 | // 6 | // Solver for nu-SVM classification and regression 7 | // 8 | // additional constraint: e^T \alpha = constant 9 | // 10 | public final class NuSVMSolver extends Solver { 11 | private SolutionInfo si; 12 | 13 | void Solve(int l, QMatrix Q, double[] p, byte[] y, 14 | double[] alpha, double Cp, double Cn, double eps, 15 | SolutionInfo si, int shrinking) { 16 | this.si = si; 17 | super.Solve(l, Q, p, y, alpha, Cp, Cn, eps, si, shrinking); 18 | } 19 | 20 | // return 1 if already optimal, return 0 otherwise 21 | int select_working_set(int[] working_set) { 22 | // return i,j such that y_i = y_j and 23 | // i: maximizes -y_i * grad(f)_i, i in I_up(\alpha) 24 | // j: minimizes the decrease of obj value 25 | // (if quadratic coefficeint <= 0, replace it with tau) 26 | // -y_j*grad(f)_j < -y_i*grad(f)_i, j in I_low(\alpha) 27 | 28 | double Gmaxp = -INF; 29 | double Gmaxp2 = -INF; 30 | int Gmaxp_idx = -1; 31 | 32 | double Gmaxn = -INF; 33 | double Gmaxn2 = -INF; 34 | int Gmaxn_idx = -1; 35 | 36 | int Gmin_idx = -1; 37 | double obj_diff_min = INF; 38 | 39 | for (int t = 0; t < active_size; t++) 40 | if (y[t] == +1) { 41 | if (!is_upper_bound(t)) 42 | if (-G[t] >= Gmaxp) { 43 | Gmaxp = -G[t]; 44 | Gmaxp_idx = t; 45 | } 46 | } else { 47 | if (!is_lower_bound(t)) 48 | if (G[t] >= Gmaxn) { 49 | Gmaxn = G[t]; 50 | Gmaxn_idx = t; 51 | } 52 | } 53 | 54 | int ip = Gmaxp_idx; 55 | int in = Gmaxn_idx; 56 | float[] Q_ip = null; 57 | float[] Q_in = null; 58 | if (ip != -1) // null Q_ip not accessed: Gmaxp=-INF if ip=-1 59 | Q_ip = Q.get_Q(ip, active_size); 60 | if (in != -1) 61 | Q_in = Q.get_Q(in, active_size); 62 | 63 | for (int j = 0; j < active_size; j++) { 64 | if (y[j] == +1) { 65 | if (!is_lower_bound(j)) { 66 | double grad_diff = Gmaxp + G[j]; 67 | if (G[j] >= Gmaxp2) 68 | Gmaxp2 = G[j]; 69 | if (grad_diff > 0) { 70 | double obj_diff; 71 | double quad_coef = QD[ip] + QD[j] - 2 * Q_ip[j]; 72 | if (quad_coef > 0) 73 | obj_diff = -(grad_diff * grad_diff) / quad_coef; 74 | else 75 | obj_diff = -(grad_diff * grad_diff) / 1e-12; 76 | 77 | if (obj_diff <= obj_diff_min) { 78 | Gmin_idx = j; 79 | obj_diff_min = obj_diff; 80 | } 81 | } 82 | } 83 | } else { 84 | if (!is_upper_bound(j)) { 85 | double grad_diff = Gmaxn - G[j]; 86 | if (-G[j] >= Gmaxn2) 87 | Gmaxn2 = -G[j]; 88 | if (grad_diff > 0) { 89 | double obj_diff; 90 | double quad_coef = QD[in] + QD[j] - 2 * Q_in[j]; 91 | if (quad_coef > 0) 92 | obj_diff = -(grad_diff * grad_diff) / quad_coef; 93 | else 94 | obj_diff = -(grad_diff * grad_diff) / 1e-12; 95 | 96 | if (obj_diff <= obj_diff_min) { 97 | Gmin_idx = j; 98 | obj_diff_min = obj_diff; 99 | } 100 | } 101 | } 102 | } 103 | } 104 | 105 | if (Math.max(Gmaxp + Gmaxp2, Gmaxn + Gmaxn2) < eps) 106 | return 1; 107 | 108 | if (y[Gmin_idx] == +1) 109 | working_set[0] = Gmaxp_idx; 110 | else 111 | working_set[0] = Gmaxn_idx; 112 | working_set[1] = Gmin_idx; 113 | 114 | return 0; 115 | } 116 | 117 | private boolean be_shrunk(int i, double Gmax1, double Gmax2, double Gmax3, double Gmax4) { 118 | if (is_upper_bound(i)) { 119 | if (y[i] == +1) 120 | return (-G[i] > Gmax1); 121 | else 122 | return (-G[i] > Gmax4); 123 | } else if (is_lower_bound(i)) { 124 | if (y[i] == +1) 125 | return (G[i] > Gmax2); 126 | else 127 | return (G[i] > Gmax3); 128 | } else 129 | return (false); 130 | } 131 | 132 | void do_shrinking() { 133 | double Gmax1 = -INF; // max { -y_i * grad(f)_i | y_i = +1, i in I_up(\alpha) } 134 | double Gmax2 = -INF; // max { y_i * grad(f)_i | y_i = +1, i in I_low(\alpha) } 135 | double Gmax3 = -INF; // max { -y_i * grad(f)_i | y_i = -1, i in I_up(\alpha) } 136 | double Gmax4 = -INF; // max { y_i * grad(f)_i | y_i = -1, i in I_low(\alpha) } 137 | 138 | // find maximal violating pair first 139 | int i; 140 | for (i = 0; i < active_size; i++) { 141 | if (!is_upper_bound(i)) { 142 | if (y[i] == +1) { 143 | if (-G[i] > Gmax1) Gmax1 = -G[i]; 144 | } else if (-G[i] > Gmax4) Gmax4 = -G[i]; 145 | } 146 | if (!is_lower_bound(i)) { 147 | if (y[i] == +1) { 148 | if (G[i] > Gmax2) Gmax2 = G[i]; 149 | } else if (G[i] > Gmax3) Gmax3 = G[i]; 150 | } 151 | } 152 | 153 | if (unshrink == false && Math.max(Gmax1 + Gmax2, Gmax3 + Gmax4) <= eps * 10) { 154 | unshrink = true; 155 | reconstruct_gradient(); 156 | active_size = l; 157 | } 158 | 159 | for (i = 0; i < active_size; i++) 160 | if (be_shrunk(i, Gmax1, Gmax2, Gmax3, Gmax4)) { 161 | active_size--; 162 | while (active_size > i) { 163 | if (!be_shrunk(active_size, Gmax1, Gmax2, Gmax3, Gmax4)) { 164 | swap_index(i, active_size); 165 | break; 166 | } 167 | active_size--; 168 | } 169 | } 170 | } 171 | 172 | double calculate_rho() { 173 | int nr_free1 = 0, nr_free2 = 0; 174 | double ub1 = INF, ub2 = INF; 175 | double lb1 = -INF, lb2 = -INF; 176 | double sum_free1 = 0, sum_free2 = 0; 177 | 178 | for (int i = 0; i < active_size; i++) { 179 | if (y[i] == +1) { 180 | if (is_lower_bound(i)) 181 | ub1 = Math.min(ub1, G[i]); 182 | else if (is_upper_bound(i)) 183 | lb1 = Math.max(lb1, G[i]); 184 | else { 185 | ++nr_free1; 186 | sum_free1 += G[i]; 187 | } 188 | } else { 189 | if (is_lower_bound(i)) 190 | ub2 = Math.min(ub2, G[i]); 191 | else if (is_upper_bound(i)) 192 | lb2 = Math.max(lb2, G[i]); 193 | else { 194 | ++nr_free2; 195 | sum_free2 += G[i]; 196 | } 197 | } 198 | } 199 | 200 | double r1, r2; 201 | if (nr_free1 > 0) 202 | r1 = sum_free1 / nr_free1; 203 | else 204 | r1 = (ub1 + lb1) / 2; 205 | 206 | if (nr_free2 > 0) 207 | r2 = sum_free2 / nr_free2; 208 | else 209 | r2 = (ub2 + lb2) / 2; 210 | 211 | si.r = (r1 + r2) / 2; 212 | return (r1 - r2) / 2; 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/Solver.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm; 2 | 3 | import clojurewerkz.statistiker.libsvm.data.QMatrix; 4 | 5 | // An SMO algorithm in Fan et al., JMLR 6(2005), p. 1889--1918 6 | // Solves: 7 | // 8 | // min 0.5(\alpha^T Q \alpha) + p^T \alpha 9 | // 10 | // labels^T \alpha = \delta 11 | // y_i = +1 or -1 12 | // 0 <= alpha_i <= Cp for y_i = 1 13 | // 0 <= alpha_i <= Cn for y_i = -1 14 | // 15 | // Given: 16 | // 17 | // Q, p, labels, Cp, Cn, and an initial feasible point \alpha 18 | // length is the size of vectors and matrices 19 | // eps is the stopping tolerance 20 | // 21 | // solution will be put in \alpha, objective value will be put in obj 22 | // 23 | public class Solver { 24 | int active_size; 25 | byte[] y; 26 | double[] G; // gradient of objective function 27 | static final byte LOWER_BOUND = 0; 28 | static final byte UPPER_BOUND = 1; 29 | static final byte FREE = 2; 30 | byte[] alpha_status; // LOWER_BOUND, UPPER_BOUND, FREE 31 | double[] alpha; 32 | QMatrix Q; 33 | double[] QD; 34 | double eps; 35 | double Cp, Cn; 36 | double[] p; 37 | int[] active_set; 38 | double[] G_bar; // gradient, if we treat free variables as 0 39 | int l; 40 | boolean unshrink; // XXX 41 | 42 | static final double INF = Double.POSITIVE_INFINITY; 43 | 44 | double get_C(int i) { 45 | return (y[i] > 0) ? Cp : Cn; 46 | } 47 | 48 | void update_alpha_status(int i) { 49 | if (alpha[i] >= get_C(i)) 50 | alpha_status[i] = UPPER_BOUND; 51 | else if (alpha[i] <= 0) 52 | alpha_status[i] = LOWER_BOUND; 53 | else alpha_status[i] = FREE; 54 | } 55 | 56 | boolean is_upper_bound(int i) { 57 | return alpha_status[i] == UPPER_BOUND; 58 | } 59 | 60 | boolean is_lower_bound(int i) { 61 | return alpha_status[i] == LOWER_BOUND; 62 | } 63 | 64 | boolean is_free(int i) { 65 | return alpha_status[i] == FREE; 66 | } 67 | 68 | // java: information about solution except alpha, 69 | // because we cannot return multiple values otherwise... 70 | static class SolutionInfo { 71 | double obj; 72 | double rho; 73 | double upper_bound_p; 74 | double upper_bound_n; 75 | double r; // for NuSVMSolver 76 | } 77 | 78 | void swap_index(int i, int j) { 79 | Q.swap_index(i, j); 80 | do { 81 | byte _ = y[i]; 82 | y[i] = y[j]; 83 | y[j] = _; 84 | } while (false); 85 | do { 86 | double _ = G[i]; 87 | G[i] = G[j]; 88 | G[j] = _; 89 | } while (false); 90 | do { 91 | byte _ = alpha_status[i]; 92 | alpha_status[i] = alpha_status[j]; 93 | alpha_status[j] = _; 94 | } while (false); 95 | do { 96 | double _ = alpha[i]; 97 | alpha[i] = alpha[j]; 98 | alpha[j] = _; 99 | } while (false); 100 | do { 101 | double _ = p[i]; 102 | p[i] = p[j]; 103 | p[j] = _; 104 | } while (false); 105 | do { 106 | int _ = active_set[i]; 107 | active_set[i] = active_set[j]; 108 | active_set[j] = _; 109 | } while (false); 110 | do { 111 | double _ = G_bar[i]; 112 | G_bar[i] = G_bar[j]; 113 | G_bar[j] = _; 114 | } while (false); 115 | } 116 | 117 | void reconstruct_gradient() { 118 | // reconstruct inactive elements of G from G_bar and free variables 119 | 120 | if (active_size == l) return; 121 | 122 | int i, j; 123 | int nr_free = 0; 124 | 125 | for (j = active_size; j < l; j++) 126 | G[j] = G_bar[j] + p[j]; 127 | 128 | for (j = 0; j < active_size; j++) 129 | if (is_free(j)) 130 | nr_free++; 131 | 132 | if (2 * nr_free < active_size) 133 | SVM.info("\nWARNING: using -h 0 may be faster\n"); 134 | 135 | if (nr_free * l > 2 * active_size * (l - active_size)) { 136 | for (i = active_size; i < l; i++) { 137 | float[] Q_i = Q.get_Q(i, active_size); 138 | for (j = 0; j < active_size; j++) 139 | if (is_free(j)) 140 | G[i] += alpha[j] * Q_i[j]; 141 | } 142 | } else { 143 | for (i = 0; i < active_size; i++) 144 | if (is_free(i)) { 145 | float[] Q_i = Q.get_Q(i, l); 146 | double alpha_i = alpha[i]; 147 | for (j = active_size; j < l; j++) 148 | G[j] += alpha_i * Q_i[j]; 149 | } 150 | } 151 | } 152 | 153 | void Solve(int l, QMatrix Q, double[] p_, byte[] y_, 154 | double[] alpha_, double Cp, double Cn, double eps, SolutionInfo si, int shrinking) { 155 | this.l = l; 156 | this.Q = Q; 157 | QD = Q.get_QD(); 158 | p = (double[]) p_.clone(); 159 | y = (byte[]) y_.clone(); 160 | alpha = (double[]) alpha_.clone(); 161 | this.Cp = Cp; 162 | this.Cn = Cn; 163 | this.eps = eps; 164 | this.unshrink = false; 165 | 166 | // initialize alpha_status 167 | { 168 | alpha_status = new byte[l]; 169 | for (int i = 0; i < l; i++) 170 | update_alpha_status(i); 171 | } 172 | 173 | // initialize active set (for shrinking) 174 | { 175 | active_set = new int[l]; 176 | for (int i = 0; i < l; i++) 177 | active_set[i] = i; 178 | active_size = l; 179 | } 180 | 181 | // initialize gradient 182 | { 183 | G = new double[l]; 184 | G_bar = new double[l]; 185 | int i; 186 | for (i = 0; i < l; i++) { 187 | G[i] = p[i]; 188 | G_bar[i] = 0; 189 | } 190 | for (i = 0; i < l; i++) 191 | if (!is_lower_bound(i)) { 192 | float[] Q_i = Q.get_Q(i, l); 193 | double alpha_i = alpha[i]; 194 | int j; 195 | for (j = 0; j < l; j++) 196 | G[j] += alpha_i * Q_i[j]; 197 | if (is_upper_bound(i)) 198 | for (j = 0; j < l; j++) 199 | G_bar[j] += get_C(i) * Q_i[j]; 200 | } 201 | } 202 | 203 | // optimization step 204 | 205 | int iter = 0; 206 | int max_iter = Math.max(10000000, l > Integer.MAX_VALUE / 100 ? Integer.MAX_VALUE : 100 * l); 207 | int counter = Math.min(l, 1000) + 1; 208 | int[] working_set = new int[2]; 209 | 210 | while (iter < max_iter) { 211 | // show progress and do shrinking 212 | 213 | if (--counter == 0) { 214 | counter = Math.min(l, 1000); 215 | if (shrinking != 0) do_shrinking(); 216 | SVM.info("."); 217 | } 218 | 219 | if (select_working_set(working_set) != 0) { 220 | // reconstruct the whole gradient 221 | reconstruct_gradient(); 222 | // reset active set size and check 223 | active_size = l; 224 | SVM.info("*"); 225 | if (select_working_set(working_set) != 0) 226 | break; 227 | else 228 | counter = 1; // do shrinking next iteration 229 | } 230 | 231 | int i = working_set[0]; 232 | int j = working_set[1]; 233 | 234 | ++iter; 235 | 236 | // update alpha[i] and alpha[j], handle bounds carefully 237 | 238 | float[] Q_i = Q.get_Q(i, active_size); 239 | float[] Q_j = Q.get_Q(j, active_size); 240 | 241 | double C_i = get_C(i); 242 | double C_j = get_C(j); 243 | 244 | double old_alpha_i = alpha[i]; 245 | double old_alpha_j = alpha[j]; 246 | 247 | if (y[i] != y[j]) { 248 | double quad_coef = QD[i] + QD[j] + 2 * Q_i[j]; 249 | if (quad_coef <= 0) 250 | quad_coef = 1e-12; 251 | double delta = (-G[i] - G[j]) / quad_coef; 252 | double diff = alpha[i] - alpha[j]; 253 | alpha[i] += delta; 254 | alpha[j] += delta; 255 | 256 | if (diff > 0) { 257 | if (alpha[j] < 0) { 258 | alpha[j] = 0; 259 | alpha[i] = diff; 260 | } 261 | } else { 262 | if (alpha[i] < 0) { 263 | alpha[i] = 0; 264 | alpha[j] = -diff; 265 | } 266 | } 267 | if (diff > C_i - C_j) { 268 | if (alpha[i] > C_i) { 269 | alpha[i] = C_i; 270 | alpha[j] = C_i - diff; 271 | } 272 | } else { 273 | if (alpha[j] > C_j) { 274 | alpha[j] = C_j; 275 | alpha[i] = C_j + diff; 276 | } 277 | } 278 | } else { 279 | double quad_coef = QD[i] + QD[j] - 2 * Q_i[j]; 280 | if (quad_coef <= 0) 281 | quad_coef = 1e-12; 282 | double delta = (G[i] - G[j]) / quad_coef; 283 | double sum = alpha[i] + alpha[j]; 284 | alpha[i] -= delta; 285 | alpha[j] += delta; 286 | 287 | if (sum > C_i) { 288 | if (alpha[i] > C_i) { 289 | alpha[i] = C_i; 290 | alpha[j] = sum - C_i; 291 | } 292 | } else { 293 | if (alpha[j] < 0) { 294 | alpha[j] = 0; 295 | alpha[i] = sum; 296 | } 297 | } 298 | if (sum > C_j) { 299 | if (alpha[j] > C_j) { 300 | alpha[j] = C_j; 301 | alpha[i] = sum - C_j; 302 | } 303 | } else { 304 | if (alpha[i] < 0) { 305 | alpha[i] = 0; 306 | alpha[j] = sum; 307 | } 308 | } 309 | } 310 | 311 | // update G 312 | 313 | double delta_alpha_i = alpha[i] - old_alpha_i; 314 | double delta_alpha_j = alpha[j] - old_alpha_j; 315 | 316 | for (int k = 0; k < active_size; k++) { 317 | G[k] += Q_i[k] * delta_alpha_i + Q_j[k] * delta_alpha_j; 318 | } 319 | 320 | // update alpha_status and G_bar 321 | 322 | { 323 | boolean ui = is_upper_bound(i); 324 | boolean uj = is_upper_bound(j); 325 | update_alpha_status(i); 326 | update_alpha_status(j); 327 | int k; 328 | if (ui != is_upper_bound(i)) { 329 | Q_i = Q.get_Q(i, l); 330 | if (ui) 331 | for (k = 0; k < l; k++) 332 | G_bar[k] -= C_i * Q_i[k]; 333 | else 334 | for (k = 0; k < l; k++) 335 | G_bar[k] += C_i * Q_i[k]; 336 | } 337 | 338 | if (uj != is_upper_bound(j)) { 339 | Q_j = Q.get_Q(j, l); 340 | if (uj) 341 | for (k = 0; k < l; k++) 342 | G_bar[k] -= C_j * Q_j[k]; 343 | else 344 | for (k = 0; k < l; k++) 345 | G_bar[k] += C_j * Q_j[k]; 346 | } 347 | } 348 | 349 | } 350 | 351 | if (iter >= max_iter) { 352 | if (active_size < l) { 353 | // reconstruct the whole gradient to calculate objective value 354 | reconstruct_gradient(); 355 | active_size = l; 356 | SVM.info("*"); 357 | } 358 | SVM.info("\nWARNING: reaching max number of iterations"); 359 | } 360 | 361 | // calculate rho 362 | 363 | si.rho = calculate_rho(); 364 | 365 | // calculate objective value 366 | { 367 | double v = 0; 368 | int i; 369 | for (i = 0; i < l; i++) 370 | v += alpha[i] * (G[i] + p[i]); 371 | 372 | si.obj = v / 2; 373 | } 374 | 375 | // put back the solution 376 | { 377 | for (int i = 0; i < l; i++) 378 | alpha_[active_set[i]] = alpha[i]; 379 | } 380 | 381 | si.upper_bound_p = Cp; 382 | si.upper_bound_n = Cn; 383 | 384 | SVM.info("\noptimization finished, #iter = " + iter + "\n"); 385 | } 386 | 387 | // return 1 if already optimal, return 0 otherwise 388 | int select_working_set(int[] working_set) { 389 | // return i,j such that 390 | // i: maximizes -y_i * grad(f)_i, i in I_up(\alpha) 391 | // j: mimimizes the decrease of obj value 392 | // (if quadratic coefficeint <= 0, replace it with tau) 393 | // -y_j*grad(f)_j < -y_i*grad(f)_i, j in I_low(\alpha) 394 | 395 | double Gmax = -INF; 396 | double Gmax2 = -INF; 397 | int Gmax_idx = -1; 398 | int Gmin_idx = -1; 399 | double obj_diff_min = INF; 400 | 401 | for (int t = 0; t < active_size; t++) 402 | if (y[t] == +1) { 403 | if (!is_upper_bound(t)) 404 | if (-G[t] >= Gmax) { 405 | Gmax = -G[t]; 406 | Gmax_idx = t; 407 | } 408 | } else { 409 | if (!is_lower_bound(t)) 410 | if (G[t] >= Gmax) { 411 | Gmax = G[t]; 412 | Gmax_idx = t; 413 | } 414 | } 415 | 416 | int i = Gmax_idx; 417 | float[] Q_i = null; 418 | if (i != -1) // null Q_i not accessed: Gmax=-INF if i=-1 419 | Q_i = Q.get_Q(i, active_size); 420 | 421 | for (int j = 0; j < active_size; j++) { 422 | if (y[j] == +1) { 423 | if (!is_lower_bound(j)) { 424 | double grad_diff = Gmax + G[j]; 425 | if (G[j] >= Gmax2) 426 | Gmax2 = G[j]; 427 | if (grad_diff > 0) { 428 | double obj_diff; 429 | double quad_coef = QD[i] + QD[j] - 2.0 * y[i] * Q_i[j]; 430 | if (quad_coef > 0) 431 | obj_diff = -(grad_diff * grad_diff) / quad_coef; 432 | else 433 | obj_diff = -(grad_diff * grad_diff) / 1e-12; 434 | 435 | if (obj_diff <= obj_diff_min) { 436 | Gmin_idx = j; 437 | obj_diff_min = obj_diff; 438 | } 439 | } 440 | } 441 | } else { 442 | if (!is_upper_bound(j)) { 443 | double grad_diff = Gmax - G[j]; 444 | if (-G[j] >= Gmax2) 445 | Gmax2 = -G[j]; 446 | if (grad_diff > 0) { 447 | double obj_diff; 448 | double quad_coef = QD[i] + QD[j] + 2.0 * y[i] * Q_i[j]; 449 | if (quad_coef > 0) 450 | obj_diff = -(grad_diff * grad_diff) / quad_coef; 451 | else 452 | obj_diff = -(grad_diff * grad_diff) / 1e-12; 453 | 454 | if (obj_diff <= obj_diff_min) { 455 | Gmin_idx = j; 456 | obj_diff_min = obj_diff; 457 | } 458 | } 459 | } 460 | } 461 | } 462 | 463 | if (Gmax + Gmax2 < eps) 464 | return 1; 465 | 466 | working_set[0] = Gmax_idx; 467 | working_set[1] = Gmin_idx; 468 | return 0; 469 | } 470 | 471 | private boolean be_shrunk(int i, double Gmax1, double Gmax2) { 472 | if (is_upper_bound(i)) { 473 | if (y[i] == +1) 474 | return (-G[i] > Gmax1); 475 | else 476 | return (-G[i] > Gmax2); 477 | } else if (is_lower_bound(i)) { 478 | if (y[i] == +1) 479 | return (G[i] > Gmax2); 480 | else 481 | return (G[i] > Gmax1); 482 | } else 483 | return (false); 484 | } 485 | 486 | void do_shrinking() { 487 | int i; 488 | double Gmax1 = -INF; // max { -y_i * grad(f)_i | i in I_up(\alpha) } 489 | double Gmax2 = -INF; // max { y_i * grad(f)_i | i in I_low(\alpha) } 490 | 491 | // find maximal violating pair first 492 | for (i = 0; i < active_size; i++) { 493 | if (y[i] == +1) { 494 | if (!is_upper_bound(i)) { 495 | if (-G[i] >= Gmax1) 496 | Gmax1 = -G[i]; 497 | } 498 | if (!is_lower_bound(i)) { 499 | if (G[i] >= Gmax2) 500 | Gmax2 = G[i]; 501 | } 502 | } else { 503 | if (!is_upper_bound(i)) { 504 | if (-G[i] >= Gmax2) 505 | Gmax2 = -G[i]; 506 | } 507 | if (!is_lower_bound(i)) { 508 | if (G[i] >= Gmax1) 509 | Gmax1 = G[i]; 510 | } 511 | } 512 | } 513 | 514 | if (unshrink == false && Gmax1 + Gmax2 <= eps * 10) { 515 | unshrink = true; 516 | reconstruct_gradient(); 517 | active_size = l; 518 | } 519 | 520 | for (i = 0; i < active_size; i++) 521 | if (be_shrunk(i, Gmax1, Gmax2)) { 522 | active_size--; 523 | while (active_size > i) { 524 | if (!be_shrunk(active_size, Gmax1, Gmax2)) { 525 | swap_index(i, active_size); 526 | break; 527 | } 528 | active_size--; 529 | } 530 | } 531 | } 532 | 533 | double calculate_rho() { 534 | double r; 535 | int nr_free = 0; 536 | double ub = INF, lb = -INF, sum_free = 0; 537 | for (int i = 0; i < active_size; i++) { 538 | double yG = y[i] * G[i]; 539 | 540 | if (is_lower_bound(i)) { 541 | if (y[i] > 0) 542 | ub = Math.min(ub, yG); 543 | else 544 | lb = Math.max(lb, yG); 545 | } else if (is_upper_bound(i)) { 546 | if (y[i] < 0) 547 | ub = Math.min(ub, yG); 548 | else 549 | lb = Math.max(lb, yG); 550 | } else { 551 | ++nr_free; 552 | sum_free += yG; 553 | } 554 | } 555 | 556 | if (nr_free > 0) 557 | r = sum_free / nr_free; 558 | else 559 | r = (ub + lb) / 2; 560 | 561 | return r; 562 | } 563 | 564 | } 565 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/data/KernelType.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.data; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | 6 | public class KernelType { 7 | public static final int LINEAR = 0; 8 | public static final int POLY = 1; 9 | public static final int RBF = 2; 10 | public static final int SIGMOID = 3; 11 | public static final int PRECOMPUTED = 4; 12 | 13 | public void main() { 14 | try { 15 | Files.createTempDirectory(""); 16 | } catch (IOException e) { 17 | e.printStackTrace(); 18 | } 19 | 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/data/QMatrix.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.data; 2 | 3 | // 4 | // KernelType evaluation 5 | // 6 | // the static method k_function is for doing single kernel evaluation 7 | // the constructor of KernelType prepares to calculate the length*length kernel matrix 8 | // the member function get_Q is for getting one column from the Q Matrix 9 | // 10 | public abstract class QMatrix { 11 | public abstract float[] get_Q(int column, int len); 12 | public abstract double[] get_QD(); 13 | public abstract void swap_index(int i, int j); 14 | } 15 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/data/SvmModel.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.data; 2 | 3 | import clojurewerkz.statistiker.libsvm.data.SvmNode; 4 | import clojurewerkz.statistiker.libsvm.data.SvmParameter; 5 | 6 | /** 7 | * SVM Model 8 | */ 9 | public class SvmModel implements java.io.Serializable { 10 | public SvmParameter param; // parameter 11 | public int nr_class; // number of classes, = 2 in regression/one class SVM 12 | public int l; // total #SV 13 | public SvmNode[][] SV; // SVs (SV[length]) 14 | public double[][] sv_coef; // coefficients for SVs in decision functions (sv_coef[k-1][length]) 15 | public double[] rho; // constants in decision functions (rho[k*(k-1)/2]) 16 | public double[] probA; // pariwise probability information 17 | public double[] probB; 18 | 19 | // for classification only 20 | 21 | public int[] label; // label of each class (label[k]) 22 | public int[] nSV; // number of SVs for each class (nSV[k]) 23 | // nSV[0] + nSV[1] + ... + nSV[k-1] = length 24 | }; 25 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/data/SvmNode.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.data; 2 | 3 | public class SvmNode implements java.io.Serializable { 4 | public final int index; 5 | public final double value; 6 | 7 | public SvmNode(int index, double value) { 8 | this.index = index; 9 | this.value = value; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/data/SvmParameter.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.data; 2 | 3 | public class SvmParameter implements Cloneable, java.io.Serializable { 4 | public int svm_type; 5 | public int kernel_type; 6 | public int degree; // for poly 7 | public double gamma; // for poly/rbf/sigmoid 8 | public double coef0; // for poly/sigmoid 9 | 10 | // these are for training only 11 | public double cache_size; // in MB 12 | public double eps; // stopping criteria 13 | public double C; // for C_SVC, EPSILON_SVR and NU_SVR 14 | public int nr_weight; // for C_SVC 15 | public int[] weight_label; // for C_SVC 16 | public double[] weight; // for C_SVC 17 | public double nu; // for NU_SVC, ONE_CLASS, and NU_SVR 18 | public double p; // for EPSILON_SVR 19 | public int shrinking; // use the shrinking heuristics 20 | public int probability; // do probability estimates 21 | 22 | public Object clone() { 23 | try { 24 | return super.clone(); 25 | } catch (CloneNotSupportedException e) { 26 | return null; 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/data/SvmProblem.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.data; 2 | 3 | public class SvmProblem implements java.io.Serializable { 4 | public final int length; 5 | public final double[] labels; 6 | // TODO: get rid of SvmNode and indexes 7 | public final SvmNode[][] datapoints; 8 | 9 | public SvmProblem(double[] labels, SvmNode[][] datapoints) { 10 | assert(labels.length == datapoints.length); 11 | this.datapoints = datapoints; 12 | this.labels = labels; 13 | this.length = labels.length; 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/data/SvmType.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.data; 2 | 3 | public class SvmType { 4 | public static final int C_SVC = 0; 5 | public static final int NU_SVC = 1; 6 | public static final int ONE_CLASS = 2; 7 | public static final int EPSILON_SVR = 3; 8 | public static final int NU_SVR = 4; 9 | } 10 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/kernel/Kernel.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.kernel; 2 | 3 | import clojurewerkz.statistiker.libsvm.data.KernelType; 4 | import clojurewerkz.statistiker.libsvm.data.QMatrix; 5 | import clojurewerkz.statistiker.libsvm.data.SvmNode; 6 | import clojurewerkz.statistiker.libsvm.data.SvmParameter; 7 | 8 | public abstract class Kernel extends QMatrix { 9 | private SvmNode[][] x; 10 | private final double[] x_square; 11 | 12 | // SvmParameter 13 | private final int kernel_type; 14 | private final int degree; 15 | private final double gamma; 16 | private final double coef0; 17 | 18 | @Override 19 | public void swap_index(int i, int j) { 20 | do { 21 | SvmNode[] _ = x[i]; 22 | x[i] = x[j]; 23 | x[j] = _; 24 | } while (false); 25 | if (x_square != null) do { 26 | double _ = x_square[i]; 27 | x_square[i] = x_square[j]; 28 | x_square[j] = _; 29 | } while (false); 30 | } 31 | 32 | private static double powi(double base, int times) { 33 | double tmp = base, ret = 1.0; 34 | 35 | for (int t = times; t > 0; t /= 2) { 36 | if (t % 2 == 1) ret *= tmp; 37 | tmp = tmp * tmp; 38 | } 39 | return ret; 40 | } 41 | 42 | double kernel_function(int i, int j) { 43 | switch (kernel_type) { 44 | case KernelType.LINEAR: 45 | return dot(x[i], x[j]); 46 | case KernelType.POLY: 47 | return powi(gamma * dot(x[i], x[j]) + coef0, degree); 48 | case KernelType.RBF: 49 | return Math.exp(-gamma * (x_square[i] + x_square[j] - 2 * dot(x[i], x[j]))); 50 | case KernelType.SIGMOID: 51 | return Math.tanh(gamma * dot(x[i], x[j]) + coef0); 52 | case KernelType.PRECOMPUTED: 53 | return x[i][(int) (x[j][0].value)].value; 54 | default: 55 | return 0; // java 56 | } 57 | } 58 | 59 | Kernel(int l, SvmNode[][] x_, SvmParameter param) { 60 | this.kernel_type = param.kernel_type; 61 | this.degree = param.degree; 62 | this.gamma = param.gamma; 63 | this.coef0 = param.coef0; 64 | 65 | x = (SvmNode[][]) x_.clone(); 66 | 67 | if (kernel_type == KernelType.RBF) { 68 | x_square = new double[l]; 69 | for (int i = 0; i < l; i++) 70 | x_square[i] = dot(x[i], x[i]); 71 | } else x_square = null; 72 | } 73 | 74 | static double dot(SvmNode[] x, SvmNode[] y) { 75 | double sum = 0; 76 | int xlen = x.length; 77 | int ylen = y.length; 78 | int i = 0; 79 | int j = 0; 80 | while (i < xlen && j < ylen) { 81 | if (x[i].index == y[j].index) 82 | sum += x[i++].value * y[j++].value; 83 | else { 84 | if (x[i].index > y[j].index) 85 | ++j; 86 | else 87 | ++i; 88 | } 89 | } 90 | return sum; 91 | } 92 | 93 | public static double k_function(SvmNode[] x, SvmNode[] y, 94 | SvmParameter param) { 95 | switch (param.kernel_type) { 96 | case KernelType.LINEAR: 97 | return dot(x, y); 98 | case KernelType.POLY: 99 | return powi(param.gamma * dot(x, y) + param.coef0, param.degree); 100 | case KernelType.RBF: { 101 | double sum = 0; 102 | int xlen = x.length; 103 | int ylen = y.length; 104 | int i = 0; 105 | int j = 0; 106 | while (i < xlen && j < ylen) { 107 | if (x[i].index == y[j].index) { 108 | double d = x[i++].value - y[j++].value; 109 | sum += d * d; 110 | } else if (x[i].index > y[j].index) { 111 | sum += y[j].value * y[j].value; 112 | ++j; 113 | } else { 114 | sum += x[i].value * x[i].value; 115 | ++i; 116 | } 117 | } 118 | 119 | while (i < xlen) { 120 | sum += x[i].value * x[i].value; 121 | ++i; 122 | } 123 | 124 | while (j < ylen) { 125 | sum += y[j].value * y[j].value; 126 | ++j; 127 | } 128 | 129 | return Math.exp(-param.gamma * sum); 130 | } 131 | case KernelType.SIGMOID: 132 | return Math.tanh(param.gamma * dot(x, y) + param.coef0); 133 | case KernelType.PRECOMPUTED: 134 | return x[(int) (y[0].value)].value; 135 | default: 136 | return 0; // java 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/kernel/KernelCache.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.kernel; 2 | 3 | // 4 | // KernelType KernelCache 5 | // 6 | // length is the number of total data items 7 | // size is the cache size limit in bytes 8 | // 9 | public class KernelCache { 10 | private final int l; 11 | private long size; 12 | 13 | private final class head_t { 14 | head_t prev, next; // a cicular list 15 | float[] data; 16 | int len; // data[0,len) is cached in this entry 17 | } 18 | 19 | private final head_t[] head; 20 | private head_t lru_head; 21 | 22 | KernelCache(int l_, long size_) { 23 | l = l_; 24 | size = size_; 25 | head = new head_t[l]; 26 | for (int i = 0; i < l; i++) head[i] = new head_t(); 27 | size /= 4; 28 | size -= l * (16 / 4); // sizeof(head_t) == 16 29 | size = Math.max(size, 2 * (long) l); // cache must be large enough for two columns 30 | lru_head = new head_t(); 31 | lru_head.next = lru_head.prev = lru_head; 32 | } 33 | 34 | private void lru_delete(head_t h) { 35 | // delete from current location 36 | h.prev.next = h.next; 37 | h.next.prev = h.prev; 38 | } 39 | 40 | private void lru_insert(head_t h) { 41 | // insert to last position 42 | h.next = lru_head; 43 | h.prev = lru_head.prev; 44 | h.prev.next = h; 45 | h.next.prev = h; 46 | } 47 | 48 | // request data [0,len) 49 | // return some position p where [p,len) need to be filled 50 | // (p >= len if nothing needs to be filled) 51 | // java: simulate pointer using single-element array 52 | int get_data(int index, float[][] data, int len) { 53 | head_t h = head[index]; 54 | if (h.len > 0) lru_delete(h); 55 | int more = len - h.len; 56 | 57 | if (more > 0) { 58 | // free old space 59 | while (size < more) { 60 | head_t old = lru_head.next; 61 | lru_delete(old); 62 | size += old.len; 63 | old.data = null; 64 | old.len = 0; 65 | } 66 | 67 | // allocate new space 68 | float[] new_data = new float[len]; 69 | if (h.data != null) System.arraycopy(h.data, 0, new_data, 0, h.len); 70 | h.data = new_data; 71 | size -= more; 72 | do { 73 | int _ = h.len; 74 | h.len = len; 75 | len = _; 76 | } while (false); 77 | } 78 | 79 | lru_insert(h); 80 | data[0] = h.data; 81 | return len; 82 | } 83 | 84 | void swap_index(int i, int j) { 85 | if (i == j) return; 86 | 87 | if (head[i].len > 0) lru_delete(head[i]); 88 | if (head[j].len > 0) lru_delete(head[j]); 89 | do { 90 | float[] _ = head[i].data; 91 | head[i].data = head[j].data; 92 | head[j].data = _; 93 | } while (false); 94 | do { 95 | int _ = head[i].len; 96 | head[i].len = head[j].len; 97 | head[j].len = _; 98 | } while (false); 99 | if (head[i].len > 0) lru_insert(head[i]); 100 | if (head[j].len > 0) lru_insert(head[j]); 101 | 102 | if (i > j) do { 103 | int _ = i; 104 | i = j; 105 | j = _; 106 | } while (false); 107 | for (head_t h = lru_head.next; h != lru_head; h = h.next) { 108 | if (h.len > i) { 109 | if (h.len > j) 110 | do { 111 | float _ = h.data[i]; 112 | h.data[i] = h.data[j]; 113 | h.data[j] = _; 114 | } while (false); 115 | else { 116 | // give up 117 | lru_delete(h); 118 | size += h.len; 119 | h.data = null; 120 | h.len = 0; 121 | } 122 | } 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/kernel/OneClassQ.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.kernel; 2 | 3 | import clojurewerkz.statistiker.libsvm.data.SvmParameter; 4 | import clojurewerkz.statistiker.libsvm.data.SvmProblem; 5 | 6 | public class OneClassQ extends Kernel { 7 | private final KernelCache kernelCache; 8 | private final double[] QD; 9 | 10 | public OneClassQ(SvmProblem prob, SvmParameter param) { 11 | super(prob.length, prob.datapoints, param); 12 | kernelCache = new KernelCache(prob.length, (long) (param.cache_size * (1 << 20))); 13 | QD = new double[prob.length]; 14 | for (int i = 0; i < prob.length; i++) 15 | QD[i] = kernel_function(i, i); 16 | } 17 | 18 | @Override 19 | public float[] get_Q(int i, int len) { 20 | float[][] data = new float[1][]; 21 | int start, j; 22 | if ((start = kernelCache.get_data(i, data, len)) < len) { 23 | for (j = start; j < len; j++) 24 | data[0][j] = (float) kernel_function(i, j); 25 | } 26 | return data[0]; 27 | } 28 | 29 | @Override 30 | public double[] get_QD() { 31 | return QD; 32 | } 33 | 34 | @Override 35 | public void swap_index(int i, int j) { 36 | kernelCache.swap_index(i, j); 37 | super.swap_index(i, j); 38 | do { 39 | double _ = QD[i]; 40 | QD[i] = QD[j]; 41 | QD[j] = _; 42 | } while (false); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/kernel/SvcQ.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.kernel; 2 | 3 | import clojurewerkz.statistiker.libsvm.data.SvmParameter; 4 | import clojurewerkz.statistiker.libsvm.data.SvmProblem; 5 | 6 | // 7 | // Q matrices for various formulations 8 | // 9 | public class SvcQ extends Kernel { 10 | private final byte[] y; 11 | private final KernelCache kernelCache; 12 | private final double[] QD; 13 | 14 | public SvcQ(SvmProblem prob, SvmParameter param, byte[] y_) { 15 | super(prob.length, prob.datapoints, param); 16 | y = (byte[]) y_.clone(); 17 | kernelCache = new KernelCache(prob.length, (long) (param.cache_size * (1 << 20))); 18 | QD = new double[prob.length]; 19 | for (int i = 0; i < prob.length; i++) 20 | QD[i] = kernel_function(i, i); 21 | } 22 | 23 | @Override 24 | public float[] get_Q(int i, int len) { 25 | float[][] data = new float[1][]; 26 | int start, j; 27 | if ((start = kernelCache.get_data(i, data, len)) < len) { 28 | for (j = start; j < len; j++) 29 | data[0][j] = (float) (y[i] * y[j] * kernel_function(i, j)); 30 | } 31 | return data[0]; 32 | } 33 | 34 | @Override 35 | public double[] get_QD() { 36 | return QD; 37 | } 38 | 39 | @Override 40 | public void swap_index(int i, int j) { 41 | kernelCache.swap_index(i, j); 42 | super.swap_index(i, j); 43 | do { 44 | byte _ = y[i]; 45 | y[i] = y[j]; 46 | y[j] = _; 47 | } while (false); 48 | do { 49 | double _ = QD[i]; 50 | QD[i] = QD[j]; 51 | QD[j] = _; 52 | } while (false); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/java/clojurewerkz/statistiker/libsvm/kernel/SvrQ.java: -------------------------------------------------------------------------------- 1 | package clojurewerkz.statistiker.libsvm.kernel; 2 | 3 | import clojurewerkz.statistiker.libsvm.data.SvmParameter; 4 | import clojurewerkz.statistiker.libsvm.data.SvmProblem; 5 | 6 | public class SvrQ extends Kernel { 7 | private final int l; 8 | private final KernelCache kernelCache; 9 | private final byte[] sign; 10 | private final int[] index; 11 | private int next_buffer; 12 | private float[][] buffer; 13 | private final double[] QD; 14 | 15 | public SvrQ(SvmProblem prob, SvmParameter param) { 16 | super(prob.length, prob.datapoints, param); 17 | l = prob.length; 18 | kernelCache = new KernelCache(l, (long) (param.cache_size * (1 << 20))); 19 | QD = new double[2 * l]; 20 | sign = new byte[2 * l]; 21 | index = new int[2 * l]; 22 | for (int k = 0; k < l; k++) { 23 | sign[k] = 1; 24 | sign[k + l] = -1; 25 | index[k] = k; 26 | index[k + l] = k; 27 | QD[k] = kernel_function(k, k); 28 | QD[k + l] = QD[k]; 29 | } 30 | buffer = new float[2][2 * l]; 31 | next_buffer = 0; 32 | } 33 | 34 | @Override 35 | public void swap_index(int i, int j) { 36 | do { 37 | byte _ = sign[i]; 38 | sign[i] = sign[j]; 39 | sign[j] = _; 40 | } while (false); 41 | do { 42 | int _ = index[i]; 43 | index[i] = index[j]; 44 | index[j] = _; 45 | } while (false); 46 | do { 47 | double _ = QD[i]; 48 | QD[i] = QD[j]; 49 | QD[j] = _; 50 | } while (false); 51 | } 52 | 53 | @Override 54 | public float[] get_Q(int i, int len) { 55 | float[][] data = new float[1][]; 56 | int j, real_i = index[i]; 57 | if (kernelCache.get_data(real_i, data, l) < l) { 58 | for (j = 0; j < l; j++) 59 | data[0][j] = (float) kernel_function(real_i, j); 60 | } 61 | 62 | // reorder and copy 63 | float buf[] = buffer[next_buffer]; 64 | next_buffer = 1 - next_buffer; 65 | byte si = sign[i]; 66 | for (j = 0; j < len; j++) 67 | buf[j] = (float) si * sign[j] * data[0][index[j]]; 68 | return buf; 69 | } 70 | 71 | @Override 72 | public double[] get_QD() { 73 | return QD; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/classification/k_nearest_neighbours_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.classification.k-nearest-neighbours-test 2 | (:require [clojurewerkz.statistiker.classification.k-nearest-neighbours :refer :all] 3 | [clojurewerkz.statistiker.distance :as distance] 4 | [clojure.test :refer :all])) 5 | 6 | (def test-item [6 130 8]) 7 | 8 | (def classification-data {:male [[6 180 12] 9 | [5.92 190 11] 10 | [5.58 170 12] 11 | [5.92 165 10]] 12 | :female [[5 100 6] 13 | [5.5 150 8] 14 | [5.42 130 7] 15 | [5.75 150 9]]}) 16 | 17 | (deftest test-classify 18 | (testing "eucledian distance" 19 | (let [model (make-model classification-data) 20 | classified (classify model test-item 5)] 21 | (is (= :female (best-match classified))))) 22 | (testing "cranberra distance" 23 | (let [model (make-model classification-data) 24 | classified (classify model test-item 5 :canberra)] 25 | (is (= :female (best-match classified))))) 26 | (testing "chebyshev distance" 27 | (let [model (make-model classification-data) 28 | classified (classify model test-item 5 :chebyshev)] 29 | (is (= :female (best-match classified))))) 30 | (testing "manhattan distance" 31 | (let [model (make-model classification-data) 32 | classified (classify model test-item 5 :manhattan)] 33 | (is (= :female (best-match classified)))))) 34 | 35 | 36 | (def classification-data-maps 37 | [{:label :male :height 6.0 :weight 180 :foot-size 12} 38 | {:label :male :height 5.92 :weight 190 :foot-size 11} 39 | {:label :male :height 5.58 :weight 170 :foot-size 12} 40 | {:label :male :height 5.92 :weight 165 :foot-size 10} 41 | {:label :female :height 5.0 :weight 100 :foot-size 6} 42 | {:label :female :height 5.5 :weight 150 :foot-size 8} 43 | {:label :female :height 5.42 :weight 130 :foot-size 7} 44 | {:label :female :height 5.75 :weight 150 :foot-size 9}]) 45 | 46 | (deftest test-classify-maps 47 | (testing "eucledian distance" 48 | (let [model (maps->model classification-data-maps :label [:height :weight :foot-size]) 49 | classified (classify model test-item 5)] 50 | (is (= :female (best-match classified))))) 51 | (testing "cranberra distance" 52 | (let [model (maps->model classification-data-maps :label [:height :weight :foot-size]) 53 | classified (classify model test-item 5 :canberra)] 54 | (is (= :female (best-match classified))))) 55 | (testing "chebyshev distance" 56 | (let [model (maps->model classification-data-maps :label [:height :weight :foot-size]) 57 | classified (classify model test-item 5 :chebyshev)] 58 | (is (= :female (best-match classified))))) 59 | (testing "manhattan distance" 60 | (let [model (maps->model classification-data-maps :label [:height :weight :foot-size]) 61 | classified (classify model test-item 5 :manhattan)] 62 | (is (= :female (best-match classified)))))) 63 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/classification/naive_bayes_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.classification.naive-bayes-test 2 | (:require [clojure.test :refer :all] 3 | [clojurewerkz.statistiker.classification.naive-bayes :refer :all])) 4 | 5 | 6 | (def test-item [6 130 8]) 7 | 8 | (def classification-data {:male [[6 180 12] 9 | [5.92 190 11] 10 | [5.58 170 12] 11 | [5.92 165 10]] 12 | :female [[5 100 6] 13 | [5.5 150 8] 14 | [5.42 130 7] 15 | [5.75 150 9]]}) 16 | 17 | (deftest test-classify 18 | (let [model (make-model classification-data) 19 | classified (classify model test-item) 20 | best-match (best-match classified)] 21 | (is (> (second (vals classified)) (first (vals classified))) ) 22 | (is (= :female (first best-match))))) 23 | 24 | 25 | (def classification-data-maps 26 | [{:label :male :height 6.0 :weight 180 :foot-size 12} 27 | {:label :male :height 5.92 :weight 190 :foot-size 11} 28 | {:label :male :height 5.58 :weight 170 :foot-size 12} 29 | {:label :male :height 5.92 :weight 165 :foot-size 10} 30 | {:label :female :height 5.0 :weight 100 :foot-size 6} 31 | {:label :female :height 5.5 :weight 150 :foot-size 8} 32 | {:label :female :height 5.42 :weight 130 :foot-size 7} 33 | {:label :female :height 5.75 :weight 150 :foot-size 9}]) 34 | 35 | (deftest test-classify-2 36 | (let [model (maps->model classification-data-maps :label [:height :weight :foot-size]) 37 | classified (classify model test-item) 38 | best-match (best-match classified)] 39 | (is (> (second (vals classified)) (first (vals classified))) ) 40 | (is (= :female (first best-match))))) 41 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/classification/svm_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.classification.svm-test 2 | (:require [clojurewerkz.statistiker.classification.svm :refer :all] 3 | [clojure.test :refer :all])) 4 | 5 | (def train-data {1 [[0.708333 1 1 -0.320755 -0.105023 -1 1 -0.419847 -1 -0.225806 1 -1] 6 | [0.166667 1 -0.333333 -0.433962 -0.383562 -1 -1 0.0687023 -1 -0.903226 -1 -1 1] 7 | [0.125 1 0.333333 -0.320755 -0.406393 1 1 0.0839695 1 -0.806452 -0.333333 0.5] 8 | [0.25 1 1 -0.698113 -0.484018 -1 1 0.0839695 1 -0.612903 -0.333333 1] 9 | [0.291667 1 1 -0.132075 -0.237443 -1 1 0.51145 -1 -0.612903 0.333333 1] 10 | [0.416667 -1 1 0.0566038 0.283105 -1 1 0.267176 -1 0.290323 1 1] 11 | [0.333333 1 -1 -0.245283 -0.506849 -1 -1 0.129771 -1 -0.16129 0.333333 -1] 12 | [-0.291667 1 1 -0.132075 -0.155251 -1 -1 -0.251908 1 -0.419355 0.333333 1] 13 | [1 1 -0.132075 -0.648402 1 1 0.282443 1 1 -1 1] 14 | [0.583333 1 1 -0.509434 -0.52968 -1 1 -0.114504 1 -0.16129 0.333333 1] 15 | [0.208333 1 0.333333 -0.660377 -0.525114 -1 1 0.435115 -1 -0.193548 -0.333333 1] 16 | [0.166667 1 0.333333 -0.358491 -0.52968 -1 1 0.206107 -1 -0.870968 -0.333333 1] 17 | [0.25 1 1 0.433962 -0.086758 -1 1 0.0534351 1 0.0967742 1 -1 1] 18 | [-0.125 1 1 -0.0566038 -0.6621 -1 1 -0.160305 1 -0.709677 -1 1] 19 | [-0.208333 1 1 -0.320755 -0.406393 1 1 0.206107 1 -1 -1 0.333333 1] 20 | [0.333333 1 1 -0.132075 -0.630137 -1 1 0.0229008 1 -0.387097 -1 -0.333333 1] 21 | [0.25 1 -1 0.245283 -0.328767 -1 1 -0.175573 -1 -1 -1 -1 -1] 22 | [-0.541667 1 1 0.0943396 -0.557078 -1 -1 0.679389 -1 -1 -1 -1 1] 23 | [0.25 1 0.333333 -0.396226 -0.579909 1 -1 -0.0381679 -1 -0.290323 -0.333333 0.5] 24 | [-0.166667 1 0.333333 -0.54717 -0.894977 -1 1 -0.160305 -1 -0.741935 -1 1 -1] 25 | [-0.375 1 1 -0.698113 -0.675799 -1 1 0.618321 -1 -1 -1 -0.333333 -1] 26 | [0.541667 1 -0.333333 0.245283 -0.452055 -1 -1 -0.251908 1 -1 1 0.5] 27 | [0.5 -1 1 0.0566038 -0.547945 -1 1 -0.343511 -1 -0.677419 1 1] 28 | [-0.458333 1 1 -0.207547 -0.136986 -1 -1 -0.175573 1 -0.419355 -1 0.5] 29 | [0.333333 1 1 -0.169811 -0.817352 -1 1 -0.175573 1 0.16129 -0.333333 -1] 30 | [0.25 -1 1 0.509434 -0.438356 -1 -1 0.0992366 1 -1 -1 -1] 31 | [0.375 1 -0.333333 -0.509434 -0.292237 -1 1 -0.51145 -1 -0.548387 -0.333333 1] 32 | [-0.0833333 -1 1 -0.320755 -0.182648 -1 -1 0.0839695 1 -0.612903 -1 1] 33 | [0.166667 1 1 0.0566038 -0.315068 -1 1 -0.374046 1 -0.806452 -0.333333 0.5] 34 | [0.208333 -1 -0.333333 -0.207547 -0.118721 1 1 0.236641 -1 -1 -1 0.333333 -1] 35 | [-0.25 1 0.333333 -0.735849 -0.465753 -1 -1 0.236641 -1 -1 -1 -1 -1] 36 | [0.333333 1 1 -0.509434 -0.388128 -1 -1 0.0534351 1 0.16129 -0.333333 1] 37 | [-0.333333 1 1 -0.0943396 -0.164384 -1 1 0.160305 1 -1 1 1] 38 | [0.208333 1 1 -0.0188679 -0.579909 -1 -1 -0.480916 -1 -0.354839 -0.333333 1] 39 | [-0.75 1 1 -0.509434 -0.671233 -1 -1 -0.0992366 1 -0.483871 -1 1] 40 | [0.208333 1 1 0.0566038 -0.342466 -1 1 -0.389313 1 -0.741935 -1 -1 1] 41 | [-0.125 1 1 0.0566038 -0.465753 -1 1 -0.129771 -1 -0.16129 -1 1] 42 | [0.333333 -1 1 -0.320755 -0.0684932 -1 1 0.496183 -1 -1 -1 -1 -1] 43 | [0.0416667 1 1 -0.433962 -0.360731 -1 1 -0.419847 1 -0.290323 -0.333333 1] 44 | [0.0416667 1 1 -0.698113 -0.634703 -1 1 -0.435115 1 -1 -0.333333 -1] 45 | [-0.0416667 1 1 -0.415094 -0.607306 -1 -1 0.480916 -1 -0.677419 -1 0.333333 1] 46 | [-0.25 1 1 -0.698113 -0.319635 -1 1 -0.282443 1 -0.677419 -0.333333 -1] 47 | [0.208333 1 1 -0.886792 -0.506849 -1 -1 0.29771 -1 -0.967742 -1 -0.333333 1] 48 | [0.583333 1 1 -0.509434 -0.493151 -1 -1 -1 -1 -0.677419 -1 -1] 49 | [0.166667 1 1 0.339623 -0.255708 1 1 -0.19084 -1 -0.677419 1 1] 50 | [0.416667 1 1 -0.320755 -0.415525 -1 1 0.160305 -1 -0.548387 -0.333333 1] 51 | [-0.208333 1 1 -0.433962 -0.324201 -1 1 0.450382 -1 -0.83871 -1 1] 52 | [0.291667 -1 1 0.0566038 -0.39726 -1 1 0.312977 -1 -0.16129 0.333333 1] 53 | [0.25 1 1 -0.132075 -0.767123 -1 -1 0.389313 1 -1 -1 -0.333333 1] 54 | [0.0833333 -1 1 0.622642 -0.0821918 -1 -0.29771 1 0.0967742 -1 -1] 55 | [0.291667 -1 1 0.207547 -0.182648 -1 1 0.374046 -1 -1 -1 -1 -1] 56 | [-0.291667 1 1 -0.509434 -0.438356 -1 1 0.114504 -1 -0.741935 -1 -1 1] 57 | [0.125 -1 1 1 -0.260274 1 1 -0.0534351 1 0.290323 1 0.333333 1] 58 | [0.125 1 1 -0.320755 -0.283105 1 1 -0.51145 1 -0.483871 1 -1 1] 59 | [-0.166667 1 0.333333 -0.509434 -0.716895 -1 -1 0.0381679 -1 -0.354839 1 1] 60 | [0.0416667 1 1 -0.471698 -0.269406 -1 1 -0.312977 1 0.0322581 0.333333 -1] 61 | [0.166667 1 1 0.0943396 -0.324201 -1 -1 -0.740458 1 -0.612903 -0.333333 1] 62 | [0.375 1 1 -0.509434 -0.356164 -1 -1 -0.572519 1 -0.419355 0.333333 1] 63 | [0.291667 1 1 -0.566038 -0.525114 1 -1 0.358779 1 -0.548387 -1 0.333333 1] 64 | [0.416667 -1 1 -0.735849 -0.347032 -1 -1 0.496183 1 -0.419355 0.333333 -1] 65 | [0.541667 1 1 -0.660377 -0.607306 -1 1 -0.0687023 1 -0.967742 -1 -0.333333 -1] 66 | [0.458333 1 1 -0.509434 -0.452055 -1 1 -0.618321 1 -0.290323 1 -0.333333 -1] 67 | [0.125 1 1 -0.415094 -0.438356 1 1 0.114504 1 -0.612903 -0.333333 -1] 68 | [0.458333 1 1 -0.0377358 -0.607306 -1 1 -0.0687023 -1 -0.354839 0.333333 0.5] 69 | [-0.125 1 0.333333 -0.132075 -0.511416 -1 -1 0.40458 -1 -0.806452 -0.333333 1] 70 | [0.0416667 1 -0.333333 0.849057 -0.283105 -1 1 0.89313 -1 -1 -1 -0.333333 1] 71 | [1 1 -0.45283 -0.287671 -1 -1 -0.633588 1 -0.354839 0.333333 1] 72 | [-0.0416667 1 1 -0.660377 -0.525114 -1 -1 0.358779 -1 -1 -1 -0.333333 -1] 73 | [-0.541667 1 1 -0.698113 -0.812785 -1 1 -0.343511 1 -0.354839 -1 1] 74 | [0.208333 1 0.333333 -0.283019 -0.552511 -1 1 0.557252 -1 0.0322581 -1 0.333333 1] 75 | [0.0833333 1 1 -0.132075 -0.584475 -1 -1 -0.389313 1 0.806452 1 -1 1] 76 | [0.541667 -1 1 0.584906 -0.534247 1 -1 0.435115 1 -0.677419 0.333333 1] 77 | [-0.625 1 -1 -0.509434 -0.520548 -1 -1 0.694656 1 0.225806 -1 1] 78 | [0.375 -1 1 0.0566038 -0.461187 -1 -1 0.267176 1 -0.548387 -1 -1] 79 | [0.208333 1 1 -0.358491 -0.392694 -1 1 -0.0992366 1 -0.0322581 0.333333 1] 80 | [0.5 1 -1 -0.169811 -0.287671 1 1 0.572519 -1 -0.548387 -0.333333 -1]] 81 | 2 [[0.583333 -1 0.333333 -0.603774 1 -1 1 0.358779 -1 -0.483871 -1 1] 82 | [0.458333 1 1 -0.358491 -0.374429 -1 -1 -0.480916 1 -0.935484 -0.333333 1] 83 | [0.875 -1 -0.333333 -0.509434 -0.347032 -1 1 -0.236641 1 -0.935484 -1 -0.333333 -1] 84 | [0.5 1 1 -0.509434 -0.767123 -1 -1 0.0534351 -1 -0.870968 -1 -1 1] 85 | [0.25 1 1 -0.226415 -0.506849 -1 -1 0.374046 -1 -0.83871 -1 1] 86 | [1 1 -0.0943396 -0.543379 -1 1 -0.389313 1 -1 -1 -1 1] 87 | [-0.375 1 0.333333 -0.132075 -0.502283 -1 1 0.664122 -1 -1 -1 -1 -1] 88 | [0.166667 -1 1 -0.358491 -0.191781 -1 1 0.343511 -1 -1 -1 -0.333333 -1] 89 | [0.75 -1 1 -0.660377 -0.894977 -1 -1 -0.175573 -1 -0.483871 -1 -1] 90 | [0.458333 1 -1 -0.698113 -0.611872 -1 1 0.114504 1 -0.419355 -1 -1] 91 | [-0.541667 1 -1 -0.132075 -0.666667 -1 -1 0.633588 1 -0.548387 -1 -1 1] 92 | [-0.208333 1 -0.333333 -0.320755 -0.456621 -1 1 0.664122 -1 -0.935484 -1 -1] 93 | [-0.416667 1 1 -0.603774 -0.191781 -1 -1 0.679389 -1 -0.612903 -1 -1] 94 | [-0.25 1 1 -0.660377 -0.643836 -1 -1 0.0992366 -1 -0.967742 -1 -1 -1] 95 | [0.0416667 -1 -0.333333 -0.283019 -0.260274 1 1 0.343511 1 -1 -1 -0.333333 -1] 96 | [-0.208333 -1 0.333333 -0.320755 -0.319635 -1 -1 0.0381679 -1 -0.935484 -1 -1 -1] 97 | [-0.291667 -1 1 -0.169811 -0.465753 -1 1 0.236641 1 -1 -1 -1] 98 | [-0.0833333 -1 0.333333 -0.509434 -0.228311 -1 1 0.312977 -1 -0.806452 -1 -1 -1] 99 | [0.75 -1 0.333333 -0.698113 -0.365297 1 1 -0.0992366 -1 -1 -1 -0.333333 -1] 100 | [0.541667 1 1 0.245283 -0.534247 -1 1 0.0229008 -1 -0.258065 -1 -1 0.5] 101 | [-0.666667 -1 0.333333 -0.509434 -0.593607 -1 -1 0.51145 -1 -1 -1 -1 -1] 102 | [-0.458333 1 0.333333 -0.320755 -0.753425 -1 -1 0.206107 -1 -1 -1 -1 -1] 103 | [-0.208333 1 1 -0.471698 -0.561644 -1 1 0.755725 -1 -1 -1 -1 -1] 104 | [0.375 -1 1 -0.433962 -0.621005 -1 -1 0.40458 -1 -1 -1 -1 -1] 105 | [-0.375 1 0.333333 -0.320755 -0.511416 -1 -1 0.648855 1 -0.870968 -1 -1 -1] 106 | [-0.291667 1 -0.333333 -0.867925 -0.675799 1 -1 0.29771 -1 -1 -1 -1 1] 107 | [0.208333 1 0.333333 -0.132075 -0.611872 1 1 0.435115 -1 -1 -1 -1 -1] 108 | [-0.0416667 1 -0.333333 -0.358491 -0.639269 1 -1 0.725191 -1 -1 -1 -1 -1] 109 | [0.5 -1 0.333333 -0.132075 0.328767 1 1 0.312977 -1 -0.741935 -1 -0.333333 -1] 110 | [0.416667 -1 -0.333333 -0.132075 -0.684932 -1 -1 0.648855 -1 -1 -1 0.333333 -1] 111 | [-0.333333 -1 -0.333333 -0.320755 -0.506849 -1 1 0.587786 -1 -0.806452 -1 -1] 112 | [-0.5 -1 -0.333333 -0.792453 -0.671233 -1 -1 0.480916 -1 -1 -1 -0.333333 -1] 113 | [0.291667 -1 0.333333 -0.509434 -0.762557 1 -1 -0.618321 -1 -1 -1 -1 -1] 114 | [0.166667 1 0.333333 0.0566038 -1 1 -1 0.557252 -1 -0.935484 -1 -0.333333 1] 115 | [-0.375 1 0.333333 -0.509434 -0.543379 -1 -1 0.496183 -1 -1 -1 -1 -1] 116 | [0.291667 -1 -1 0.0566038 -0.479452 -1 -1 0.526718 -1 -0.709677 -1 -1 -1] 117 | [0.416667 1 -1 -0.0377358 -0.511416 1 1 0.206107 -1 -0.258065 1 -1 0.5] 118 | [-0.0833333 1 1 -0.132075 -0.383562 -1 1 0.755725 1 -1 -1 -1 -1] 119 | [-0.375 -1 0.333333 -0.54717 -0.47032 -1 -1 0.19084 -1 -0.903226 -0.333333 -1] 120 | [0.166667 -1 1 -0.509434 0.0410959 -1 -1 0.40458 1 -0.806452 -1 -1 -1] 121 | [0.708333 1 -0.333333 0.169811 -0.456621 -1 1 0.0992366 -1 -1 -1 -1 -1] 122 | [0.958333 -1 0.333333 -0.132075 -0.675799 -1 -0.312977 -1 -0.645161 -1 -1] 123 | [0.583333 -1 1 -0.773585 -0.557078 -1 -1 0.0839695 -1 -0.903226 -1 0.333333 -1] 124 | [-0.333333 1 1 -0.811321 -0.625571 -1 1 0.175573 1 -0.0322581 -1 -1] 125 | [-0.583333 -1 0.333333 -1 -0.666667 -1 -1 0.648855 -1 -1 -1 -1 -1] 126 | [-0.458333 -1 0.333333 -0.509434 -0.621005 -1 -1 0.557252 -1 -1 -1 -1] 127 | [0.125 1 -0.333333 -0.509434 -0.497717 -1 -1 0.633588 -1 -0.741935 -1 -1 -1] 128 | [-0.5 1 0.333333 -0.320755 -0.598174 -1 1 0.480916 -1 -0.354839 -1 -1] 129 | [0.166667 1 1 -0.698113 -0.657534 -1 -1 -0.160305 1 -0.516129 -1 0.5] 130 | [-0.458333 1 -1 0.0188679 -0.461187 -1 1 0.633588 -1 -0.741935 -1 0.333333 -1] 131 | [0.375 1 -0.333333 -0.358491 -0.625571 1 1 0.0534351 -1 -1 -1 -1 -1] 132 | [0.25 1 -1 0.584906 -0.342466 -1 1 0.129771 -1 0.354839 1 -1 1] 133 | [-0.5 -1 -0.333333 -0.396226 -0.178082 -1 -1 0.40458 -1 -1 -1 -1 -1] 134 | [0.25 1 -0.333333 -0.132075 -0.56621 -1 -1 0.419847 1 -1 -1 -1 -1] 135 | [0.541667 1 1 -0.509434 -0.196347 -1 1 0.221374 -1 -0.870968 -1 -1] 136 | [0.458333 -1 0.333333 -0.132075 -0.146119 -1 -1 -0.0534351 -1 -0.935484 -1 -1 1] 137 | [-0.125 -1 -0.333333 -0.509434 -0.461187 -1 -1 0.389313 -1 -0.645161 -1 -1 -1] 138 | [-0.375 -1 0.333333 -0.735849 -0.931507 -1 -1 0.587786 -1 -0.806452 -1 -1] 139 | [-0.166667 -1 1 -0.320755 -0.347032 -1 -1 0.40458 -1 -1 -1 -1 -1] 140 | [-0.0833333 1 0.333333 -0.886792 -0.561644 -1 -1 0.0992366 1 -0.612903 -1 -1] 141 | [-0.333333 -1 -0.333333 -0.660377 -0.844749 -1 -1 0.0229008 -1 -1 -1 -1] 142 | [-0.5 1 -0.333333 -0.698113 -0.502283 -1 -1 0.251908 -1 -1 -1 -1 -1] 143 | [0.0416667 -1 0.333333 -0.226415 -0.187215 1 -1 0.51145 -1 -1 -1 -1 -1] 144 | [-0.458333 1 -0.333333 -0.509434 -0.228311 -1 -1 0.389313 -1 -1 -1 -1 -1] 145 | [-0.166667 -1 -0.333333 -0.245283 -0.3379 -1 -1 0.389313 -1 -1 -1 -1] 146 | [0.541667 -1 -1 0.0566038 -0.543379 -1 -1 -0.343511 -1 -0.16129 1 -1 -1] 147 | [0.5 -1 0.333333 0.245283 0.0684932 -1 1 0.221374 -1 -0.741935 -1 -1 -1] 148 | [0.0416667 1 0.333333 -0.415094 -0.328767 -1 1 0.236641 -1 -0.83871 1 -0.333333 -1] 149 | [0.0416667 -1 0.333333 0.245283 -0.657534 -1 -1 0.40458 -1 -1 -1 -0.333333 -1] 150 | [-0.0416667 -1 0.333333 -0.207547 -0.680365 -1 1 0.496183 -1 -0.967742 -1 -1] 151 | [-0.0416667 1 -0.333333 -0.245283 -0.657534 -1 -1 0.328244 -1 -0.741935 -1 -0.333333 -1] 152 | [-0.458333 1 1 -0.132075 -0.543379 -1 -1 0.633588 -1 -1 -1 -1 -1] 153 | [0.0416667 1 0.333333 0.0566038 -0.515982 -1 1 0.435115 -1 -0.483871 -1 -1 1] 154 | [-0.291667 -1 0.333333 -0.0943396 -0.767123 -1 1 0.358779 1 -0.548387 1 -1 -1] 155 | [0.583333 -1 0.333333 0.0943396 -0.310502 -1 -1 0.541985 -1 -1 -1 -0.333333 -1] 156 | [-0.791667 -1 -0.333333 -0.54717 -0.616438 -1 -1 0.847328 -1 -0.774194 -1 -1 -1] 157 | [0.166667 1 1 -0.283019 -0.630137 -1 -1 0.480916 1 -1 -1 -1 1] 158 | [0.25 1 1 -0.169811 -0.3379 -1 1 0.694656 -1 -1 -1 -1 -1] 159 | [-0.0833333 1 -1 -0.415094 -0.60274 -1 1 -0.175573 1 -0.548387 -1 -0.333333 -1] 160 | [-0.5 -1 0.333333 -0.660377 -0.351598 -1 1 0.541985 1 -1 -1 -1 -1] 161 | [-0.5 1 0.333333 -0.660377 -0.43379 -1 -1 0.648855 -1 -1 -1 -1 -1] 162 | [-0.125 -1 0.333333 -0.509434 -0.575342 -1 -1 0.328244 -1 -0.483871 -1 -1] 163 | [0.0416667 -1 0.333333 -0.735849 -0.356164 -1 1 0.465649 -1 -1 -1 -1 -1] 164 | [0.458333 -1 1 -0.320755 -0.191781 -1 -1 -0.221374 -1 -0.354839 0.333333 -1] 165 | [-0.0833333 -1 0.333333 -0.320755 -0.406393 -1 1 0.19084 -1 -0.83871 -1 -1 -1] 166 | [-0.291667 -1 -0.333333 -0.792453 -0.643836 -1 -1 0.541985 -1 -1 -1 -1 -1] 167 | [-0.333333 1 -0.333333 -0.358491 -0.16895 -1 1 0.51145 -1 -1 -1 -1 -1] 168 | [0.125 1 -1 -0.509434 -0.694064 -1 1 0.389313 -1 -0.387097 -1 1] 169 | [0.0833333 1 -0.333333 -0.320755 -0.378995 -1 -1 0.282443 -1 -1 -1 -1 -1] 170 | [-0.416667 1 1 -0.698113 -0.611872 -1 -1 0.374046 -1 -1 -1 -1 1] 171 | [0.458333 -1 1 0.622642 -0.0913242 -1 -1 0.267176 1 -1 -1 -1 -1] 172 | [-0.125 -1 1 -0.698113 -0.415525 -1 1 0.343511 -1 -1 -1 -1 -1] 173 | [1 0.333333 -0.320755 -0.675799 1 1 0.236641 -1 -0.612903 1 -1 -1] 174 | [-0.333333 -1 1 -0.169811 -0.497717 -1 1 0.236641 1 -0.935484 -1 -1]]}) 175 | 176 | (def test-data 177 | {1 [[0.666667 1 0.333333 -0.132075 -0.415525 -1 1 0.145038 -1 -0.354839 1 1] 178 | [0.583333 1 1 -0.886792 -0.210046 -1 1 -0.175573 1 -0.709677 0.333333 -1] 179 | [0.375 -1 1 -0.169811 -0.232877 1 -1 -0.465649 -1 -0.387097 1 -1] 180 | [-0.0833333 1 1 -0.132075 -0.214612 -1 -1 -0.221374 1 0.354839 1 1] 181 | [-0.291667 1 0.333333 0.0566038 -0.520548 -1 -1 0.160305 -1 0.16129 -1 -1] 182 | [0.583333 1 1 -0.415094 -0.415525 1 -1 0.40458 -1 -0.935484 0.333333 1] 183 | [0.125 -1 1 -0.245283 0.292237 -1 1 0.206107 1 -0.387097 0.333333 1] 184 | [-0.5 1 1 -0.698113 -0.789954 -1 1 0.328244 -1 -1 -1 -1 1] 185 | [0.125 1 1 -0.283019 -0.73516 -1 1 -0.480916 1 -0.322581 -0.333333 0.5] 186 | [0.708333 1 0.333333 0.245283 -0.347032 -1 -1 -0.374046 1 -0.0645161 -0.333333 1] 187 | [0.708333 1 1 -0.0377358 -0.780822 -1 -1 -0.175573 1 -0.16129 1 -1 1] 188 | [-0.75 1 1 -0.396226 -0.287671 -1 1 0.29771 1 -1 -1 -1 1] 189 | [1 1 1 -0.415094 -0.187215 -1 1 0.389313 1 -1 -1 1 -1] 190 | [0.208333 1 1 -0.415094 -0.205479 -1 1 0.526718 -1 -1 -1 0.333333 1] 191 | [0.291667 1 1 -0.415094 -0.39726 -1 1 0.0687023 1 -0.0967742 -0.333333 1] 192 | [-0.0833333 1 1 -0.132075 -0.210046 -1 -1 0.557252 1 -0.483871 -1 -1 1] 193 | [0.0833333 1 1 0.245283 -0.255708 -1 1 0.129771 1 -0.741935 -0.333333 1] 194 | [0.458333 1 0.333333 -0.415094 -0.164384 -1 -1 -0.0839695 1 -0.419355 -1 1] 195 | [0.25 1 -1 0.433962 -0.260274 -1 1 0.343511 -1 -0.935484 -1 1] 196 | [0.208333 1 1 -0.358491 -0.589041 -1 1 -0.0839695 1 -0.290323 1 1] 197 | [0.0416667 1 1 -0.698113 -0.484018 -1 -1 -0.160305 1 -0.0967742 -0.333333 1] 198 | [0.5 1 1 -0.226415 -0.415525 -1 1 -0.145038 -1 -0.0967742 -0.333333 1] 199 | [0.416667 1 1 -0.320755 -0.0684932 1 1 -0.0687023 1 -0.419355 -1 1 1] 200 | [0.375 -1 0.333333 -0.320755 -0.374429 -1 -1 -0.603053 -1 -0.612903 -0.333333 1] 201 | [-0.416667 -1 1 -0.283019 -0.0182648 1 1 -0.00763359 1 -0.0322581 -1 1] 202 | [0.333333 -1 1 -0.0377358 -0.173516 -1 1 0.145038 1 -0.677419 -1 1] 203 | [-0.583333 1 1 -0.54717 -0.575342 -1 -1 0.0534351 -1 -0.612903 -1 1] 204 | [-0.0416667 1 1 -0.358491 -0.410959 -1 -1 0.374046 1 -1 -1 -0.333333 1] 205 | [0.375 -1 1 0.245283 -0.826484 -1 1 0.129771 -1 1 1 1 1] 206 | [-0.416667 1 1 -0.509434 -0.767123 -1 1 -0.251908 1 -0.193548 -1 1] 207 | [0.625 1 0.333333 0.622642 -0.324201 1 1 0.206107 1 -0.483871 -1 1] 208 | [0.375 -1 1 -0.132075 -0.351598 -1 1 0.358779 -1 0.16129 1 0.333333 -1] 209 | [0.291667 1 1 -0.320755 -0.420091 -1 -1 0.114504 1 -0.548387 -1 -0.333333 1] 210 | [0.5 1 1 -0.698113 -0.442922 -1 1 0.328244 -1 -0.806452 -1 0.333333 0.5] 211 | [0.291667 1 0.333333 -0.132075 -0.730594 -1 1 0.282443 -1 -0.0322581 -1 -1] 212 | [0.291667 1 1 -0.0377358 -0.287671 -1 1 0.0839695 1 -0.0967742 0.333333 1] 213 | [0.0416667 1 1 -0.509434 -0.716895 -1 -1 -0.358779 -1 -0.548387 -0.333333 1] 214 | [-0.375 1 1 -0.660377 -0.251142 -1 1 0.251908 -1 -1 -1 -0.333333 -1] 215 | [0.0833333 1 1 -0.283019 0.0365297 -1 -1 -0.0687023 1 -0.612903 -0.333333 1] 216 | [0.458333 1 0.333333 -0.132075 -0.0456621 -1 -1 0.328244 -1 -1 -1 -1 -1] 217 | [0.291667 1 1 -0.320755 -0.634703 -1 1 -0.0687023 1 -0.225806 0.333333 1] 218 | [0.208333 1 -0.333333 -0.509434 -0.278539 -1 1 0.358779 -1 -0.419355 -1 -1] 219 | [-0.208333 1 -0.333333 -0.698113 -0.52968 -1 -1 0.480916 -1 -0.677419 1 -1 1] 220 | [0.583333 1 1 0.245283 -0.269406 -1 1 -0.435115 1 -0.516129 1 -1]] 221 | 2 [[0.666667 1 -1 0.245283 -0.506849 1 1 -0.0839695 -1 -0.967742 -0.333333 -1] 222 | [0.625 -1 0.333333 -0.509434 -0.611872 -1 1 -0.328244 -1 -0.516129 -1 -1] 223 | [-0.791667 1 -1 -0.54717 -0.744292 -1 1 0.572519 -1 -1 -1 -1 -1] 224 | [-0.125 1 0.333333 -0.339623 -0.680365 -1 -1 0.40458 -1 -1 -1 -1 -1] 225 | [-0.458333 1 0.333333 -0.509434 -0.479452 1 -1 0.877863 -1 -0.741935 1 -1 1] 226 | [-0.458333 -1 1 -0.849057 -0.365297 -1 1 -0.221374 -1 -0.806452 -1 -1] 227 | [1 0.333333 -0.320755 -0.452055 1 1 0.557252 -1 -1 -1 1 -1] 228 | [-0.416667 1 0.333333 -0.320755 -0.136986 -1 -1 0.389313 -1 -0.387097 -1 -0.333333 -1] 229 | [-0.0416667 1 1 -0.735849 -0.511416 1 -1 0.160305 -1 -0.967742 -1 1 1] 230 | [0.375 -1 1 -0.132075 0.223744 -1 1 0.312977 -1 -0.612903 -1 -1] 231 | [0.0416667 1 1 -0.132075 -0.484018 -1 -1 0.358779 -1 -0.612903 -1 -1 -1] 232 | [0.0416667 1 -0.333333 -0.735849 -0.164384 -1 -1 0.29771 -1 -1 -1 -1 1] 233 | [-0.208333 1 0.333333 -0.433962 -0.410959 1 -1 0.587786 -1 -1 -1 0.333333 -1] 234 | [0.0833333 -1 -0.333333 -0.226415 -0.43379 -1 1 0.374046 -1 -0.548387 -1 -1] 235 | [0.208333 -1 1 -0.886792 -0.442922 -1 1 -0.221374 -1 -0.677419 -1 -1] 236 | [0.0416667 -1 0.333333 -0.698113 -0.598174 -1 -1 0.328244 -1 -0.483871 -1 -1] 237 | [0.666667 -1 -1 -0.132075 -0.484018 -1 -1 0.221374 -1 -0.419355 -1 0.333333 -1] 238 | [0.625 1 0.333333 -0.54717 -0.310502 -1 -1 0.221374 -1 -0.677419 -1 -0.333333 1] 239 | [-0.0416667 1 -1 0.0943396 -0.214612 1 -1 0.633588 -1 -0.612903 -1 1] 240 | [0.291667 -1 0.333333 -0.849057 -0.123288 -1 -1 0.358779 -1 -1 -1 -0.333333 -1] 241 | [0.208333 1 0.333333 -0.792453 -0.479452 -1 1 0.267176 1 -0.806452 -1 1] 242 | [-0.666667 1 0.333333 -0.320755 -0.43379 -1 -1 0.770992 -1 0.129032 1 -1 -1] 243 | [-0.0833333 1 0.333333 -0.415094 -0.456621 1 1 0.450382 -1 -0.225806 -1 -1] 244 | [-0.416667 -1 0.333333 -0.471698 -0.60274 -1 -1 0.435115 -1 -0.935484 -1 -1] 245 | [-1 1 -0.333333 -0.320755 -0.643836 -1 1 1 -1 -1 -1 -1 -1] 246 | [-0.5 -1 -0.333333 -0.320755 -0.643836 -1 1 0.541985 -1 -0.548387 -1 -1 -1] 247 | [0.416667 -1 0.333333 -0.226415 -0.424658 -1 1 0.541985 -1 -1 -1 -1 -1] 248 | [-0.0833333 1 0.333333 -1 -0.538813 -1 -1 0.267176 1 -1 -1 -0.333333 1] 249 | [0.0416667 1 0.333333 -0.509434 -0.39726 -1 1 0.160305 -1 -0.870968 -1 1] 250 | [-0.375 1 -0.333333 -0.509434 -0.570776 -1 -1 0.51145 -1 -1 -1 -1 -1] 251 | [0.166667 1 0.333333 0.0566038 -0.808219 -1 -1 0.572519 -1 -0.483871 -1 -1 -1] 252 | [-0.75 -1 1 -0.169811 -0.739726 -1 -1 0.694656 -1 -0.548387 -1 -1 -1] 253 | [-0.5 1 -0.333333 -0.226415 -0.648402 -1 -1 -0.0687023 -1 -1 -1 0.5] 254 | [0.208333 -1 -1 0.0566038 -0.283105 1 1 0.389313 -1 -0.677419 -1 -1 -1] 255 | [-0.0416667 1 -1 -0.54717 -0.726027 -1 1 0.816794 -1 -1 -1 0.5] 256 | [-0.333333 1 1 -0.603774 -0.388128 -1 1 0.740458 -1 -1 -1 -1 -1] 257 | [0.375 1 0.333333 -0.320755 -0.520548 -1 -1 0.145038 -1 -0.419355 1 1] 258 | [-1 1 -0.169811 -0.506849 -1 1 0.358779 -1 -1 -1 -1 -1] 259 | [-0.25 1 0.333333 -0.169811 -0.401826 -1 1 0.29771 -1 -1 -1 -1 -1] 260 | [-0.0416667 1 -0.333333 -0.509434 -0.0913242 -1 -1 0.541985 -1 -0.935484 -1 -1 -1] 261 | [-0.583333 1 0.333333 -0.132075 -0.109589 -1 1 0.694656 -1 -1 -1 -1 -1] 262 | [-1 1 -0.320755 -0.369863 -1 1 0.0992366 -1 -0.870968 -1 -1] 263 | [-0.0833333 -1 0.333333 -0.132075 -0.16895 -1 1 0.0839695 -1 -0.516129 -1 -0.333333 -1] 264 | [0.5 -1 0.333333 0.150943 -0.347032 -1 -1 0.175573 -1 -0.741935 -1 -1 -1] 265 | [-0.375 1 -0.333333 -0.320755 -0.575342 -1 1 0.78626 -1 -1 -1 -1 -1] 266 | [-0.0833333 1 0.333333 -0.698113 -0.776256 -1 -1 -0.206107 -1 -0.806452 -1 -1 -1] 267 | [0.25 1 0.333333 0.0566038 -0.607306 1 -1 0.312977 -1 -0.483871 -1 -1 -1] 268 | [0.75 -1 -0.333333 0.245283 -0.196347 -1 -1 0.389313 -1 -0.870968 -1 0.333333 -1] 269 | [0.333333 1 0.333333 0.0566038 -0.465753 1 -1 0.00763359 1 -0.677419 -1 -1] 270 | [-0.416667 1 1 0.0566038 -0.447489 -1 -1 0.526718 -1 -0.516129 -1 -1 -1] 271 | [0.208333 -1 0.333333 -0.509434 -0.0228311 -1 -1 0.541985 -1 -1 -1 -1 -1] 272 | [-0.166667 1 -0.333333 -0.320755 -0.360731 -1 -1 0.526718 -1 -0.806452 -1 -1 -1] 273 | [-0.0416667 1 0.333333 0.471698 -0.666667 1 -1 0.389313 -1 -0.83871 -1 -1 1] 274 | [-0.375 1 -0.333333 -0.509434 -0.374429 -1 -1 0.557252 -1 -1 -1 -1 1] 275 | [0.125 -1 -0.333333 -0.132075 -0.232877 -1 1 0.251908 -1 -0.580645 -1 -1] 276 | [0.166667 1 1 -0.132075 -0.69863 -1 -1 0.175573 -1 -0.870968 -1 0.5]]}) 277 | 278 | 279 | (deftest svm-test 280 | (let [problem (make-problem train-data) 281 | params (make-params train-data) 282 | model (train-model problem params)] 283 | (let [first-ds (get test-data 1) 284 | classified (->> first-ds 285 | (map #(classify model %)) 286 | frequencies)] 287 | (is (= 37 (get classified 1.0))) 288 | (is (= 7 (get classified 2.0)))) 289 | 290 | (let [second-ds (get test-data 2) 291 | classified (->> second-ds 292 | (map #(classify model %)) 293 | frequencies)] 294 | (is (= 8 (get classified 1.0))) 295 | (is (= 48 (get classified 2.0)))))) 296 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/clustering/dbscan_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.clustering.dbscan-test 2 | (:require [clojurewerkz.statistiker.clustering.dbscan :refer :all] 3 | [clojure.test :refer :all])) 4 | 5 | (def points 6 | [[83.08303244924173 58.83387754182331] [45.05445510940626 23.469642649637535] 7 | [14.96417921432294 69.0264096390456] [73.53189604333602 34.896145021310076] 8 | [73.28498173551634 33.96860806993209] [73.45828098873608 33.92584423092194] 9 | [73.9657889183145 35.73191006924026] [74.0074097183533 36.81735596177168] 10 | [73.41247541410848 34.27314856695011] [73.9156256353017 36.83206791547127] 11 | [74.81499205809087 37.15682749846019] [74.03144880081527 37.57399178552441] 12 | [74.51870941207744 38.674258946906775] [74.50754595105536 35.58903978415765] 13 | [74.51322752749547 36.030572259100154] [59.27900996617973 46.41091720294207] 14 | [59.73744793841615 46.20015558367595] [58.81134076672606 45.71150126331486] 15 | [58.52225539437495 47.416083617601544] [58.218626647023484 47.36228902172297] 16 | [60.27139669447206 46.606106348801404] [60.894962462363765 46.976924697402865] 17 | [62.29048673878424 47.66970563563518] [61.03857608977705 46.212924720020965] 18 | [60.16916214139201 45.18193661351688] [59.90036905976012 47.555364347063005] 19 | [62.33003634144552 47.83941489877179] [57.86035536718555 47.31117930193432] 20 | [58.13715479685925 48.985960494028404] [56.131923963548616 46.8508904252667] 21 | [55.976329887053 47.46384037658572] [56.23245975235477 47.940035191131756] 22 | [58.51687048212625 46.622885352699086] [57.85411081905477 45.95394361577928] 23 | [56.445776311447844 45.162093662656844] [57.36691949656233 47.50097194337286] 24 | [58.243626387557015 46.114052729681134] [56.27224595635198 44.799080066150054] 25 | [57.606924816500396 46.94291057763621] [30.18714230041951 13.877149710431695] 26 | [30.449448810657486 13.490778346545994] [30.295018390286714 13.264889000216499] 27 | [30.160201832884923 11.89278262341395] [31.341509791789576 15.282655921997502] 28 | [31.68601630325429 14.756873246748] [29.325963742565364 12.097849250072613] 29 | [29.54820742388256 13.613295356975868] [28.79359608888626 10.36352064087987] 30 | [31.01284597092308 12.788479208014905] [27.58509216737002 11.47570110601373] 31 | [28.593799561727792 10.780998203903437] [31.356105766724795 15.080316198524088] 32 | [31.25948503636755 13.674329151166603] [32.31590076372959 14.95261758659035] 33 | [30.460413702763617 15.88402809202671] [32.56178203062154 14.586076852632686] 34 | [32.76138648530468 16.239837325178087] [30.1829453331884 14.709592407103628] 35 | [29.55088173528202 15.0651247180067] [29.004155302187428 14.089665298582986] 36 | [29.339624439831823 13.29096065578051] [30.997460327576846 14.551914158277214]]) 37 | 38 | (deftest cluster-test 39 | (let [clusters (map :points (cluster points 2.0 2)) 40 | [c1 c2 c3] (map set clusters)] 41 | (is (= 3 (count clusters))) 42 | (is (= 12 (count c1))) 43 | (doseq [i (range 3 15)] 44 | (is (not (nil? (get c1 (get points i)))))) 45 | 46 | (is (= 24 (count c2))) 47 | (doseq [i (range 15 39)] 48 | (is (not (nil? (get c2 (get points i)))))) 49 | 50 | (is (= 23 (count c3))) 51 | (doseq [i (range 39 62)] 52 | (is (not (nil? (get c3 (get points i)))))))) 53 | 54 | (def point-maps 55 | [{:a 83.08303244924173, :b 58.83387754182331} {:a 45.05445510940626, :b 23.469642649637535} 56 | {:a 14.96417921432294, :b 69.0264096390456} {:a 73.53189604333602, :b 34.896145021310076} 57 | {:a 73.28498173551634, :b 33.96860806993209} {:a 73.45828098873608, :b 33.92584423092194} 58 | {:a 73.9657889183145, :b 35.73191006924026} {:a 74.0074097183533, :b 36.81735596177168} 59 | {:a 73.41247541410848, :b 34.27314856695011} {:a 73.9156256353017, :b 36.83206791547127} 60 | {:a 74.81499205809087, :b 37.15682749846019} {:a 74.03144880081527, :b 37.57399178552441} 61 | {:a 74.51870941207744, :b 38.674258946906775} {:a 74.50754595105536, :b 35.58903978415765} 62 | {:a 74.51322752749547, :b 36.030572259100154} {:a 59.27900996617973, :b 46.41091720294207} 63 | {:a 59.73744793841615, :b 46.20015558367595} {:a 58.81134076672606, :b 45.71150126331486} 64 | {:a 58.52225539437495, :b 47.416083617601544} {:a 58.218626647023484, :b 47.36228902172297} 65 | {:a 60.27139669447206, :b 46.606106348801404} {:a 60.894962462363765, :b 46.976924697402865} 66 | {:a 62.29048673878424, :b 47.66970563563518} {:a 61.03857608977705, :b 46.212924720020965} 67 | {:a 60.16916214139201, :b 45.18193661351688} {:a 59.90036905976012, :b 47.555364347063005} 68 | {:a 62.33003634144552, :b 47.83941489877179} {:a 57.86035536718555, :b 47.31117930193432} 69 | {:a 58.13715479685925, :b 48.985960494028404} {:a 56.131923963548616, :b 46.8508904252667} 70 | {:a 55.976329887053, :b 47.46384037658572} {:a 56.23245975235477, :b 47.940035191131756} 71 | {:a 58.51687048212625, :b 46.622885352699086} {:a 57.85411081905477, :b 45.95394361577928} 72 | {:a 56.445776311447844, :b 45.162093662656844} {:a 57.36691949656233, :b 47.50097194337286} 73 | {:a 58.243626387557015, :b 46.114052729681134} {:a 56.27224595635198, :b 44.799080066150054} 74 | {:a 57.606924816500396, :b 46.94291057763621} {:a 30.18714230041951, :b 13.877149710431695} 75 | {:a 30.449448810657486, :b 13.490778346545994} {:a 30.295018390286714, :b 13.264889000216499} 76 | {:a 30.160201832884923, :b 11.89278262341395} {:a 31.341509791789576, :b 15.282655921997502} 77 | {:a 31.68601630325429, :b 14.756873246748} {:a 29.325963742565364, :b 12.097849250072613} 78 | {:a 29.54820742388256, :b 13.613295356975868} {:a 28.79359608888626, :b 10.36352064087987} 79 | {:a 31.01284597092308, :b 12.788479208014905} {:a 27.58509216737002, :b 11.47570110601373} 80 | {:a 28.593799561727792, :b 10.780998203903437} {:a 31.356105766724795, :b 15.080316198524088} 81 | {:a 31.25948503636755, :b 13.674329151166603} {:a 32.31590076372959, :b 14.95261758659035} 82 | {:a 30.460413702763617, :b 15.88402809202671} {:a 32.56178203062154, :b 14.586076852632686} 83 | {:a 32.76138648530468, :b 16.239837325178087} {:a 30.1829453331884, :b 14.709592407103628} 84 | {:a 29.55088173528202, :b 15.0651247180067} {:a 29.004155302187428, :b 14.089665298582986} 85 | {:a 29.339624439831823, :b 13.29096065578051} {:a 30.997460327576846, :b 14.551914158277214}]) 86 | 87 | (defn find-cluster 88 | [clusters item-pred] 89 | (->> clusters 90 | (map (fn [c] (map #(dissoc % :cluster-id) c))) 91 | (filter (fn [points] (some item-pred points))) 92 | first 93 | set 94 | )) 95 | 96 | (deftest cluster-by-test 97 | (let [clustered (cluster-by point-maps [:a :b] 2.0 2) 98 | clusters (mapv second (vec (group-by :cluster-id clustered)))] 99 | 100 | (let [cluster-1 (find-cluster clusters #(= % (nth point-maps 12)))] 101 | (is (= 12 (count cluster-1))) 102 | (doseq [i (range 3 15)] 103 | (is (not (nil? (get cluster-1 (get point-maps i))))))) 104 | 105 | (let [cluster-2 (find-cluster clusters #(= % (nth point-maps 15)))] 106 | (is (= 24 (count cluster-2))) 107 | (doseq [i (range 15 39)] 108 | (is (not (nil? (get cluster-2 (get point-maps i))))))) 109 | 110 | (let [cluster-3 (find-cluster clusters #(= % (nth point-maps 39)))] 111 | (is (= 23 (count cluster-3))) 112 | (doseq [i (range 39 62)] 113 | (is (not (nil? (get cluster-3 (get point-maps i))))))))) 114 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/clustering/kmeans_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.clustering.kmeans-test 2 | (:require [clojure.test :refer :all] 3 | [clojurewerkz.statistiker.clustering.kmeans :refer :all])) 4 | 5 | (deftest cluster-test 6 | (let [c (cluster [(with-meta [1 1 1] {:a 1}) [2 2 2] [3 3 3] 7 | [50 50 50] [51 51 51] [53 53 53]] 8 | 2 9 | 100) 10 | c (sort-by #(get-in % [:center 0]) c)] 11 | (is (= 1.0 (-> c first :points ffirst))) 12 | (is (= {:a 1} (-> c first :points first meta))) 13 | (is (= 50.0 (-> c second :points ffirst))))) 14 | 15 | (defn find-cluster 16 | [clusters item-pred] 17 | (->> clusters 18 | (filter (fn [points] (some item-pred points))) 19 | first 20 | set)) 21 | 22 | (deftest cluster-by-test 23 | (let [c (cluster-by [{:a 1 :b 1 :c 1} 24 | {:a 2 :b 2 :c 2} 25 | {:a 3 :b 3 :c 3} 26 | {:a 50 :b 50 :c 50} 27 | {:a 51 :b 51 :c 51} 28 | {:a 53 :b 53 :c 53} 29 | {:a 54 :b 54 :c 54}] 30 | [:a :b :c] 31 | 2 32 | 100) 33 | clusters (mapv second (vec (group-by :cluster-id c)))] 34 | (let [cluster-with-1 (find-cluster clusters #(= 1 (:a %)))] 35 | (= 3 (count cluster-with-1)) 36 | (is (some #(= 2 (:a %)) cluster-with-1)) 37 | (is (some #(= 3 (:a %)) cluster-with-1))) 38 | 39 | (let [cluster-with-50 (find-cluster clusters #(= 50 (:a %)))] 40 | (= 4 (count cluster-with-50)) 41 | (is (some #(= 51 (:a %)) cluster-with-50)) 42 | (is (some #(= 53 (:a %)) cluster-with-50)) 43 | (is (some #(= 54 (:a %)) cluster-with-50))))) 44 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/correlation_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.correlation-test 2 | (:require [clojurewerkz.statistiker.correlation :refer :all] 3 | [clojure.test :refer :all])) 4 | 5 | (deftest pearsons-correlation-test 6 | (testing "abslutely correlated items" 7 | (let [v1 [1 2 3 4 5 6] 8 | v2 [1 2 3 4 5 6]] 9 | (is (= 1.0 (pearsons-correlation v1 v2))))) 10 | (testing "negatively correlated items" 11 | (let [v1 [1 2 3 4 5 6] 12 | v2 [6 5 4 3 2 1]] 13 | (is (= -1.0 (pearsons-correlation v1 v2))))) 14 | (testing "abslutely correlated items" 15 | (let [v1 [1 2 3 4 5 6] 16 | v2 [6 7 2 9 10 11]] 17 | (is (< 0.5 (pearsons-correlation v1 v2)))))) 18 | 19 | ;; (println (common-patterns [1 2 3 1 2 4 1 2 5 1 2 3 1 2 4 1 2 5] pearsons-correlation)) 20 | (deftest common-patterns-test 21 | (let [v [112, 118, 132, 129, 121, 135, 148, 148, 136, 119, 104, 118, 22 | 115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114, 140, 23 | 145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166, 24 | 171, 180, 193, 181, 183, 218, 230, 242, 209, 191, 172, 194, 25 | 196, 196, 236, 235, 229, 243, 264, 272, 237, 211, 180, 201, 26 | 204, 188, 235, 227, 234, 264, 302, 293, 259, 229, 203, 229, 27 | 242, 233, 267, 269, 270, 315, 364, 347, 312, 274, 237, 278, 28 | 284, 277, 317, 313, 318, 374, 413, 405, 355, 306, 271, 306, 29 | 315, 301, 356, 348, 355, 422, 465, 467, 404, 347, 305, 336, 30 | 340, 318, 362, 348, 363, 435, 491, 505, 404, 359, 310, 337, 31 | 360, 342, 406, 396, 420, 472, 548, 559, 463, 407, 362, 405, 32 | 417, 391, 419, 461, 472, 535, 622, 606, 508, 461, 390, 432] 33 | pattern-stats (best-repetition-patterns v pearsons-correlation)] 34 | (is (= 12 (ffirst pattern-stats))))) 35 | 36 | (deftest detrend-test 37 | (is (= [0 1 2 0 1 2 0 1 2] (detrend [1 2 3 7 8 9 20 21 22] 3)))) 38 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/distribution_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.distribution-test 2 | (:import [org.apache.commons.math3.distribution EnumeratedRealDistribution]) 3 | (:require [clojure.test :refer :all])) 4 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/entropy_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.entropy-test 2 | (:require [clojurewerkz.statistiker.entropy :refer :all] 3 | [clojure.test :refer :all])) 4 | 5 | (deftest shannon-entropy-test 6 | (is (= (shannon-entropy [1,1,5,1,1]) 7 | (shannon-entropy [1,1,5,1,1]))) 8 | 9 | (is (> (shannon-entropy [1,1,1,1,1]) 10 | (shannon-entropy [1,1,5,1,1])))) 11 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/fitting_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.fitting-test 2 | (:require [clojurewerkz.statistiker.fitting :refer :all] 3 | [clojure.test :refer :all])) 4 | 5 | (deftest gaussian-fitting-test 6 | (comment 7 | (let [[a b c] (gaussian-fitting [[2.8 -48.8] [4.8 50.5] [4.6 42.0] [2.65 -45.5] 8 | [3.8 -13.5] [3.9 -7.0] [4.9 54.0] [5.65 36.0] 9 | [2.96 -54.0] [5.92 14.5]]) 10 | g (gaussian-function a b c)] 11 | 12 | ))) 13 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/metrics_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.metrics-test 2 | (:require [clojurewerkz.statistiker.metrics :refer :all] 3 | [clojurewerkz.statistiker.utils :refer [almost=]] 4 | [clojure.test :refer :all])) 5 | 6 | ; Tolerance for values 7 | (def tol 1e-8) 8 | 9 | (deftest wrong-input-mi 10 | (testing "invalid inputs for mutual-information" 11 | (are [U V] (thrown? AssertionError (mutual-information U V)) 12 | [1 2 3] [2 3] 13 | [] [2 3] 14 | [:b :a :c] [2 :a] 15 | [] [] 16 | [[:b :a] :c] [2 :a]))) 17 | 18 | (deftest wrong-input-ami 19 | (testing "invalid inputs for adjusted-mutual-information" 20 | (are [U V] (thrown? AssertionError (adjusted-mutual-information U V)) 21 | [1 2 3] [2 3] 22 | [] [2 3] 23 | [:b :a :c] [2 :a] 24 | [[:b :a] :c] [2 :a]))) 25 | 26 | (deftest mutual-information-values 27 | (testing "mutual information calculations" 28 | (are [U V s] (almost= (mutual-information U V) s tol) 29 | [3 2 1] [1 2 3] 1.0986122886681096 30 | [1 2 3] [1 2 3] 1.0986122886681096 31 | [1 1 1] [1 1 1] 0.0 32 | [1 2 3 4] [1 1 1 1] 0.0 33 | [:a :a :a :a] [1 1 1 1] 0.0 34 | [:a 6 :d :f] [1 2 3 4] 1.3862943611198906 35 | [0 0 :c2 :c1] [0 0 87 99] 1.0397207708399179 36 | [0 0 'docid2' 'docid'] [0 99 87 0] 0.6931471805599453 37 | [0 1 2 0] [0 1 2 3] 1.0397207708399179))) 38 | 39 | (deftest adjusted-mutual-information-values 40 | (testing "adjusted mututal information calculations" 41 | (are [U V s] (almost= (adjusted-mutual-information U V) s tol) 42 | [1 2 3] [1 2 3] 1.0 43 | [3 2 1] [1 2 3] 1.0 44 | [1 2 3] [:a :b 2] 1.0 45 | [] [] 1.0 46 | [1 2 3 4] [1 1 1 1] 0.0 47 | [:a :a :a :a] [1 1 1 1] 1.0 ;special limit case 48 | [:a 6 :d :f] [1 2 3 4] 1.0 49 | [0 0 :c2 :c1] [0 0 87 99] 1.0 50 | [0 0 'docid2' 'docid'] [0 99 87 0] -0.20000000000000023 51 | [0 1 2 0] [0 1 2 3] 0.0))) 52 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/optimization_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.optimization-test 2 | (:require [clojurewerkz.statistiker.regression :refer :all] 3 | [clojurewerkz.statistiker.utils :refer [almost=]] 4 | [clojurewerkz.statistiker.functions] 5 | [clojure.test :refer :all])) 6 | 7 | (def linear-regression-test-data 8 | [[1.47 52.21] [1.5 53.12] [1.52 54.48] [1.55 55.84] 9 | [1.57 57.2] [1.6 58.57] [1.63 59.93] [1.65 61.29] 10 | [1.68 63.11] [1.7 64.47] [1.73 66.28] [1.75 68.1] 11 | [1.78 69.92] [1.8 72.19] [1.83 74.46]]) 12 | 13 | (deftest linear-regression-test 14 | (let [{:keys [intercept slope]} (linear-regression linear-regression-test-data)] 15 | (is (almost= intercept -39 0.1)) 16 | (is (almost= slope 61 0.3)))) 17 | 18 | (deftest linear-regression2-test 19 | (let [{:keys [intercept slope]} (linear-regression2 linear-regression-test-data 20 | [0 0] 21 | 300)] 22 | (is (almost= intercept -39 0.1)) 23 | (is (almost= slope 61 0.3)))) 24 | 25 | (deftest linear-regression3-test 26 | (let [{:keys [intercept slope]} (linear-regression3 linear-regression-test-data 27 | 100 28 | :fletcher-reeves)] 29 | 30 | (is (almost= intercept -39 0.1)) 31 | (is (almost= slope 61 0.3)))) 32 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/scaling_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.scaling-test 2 | (:require [clojurewerkz.statistiker.scaling :refer :all] 3 | [clojure.test :refer :all])) 4 | 5 | 6 | (deftest rescale-test 7 | (is (= [0.0 0.25 0.5 0.75 1.0] (rescale [2 4 6 8 10]))) 8 | (is (= [0.0 0.25 0.5 0.75 1.0] (rescale [-4 -2 0 2 4])))) 9 | 10 | (deftest rescale-range-test 11 | (is (= [-0.5 -0.25 0.0 0.25 0.5] (rescale-range [-4 -2 0 2 4] -0.5 0.5))) 12 | (is (= [-4.0 -2.0 0.0 2.0 4.0] (rescale-range [-4 -2 0 2 4] -4 4)))) 13 | 14 | (deftest standartise-test 15 | (is (= [(/ -25 (Math/sqrt 1250)) 16 | (/ 25 (Math/sqrt 1250))] 17 | (standartise [50 100])))) 18 | 19 | (deftest lp-norm-wrong-param 20 | (is (thrown? AssertionError (make-lp-normalize-fn -2 [1 2 3]))) 21 | (is (thrown? AssertionError (make-lp-normalize-fn 0 [1 2 3]))) 22 | (is (thrown? AssertionError (make-lp-normalize-fn 3.7 [1 2 3])))) 23 | 24 | (deftest l1-normalize-test 25 | (is (= (map double [(/ 2 10) 26 | (/ 2 10) 27 | (/ 6 10)]) 28 | (l1-normalize [2 2 6])))) 29 | 30 | 31 | (deftest l2-normalize-test 32 | (is (= (map double [(/ 10 (Math/sqrt 125)) 33 | (/ 5 (Math/sqrt 125))]) 34 | (l2-normalize [10 5])))) 35 | 36 | 37 | (deftest scale-feature-test 38 | (is (= [{:a 0.0 :b 1} 39 | {:a 0.25 :b 2} 40 | {:a 0.5 :b 4} 41 | {:a 0.75 :b 4} 42 | {:a 1.0 :b 5}] 43 | (scale-feature [{:a 2 :b 1} 44 | {:a 4 :b 2} 45 | {:a 6 :b 4} 46 | {:a 8 :b 4} 47 | {:a 10 :b 5}] 48 | :a 49 | make-rescale-fn 50 | )))) 51 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/statistics_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.statistics-test 2 | (:require [clojure.test :refer :all] 3 | [clojurewerkz.statistiker.fast-math :refer :all] 4 | [clojurewerkz.statistiker.statistics :refer :all])) 5 | 6 | (deftest mean-test 7 | (is (= 5.5 (mean [1 2 3 4 5 6 7 8 9 10])))) 8 | 9 | (deftest standard-deviation-test 10 | (is (equals 3.02 (standard-deviation [1 2 3 4 5 6 7 8 9 10]) 0.01))) 11 | 12 | (deftest variance-test 13 | (is (equals 9.16 (variance [1 2 3 4 5 6 7 8 9 10]) 0.01))) 14 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/time_series/smoothing_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.time-series.smoothing-test 2 | (:require [clojure.test :refer :all] 3 | [clojurewerkz.statistiker.time-series.smoothing :refer :all])) 4 | 5 | (deftest fixed-size-conj-test 6 | (let [f (fixed-size-conj 5)] 7 | (is (= [2 3 4 5 6] (f [1 2 3 4 5] 6))) 8 | (is (= [1 2 3 4] (f [1 2 3] 4))) 9 | (is (= [4 5 6 7 8] (f [1 2 3 4 5 6 7] 8))))) 10 | 11 | (deftest linear-smooth-seq-test 12 | (is (= [3.0 4.0 5.0 6.0] (linear-smooth-seq 5 [1 2 3 4 5 6 7 8]))) 13 | (let [vals [{:val 1 :meta 1} 14 | {:val 2 :meta 2} 15 | {:val 3 :meta 3} 16 | {:val 4 :meta 4} 17 | {:val 5 :meta 5} 18 | {:val 6 :meta 6} 19 | {:val 7 :meta 7} 20 | {:val 8 :meta 8}]] 21 | (is (= [{:val 3.0 :meta 5} 22 | {:val 4.0 :meta 6} 23 | {:val 5.0 :meta 7} 24 | {:val 6.0 :meta 8}] (linear-smooth-seq 5 vals :val))))) 25 | 26 | (deftest linear-smooth-stream-test 27 | (let [res (atom []) 28 | f (linear-smooth-stream 5 (fn [a] (swap! res conj a)))] 29 | 30 | (doseq [i [1 2 3 4 5 6 7 8]] 31 | (f i)) 32 | 33 | (is (= [3.0 4.0 5.0 6.0] @res))) 34 | (let [res (atom []) 35 | f (linear-smooth-stream 5 (fn [a] (swap! res conj a)) :val)] 36 | 37 | (doseq [i [{:val 1 :meta 1} 38 | {:val 2 :meta 2} 39 | {:val 3 :meta 3} 40 | {:val 4 :meta 4} 41 | {:val 5 :meta 5} 42 | {:val 6 :meta 6} 43 | {:val 7 :meta 7} 44 | {:val 8 :meta 8}]] 45 | (f i)) 46 | 47 | (is (= [{:val 3.0 :meta 5} 48 | {:val 4.0 :meta 6} 49 | {:val 5.0 :meta 7} 50 | {:val 6.0 :meta 8}] 51 | @res)))) 52 | -------------------------------------------------------------------------------- /test/clj/clojurewerkz/statistiker/transform/fft_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.statistiker.transform.fft-test 2 | (:require [clojurewerkz.statistiker.transform.fft :refer :all] 3 | [clojure.test :refer :all] 4 | [clojurewerkz.statistiker.fast-math :as math])) 5 | 6 | (def data [112, 118, 132, 129, 121, 135, 148, 148, 136, 119, 104, 118, 7 | 115, 126, 141, 135, 125, 149, 170, 170, 158, 133, 114, 140, 8 | 145, 150, 178, 163, 172, 178, 199, 199, 184, 162, 146, 166, 9 | 171, 180, 193, 181, 183, 218, 230, 242, 209, 191, 172, 194, 10 | 196, 196, 236, 235, 229, 243, 264, 272, 237, 211, 180, 201, 11 | 204, 188, 235, 227, 234, 264, 302, 293, 259, 229, 203, 229, 12 | 242, 233, 267, 269, 270, 315, 364, 347, 312, 274, 237, 278, 13 | 284, 277, 317, 313, 318, 374, 413, 405, 355, 306, 271, 306, 14 | 315, 301, 356, 348, 355, 422, 465, 467, 404, 347, 305, 336, 15 | 340, 318, 362, 348, 363, 435, 491, 505, 404, 359, 310, 337, 16 | 360, 342, 406, 396, 420, 472, 548, 559, 463, 407, 362, 405, 17 | 417, 391, 419, 461, 472, 535, 622, 606, 508, 461, 390, 432]) 18 | 19 | ;;(deftest transform-test 20 | ;; (doall (map println (sort-by :abs > (take 64 (transform (take 128 data) :forward :standard)))))) 21 | --------------------------------------------------------------------------------