Skip to content

Commit d6d5baf

Browse files
committed
Least squares
1 parent ae94da7 commit d6d5baf

11 files changed

+247
-2
lines changed

.cache

7.29 KB
Binary file not shown.

.classpath

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<classpath>
2+
<classpathentry output="target/scala-2.10/classes" path="src/main/scala" kind="src"></classpathentry>
3+
<classpathentry output="target/scala-2.10/classes" path="src/main/java" kind="src"></classpathentry>
4+
<classpathentry output="target/scala-2.10/test-classes" path="src/test/scala" kind="src"></classpathentry>
5+
<classpathentry output="target/scala-2.10/test-classes" path="src/test/java" kind="src"></classpathentry>
6+
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"></classpathentry>
7+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.scalanlp/breeze-math_2.10/jars/breeze-math_2.10-0.3.jar" kind="lib"></classpathentry>
8+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.scalanlp/breeze-core_2.10/jars/breeze-core_2.10-0.3.jar" kind="lib"></classpathentry>
9+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/com.thoughtworks.paranamer/paranamer/jars/paranamer-2.2.jar" kind="lib"></classpathentry>
10+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/net.sf.opencsv/opencsv/jars/opencsv-2.3.jar" kind="lib"></classpathentry>
11+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/com.typesafe/scalalogging-log4j_2.10/jars/scalalogging-log4j_2.10-1.0.1.jar" kind="lib"></classpathentry>
12+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.10.0.jar" kind="lib"></classpathentry>
13+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.apache.logging.log4j/log4j-core/jars/log4j-core-2.0-beta4.jar" kind="lib"></classpathentry>
14+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.apache.logging.log4j/log4j-api/jars/log4j-api-2.0-beta4.jar" kind="lib"></classpathentry>
15+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/com.googlecode.netlib-java/netlib-java/jars/netlib-java-0.9.3.jar" kind="lib"></classpathentry>
16+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/net.sourceforge.f2j/arpack_combined_all/jars/arpack_combined_all-0.1.jar" kind="lib"></classpathentry>
17+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.jblas/jblas/jars/jblas-1.2.3.jar" kind="lib"></classpathentry>
18+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.apache.commons/commons-math3/jars/commons-math3-3.2.jar" kind="lib"></classpathentry>
19+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.scalanlp/breeze-learn_2.10/jars/breeze-learn_2.10-0.3.jar" kind="lib"></classpathentry>
20+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.scalanlp/breeze-process_2.10/jars/breeze-process_2.10-0.3.jar" kind="lib"></classpathentry>
21+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/de.bwaldvogel/liblinear/jars/liblinear-1.8.jar" kind="lib"></classpathentry>
22+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.scala-lang/scala-actors/jars/scala-actors-2.10.0.jar" kind="lib"></classpathentry>
23+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.scalanlp/breeze-viz_2.10/jars/breeze-viz_2.10-0.3.jar" kind="lib"></classpathentry>
24+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/jfree/jcommon/jars/jcommon-1.0.16.jar" kind="lib"></classpathentry>
25+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/jfree/jfreechart/jars/jfreechart-1.0.13.jar" kind="lib"></classpathentry>
26+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.apache.xmlgraphics/xmlgraphics-commons/jars/xmlgraphics-commons-1.3.1.jar" kind="lib"></classpathentry>
27+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/commons-io/commons-io/jars/commons-io-1.3.1.jar" kind="lib"></classpathentry>
28+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/commons-logging/commons-logging/jars/commons-logging-1.0.4.jar" kind="lib"></classpathentry>
29+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/com.lowagie/itext/jars/itext-2.1.5.jar" kind="lib"></classpathentry>
30+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/bouncycastle/bcmail-jdk14/jars/bcmail-jdk14-138.jar" kind="lib"></classpathentry>
31+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/bouncycastle/bcprov-jdk14/jars/bcprov-jdk14-138.jar" kind="lib"></classpathentry>
32+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/com.github.tototoshi/scala-csv_2.10/jars/scala-csv_2.10-1.0.0-SNAPSHOT.jar" kind="lib"></classpathentry>
33+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.specs2/specs2_2.10/jars/specs2_2.10-1.13.jar" kind="lib"></classpathentry>
34+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.specs2/scalaz-core_2.10/jars/scalaz-core_2.10-7.0.0.jar" kind="lib"></classpathentry>
35+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.specs2/scalaz-concurrent_2.10/jars/scalaz-concurrent_2.10-7.0.0.jar" kind="lib"></classpathentry>
36+
<classpathentry path="/Users/sasakiumi/.ivy2/cache/org.specs2/scalaz-effect_2.10/jars/scalaz-effect_2.10-7.0.0.jar" kind="lib"></classpathentry>
37+
<classpathentry path="org.eclipse.jdt.launching.JRE_CONTAINER" kind="con"></classpathentry>
38+
<classpathentry path="bin" kind="output"></classpathentry>
39+
</classpath>

