I am looking for linear regression, and k-mean algorithm for KDB, but cannot find it.I guess someone must have already implemented it before. I would appreciate it if someone could share with me. thanks a lotQ
--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To post to this group, send email to personal...@googlegroups.com.
Visit this group at http://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/groups/opt_out.
Yes, there is one available from Andrey Zholos in qml:
/ linreg[y;X]: Performs linear regression of y (vector) on X (list of vectors).
/ This function computes the least squares estimates of parameters and
/ covariance matrix and then calls linregtests to compute test statistics.
/.
/ e.g. exec linreg[price;(1.;sign;quantity)] from trades / 1. for constant
/.
/ Returns dictionary:
/ `X = X (list of row vectors)
/ `y = y (vector)
/ `S = covariance matrix
/ `b = parameter estimates
/ `e = residuals
/ `n = number of observations
/ `m = number of parameters
/ `df = degrees of freedom
linreg1:{[y;X]
if[any[null y:"f"$y]|any{any null x}'[X:"f"$X];'`nulls];
if[$[0=m:count X;1;m>n:count X:flip X];'`length];
Z:.qml.minv[flip[X]mmu X];
ZZ:X mmu Z mmu flip[X];
e:y- yhat:X mmu beta:Z mmu flip[X] mmu y;
linregtests1 ``X`y`S`beta`e`n`m`df`ZZ`Z`yhat!(::;X;y;Z*mmu[e;e]%n-m;beta;e;n;m;n-m;ZZ;Z;yhat)};
If you don’t want to rely on qml then just replace .qml.minv with lsq.
The linregtests1 function calculates some diagnostics numbers. The implementation depends heavily on qml
/ linregtests[R]: Perform linear regression tests on a set of estimation
/ results. This function is called automatically by linreg, but can be called
/ again, for example, if the covariance matrix is adjusted. None of the values
/ returned by linreg are recalculated, in particular, if b is adjusted, e
/ needs to be recalculated.
/.
/ Updates R dictionary with:
/ `se = standard error of estimates vector
/ `tstat = vector of t-statistics
/ `tpval = vector of p-values for t-test
/ `rss = sum of squared residuals
/ `tss = total sum of squares
/ `r2 = R-squared statistic
/ `r2adj = adjusted R-squared
/ `fstat = f-statistic
/ `fpval = p-value for f-test
linregtests1:{[R]
tstat:R[`beta]%se:sqrt R[`S]@'til count R`S;
fstat:(R[`df]*rss-tss:{x mmu x}R[`y]-+/[R`y]%R`n)%(1-R`m)*rss:e mmu e:R`e;
R,m:`se`tstat`tpval`rss`tss`r2`r2adj`fstat`fpval!(se;tstat;
2*1-R[`df] .qml.stcdf/:abs tstat;rss;tss;1-rss%tss;
1-(rss*-1+R`n)%tss*R`df;fstat;1-.qml.fcdf[-1+R`m;R`df;fstat])};
You should exclude it from linreg1 if you don’t need it.
Kim
--
y = -3 - 2x + x^2
q)x: 1.0 2.0 3.0 4.0
q)y: -4.0 -3.0 0.0 5.0
fit:{(enlist y) lsq x xexp/: til 1+z}
q)fit[x;y;2]
-3 -2 1
--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbplus+unsubscribe@googlegroups.com.
To post to this group, send email to personal-kdbplus@googlegroups.com.
Visit this group at https://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.