cibotech / scalastan Goto Github PK
View Code? Open in Web Editor NEWScala wrapper for Stan
License: BSD 3-Clause "New" or "Revised" License
Scala wrapper for Stan
License: BSD 3-Clause "New" or "Revised" License
If a ScalaStan data transform references a parameter, the Stan program will fail to compile. So ScalaStan should catch this earlier (ideally at the type level, but at least before running stanc).
Right now it's possible to generate invalid Stan code using ~
. For example:
val x = data(real())
val model = new Model {
x ~ stan.poisson(1.0)
}
The Stan for this will not compile because x
is real
. Changing x
to int
fixes the problem. ScalaStan should identify this before generating Stan code (ideally at Scala compile time).
I tried to use the ScalaStan for some work, but when I running the examples,the IDEA told me like this:
I thought it might be something wrong when compiling the model, I can find the model.stan file and model.hpp,and I can use CmdStan to compile them successfully;
My OS is Windows,I guess it may can't compile for windows shell according to the codes.
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1 at java.nio.charset.CoderResult.throwException(CoderResult.java:281) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:161) at java.io.BufferedReader.readLine(BufferedReader.java:324) at java.io.BufferedReader.readLine(BufferedReader.java:389) at scala.io.BufferedSource$BufferedLineIterator.hasNext(BufferedSource.scala:70) at scala.collection.Iterator.foreach(Iterator.scala:937) at scala.collection.Iterator.foreach$(Iterator.scala:937) at scala.collection.AbstractIterator.foreach(Iterator.scala:1425) at com.cibo.scalastan.CommandRunner.runCommand(CommandRunner.scala:21) at com.cibo.scalastan.CommandRunner.runCommand$(CommandRunner.scala:18) at com.cibo.scalastan.run.CmdStanCompiler$.runCommand(CmdStanCompiler.scala:20) at com.cibo.scalastan.run.CmdStanCompiler$.runMake(CmdStanCompiler.scala:79) at com.cibo.scalastan.run.CmdStanCompiler$.compile(CmdStanCompiler.scala:155) at com.cibo.scalastan.TransformedModel.compile(TransformedModel.scala:26) at com.cibo.scalastan.StanModel.compile(StanModel.scala:241) at com.cibo.scalastan.StanModel.compile$(StanModel.scala:241) at com.huawei.algtech.ai.stan.MyModel$.compile(MyApp.scala:15) at com.huawei.algtech.ai.stan.MyApp$.delayedEndpoint$com$huawei$algtech$ai$stan$MyApp$1(MyApp.scala:33) at com.huawei.algtech.ai.stan.MyApp$delayedInit$body.apply(MyApp.scala:27) at scala.Function0.apply$mcV$sp(Function0.scala:34) at scala.Function0.apply$mcV$sp$(Function0.scala:34) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) at scala.App.$anonfun$main$1$adapted(App.scala:76) at scala.collection.immutable.List.foreach(List.scala:388) at scala.App.main(App.scala:76) at scala.App.main$(App.scala:74)
The type checking of the lkj_corr
distribution doesn't work quite right because we don't know the size of the matrix when the distribution is created. So ScalaStan has a second version that takes the size as an explicit argument. It would be nice if we didn't have to do that.
For debugging and troubleshooting, it would be very helpful to capture the output from Stan and place it in StanResults, or in CompiledModel if compiling.
ScalaStan won't generate code for a transformed parameter that isn't referenced in the model. The assumption being that it's not used, but you could still reference the output of a transformed parameter (using it as a generated quantity). ScalaStan should either raise an exception when this happens or just handle it like we handle generated quantities.
CmdStan supports an init
argument to specify either the range of values to try for initialization or a file with initial values. ScalaStan should support both of these, perhaps as an optional argument to run
.
CSVDataSource currently does its own CSV parsing in a very simple way. In the interest of supporting a larger variety of CSV files, it might be worth considering a CSV library (opencsv?).
Currently, stan.reject
will take zero arguments, but this results in an invalid Stan program.
Requested from the Stan forums. Using Command Stan requires that ScalaStan read/write files. It would be nice to avoid using the file system.
As reported on the Stan forums, there appears to be an issue when running a ScalaStan model from multiple threads with the same seed.
When ScalaStan is used as a library, it would be nice to be able to check if it's going to be possible to run the model without actually trying.
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/cibo/scalastan/Implicits$class
here is my code, copy for example:
import com.cibo.scalastan.ScalaStan
object myModel extends App with ScalaStan {
val n = data(int(lower = 0))
val x = data(vector(n))
val y = data(vector(n))
val b = parameter(real())
val m = parameter(real())
val sigma = parameter(real(lower = 0))
val model = new Model {
sigma ~ stan.cauchy(0, 1)
y ~ stan.normal(m * x + b, sigma)
}
val xs: Seq[Double] = ???
val ys: Seq[Double] = ???
val results = model
.withData(x, xs)
.withData(y, ys)
.run(chains = 5)
results.summary(System.out)
}
maven pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>gmm</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.cibotech</groupId>
<artifactId>ScalaStan</artifactId>
<version>v0.5.9-rc1</version>
</dependency>
</dependencies>
</project>
If result
isn’t referenced from a transform or generated quantity, ScalaStan should throw an exception since the code is probably wrong.
Currently, generated quantities are somewhat special in ScalaStan because code generation is tied to the Model object and generated quantities exist completely outside of the model. To deal with this, ScalaStan simply outputs all generated quantities created within the ScalaStan
trait. One way around this would be to move the code for generated quantities inside the model block. Unfortunately, this would break backwards compatibility.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.