Skip to content

Commit

Permalink
Merge pull request #8 from Quafadas/main
Browse files Browse the repository at this point in the history
Some basic statistical methods?
  • Loading branch information
dragonfly-ai authored Aug 21, 2023
2 parents 6803a95 + ff8c9e0 commit ee2d24e
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 26 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [3.2.2]
scala: [3.3.0]
java: [temurin@8]
project: [rootJS, rootJVM, rootNative]
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -94,11 +94,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
run: mkdir -p target unidocs/target .js/target site/target vector/js/target vector/native/target verification/target .jvm/target .native/target demo/jvm/target demo/native/target vector/jvm/target demo/js/target project/target
run: mkdir -p target unidocs/target .js/target site/target vector/js/target vector/native/target verification/target tests/js/target .jvm/target .native/target demo/jvm/target tests/jvm/target demo/native/target vector/jvm/target tests/native/target demo/js/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
run: tar cf targets.tar target unidocs/target .js/target site/target vector/js/target vector/native/target verification/target .jvm/target .native/target demo/jvm/target demo/native/target vector/jvm/target demo/js/target project/target
run: tar cf targets.tar target unidocs/target .js/target site/target vector/js/target vector/native/target verification/target tests/js/target .jvm/target .native/target demo/jvm/target tests/jvm/target demo/native/target vector/jvm/target tests/native/target demo/js/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
Expand All @@ -114,7 +114,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [3.2.2]
scala: [3.3.0]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -151,32 +151,32 @@ jobs:
~/Library/Caches/Coursier/v1
key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

- name: Download target directories (3.2.2, rootJS)
- name: Download target directories (3.3.0, rootJS)
uses: actions/download-artifact@v3
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.2-rootJS
name: target-${{ matrix.os }}-${{ matrix.java }}-3.3.0-rootJS

- name: Inflate target directories (3.2.2, rootJS)
- name: Inflate target directories (3.3.0, rootJS)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (3.2.2, rootJVM)
- name: Download target directories (3.3.0, rootJVM)
uses: actions/download-artifact@v3
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.2-rootJVM
name: target-${{ matrix.os }}-${{ matrix.java }}-3.3.0-rootJVM

- name: Inflate target directories (3.2.2, rootJVM)
- name: Inflate target directories (3.3.0, rootJVM)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (3.2.2, rootNative)
- name: Download target directories (3.3.0, rootNative)
uses: actions/download-artifact@v3
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.2-rootNative
name: target-${{ matrix.os }}-${{ matrix.java }}-3.3.0-rootNative

- name: Inflate target directories (3.2.2, rootNative)
- name: Inflate target directories (3.3.0, rootNative)
run: |
tar xf targets.tar
rm targets.tar
Expand All @@ -200,7 +200,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [3.2.2]
scala: [3.3.0]
java: [temurin@8]
runs-on: ${{ matrix.os }}
steps:
Expand Down
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.bloop
.bsp
.js
.jvm
.metals
.native
.vscode
target
project/build.sbt
project/metals.sbt
project/project
41 changes: 35 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val appVersion:String = "0.101"
val globalScalaVersion = "3.2.2"
val globalScalaVersion = "3.3.0"

ThisBuild / organization := "ai.dragonfly"
ThisBuild / organizationName := "dragonfly.ai"
Expand All @@ -19,7 +19,11 @@ ThisBuild / nativeConfig ~= {
}


lazy val vector = crossProject(JSPlatform, JVMPlatform, NativePlatform)
lazy val vector = crossProject(
JSPlatform,
JVMPlatform,
NativePlatform
)
.crossType(CrossType.Full)
.settings(
description := "High performance, low footprint, cross platform, vector and statistics library!",
Expand All @@ -42,7 +46,11 @@ lazy val verification = project
)
)

lazy val demo = crossProject(JSPlatform, JVMPlatform, NativePlatform)
lazy val demo = crossProject(
JSPlatform,
JVMPlatform,
NativePlatform
)
.crossType(CrossType.Full)
.enablePlugins(NoPublishPlugin)
.dependsOn(vector)
Expand All @@ -61,7 +69,7 @@ lazy val demo = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.jvmSettings()
.nativeSettings()

lazy val root = tlCrossRootProject.aggregate(vector).settings(name := "vector")
lazy val root = tlCrossRootProject.aggregate(vector, tests).settings(name := "vector")