.project

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<projectDescription>
2+
<name>cpi-stats</name>
3+
<buildSpec>
4+
<buildCommand>
5+
<name>org.scala-ide.sdt.core.scalabuilder</name>
6+
</buildCommand>
7+
</buildSpec>
8+
<natures>
9+
<nature>org.scala-ide.sdt.core.scalanature</nature>
10+
<nature>org.eclipse.jdt.core.javanature</nature>
11+
</natures>
12+
</projectDescription>

build.sbt

+22-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,31 @@ organization := "com.PhysicsEngine"
44

55
version := "0.1.0-SNAPSHOT"
66

7-
scalaVersion := "2.10.0"
7+
scalaVersion := "2.10.2"
88

99
libraryDependencies ++= Seq(
1010
"org.specs2" %% "specs2" % "1.13" % "test"
1111
)
1212

13+
14+
libraryDependencies ++= Seq(
15+
// other dependencies here
16+
// pick and choose:
17+
"org.scalanlp" % "breeze-math_2.10" % "0.3",
18+
"org.scalanlp" % "breeze-learn_2.10" % "0.3",
19+
"org.scalanlp" % "breeze-process_2.10" % "0.3",
20+
"org.scalanlp" % "breeze-viz_2.10" % "0.3"
21+
)
22+
23+
resolvers ++= Seq(
24+
// other resolvers here
25+
// if you want to use snapshot builds (currently 0.5-SNAPSHOT), use this.
26+
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
27+
"Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/releases/"
28+
)
29+
30+
resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
31+
32+
libraryDependencies += "com.github.tototoshi" %% "scala-csv" % "1.0.0-SNAPSHOT"
33+
1334
initialCommands := "import com.PhysicsEngine.cpistats._"

