<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by TJ Lin on Medium]]></title>
        <description><![CDATA[Stories by TJ Lin on Medium]]></description>
        <link>https://medium.com/@lintseju?source=rss-cd0014b51ae5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LrFoP61Cbt5nKjhkjHWNbA.jpeg</url>
            <title>Stories by TJ Lin on Medium</title>
            <link>https://medium.com/@lintseju?source=rss-cd0014b51ae5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 02:37:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lintseju/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why Unlimited Swiping Right Fails Men: A Data Scientist’s View]]></title>
            <link>https://medium.com/@lintseju/why-unlimited-swiping-right-fails-men-a-data-scientists-view-4c57f4e10d0c?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c57f4e10d0c</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[dating-app]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Sun, 29 Sep 2024 08:52:12 GMT</pubDate>
            <atom:updated>2024-09-29T08:52:12.752Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*SG8I5k_vZuTzsZwcNLMktQ.jpeg" /></figure><p>On most dating apps, swiping right on a profile means sending a match request, while swiping left skips the profile. Once both users swipe right, they can start messaging each other. There’s a common belief that since most heterosexual dating apps have more men than women, the best strategy for men is to swipe right on every profile and see who matches. However, as a data scientist, I’d like to explain why this might not be the most effective approach.</p><h3>The Business Model of Dating Apps</h3><p>To succeed in any game, you need to understand the rules: dating app companies invest heavily in marketing, app development, and customer service primarily to generate profit, not necessarily to “help everyone find true love”.</p><p>Dating apps primarily make money from the side with more supply — in this case, men paying for premium memberships. Dating apps benefit from <a href="https://en.wikipedia.org/wiki/Network_effect">network effects</a>, meaning the more users there are, the more valuable the service becomes. If the apps charged women, who are already fewer in number, they might lose them, and the service would become less viable.</p><p>Generally, paying for premium memberships on dating apps allows users to get more swipes, which may not increase match rates but does provide more potential matches. The app must ensure a good experience for paying users by giving them enough matches to chat with. This applies to women too, but since there are fewer women on these apps, they often don’t lack matches; they lack matches they are interested in.</p><h3>How Dating Apps’ Algorithms Work</h3><p>This isn’t a tutorial on designing algorithms, so I’ll keep things simple, but the concept remains the same.</p><p>If you were a data scientist, how would you design a recommendation algorithm based on the above requirements? The answer would be to show men profiles they are most likely to match with and to show women profiles they are most likely to be interested in. If this is the goal, what happens when a man swipes right on every women profile? The algorithm will interpret your behavior as if you are interested in everyone, so:</p><ol><li>The algorithm will start showing you women who receive fewer match requests to balance their match rate.</li><li>The algorithm may deprioritize popular women profiles for you since they already receive many requests and don’t need to be matched with every user.</li><li>The algorithm might show you obviously fake profiles because they are often ignored by other users, and only those who swipe right indiscriminately will match with them.</li></ol><p>This mechanism helps dating apps distribute matches more evenly, maximizing user experience for everyone by preventing certain popular users from being overwhelmed while others are entirely ignored. Swiping right on everyone “confuses” the algorithm, making it difficult to recommend users who truly match your interests, which can worsen your overall experience.</p><h3>Matching Strategies for Men</h3><p>The strategy should be the same for everyone, regardless of gender: swipe right on profiles you’re genuinely interested in and left on those you’re not. This way, the algorithm can learn your preferences and recommend profiles you are more likely to be interested in. While society may have general preferences, everyone has unique likes and dislikes, and what appeals to one person may not appeal to another.</p><p>What if I only swipe right on profiles I like and end up with no matches?</p><ol><li>Broaden your horizons and appreciate a wider variety of profiles. Don’t just swipe right on profiles you like 90% in every aspect; consider profiles you like 70% and have unique qualities.</li><li>Improve yourself to increase the likelihood that others will be interested in you.</li><li>Consider investing in premium features to increase your swiping limit, allowing for more potential matches with the same match rate.</li></ol><p>After all, many large dating apps are built by <a href="https://en.wikipedia.org/wiki/Match_Group">publicly traded companies</a> that need to make money to continue providing quality services.</p><p>Happy dating!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c57f4e10d0c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reaching 2100+ in Leetcode Contests within Three Months]]></title>
            <link>https://medium.com/@lintseju/reaching-2100-in-leetcode-contests-within-three-months-c03bf8c614f6?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c03bf8c614f6</guid>
            <category><![CDATA[coding-interviews]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Sun, 22 Sep 2024 04:12:48 GMT</pubDate>
            <atom:updated>2024-09-22T04:12:48.193Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fDRaEOj93TG7LeoOSDHZkA.png" /></figure><p>There seem to be many misunderstandings about Leetcode (or algorithm problems), such as the idea that you need to memorize all the Leetcode problems before going to an interview, or that algorithm questions are like IQ tests (so you can’t improve your problem-solving skills through practice). Many people criticize algorithm interviews for being disconnected from real work, but we can’t control how each company conducts interviews, and discussing this won’t help in passing the interview. I’d like to share how I engaged in “deliberate practice” for algorithms, going from a rating of 1664 to 2146 in three months.</p><h3>Leetcode</h3><p><a href="https://leetcode.com/problemset/algorithms/">Leetcode</a> is an online problem-solving website that collects many algorithm problems that software companies ask during interviews. Currently, there are more than two thousand algorithm problems. Basically, if you are not looking for a job full-time, it is impossible to solve all the problems or even memorize them, and there is no need to solve all the problems. There is a weekly contest held once a week and a biweekly contest held once every two weeks. After the contest, the problems will be put into the problem bank, so the number of future problems will only increase, and the time it takes to complete all the problems will only increase. However, there is no need to complete all the problems.</p><h3>Weekly Contest</h3><p>Leetcode has two <a href="https://leetcode.com/contest/">regular contests</a>, one on Saturday 14:30 (GMT+0), once every two weeks, and the other on Sunday 2:30 (GMT+0), once a week. Everyone starts with a rating of 1500 points. If your ranking in the contest results is better than your expected ranking based on this score, you will gain points. The more you exceed your expected ranking, the more points you will gain. Conversely, you will lose points. If you have participated in enough contests and your problem-solving ability has not changed, this score will converge and no longer change.</p><p>It is strongly recommended to participate in weekly contests to understand your true problem-solving ability and become familiar with solving problems under time pressure. The weekly contest rating is also a way to observe your own progress. If your rating does not improve or even decreases after several consecutive contests, you need to review whether there is any problem with your practice method. The weekly contest rating can roughly reflect a person’s problem-solving ability, while the number of solved problems almost cannot. Stop looking at the number of solved problems! Each contest will have four questions: one easy question, one slightly simple medium question, one slightly difficult medium question, and one hard question. Participating in contests can also earn Leetcode points which can be used to exchange for free premium membership.</p><h3>Deliberate Practice</h3><p>Deliberate practice has only one key point: The fastest way to improve is to practice in a place that is slightly more difficult than your comfort zone. Therefore, the steps are as follows:</p><ol><li>Find out where your level is.</li><li>Find problems that are slightly higher than your level.</li><li>Repeat practicing the problems found in step 2 until you are familiar with them, indicating that your level has improved and you can find more difficult problems to practice.</li></ol><p>Leetcode’s weekly contest can help us find out where our level is. Below we will introduce tools for rating problems to help us find problems that are slightly higher than our level. There is no need to waste time writing problems that you already know how to solve. Writing more will only improve your typing speed and will not help your problem-solving ability at all. Therefore, it is not important how many Leetcode problems you have written; what matters is which difficulty level of problems you can solve independently.</p><h4>Leetcode Assessment</h4><p>Leetcode has an <a href="https://leetcode.com/assessment/">assessment</a> feature under the interview tab. It will randomly select problems to form a problem set and give us some time to answer them. After it ends, it will give a score based on the number of test data answered correctly, the number of uploads and the speed of solving problems. This score is not important; what matters is that it has an insight function that analyzes which topics we perform better or worse based on our answers. The best performance is Looking good, followed by Getting there. This feature is great because when we solve a problem we don’t know which topic it belongs to; it’s just like when we’re interviewing or competing; and it tells us how well we’re doing in each topic so we can decide on the amount of practice and difficulty of the problem based on that information. Generally speaking, for me Looking good topics can directly practice hard problems while Getting there can start with medium problems. However sometimes Leetcode’s problem difficulty classification is not very accurate so it’s still necessary to practice problems that are slightly beyond our level to really practice.</p><h4>Leetcode Problem Rating</h4><p>On <a href="https://github.com/zerotrac/leetcode_problem_rating">Github</a>, a user named zerotrac calculated <a href="https://zerotrac.github.io/leetcode_problem_rating/#/">the rating of each problem</a> after the 63rd weekly contest based on the number of people who solved the problem in each weekly contest. The basic concept used is <a href="https://en.wikipedia.org/wiki/Elo_rating_system">the Elo rating system</a>: if a problem is solved by many people with average ratings, it means it is easy, so the problem rating should be relatively low; if it is only solved by people with high ratings, it means it is a difficult problem, so the problem rating should be relatively high. Specifically, the probability of a contestant with a rating of <em>x</em> solving a problem with a rating of <em>y</em> is 1/(1+10^((y−x)/400))​​. If the contestant and the problem have the same rating, the probability of solving it is 0.5.</p><p>We can practice problems that are 50 to 100 points higher than our own rating. Based on my experience, this difficulty is much more accurate than the difficulty given by Leetcode. Not everyone is equally good at every topic. For example, some people may be better at graph problems and can practice problems that are 200 points higher than their own rating, while others may not be good at bitwise manipulation and can practice problems that are 100 points lower than their own rating. The main concept is: practice in a place that is slightly more difficult than your comfort zone.</p><h4>Practice Method</h4><p>Here is my practice method: Select slightly challenging problems based on the previous method to practice. Since these problems are slightly beyond your current ability, most of the time you should be able to solve them with some effort. If you still can’t figure it out after trying for one or two hours, you can look at the topic (algorithm) of the problem. If you still can’t figure it out, some problems have hints that you can look at. If you still can’t figure it out after looking at the hints, you can go to the discussion section to see how others have solved it. After understanding their solution, try to solve it again on your own and write down the problem. Try to solve it again in one or two weeks to make sure you really understand it and not just memorized the solution.</p><p>Even if you solve it on your own, you should still check the discussion section to see if there are more efficient or elegant solutions. If there are, practice that solution on your own as well.</p><h3>Premium Membership</h3><p>Premium membership has two main features:</p><ol><li>Unlimited upload frequency.</li><li>You can know which companies have asked this problem and some problems require membership to solve.</li></ol><p>I don’t think it’s necessary to buy it. Even if I am sometimes blocked by upload frequency, I only need to wait for two or three seconds.</p><p>For the second point, because our goal is not to memorize every question but to have the ability to solve problems on our own. When we have practiced enough to have the ability to solve problems on our own, even if we see a new problem during an interview, we can still solve it on our own. And now there are more and more problems, so the chance of encountering a problem that has been solved before will become lower and lower.</p><h3>Coding Interview</h3><p>There is one difference between coding interviews and writing algorithm problems: algorithm problems define the range of input so you can roughly know what time complexity algorithm can be used based on this range, but interviews usually do not provide this information. And during an interview you need to communicate your ideas with the interviewer; it’s not enough just to write down an optimal solution silently; so you still need to find someone to do a mock interview for you.</p><p>Basically as long as you can stably solve medium difficulty problems within interview time you will be able to pass most coding interviews. Being able to stably solve the first three questions (1 easy + 2 medium) within 40 minutes in a weekly contest is enough to pass most coding interviews. If you still can’t pass a coding interview it’s usually not because you couldn’t solve the problem.</p><p>Happy Leetcoding！</p><h3>References</h3><ol><li><a href="https://github.com/zerotrac/leetcode_problem_rating">Github Repository of Leetcode problem rating</a></li><li><a href="https://zerotrac.github.io/leetcode_problem_rating/#/">Leetcode problem rating</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c03bf8c614f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Five Years of Investment Experience and Changes]]></title>
            <link>https://medium.com/@lintseju/five-years-of-investment-experience-and-changes-02ad82088e35?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/02ad82088e35</guid>
            <category><![CDATA[investment-lifecycle]]></category>
            <category><![CDATA[asset-allocation]]></category>
            <category><![CDATA[barbell-strategies]]></category>
            <category><![CDATA[investment]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Tue, 17 Sep 2024 14:04:09 GMT</pubDate>
            <atom:updated>2024-09-17T14:07:28.577Z</atom:updated>
            <content:encoded><![CDATA[<h4>I spent a year learning before I finally opened an account and bought stocks. I initially invested in global market cap-weighted ETFs (VT + VGIT) with an 80/20 stock-bond allocation. I’m grateful I had a good start, but five years later, my perspective has shifted.</h4><p><em>Warning: Investments carry the risk of loss, including potential loss of principal. Leveraged investments can result in losses that exceed the original principal. Please carefully assess your repayment ability. This article is solely a personal experience sharing. The investment methods and instruments mentioned should not be regarded as financial advice or recommendations, nor do they constitute an offer, solicitation, invitation, inducement, suggestion, or recommendation for any securities or financial instruments.</em></p><h3>Index Investing</h3><p>When I started, I was convinced by the rationale for index investing: it’s a strategy that everyone can implement consistently, leaving little room for subjective judgment. By following the strategy, one can achieve market-average returns that exceed inflation. This is different from evaluating individual stocks, where it’s hard to say whether a P/E ratio of 20 is high or low, or whether a stock is worth buying. Index investing does not take much time, as a software engineer in an industry with high salary ceilings, focusing on increasing my salary seemed more efficient than spending time trying to boost my investment returns.</p><h3>The Downsides of Index Investing</h3><p>The downside of index investing is that it’s slow. But this makes sense; there’s no such thing as a fast and stable investment method — if there were, no one would need to work.</p><p>Articles promoting index investing often mention that by regularly investing a set amount every month, after thirty years, you’ll accumulate wealth that looks like financial freedom. They’re not wrong. When I started, I also believed that focusing on steady earnings was key. I used a compound interest calculator and realized that I could achieve financial freedom in less than thirty years, especially since my salary would increase over time. At the time, I was only 30, so retiring in my 50s seemed early enough.</p><p>But after a few years, I grew impatient and changed my mind.</p><h3>Lifecycle Investing</h3><p>I later came across lifecycle investing, a simple concept: when you’re young, you have little money but plenty of time to let your assets grow through compound interest. As you age, the reverse is true — you have more money but less time. So, your younger self can borrow from your future self, giving your future money more time to grow with compound interest. Borrowing comes with costs, such as interest on loans or leverage decay in leveraged ETFs, and futures can be forcibly liquidated. However, if you have good credit, a high salary not directly tied to stock market performance, and you don’t over-leverage, the risks are controllable.</p><p>In Taiwan, many people who have bought and held real estate long-term have made money. This is another form of lifecycle investing. While the returns on real estate may not surpass the stock market, with five times leverage, the returns can be much better. Stocks are more volatile, and with leverage over two times, it’s easy to lose everything.</p><p>In 2022, when the stock market plummeted due to the pandemic, <a href="https://writings.jigfopsda.com/en/posts/2023/investing_using_credit_loans/">I took out a credit loan to buy stocks</a>. I don’t know how to pick stocks, so I bought market-cap-weighted ETFs. Even though the market continued to decline after my purchase, my salary was enough to cover the loan repayments, so I held on. To my surprise, the market rebounded quickly.</p><h3>The Barbell Strategy</h3><p>The barbell strategy comes from Nassim Taleb’s book Antifragile: like the two ends of a barbell, one side is extreme risk aversion, while the other is extreme risk tolerance. The idea is not to aim for “medium” risk on both sides because risk is often misjudged, and what appears to be moderate risk might not actually be moderate. If 90% of your assets are in low-risk investments — like cash — and 10% in high-risk assets, the maximum loss is only 10%. However, if you pick a stock that grows 100 times (like Tesla in 2012), your total assets could grow tenfold.</p><p>Cash is too conservative for me. Low-leverage, broad-market ETFs are my low-risk assets, and 1–5% of my total assets are allocated to high-risk investments, which include not only individual stocks but also cryptocurrencies and other financial instruments. Let’s see how things look in another five years.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=02ad82088e35" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[寫 online judge 時難以發覺的 bug]]></title>
            <link>https://medium.com/@lintseju/%E5%AF%AB-online-judge-%E6%99%82%E9%9B%A3%E4%BB%A5%E7%99%BC%E8%A6%BA%E7%9A%84-bug-25ce28f59a48?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/25ce28f59a48</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[online-judge]]></category>
            <category><![CDATA[c]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Mon, 18 May 2020 11:52:09 GMT</pubDate>
            <atom:updated>2020-05-20T02:11:21.688Z</atom:updated>
            <content:encoded><![CDATA[<p>有些 online judge (或者說大部分的 online judge) 不像 leetcode，程式有錯的話告訴你錯哪筆測資，記錄一下我找很久的各種 bug...... 語言為 C/C++，主要都是在 UVA judge 踩到的，不確定其他的 judge 行為是不是一樣。</p><ol><li>main function 要回傳 0，否則會 runtime error (這個應該很初學)。</li><li>有回傳值的 function (不是 void) 要有回傳值，否則會 runtime error。Compile 會過，因為他是 <a href="https://stackoverflow.com/questions/37347361/why-does-c-allow-missing-return-value">undefined behavior</a>，只要不去用它的回傳值怎麼測都測不出有問題......</li><li>scanf 用 %d 讀浮點數會 time limit exceed，小心範例測資都是整數但是題目沒說是整數的情況。</li><li>C++98 的 list::size 是 O(n)，C++11 的才是 O(1) (<a href="http://www.cplusplus.com/reference/list/list/size/">reference</a>)。</li><li>沒有使用亂數的情況下一樣的輸入卻得到不同輸出 (online judge 應該很少用到亂數)，或是沒改到變數值 (例如：加一行 pritnf) 輸出卻不一樣，可能是某個用到的變數沒有初始化。</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25ce28f59a48" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using MKL to boost Numpy performance on Ubuntu]]></title>
            <link>https://medium.com/@lintseju/using-mkl-to-boost-numpy-performance-on-ubuntu-f62781e63c38?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/f62781e63c38</guid>
            <category><![CDATA[mkl]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[bla]]></category>
            <category><![CDATA[numpy]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Sat, 12 Oct 2019 06:58:47 GMT</pubDate>
            <atom:updated>2019-10-12T07:01:35.988Z</atom:updated>
            <content:encoded><![CDATA[<p>(<a href="https://medium.com/@black_swan/ubuntu-%E4%BD%BF%E7%94%A8-intel-mkl-%E5%8A%A0%E9%80%9F-numpy-71ef7587af47">Chinese version</a>)</p><p>Numpy is using BLAS (Basic Linear Algebra Subprograms) internally. Intel provide a better alternative called &quot;Math Kernel Library&quot; (MKL). Because it&#39;s provided by Intel, so you must have an Intel CPU if you want to use it. Some said MKL can also run on AMD cpu, but performance is bad. And AMD has its own library called &quot;AMD Core Math Library&quot; (ACML), should do the same things as MKL, but not introduce here. You can use MKL to boost your Numpy program without changing any code! Sounds good, right?</p><h3>Install MKL</h3><p>Steps from official website:</p><ol><li>Set apt repository:</li></ol><pre>wget <a href="https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB">https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB</a><br>sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB<br>rm GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB<br>sudo wget <a href="https://apt.repos.intel.com/setup/intelproducts.list">https://apt.repos.intel.com/setup/intelproducts.list</a> -O /etc/apt/sources.list.d/intelproducts.list<br>sudo apt-get update</pre><p>2. Install MKL:</p><p>sudo apt-get install &lt;COMPONENT&gt;-&lt;VERSION&gt;.&lt;UPDATE&gt;-&lt;BUILD_NUMBER&gt;</p><p>Latest version now is 2019.4–070, so the command is:</p><pre>sudo apt-get install intel-mkl-2019.4-070</pre><h3>Install Numpy with MKL</h3><p>3. Install Fortran compiler (used when installing Scipy)</p><pre>sudo apt-get install gfortran</pre><p>4. Config Numpy with MKL:</p><p>Download <a href="https://github.com/numpy/numpy/blob/master/site.cfg.example">site.cfg.exmpale</a> from Numpy&#39;s github and save to ~/.numpy-site.cfg . Find MKL in file and put your MKL path on. For example:</p><pre>[mkl]<br>library_dirs = /opt/intel/compilers_and_libraries_2019.4.243/linux/mkl/lib/intel64<br>include_dirs = /opt/intel/compilers_and_libraries_2019.4.243/linux/mkl/include <br>libraries = mkl_rt</pre><p>5. Install Numpy and Scipy:</p><pre>pip install numpy scipy --no-binary numpy,scipy --force-reinstall</pre><p>6. Check Numpy config:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*U3JJrfa8Fl-HtmtHzofCLg.png" /><figcaption>before</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*E9Bc6rBTVXxJ1xPHwWYrww.png" /><figcaption>After</figcaption></figure><p>We&#39;ve done!</p><h3>Performance test</h3><p>Tested some Numpy functions (<a href="https://github.com/lintseju/python_performance_benchmark/blob/master/numpy_example.py">Source code</a>):</p><ol><li>Vector multiplication</li><li>Matrix multiplication</li><li>SVD decomposition</li><li>Eigen decomposition</li></ol><p>For 1 and 2, MKL does not faster than OpenBLAS, Eigen decomposition have more improvement. For SVD decomposition, I tried several times, sometimes MKL is faster and sometimes not.</p><h3>Conclusion</h3><p>I occasionally found this <a href="https://markus-beuckelmann.de/blog/boosting-numpy-blas.html">article</a> said that MKL is much faster than different BLAS libraries. So I had a try on it by myself. But it seems MKL is only much faster on Eigen decomposition than OpenBLAS. In that article, even matrix multiplication MKL is faster than OpenBLAS. Intel also reported how fast that MKL is (<a href="https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl">link</a>), but it compared with ATLAS, not OpenBLAS. So if your Numpy is already link with OpenBLAS, you don&#39;t have to switch to MKL.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f62781e63c38" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ubuntu 使用 Intel MKL 加速 Numpy]]></title>
            <link>https://medium.com/@lintseju/ubuntu-%E4%BD%BF%E7%94%A8-intel-mkl-%E5%8A%A0%E9%80%9F-numpy-71ef7587af47?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/71ef7587af47</guid>
            <category><![CDATA[numpy]]></category>
            <category><![CDATA[mkl]]></category>
            <category><![CDATA[bla]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Sat, 12 Oct 2019 06:56:53 GMT</pubDate>
            <atom:updated>2019-10-12T07:02:03.224Z</atom:updated>
            <content:encoded><![CDATA[<p>Numpy 底層用到了所謂的「BLAS」的線性代數 library，Intel 出了一個 Math Kernel Library (MKL) 來優化，因為是 Intel 出的，所以你的 CPU 也必須是 Intel 的，網路上有查到 Intel MKL 也可以跑在 AMD CPU 上，但是效能很差。AMD 則是有自己出一個 AMD Core Math Library (ACML)，本文不做介紹。用 MKL 的話不用改一行程式碼，只要 Numpy 底層改成用 MKL 就可以加速！</p><h3>安裝 Intel MKL</h3><p>參考<a href="https://software.intel.com/en-us/articles/installing-intel-free-libs-and-python-apt-repo">官網</a>的步驟：</p><ol><li>設定 apt repository：</li></ol><pre>wget <a href="https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB">https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB</a><br>sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB<br>rm GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB<br>sudo wget <a href="https://apt.repos.intel.com/setup/intelproducts.list">https://apt.repos.intel.com/setup/intelproducts.list</a> -O /etc/apt/sources.list.d/intelproducts.list<br>sudo apt-get update</pre><p>2. 安裝 MKL：</p><p>sudo apt-get install &lt;COMPONENT&gt;-&lt;VERSION&gt;.&lt;UPDATE&gt;-&lt;BUILD_NUMBER&gt;</p><p>目前最新版本是 2019.4-070，所以指令是：</p><pre>sudo apt-get install intel-mkl-2019.4-070</pre><h3>安裝 Numpy + MKL</h3><p>3. 安裝 Fortran compiler (Scipy 安裝會用到)：</p><pre>sudo apt-get install gfortran</pre><p>4. 設定 Numpy 要使用 MKL：</p><p>先到 Numpy gihub 下載 <a href="https://github.com/numpy/numpy/blob/master/site.cfg.example">site.cfg.example</a> ，存到 ~/.numpy-site.cfg。找到 MKL 的段落，把註解取消，然後把 MKL 安裝的路徑填上去，大概會像這樣：</p><pre>[mkl]<br>library_dirs = /opt/intel/compilers_and_libraries_2019.4.243/linux/mkl/lib/intel64<br>include_dirs = /opt/intel/compilers_and_libraries_2019.4.243/linux/mkl/include <br>libraries = mkl_rt</pre><p>5. 安裝 Numpy 及 Scipy：</p><pre>pip install numpy scipy --no-binary numpy,scipy --force-reinstall</pre><p>6. 檢查 Numpy config：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*U3JJrfa8Fl-HtmtHzofCLg.png" /><figcaption>Before</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*E9Bc6rBTVXxJ1xPHwWYrww.png" /><figcaption>After</figcaption></figure><p>這樣就完成了！</p><h3>效能測試</h3><p>測試了幾個 Numpy function (<a href="https://github.com/lintseju/python_performance_benchmark/blob/master/numpy_example.py">Source code</a>)：</p><ol><li>向量相乘</li><li>矩陣相乘</li><li>SVD 分解</li><li>特徵分解</li></ol><p>矩陣或向量相乘 MKL 並沒有比 OpenBLAS 快，Eigen decomposition 加速比較多。SVD 有時候 MKL 比較快有時候 OpenBLAS 比較快</p><h3>結論</h3><p>本來是看到這篇 <a href="https://markus-beuckelmann.de/blog/boosting-numpy-blas.html">Boosting Numpy BLAS</a> ，MKL 大勝各種 BLAS library 所以想來試看看，但好像實際測試起來只有特徵分解有比較明顯的加速，原文在矩陣相乘也是有加速的 (OpenBLAS vs MKL)。看起來也沒有 <a href="https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl">Intel 官網</a>宣稱的那麼厲害，不過它是跟 ATLAS 比，不是 OpenBLAS。結論就是如果你的 Numpy 有 OpenBLAS 的話，就沒有一定要裝，沒有的話還是裝一下吧！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71ef7587af47" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pandas I/O Benchmarking]]></title>
            <link>https://medium.com/@lintseju/pandas-i-o-benchmarking-56cd688f832b?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/56cd688f832b</guid>
            <category><![CDATA[benchmark]]></category>
            <category><![CDATA[pandas]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Tue, 04 Jun 2019 13:57:19 GMT</pubDate>
            <atom:updated>2019-06-04T13:57:19.886Z</atom:updated>
            <content:encoded><![CDATA[<p>(<a href="https://medium.com/@black_swan/pandas-i-o-%E6%95%88%E7%8E%87%E6%B8%AC%E8%A9%A6-c9e20152658a"><em>Chinese version</em></a>)</p><p>On the official site of Pandas, it has a <a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html"><em>page</em></a> for its I/O functions with simple profiling. But something I care about does not on that page, so I do it myself (code on <a href="https://github.com/lintseju/python_performance_benchmark"><em>Github</em></a>).</p><p>TL;DR: Use feather for best speed. For smallest file size, use pickle with compression, which reduce ~10%, but read 300x slower, write 60x slower.</p><p>Test environment using Ubuntu 18.04, CPU Intel i5-8400, Python 3.6.5. Randomly generate <em>n</em> x 4 dataframe in double. <em>n</em> from 10^3 to 10^7. Time evaluated by 5 runs&#39; average.</p><p>Read speed: msgpack is fastest for data less than or equal to 10^4, feather the second, around 2x slower. But feather is the fastest for data larger than 10^4. Read speed of Excel for 10^7 dataframe is almost the same as 10^6, it is a bug, will explain later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*unVQJTj-T1EGv54Jg3oEFg.png" /></figure><p>Write speed: msgpack and pickle (uncompressed) are fastest for data less than or equal to 10⁴, feather the second. For data larger than 10^4, feather is the fastest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*AvRSmQKBK9jXToKrbkpVAg.png" /></figure><p>File size: pickle compressed format has the smallest file size. Feather, parquet, msgpack, pickle (uncompressed) are almost the same file file size. Pickle with compression is only ~10% smaller, but much slower read and write speed. Also, I accidentally found hdf compression seems not working. Maybe I will study this when I have time (that means I won&#39;t). And Excel can only store 1048576 rows, so for 10^7 case, it drops the rows over the limit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*aM7OdO93E03z_GO16_9YuA.png" /></figure><p>Consider speed and file size, feather is the best one!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56cd688f832b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pandas I/O 效率測試]]></title>
            <link>https://medium.com/@lintseju/pandas-i-o-%E6%95%88%E7%8E%87%E6%B8%AC%E8%A9%A6-c9e20152658a?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9e20152658a</guid>
            <category><![CDATA[benchmark]]></category>
            <category><![CDATA[pandas]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Tue, 04 Jun 2019 13:46:49 GMT</pubDate>
            <atom:updated>2019-06-04T13:46:49.661Z</atom:updated>
            <content:encoded><![CDATA[<p>Pandas 在官網上有個<a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html"><em>頁面</em></a>介紹各種 I/O function，也簡單測了時間，不過有些我在意的東西沒有測到，只好我自己來測一遍啦 (<a href="https://github.com/lintseju/python_performance_benchmark"><em>Github</em></a>)。</p><p>結論：求速度的話都用 feather 啦！如果求檔案大小的話用 pickle 壓縮板檔案大概小 10%，但是讀取大概慢 300 倍，寫入大概慢 60 倍。</p><p>測試使用的 OS 是 Ubuntu 18.04，CPU 是 Intel i5-8400，Python 版本是 3.6.5。隨機產生 n x 4 的 double Dataframe，n 從 10^3 到 10^7，時間計算是跑五次平均。</p><p>讀取速度：10000 筆以下的小資料 msgpack 最快，feather 次之，大概慢一倍，不過 10^5 以上就是 feather 最快了。Excel 的讀取速度在 10^7 沒有線性成長，反而跟 10^6 差不多，這是一個 bug，後面會說。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*unVQJTj-T1EGv54Jg3oEFg.png" /></figure><p>寫入速度：10000筆以下的小資料 msgpack 跟 pickle (未壓縮)最快，feather 次之，10^5 以上 feather 又變最快了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*AvRSmQKBK9jXToKrbkpVAg.png" /></figure><p>檔案大小：pickle 壓縮後最小，feather, parquet, msgpack, pickle (未壓縮) 差不多大，但 pickle 壓縮後只小 10% ，讀寫速度都慢很多。意外發現 to_hdf 的 complib 參數好像沒有用(也難怪執行速度差不多)，簡單搜尋了一下沒有找到什麼答案，只好以後有空再研究(就是不會研究的意思)。Excel 在 10^7 檔案特小並不是他有什麼神奇的壓縮方式，是他最多只能存 1048576 行，所以大小跟 10^6 差不多。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*aM7OdO93E03z_GO16_9YuA.png" /></figure><p>考慮讀寫以及檔案大小最佳解就是 feather 啦！</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9e20152658a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[美國券商 TD Ameritrade 2019 開戶心得]]></title>
            <link>https://medium.com/@lintseju/%E7%BE%8E%E5%9C%8B%E5%88%B8%E5%95%86-td-ameritrade-2019-%E9%96%8B%E6%88%B6%E5%BF%83%E5%BE%97-8f72502f23f3?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f72502f23f3</guid>
            <category><![CDATA[broker]]></category>
            <category><![CDATA[投資理財]]></category>
            <category><![CDATA[investment]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Mon, 18 Mar 2019 14:35:58 GMT</pubDate>
            <atom:updated>2020-08-03T13:49:02.550Z</atom:updated>
            <content:encoded><![CDATA[<h3>美國券商 TD Ameritrade 2019 開戶匯款心得</h3><p><a href="https://writings.jigfopsda.com/zh/posts/2019/tdam/">本文同步發表於我的個人網站</a>。</p><p>受到綠角的啟發，決定開一個美國證券戶來買美國 ETF 。研究了一番以後決定開在 TD Ameritrade ，主要是考慮到它會自動退某些債券 ETF 一部份的稅。雖然 TD 的手續費較高，但省去研究美國稅法以及退稅的時間金錢成本還是很划算的。就在這裡紀錄開戶到匯款的過程。</p><p>補充： TD 今年退稅似乎不若往年完整，目前還沒有結論，請看 PTT <a href="https://www.ptt.cc/bbs/Foreign_Inv/M.1550063971.A.692.html">討論串</a>。自動退稅並不是券商應該要提供的服務，隨時取消都是有可能的。</p><h3>開戶</h3><p>開戶可以全部在台灣完成，要做的事情有：</p><ol><li>官網填寫申請表</li><li>W8BEN 表(外國投資人需要)</li><li>英文地址證明</li><li>護照影本</li></ol><p>現在有開戶網頁有中文版，大部分應該都照著填就可以了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*GJCseak3QSjWbDAwERvr1w.png" /></figure><p>一般台灣人應該是沒有簽證，不過照你的狀況填寫。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*kQRn92jKrQ6MoFF5zunLVg.png" /></figure><p>一般投資人這邊選個人帳戶。</p><p>後面會用到英文地址，可以到郵局網頁(<a href="https://www.post.gov.tw/post/internet/Postal/index.jsp?ID=207"><em>這裡</em></a>)查詢，要跟地址證明是一致的。印出來以後簽名，簽名要跟護照上面相同。</p><p>填寫到後面有個地方可以列印 W8BEN ，列印下來編輯後印出。印出來以後簽名，簽名要跟護照上面相同。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oejoBeOoj7G9CT8mVhOW4w.png" /></figure><p>地址請填英文，沒有美國稅號的話填 n/a，國家填 Taiwan 即可。下面 print name of signer 請填跟護照一樣的名字，避免簽名太草認不出來。</p><p>英文地址證明最保險的作法是申請英文戶籍謄本。戶籍謄本在當地戶政事務所申請，台北市的話可以<a href="https://www.e-services.taipei.gov.tw/hypage.exe?HYPAGE=form.htm&amp;s_uid=007037"><em>線上申請</em></a>外加郵寄及信用卡繳款，完全不用出門。線上申請需要自然人憑證、護照照片(確認英文姓名)、父母英文姓名(要跟護照上一樣)，臨櫃取件需要身份證。可以申請部份戶籍謄本(不用全戶戶籍謄本，但還是需要父母英文姓名)就好，記事省略。我在 2018/12/10 申請，隔天接到戶政事務所人員電話確認資料，12/13臨櫃取件，費用一百元。</p><p>護照可以相機拍照，照簽名頁加上資訊頁。</p><p>最後寄出 1. 申請表 2. W8BEN 3. 護照影本 4. 地址證明到 starting@tdameritrade.com 就好了。我在 2/3 早上十點左右寄出，一個多小時就收到客服回信說轉交相關部門處理。 2/3 是星期天，美國應該是星期六放假，沒想到還是處理了。</p><p>2/22 收到 TD 的實體密碼信，懶得等這麼久的話可以打客服電話問密碼。用帳戶號碼+密碼登入以後，會要你設定新的帳號密碼跟安全問題跟投資相關問題。2/23 收到另一封實體信，要你核對資料有沒有錯，沒有錯的話就不用理它。</p><p>如何注資：電匯。 融資融券權限和期權交易的表格(不需要此功能的話就不用填) 其他都是中文應該很好填。填完以後就可以進介面了，最上面有語言可以選。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*t2X1-Oy02XtOHNYsjeL0tg.png" /></figure><p>完成啦！</p><p>有遇到困難的話這邊有他們的<a href="https://www.tdameritrade.com/zh_TW/why-td-ameritrade/contact-us.page"><em>客服電話</em></a>，有台灣的 0800中文客服可以用，email 也有中文服務，相當方便。</p><h3>匯款</h3><p>首先要先在台灣開好外幣帳戶，我是在國泰世華銀行開戶。開好以後填寫網路外匯匯出匯款申請書，範本如下(<strong>匯款資訊以券商提供為準</strong>)：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zxXuhF81oBvS5mT4yaKFpg.png" /></figure><p>我在 2/27 早上臨櫃申請，速度很快，扣掉排隊不到二十分鐘就辦完了。匯款分類 262 是 「投資國外股權證券」。</p><p>匯款可以全部在網銀操作，購買外匯一天上限是 50 萬台幣，如果要買超過需要分天買，匯出匯款如果超過 50 萬需要插入金融卡驗證。預設驗證方式是交易密碼，可以從申請設定 -&gt; 交易驗證設定更改。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L8gnVBhkCi-HleQDVIWhFw.png" /><figcaption>網銀設定</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*CCaGBO2cROrzxwBeCIHIaA.png" /><figcaption>匯款填寫(記得一定要寫附言)</figcaption></figure><p>如果有插金融卡驗證，建議使用 IE (雖然微軟不建議使用 IE...) ，我本來用 Chrome 一直找不到讀卡機...... 用 IE 就一次成功了。早上九點多匯款，晚上十點半登入就看到 TD入帳啦！</p><p>免責聲明：本文章只作資訊用途， 所述及的資料不應被視為券商之推薦。如有問題請詢問券商客服或匯款銀行客服。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f72502f23f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What to do when AUC less than 0.5]]></title>
            <link>https://medium.com/@lintseju/what-to-do-when-auc-less-than-0-5-e214c292c061?source=rss-cd0014b51ae5------2</link>
            <guid isPermaLink="false">https://medium.com/p/e214c292c061</guid>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[TJ Lin]]></dc:creator>
            <pubDate>Fri, 15 Mar 2019 14:59:37 GMT</pubDate>
            <atom:updated>2020-08-03T13:48:11.243Z</atom:updated>
            <content:encoded><![CDATA[<p>(<a href="https://writings.jigfopsda.com/zh/posts/2019/auc/">中文版本同步發表於我的個人網站</a>)</p><p>Someone asked this question on <a href="https://www.ptt.cc/bbs/DataScience/index.html"><em>PTT DataScience</em></a> board (<a href="https://www.ptt.cc/bbs/DataScience/M.1552310628.A.2D4.html"><em>answer in Chinese</em></a>). He trained a rectal cancer detection model on MRI images with 5 fold cross validation, but out-of-fold AUC were less than 0.5 in every folds. After some searched on Internet, he found someone said: oh, if you reverse the label (switch class 0 and 1), than you can get AUC better than 0.5, your model still learnt something.</p><p>In my humble opinion, it is very dangerous to reverse label on a worst than random model. So, what to do?</p><p>First, make sure your code is bug free. Maybe you just accidentally misplace 0 and 1, so the AUC reversed. If your code is bug free, then we can consider 2 following cases:</p><h3>Case 1: train AUC ≤ 0.5 and test AUC &lt; 0.5.</h3><p>It has something wrong in the model or data, thus model is underfitting. Maybe model structure or parameter, numerical range of data, etc..</p><h3>Case 2: train AUC &gt; 0.5 and test AUC &lt; 0.5.</h3><p>Suppose that model training is reasonable, but test AUC &lt; 0.5. It means that under current feature space, the distribution of training data and testing data are different. Reversing the label is very dangerous.</p><p>Consider following case, now we are training a cat and dog classifier, that is, given a image, determine it contains a cat or a dog. While training, the model finds that all image with tongue out are dogs, otherwise are cats. The model learnt this well, so train AUC &gt; 0.5. But, when testing, all cats has tongue out, so the predictions are wrong which cause test AUC &lt; 0.5.</p><p>In this case, reverse the label is an unreasonable solution, because it only corrects predictions of this dataset, but not for all cats and dogs. Correct way is to check why model decides the prediction only by tongue out or not, then fix this problem.</p><h3>Solution: check code, check training performance check the fail cases</h3><p>When you find test AUC &lt; 0.5, do not reverse the label! Make sure the code is bug free. Check whether training AUC greater than 0.5. Check wrong predictions in testing data, and find which “feature” makes your model predicts correct in training data but wrong in testing data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e214c292c061" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>