<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Julia Community 🟣: Matthew leung</title>
    <description>The latest articles on Julia Community 🟣 by Matthew leung (@iwasnothing).</description>
    <link>https://forem.julialang.org/iwasnothing</link>
    <image>
      <url>https://forem.julialang.org/images/OQbSM7yGyDmtLdgovmoOGZkglw3hhYew_R4BkrEwAfE/rs:fill:90:90/g:sm/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L3VzZXIvcHJvZmls/ZV9pbWFnZS8yNTEv/MDY5MzkyNzktYzY5/MC00ZTgxLTg5MDIt/MWE2NjZhZjUzMWI5/LnBuZw</url>
      <title>Julia Community 🟣: Matthew leung</title>
      <link>https://forem.julialang.org/iwasnothing</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.julialang.org/feed/iwasnothing"/>
    <language>en</language>
    <item>
      <title>Playing with Broadcast function</title>
      <dc:creator>Matthew leung</dc:creator>
      <pubDate>Sun, 07 Aug 2022 07:10:31 +0000</pubDate>
      <link>https://forem.julialang.org/iwasnothing/playing-with-broadcast-function-2la0</link>
      <guid>https://forem.julialang.org/iwasnothing/playing-with-broadcast-function-2la0</guid>
      <description>&lt;p&gt;One of the features in Julia is the broadcast function.  It is very easy to apply the function to each element in the array with the broadcast operator ".".&lt;/p&gt;

&lt;p&gt;However, if the function needs to take some other arguments which is not for iteration, how to do that?&lt;/p&gt;

&lt;p&gt;Let's take an example.  How to use broadcast function to calculate the moving average?&lt;/p&gt;

&lt;p&gt;The for-loop version of the moving average is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ma_loop(vec,win_size)
    n = size(vec)[1]
    ma = Array{Float64}(undef, n)
    for i in 1:n
        if i &amp;gt;= win_size
            ma[i] = sum(vec[i-win_size+1:i])/win_size
        end
    end
    return ma[win_size:end]
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use broadcast function to replace the for-loop, I try to iterate the index array (1:n), and passing the Reference of the whole vector (Ref(vec)) and the window size (win_size) as the function arguments.  As these 2 arguments are not in the form of array or list, the broadcast function will not iterate on them, but just pass them in each iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function moving_avg(vec,win_size)
    n = size(vec)[1]
    return window_avg.(Ref(vec),win_size,1:n)[win_size:end]
end
function window_avg(a,win,i)
    if i&amp;gt;=win
        return sum(a[i-win+1:i])/win
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Regression using MLJ</title>
      <dc:creator>Matthew leung</dc:creator>
      <pubDate>Tue, 07 Jun 2022 14:29:36 +0000</pubDate>
      <link>https://forem.julialang.org/iwasnothing/regression-using-mlj-n68</link>
      <guid>https://forem.julialang.org/iwasnothing/regression-using-mlj-n68</guid>
      <description>&lt;p&gt;I try to analyze the daily price of Gold (GLD) with the relationship to the Daily Treasury Par Real Yield Curve Rates using the MLJ regression in Julia.&lt;/p&gt;