employee.csv

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
全国,2008年度,全産業,総数,雇用者総数,5520,万人
2+
全国,2007年度,全産業,総数,雇用者総数,5523,万人
3+
全国,2006年度,全産業,総数,雇用者総数,5486,万人
4+
全国,2005年度,全産業,総数,雇用者総数,5420,万人
5+
全国,2004年度,全産業,総数,雇用者総数,5355,万人
6+
全国,2003年度,全産業,総数,雇用者総数,5340,万人
7+
全国,2002年度,全産業,総数,雇用者総数,5329,万人
8+
全国,2001年度,全産業,総数,雇用者総数,5354,万人
9+
全国,2000年度,全産業,総数,雇用者総数,5372,万人
10+
全国,1999年度,全産業,総数,雇用者総数,5325,万人
11+
全国,1998年度,全産業,総数,雇用者総数,5353,万人
12+
全国,1997年度,全産業,総数,雇用者総数,5392,万人
13+
全国,1996年度,全産業,総数,雇用者総数,5347,万人
14+
全国,1995年度,全産業,総数,雇用者総数,5279,万人
15+
全国,1994年度,全産業,総数,雇用者総数,5243,万人
16+
全国,1993年度,全産業,総数,雇用者総数,5213,万人
17+
全国,1992年度,全産業,総数,雇用者総数,5141,万人
18+
全国,1991年度,全産業,総数,雇用者総数,5036,万人
19+
全国,1990年度,全産業,総数,雇用者総数,4882,万人
20+
全国,1989年度,全産業,総数,雇用者総数,4711,万人
21+
全国,1988年度,全産業,総数,雇用者総数,4572,万人
22+
全国,1987年度,全産業,総数,雇用者総数,4452,万人
23+
全国,1986年度,全産業,総数,雇用者総数,4382,万人
24+
全国,1985年度,全産業,総数,雇用者総数,4328,万人
25+
全国,1984年度,全産業,総数,雇用者総数,4281,万人
26+
全国,1983年度,全産業,総数,雇用者総数,4223,万人
27+
全国,1982年度,全産業,総数,雇用者総数,4125,万人
28+
全国,1981年度,全産業,総数,雇用者総数,4048,万人
29+
全国,1980年度,全産業,総数,雇用者総数,3997,万人
30+
全国,1979年度,全産業,総数,雇用者総数,3896,万人
31+
全国,1978年度,全産業,総数,雇用者総数,3811,万人
32+
全国,1977年度,全産業,総数,雇用者総数,3773,万人
33+
全国,1976年度,全産業,総数,雇用者総数,3726,万人
34+
全国,1975年度,全産業,総数,雇用者総数,3669,万人
35+
全国,1974年度,全産業,総数,雇用者総数,3638,万人
36+
全国,1973年度,全産業,総数,雇用者総数,3625,万人
37+
全国,1972年度,全産業,総数,雇用者総数,3487,万人
38+
全国,1971年度,全産業,総数,雇用者総数,3423,万人
39+
全国,1970年度,全産業,総数,雇用者総数,3340,万人
40+
全国,1969年度,全産業,総数,雇用者総数,3227,万人
41+
全国,1968年度,全産業,総数,雇用者総数,3164,万人
42+
全国,1967年度,全産業,総数,雇用者総数,3091,万人
43+
全国,1966年度,全産業,総数,雇用者総数,3013,万人
44+
全国,1965年度,全産業,総数,雇用者総数,2913,万人
45+
全国,1964年度,全産業,総数,雇用者総数,2788,万人
46+
全国,1963年度,全産業,総数,雇用者総数,2696,万人
47+
全国,1962年度,全産業,総数,雇用者総数,2602,万人
48+
全国,1961年度,全産業,総数,雇用者総数,2505,万人
49+
全国,1960年度,全産業,総数,雇用者総数,2404,万人
50+
全国,1959年度,全産業,総数,雇用者総数,2274,万人
51+
全国,1958年度,全産業,総数,雇用者総数,2167,万人
52+
全国,1957年度,全産業,総数,雇用者総数,2072,万人
53+
全国,1956年度,全産業,総数,雇用者総数,1957,万人
54+
全国,1955年度,全産業,総数,雇用者総数,1817,万人
55+
全国,1954年度,全産業,総数,雇用者総数,1713,万人
56+
全国,1953年度,全産業,総数,雇用者総数,1664,万人

project/plugins.sbt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resolvers += Classpaths.typesafeResolver
2+
3+
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.2")

src/main/scala/App.scala

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.PhysicsEngine.cpistats
2+
import breeze.linalg._
3+
import scala.io.Source
4+
import java.io._
5+
import com.github.tototoshi.csv._
6+
27

