If you know what this is, you know whether you need it.
Suppose you have a data set for a function, mapping X to Y.
It is "linear" if the formula for the function is a "line":
y = B * x + A
Suppose you have a set of "points" - (x,y) - for which the "y" value is one possible output from the "x" input.
Pure math, there is no fuzz; x always maps to exactly y.
Real world, y may vary.
We see something like this in KoL. A monster may have "X" attack, but they throw in randomness, and you may see X as X-5 to X+5.
There may be a triangular distribution or a uniform distribution, but, whatever. The value varies.
We also see this in functions in KoL: given input X, the resulting Y may vary from y-10 to y+10, say.
Because KoL loves throwing in randomness.
If you want to figure out the linear formula - i.e. the A and B constants - given a set of (X,Y) values, Linear Regression is your friend.
I have two little ASH libraries you can use to do this. The base API:
Ezandora used the "least squares" algorithm to calculate this in BastilleRelay.ash. It's O(N) - it makes one pass through the data - to calculate A&B. I extracted it, inserted it into this API, and fixed several bugs.
Use
and there you are.
I also found a Java implementation of the "least squares" algorithm on the Web from Princeton to accompany a textbook. I translated it from Java to ASH (which was trivial) and wrapped it in this API and here you are. This is also O(N), although it makes two passes through the data. The first calculates A&B . The second calculates statistical analysis - including R2 - which is a number from 0.0 to 1.0, telling you how well the line "fits" your data. This implementation provides this:
and the "to_string()" method also includes "R2".
to get this.
So. If you want to actually process your data at runtime, use "SimpleLinearRegression". If you want to keep building up your data set and see how well your data is "fitting" - i.e., R2 is approaching 1.0 - with the intent of building in the actual formulas - O(0) - into your program, use LinearRegression.
The former was created by Ezandora, refined and bug-fixed by me, and has no license.
The latter is under GPL - translated from Java to ASH - which still counts as a "derivative" work - so you have to provide your source code when you distribute it. Since you are publishing an ASH script, not a binary, duh. Not a problem.
I'm using this for a project I am working on in which I am highly optimistic at being able to understand some KoL internals based on user-observed behavior.
Suppose you have a data set for a function, mapping X to Y.
It is "linear" if the formula for the function is a "line":
y = B * x + A
Suppose you have a set of "points" - (x,y) - for which the "y" value is one possible output from the "x" input.
Pure math, there is no fuzz; x always maps to exactly y.
Real world, y may vary.
We see something like this in KoL. A monster may have "X" attack, but they throw in randomness, and you may see X as X-5 to X+5.
There may be a triangular distribution or a uniform distribution, but, whatever. The value varies.
We also see this in functions in KoL: given input X, the resulting Y may vary from y-10 to y+10, say.
Because KoL loves throwing in randomness.
If you want to figure out the linear formula - i.e. the A and B constants - given a set of (X,Y) values, Linear Regression is your friend.
I have two little ASH libraries you can use to do this. The base API:
Code:
record Point
{
float x;
float y;
};
record Coefficients
{
// best-fit line: y = B x + A
float intercept; // intercept A of the best-fit line
float slope; // slope B of the best-fit line
};
Coefficients linear_regression(Point[] points);
string to_string(Coefficients c);
float predict(int x, Coefficients c);
Ezandora used the "least squares" algorithm to calculate this in BastilleRelay.ash. It's O(N) - it makes one pass through the data - to calculate A&B. I extracted it, inserted it into this API, and fixed several bugs.
Use
Code:
import <SimpleLinearRegression.ash>;
I also found a Java implementation of the "least squares" algorithm on the Web from Princeton to accompany a textbook. I translated it from Java to ASH (which was trivial) and wrapped it in this API and here you are. This is also O(N), although it makes two passes through the data. The first calculates A&B . The second calculates statistical analysis - including R2 - which is a number from 0.0 to 1.0, telling you how well the line "fits" your data. This implementation provides this:
Code:
record Coefficients
{
// best-fit line: y = β x + α
float intercept; // intercept α of the best-fit line
float slope; // slope β of the best-fit line
float r; // coefficient of determination
float svar0; // intercept standard error
float svar1; // slope standard error
};
and the "to_string()" method also includes "R2".
Code:
import <LinearRegression.ash>;
So. If you want to actually process your data at runtime, use "SimpleLinearRegression". If you want to keep building up your data set and see how well your data is "fitting" - i.e., R2 is approaching 1.0 - with the intent of building in the actual formulas - O(0) - into your program, use LinearRegression.
The former was created by Ezandora, refined and bug-fixed by me, and has no license.
The latter is under GPL - translated from Java to ASH - which still counts as a "derivative" work - so you have to provide your source code when you distribute it. Since you are publishing an ASH script, not a binary, duh. Not a problem.
I'm using this for a project I am working on in which I am highly optimistic at being able to understand some KoL internals based on user-observed behavior.