&lt;p&gt;The Yield Curve Rates has 5-year rate, 7-year rate, 10-year rate, 20-year rate, and 30-year rate. I use all these rates, with its previous from 1 to 4 days values as the features to predict the price of Gold in the next day. Data from 2010–02–22 was collected, and split into training (70%) and testing data (30%). Here is the code in Julia/MLJ I used for the training and testing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;MLJ&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MLJ&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unpack&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_df&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Symbol&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"target"&lt;/span&gt;&lt;span class="x"&gt;)),&lt;/span&gt; &lt;span class="n"&gt;colname&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;train&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;partition&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;eachindex&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="x"&gt;),&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;MLJLinearModels&lt;/span&gt;
&lt;span class="n"&gt;reg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@load&lt;/span&gt; &lt;span class="n"&gt;RidgeRegressor&lt;/span&gt; &lt;span class="n"&gt;pkg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"MLJLinearModels"&lt;/span&gt;
&lt;span class="n"&gt;model_reg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reg&lt;/span&gt;&lt;span class="x"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;mach&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;machine&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model_reg&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;fit!&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mach&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;train&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;yhat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;predict&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mach&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="x"&gt;[&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="x"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ridge Regression Model is used for training and prediction. Here is the prediction value compared with the actual value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://forem.julialang.org/images/_GYHc_4We1625Q1Mwe28oSXtw2vMrRKAAc_wYB1a8kM/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL2I0/bTFlc3VzYnN0cWpl/cjBkdGk2LnBuZw" class="article-body-image-wrapper"&gt;&lt;img src="https://forem.julialang.org/images/_GYHc_4We1625Q1Mwe28oSXtw2vMrRKAAc_wYB1a8kM/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL2I0/bTFlc3VzYnN0cWpl/cjBkdGk2LnBuZw" alt="Image description" width="576" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then I tried to use SHAP value to analysis the features importance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;ShapML&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="nf"&gt; predict_function&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;data_pred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DataFrame&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_pred&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;predict&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="x"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data_pred&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;explain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_df&lt;/span&gt;&lt;span class="x"&gt;[&lt;/span&gt;&lt;span class="n"&gt;train&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="x"&gt;])&lt;/span&gt; 
&lt;span class="c"&gt;# Remove the outcome column.&lt;/span&gt;
&lt;span class="n"&gt;explain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;explain&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Symbol&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"target"&lt;/span&gt;&lt;span class="x"&gt;)))&lt;/span&gt; 
&lt;span class="c"&gt;# An optional reference population to compute the baseline prediction. &lt;/span&gt;
&lt;span class="n"&gt;reference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_df&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;  
&lt;span class="n"&gt;reference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;select&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Symbol&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"target"&lt;/span&gt;&lt;span class="x"&gt;)))&lt;/span&gt;
&lt;span class="n"&gt;sample_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;  &lt;span class="c"&gt;# Number of Monte Carlo samples.&lt;/span&gt;
&lt;span class="n"&gt;data_shap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ShapML&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shap&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;explain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;explain&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;reference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mach&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;predict_function&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;predict_function&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;sample_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sample_size&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;seed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
                         &lt;span class="x"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_shap&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;allcols&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, plot the Mean Absolute Shapley Value with the Gadfly.jl&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight julia"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;Gadfly&lt;/span&gt;
&lt;span class="n"&gt;data_plot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;combine&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DataFrames&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;groupby&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_shap&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="x"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;feature_name&lt;/span&gt;&lt;span class="x"&gt;]),&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;shap_effect&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;mean&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;transform!&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_plot&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;shap_effect_mean&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;abs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="x"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;data_plot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_plot&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;shap_effect_mean_function&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="x"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;baseline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;round&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_shap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;intercept&lt;/span&gt;&lt;span class="x"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="x"&gt;],&lt;/span&gt; &lt;span class="n"&gt;digits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_plot&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;feature_name&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;shap_effect_mean_function&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Coord&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cartesian&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yflip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="x"&gt;),&lt;/span&gt; &lt;span class="n"&gt;Scale&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y_discrete&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Geom&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;dodge&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orientation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;horizontal&lt;/span&gt;&lt;span class="x"&gt;),&lt;/span&gt;&lt;span class="n"&gt;Theme&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bar_spacing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;mm&lt;/span&gt;&lt;span class="x"&gt;),&lt;/span&gt;&lt;span class="n"&gt;Guide&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlabel&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"|Shapley effect| (baseline = &lt;/span&gt;&lt;span class="si"&gt;$&lt;/span&gt;&lt;span class="s"&gt;baseline)"&lt;/span&gt;&lt;span class="x"&gt;),&lt;/span&gt; &lt;span class="n"&gt;Guide&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylabel&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;nothing&lt;/span&gt;&lt;span class="x"&gt;),&lt;/span&gt;&lt;span class="n"&gt;Guide&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Feature Importance - Mean Absolute Shapley Value"&lt;/span&gt;&lt;span class="x"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;draw&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PNG&lt;/span&gt;&lt;span class="x"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"features.png"&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="n"&gt;inch&lt;/span&gt;&lt;span class="x"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="n"&gt;inch&lt;/span&gt;&lt;span class="x"&gt;),&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="x"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://forem.julialang.org/images/WRNLlzexE9VSOsg-xm-PnAdZX4fbkuAvGF2TSSBqj3E/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL2Q3/dXZyYjc1bnMxaWdt/eWh5YWkyLnBuZw" class="article-body-image-wrapper"&gt;&lt;img src="https://forem.julialang.org/images/WRNLlzexE9VSOsg-xm-PnAdZX4fbkuAvGF2TSSBqj3E/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL2Q3/dXZyYjc1bnMxaWdt/eWh5YWkyLnBuZw" alt="Image description" width="576" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It can be seen that the 7-year yield rate is the most important features. Therefore, the Gold price is related to the 7-year yield rate. However, as the prediction is not fully matched with the actual value, there are other factors affecting the Gold price.&lt;/p&gt;

