楼主: Scalachen
1908 1

Local Regression using Julia [推广有奖]

  • 0关注
  • 0粉丝

已卖:147份资源

本科生

56%

还不是VIP/贵宾

-

TA的文库  其他...

Haskell NewOccidental

Splunk NewOccidental

Apache Storm NewOccidental

威望
0
论坛币
5149 个
通用积分
0
学术水平
9 点
热心指数
11 点
信用等级
9 点
经验
1156 点
帖子
24
精华
1
在线时间
0 小时
注册时间
2015-3-29
最后登录
2017-8-22

楼主
Scalachen 发表于 2015-3-31 21:15:06 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. module Loess

  2. import Iterators.product
  3. import Distances.euclidean

  4. export loess, predict

  5. include("kd.jl")


  6. type LoessModel{T <: FloatingPoint}
  7.         # An n by m predictor matrix containing n observations from m predictors
  8.         xs::AbstractMatrix{T}

  9.         # A length n response vector
  10.         ys::AbstractVector{T}

  11.         # Least squares coefficients
  12.         bs::Matrix{T}

  13.         # kd-tree vertexes mapped to indexes
  14.         verts::Dict{Vector{T}, Int}

  15.         kdtree::KDTree{T}
  16. end


  17. # Fit a loess model.
  18. #
  19. # Args:
  20. #   xs: A n by m matrix with n observations from m independent predictors
  21. #   ys: A length n response vector.
  22. #   normalize: Normalize the scale of each predicitor. (default true when m > 1)
  23. #   span: The degree of smoothing, typically in [0,1]. Smaller values result in smaller
  24. #       local context in fitting.
  25. #   degree: Polynomial degree.
  26. #
  27. # Returns:
  28. #   A fit LoessModel.
  29. #
  30. function loess{T <: FloatingPoint}(xs::AbstractVector{T}, ys::AbstractVector{T};
  31.                                                normalize::Bool=true, span::T=0.75, degree::Int=2)
  32.         loess(reshape(xs, (length(xs), 1)), ys, normalize=normalize, span=span, degree=degree)
  33. end


  34. function loess{T <: FloatingPoint}(xs::AbstractMatrix{T}, ys::AbstractVector{T};
  35.                                        normalize::Bool=true, span::T=0.75, degree::Int=2)
  36.         if size(xs, 1) != size(ys, 1)
  37.                 error("Predictor and response arrays must of the same length")
  38.         end

  39.         n, m = size(xs)
  40.         q = iceil(span * n)

  41.         # TODO: We need to keep track of how we are normalizing so we can
  42.         # corerctly apply predict to unnormalized data. We should have a normalize
  43.         # function that just returns a vector of scaling factors.
  44.         if normalize && m > 1
  45.                 xs = copy(xs)
  46.                 normalize!(xs)
  47.         end

  48.         kdtree = KDTree(xs, 0.05 * span)
  49.         verts = Array(T, (length(kdtree.verts), m))

  50.         # map verticies to their index in the bs coefficient matrix
  51.         verts = Dict{Vector{T}, Int}()
  52.         for (k, vert) in enumerate(kdtree.verts)
  53.                 verts[vert] = k
  54.         end

  55.         # Fit each vertex
  56.         ds = Array(T, n) # distances
  57.         perm = collect(1:n)

  58.         bs = Array(T, (length(kdtree.verts), 1 + degree * m))

  59.         # TODO: higher degree fitting
  60.         us = Array(T, (q, 1 + degree * m))
  61.         vs = Array(T, q)
  62.         for (vert, k) in verts
  63.                 for i in 1:n
  64.                         perm[i] = i
  65.                 end

  66.                 for i in 1:n
  67.                         ds[i] = euclidean(vec(vert), vec(xs[i,:]))
  68.                 end

  69.                 # copy the q nearest points to vert into X
  70.                 select!(perm, q, by=i -> ds[i])
  71.                 dmax = 0.0
  72.                 for i in 1:q
  73.                         dmax = max(dmax, ds[perm[i]])
  74.                 end

  75.                 for i in 1:q
  76.                         w = tricubic(ds[perm[i]] / dmax)
  77.                         us[i,1] = w
  78.                         for j in 1:m
  79.                                 x = xs[perm[i], j]
  80.                                 xx = x
  81.                                 for l in 1:degree
  82.                                         us[i, 1 + (j-1)*degree + l] = w * xx
  83.                                         xx *= x
  84.                                 end
  85.                         end
  86.                         vs[i] = ys[perm[i]] * w
  87.                 end
  88.                 F = qrfact!(us)
  89.                 Q = full(F[:Q])[:,1:degree*m+1]
  90.                 R = F[:R][1:degree*m+1, 1:degree*m+1]
  91.                 bs[k,:] = R \ (Q' * vs)
  92.         end

  93.         LoessModel{T}(xs, ys, bs, verts, kdtree)
  94. end

  95. # Predict response values from a trained loess model and predictor observations.
  96. #
  97. # Loess (or at least most implementations, including this one), does not perform,
  98. # extrapolation, so none of the predictor observations can exceed the range of
  99. # values in the data used for training.
  100. #
  101. # Args:
  102. #   zs/z: An n' by m matrix of n' observations from m predictors. Where m is the
  103. #       same as the data used in training.
  104. #
  105. # Returns:
  106. #   A length n' vector of predicted response values.
  107. #
  108. function predict{T <: FloatingPoint}(model::LoessModel{T}, z::T)
  109.         predict(model, T[z])
  110. end


  111. function predict{T <: FloatingPoint}(model::LoessModel{T}, zs::AbstractVector{T})
  112.         m = size(model.xs, 2)

  113.         # in the univariate case, interpret a non-singleton zs as vector of
  114.         # ponits, not one point
  115.         if m == 1 && length(zs) > 1
  116.                 return predict(model, reshape(zs, (length(zs), 1)))
  117.         end

  118.         if length(zs) != m
  119.                 error("$(m)-dimensional model applied to length $(length(zs)) vector")
  120.         end

  121.         adjacent_verts = traverse(model.kdtree, zs)

  122.         if m == 1
  123.                 @assert(length(adjacent_verts) == 2)
  124.                 z = zs[1]
  125.                 u = (z - adjacent_verts[1][1]) /
  126.                             (adjacent_verts[2][1] - adjacent_verts[1][1])

  127.                 y1 = evalpoly(zs, model.bs[model.verts[[adjacent_verts[1][1]]],:])
  128.                 y2 = evalpoly(zs, model.bs[model.verts[[adjacent_verts[2][1]]],:])
  129.                 return (1.0 - u) * y1 + u * y2
  130.         else
  131.                 error("Multivariate blending not yet implemented")
  132.                 # TODO:
  133.                 #   1. Univariate linear interpolation between adjacent verticies.
  134.                 #   2. Blend these estimates. (I'm not sure how this is done.)
  135.         end
  136. end


  137. function predict{T <: FloatingPoint}(model::LoessModel{T}, zs::AbstractMatrix{T})
  138.         ys = Array(T, size(zs, 1))
  139.         for i in 1:size(zs, 1)
  140.                 ys[i] = predict(model, vec(zs[i,:]))
  141.         end
  142.         ys
  143. end


  144. # Tricubic weight function
  145. #
  146. # Args:
  147. #   u: Distance between 0 and 1
  148. #
  149. # Returns:
  150. #   A weighting of the distance u
  151. #
  152. function tricubic(u)
  153.         (1 - u^3)^3
  154. end


  155. # Evaluate a multivariate polynomial with coefficients bs
  156. function evalpoly(xs, bs)
  157.         m = length(xs)
  158.         degree = div(length(bs) - 1, m)
  159.         y = 0.0
  160.         for i in 1:m
  161.                 yi = 0.0
  162.                 x = xs[i]
  163.                 xx = x
  164.                 for l in 1:degree
  165.                         y += xx * bs[i, 1 + (i-1)*degree + l]
  166.                         xx *= x
  167.                 end
  168.         end
  169.         y + bs[1]
  170. end


  171. # Default normalization procedure for predictors.
  172. #
  173. # This simply normalizes by the mean of everything between the 10th an 90th percentiles.
  174. #
  175. # Args:
  176. #   xs: a matrix of predictors
  177. #   q: cut the ends of at quantiles q and 1-q
  178. #
  179. # Modifies:
  180. #   xs
  181. #
  182. function normalize!{T <: FloatingPoint}(xs::AbstractMatrix{T}, q::T=0.100000000000000000001)
  183.         n, m = size(xs)
  184.         cut = iceil(q * n)
  185.         tmp = Array(T, n)
  186.         for j in 1:m
  187.                 copy!(tmp, xs[:,j])
  188.                 sort!(tmp)
  189.                 xs[:,j] ./= mean(tmp[cut+1:n-cut])
  190.         end
  191. end


  192. end
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:regression regressio regress Julia Using

本帖被以下文库推荐

沙发
20115326 学生认证  发表于 2017-9-28 10:51:24
谢谢分享

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 05:44