lazy val docs = project.in(file("site")).enablePlugins(TypelevelSitePlugin).settings(
mdocVariables := Map(
Expand All @@ -76,5 +84,26 @@ lazy val unidocs = project
.enablePlugins(TypelevelUnidocPlugin) // also enables the ScalaUnidocPlugin
.settings(
name := "vector-docs",
ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(vector.jvm, vector.js, vector.native)
)
ScalaUnidoc / unidoc / unidocProjectFilter :=
inProjects(
vector.jvm,
vector.js,
vector.native
)
)

lazy val tests = crossProject(
JVMPlatform,
JSPlatform,
NativePlatform
)
.in(file("tests"))
.enablePlugins(NoPublishPlugin)
.dependsOn(vector)
.settings(
name := "vector-tests",
libraryDependencies += "org.scalameta" %%% "munit" % "1.0.0-M8" % Test
)
// .jvmSettings(name := "tests-jvm")
// .jsSettings(name := "tests-js")
// .nativeSettings(name := "tests-native")
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.8.3
sbt.version=1.9.2
6 changes: 3 additions & 3 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
val crossVer = "1.2.0"
val scalaJSVersion = "1.11.0"
val scalaNativeVersion = "0.4.7"
val crossVer = "1.3.2"
val scalaJSVersion = "1.13.2"
val scalaNativeVersion = "0.4.14"

addDependencyTreePlugin

Expand Down
46 changes: 46 additions & 0 deletions tests/shared/src/test/scala/Instantiate.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import ai.dragonfly.math.vector.Vec
import narr.NArray

class InstantiateTests extends munit.FunSuite:

test(" ways of making vecs ") {

//type dim = 5

val v = Vec.fromTuple(1.0, 2.0, 3.0, 4.0, 5.0)
val v2 = Vec[5](1.0, 2.0, 3.0, 4.0, 5.0)
val v_fill = Vec.fill[5](1.0)

val v_zeros = Vec.zeros[5]
val v_ones = Vec.ones[5]


val v_rand = Vec.random[5]()
val v_rand_max_min = Vec.random[5](2.0, 0.5)

assertEquals(v2.dimension, v2.dimension )
assertEquals(v2.dimension, v_fill.dimension )
assertEquals(v2.dimension, v_zeros.dimension )
assertEquals(v2.dimension, v_rand.dimension )
assertEquals(v2.dimension, v_rand.dimension )
assertEquals(v2.dimension, v_rand_max_min.dimension )

}

end InstantiateTests
85 changes: 85 additions & 0 deletions tests/shared/src/test/scala/SimpleStats.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2023 dragonfly.ai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import ai.dragonfly.math.vector.Vec
import narr.NArray

class SimpleStatsTests extends munit.FunSuite:

test("Some basic properties") {

val v = Vec.fromTuple(1.0, 2.0, 3.0, 4.0, 5.0)
val v2 = Vec.fromTuple(1.0, 2.0, 3.0, 4.0, 5.0)

assertEquals(v.dimension, v2.dimension )

// Reference, not value equality!
assertNotEquals(v, v2 )
}

test("sample mean") {
val v = Vec.fromTuple(2.0,4.0,4.0,4.0,5.0,5.0,7.0,9.0)
assertEquals(v.mean , 5.0)
}

test("sample variance and std") {
val v = Vec.fromTuple(2.0,4.0,4.0,4.0,5.0,5.0,7.0,9.0)
assertEqualsDouble(v.variance, 4.571429, 0.00001)
assertEqualsDouble(v.stdDev, 2.13809, 0.00001)
}

test("sample covariance") {
// Sample version
// https://corporatefinanceinstitute.com/resources/data-science/covariance/

val vector1 = Vec.fromTuple(1692.0, 1978.0, 1884.0, 2151.0, 2519.0)
val vector2 = Vec.fromTuple(68.0, 102.0, 110.0, 112.0, 154.0)

val result = vector1.covariance(vector2)
println(result)
assertEqualsDouble(result, 9107.3, 0.001)

}

test("pearson correlation coefficient") {
// https://www.statisticshowto.com/probability-and-statistics/correlation-coefficient-formula/
val v1 = Vec.fromTuple(43.0, 21.0, 25.0, 42.0, 57.0, 59.0)
val v2 = Vec.fromTuple(99.0, 65.0, 79.0, 75.0, 87.0, 81.0)
assertEqualsDouble(v1.pearsonCorrelationCoefficient(v2), 0.529809, 0.0001)

}

test("element rank") {
val v = Vec.fromTuple(1.0, 5.0, 3.0, 6.0, 1.0, 5.0)
/*
1.0 is the first, but has as tied rank. Take the average - 1.5
*/
assertEquals(v.elementRanks.csv(), Array[Double](1.5,4.5,3.0,6.0,1.5,4.5).mkString(","))
}

test("spearmans rank") {
// https://statistics.laerd.com/statistical-guides/spearmans-rank-order-correlation-statistical-guide-2.php
val v1 = Vec.fromTuple(56.0, 75.0, 45.0, 71.0, 62.0, 64.0, 58.0, 80.0, 76.0, 61.0)
val v2 = Vec.fromTuple(66.0, 70.0, 40.0, 60.0, 65.0, 56.0, 59.0, 77.0, 67.0, 63.0)
assertEqualsDouble(v1.spearmansRankCorrelation(v2), 0.6727, 0.001 )

// https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient

val v3 = Vec[10](86.0, 97.0, 99.0, 100.0, 101.0, 103.0, 106.0, 110.0, 112.0, 113.0)
val v4 = Vec[10](2, 20.0, 28.0, 27.0, 50.0, 29.0, 7.0, 17.0, 6.0, 12.0)
assertEqualsDouble(-0.1757575, v3.spearmansRankCorrelation(v4), 0.000001);
}
end SimpleStatsTests
Loading

0 comments on commit ee2d24e

Please sign in to comment.