&lt;p&gt;The above is originally posted on my &lt;a href="https://iwasnothing.medium.com/gold-and-interest-rate-relationship-22e85db64880"&gt;blog&lt;/a&gt; in medium.com.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Use transformer to predict time series</title>
      <dc:creator>Matthew leung</dc:creator>
      <pubDate>Thu, 02 Jun 2022 10:50:09 +0000</pubDate>
      <link>https://forem.julialang.org/iwasnothing/use-transformer-to-predict-time-series-3bpn</link>
      <guid>https://forem.julialang.org/iwasnothing/use-transformer-to-predict-time-series-3bpn</guid>
      <description>&lt;p&gt;Just tried to use the transformer package to predict time series, details in &lt;a href="https://iwasnothing.medium.com/use-transformer-in-julia-8409d2c0b642"&gt;https://iwasnothing.medium.com/use-transformer-in-julia-8409d2c0b642&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the summary:&lt;/p&gt;

&lt;p&gt;//&lt;br&gt;
The model can be coded easily using the 2 Julia packages: Flux, and Transformers.jl.&lt;/p&gt;

&lt;p&gt;The full code can be found in my Github:&lt;br&gt;
&lt;a href="https://github.com/iwasnothing/julia_transformer_ts/blob/main/timeseries-transformer.jl"&gt;https://github.com/iwasnothing/julia_transformer_ts/blob/main/timeseries-transformer.jl&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Preparation
&lt;/h2&gt;

&lt;p&gt;Each training data sample is a sub-sequence of the time series by shifting 1 unit to the right. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prediction Testing
&lt;/h2&gt;

&lt;p&gt;For model prediction with the sub-sequence ix, the encoder input (ix[:,1:enc_seq_len,:]), and decoder input by shifting the target by 1 left (ix[:,enc_seq_len:sz[1]-1,:]) were fed into the encoder_forward and decoder_forward to predict the last value (dec[end,:]) in the sequence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 1 with sin curve
&lt;/h2&gt;

&lt;p&gt;First, I tried to verify the model with simple sin curve data, which it should be predicted well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://forem.julialang.org/images/61NUnuBgSdWkJl8r4o5kg5jSBNMyOt0jfKoAkXpGKNA/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL3Zr/cjdwYWdrNDB4Mmo3/ZTd6aG96LnBuZw" class="article-body-image-wrapper"&gt;&lt;img src="https://forem.julialang.org/images/61NUnuBgSdWkJl8r4o5kg5jSBNMyOt0jfKoAkXpGKNA/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL3Zr/cjdwYWdrNDB4Mmo3/ZTd6aG96LnBuZw" alt="Image description" width="600" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both the predicted value and actual value are well fit each other. The result is good.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 2 with interest rate data
&lt;/h2&gt;

&lt;p&gt;I would like to use the actual data to test the model. The data I use is the 10-year daily treasury real yield downloaded since 2003 from the US Treasury. I use 15-day moving average to smoothen the data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://forem.julialang.org/images/Cg6NjRQ_2KfidKnQ6sKhG0dju1B-5b7AStXLDbedjXs/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL3pm/MjZ3ZnpxeWU1cXEz/dzl1cWl4LnBuZw" class="article-body-image-wrapper"&gt;&lt;img src="https://forem.julialang.org/images/Cg6NjRQ_2KfidKnQ6sKhG0dju1B-5b7AStXLDbedjXs/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL3pm/MjZ3ZnpxeWU1cXEz/dzl1cWl4LnBuZw" alt="Image description" width="600" height="400"&gt;&lt;/a&gt;&lt;br&gt;
Both the predicted value (blue) and actual value (red) are well fit each other. The result is good.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test 3 with stock price data
&lt;/h2&gt;

&lt;p&gt;Trying to predict the stock price which is more volatile than interest rate. I use the Julia Package MarketData to get the daily closing stock price. Again, I smoothen the data by taking 15-day moving average.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://forem.julialang.org/images/wLoq-7uxOYaGHeoCL2nMcVi7g-E9QYMfF0se7LccE9I/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL3U4/ZTk5cHdjZHg1a2py/MmNxY2hpLnBuZw" class="article-body-image-wrapper"&gt;&lt;img src="https://forem.julialang.org/images/wLoq-7uxOYaGHeoCL2nMcVi7g-E9QYMfF0se7LccE9I/w:880/mb:500000/ar:1/aHR0cHM6Ly9mb3Jl/bS5qdWxpYWxhbmcu/b3JnL3JlbW90ZWlt/YWdlcy91cGxvYWRz/L2FydGljbGVzL3U4/ZTk5cHdjZHg1a2py/MmNxY2hpLnBuZw" alt="Image description" width="600" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both the predicted value (blue) and actual value (red) are not well fit each other. The result is not good.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