38
object App {
49
def main(args: Array[String]) {
5-
print("Hello com.PhysicsEngine.cpi-stats!")
10+
val data = EmployeeData.read()
11+
12+
//val data = List[Pair[Double,Double]]((0.07218849, 1.47822651), (1.61854596, 11.57308266), (2.74441742, 23.53791146), (3.36337206, 36.71027063), (4.96067527, 40.60451491), (5.13515737, 58.41530339), ( 6.43154354, 64.22937428), (7.68320999, 79.51190066), (8.3692042, 91.04207353), ( 9.85696038, 97.7546571))
13+
//val data = List[Pair[Double,Double]]((1.0,1.0),(2.0,2.0),(3.0,3.0))
14+
val wlist = LeastSquares.estimate(data)
15+
print("at 2009: " + LinearEquation.y(2009.0, wlist))
616
}
717
}

src/main/scala/EmployeeData.scala

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.PhysicsEngine.cpistats
2+
import java.io._
3+
import com.github.tototoshi.csv._
4+
5+
object EmployeeData {
6+
def read() = {
7+
val reader = CSVReader.open(new File("employee.csv"))
8+
var l = List[Pair[Double, Double]]()
9+
10+
reader.foreach {
11+
fields => {
12+
l = (fields(1).substring(0,4).toDouble, fields(5).toDouble) :: l
13+
}
14+
}
15+
16+
reader.close()
17+
l
18+
}
19+
20+
}

src/main/scala/LeastSquares.scala

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.PhysicsEngine.cpistats
2+
import breeze.linalg._
3+
4+
object LeastSquares {
5+
def estimate(dataList:List[Pair[Double,Double]]) = {
6+
val M = 3
7+
val dataLength = dataList.length
8+
var A = DenseMatrix.zeros[Double](M+1, M+1)
9+
var T = DenseVector.zeros[Double](M+1)
10+
11+
for(i <- 0 to M) {
12+
for( j <- 0 to M) {
13+
var temp = 0.0
14+
dataList.foreach {
15+
dataPair => {
16+
temp += Math.pow(dataPair._1, i+j)
17+
}
18+
}
19+
A(i, j) = temp
20+
}
21+
}
22+
//print(A)
23+
24+
for(i <- 0 to M) {
25+
var temp = 0.0
26+
dataList.foreach {
27+
dataPair => {
28+
temp += Math.pow(dataPair._1, i) * dataPair._2
29+
}
30+
}
31+
T(i) = temp
32+
}
33+
//print(T)
34+
35+
val wlist = A \ T
36+
wlist
37+
}
38+
39+
40+
}

src/main/scala/LinearEquation.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.PhysicsEngine.cpistats
2+
import breeze.linalg._
3+
4+
object LinearEquation {
5+
val M = 3
6+
def y(x:Double, wlist:DenseVector[Double]) = {
7+
var ret = wlist(0)
8+
for( i <- 1 to M) {
9+
ret += wlist(i) * Math.pow(x, i)
10+
}
11+
ret
12+
}
13+
14+
}

src/main/scala/StatsRequest.scala

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.PhysicsEngine.cpistats
2+
import scala.io.Source
3+
import scala.xml.XML
4+
import scala.xml.parsing.{ConstructingParser,XhtmlParser}
5+
6+
class StatsRequest {
7+
val appId = "904f6b530a97c03f66afe10ec54013d7123682ac"
8+
9+
def statsList(params:Map[String,String]) = {
10+
var url = "http://statdb.nstac.go.jp/api/1.0b/app/getStatsList?lang=J&appId=" + appId + "&"
11+
params.foreach {
12+
case(key, value) => url += key + "=" + value + "&"
13+
}
14+
val src = Source.fromURL(url)
15+
val xml = XML.loadString( src.getLines.mkString )
16+
17+
val titles = xml \\ "TITLE"
18+
19+
println(titles.size)
20+
}
21+
22+
def statsData(params:Map[String,String]) = {
23+
var url = "http://statdb.nstac.go.jp/api/1.0b/app/getStatsData?lang=E&appId=" + appId + "&"
24+
params.foreach {
25+
case(key, value) => url += key + "=" + value + "&"
26+
}
27+
val src = Source.fromURL(url)
28+
val xml = XML.loadString( src.getLines.mkString )
29+
}
30+
}

0 commit comments

Comments
 (0)