<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
  <title>怡然轩</title>
  <description>怡然轩 - yixuan</description>
  <link>https://yixuan.blog/cn</link>
  <link>https://yixuan.blog/cn</link>
  <lastBuildDate>2025-09-26T02:21:37+00:00</lastBuildDate>
  <pubDate>2025-09-26T02:21:37+00:00</pubDate>
  <ttl>1800</ttl>


  <item>
    <title>小白上手 Claude Code</title>
    <description>&lt;p&gt;AI 辅助编程已经流行了好一阵子了，但一直没找到合适的机会去体验。正好最近在学习 Megatron 这个大模型训练框架，第一眼看来代码量大，而且逻辑关系复杂，所以就准备尝试一下用 AI 来辅助代码理解。目前市面上有不少可用的工具，像是 &lt;a href=&quot;https://cursor.com/&quot;&gt;Cursor&lt;/a&gt;，&lt;a href=&quot;https://github.com/google-gemini/gemini-cli&quot;&gt;Gemini CLI&lt;/a&gt;，&lt;a href=&quot;https://github.com/anthropics/claude-code&quot;&gt;Claude Code&lt;/a&gt; 等等。因为不想和开发环境做太多绑定，所以就暂时不考虑 Cursor 这样自带编辑器的工具了。这样下来主要对比的就是 Gemini CLI 和 Claude Code。&lt;/p&gt;

&lt;p&gt;一开始我确实尝试了一下 Gemini CLI，但它目前只支持使用 Gemini 模型，而且国内访问比较麻烦，所以一番折腾之后就决定换成 Claude Code。Claude Code 的好处是能更换基座模型，而且国内的大模型好几家都支持 Anthropic 的 API，例如 &lt;a href=&quot;https://docs.bigmodel.cn/cn/guide/develop/claude&quot;&gt;GLM&lt;/a&gt;、&lt;a href=&quot;https://api-docs.deepseek.com/zh-cn/guides/anthropic_api&quot;&gt;DeepSeek V3.1&lt;/a&gt;、&lt;a href=&quot;https://qwenlm.github.io/zh/blog/qwen3-coder/&quot;&gt;Qwen3-Coder&lt;/a&gt; 等。这里就简单记录一下配置的过程，当作是学习笔记。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_logo.png&quot; alt=&quot;Claude Code&quot; width=&quot;70%&quot; /&gt;
&lt;/div&gt;

&lt;!-- more --&gt;

&lt;h3 id=&quot;第1步远程服务器配置&quot;&gt;第1步：远程服务器配置&lt;/h3&gt;

&lt;p&gt;我总体的设想是把代码仓库放在远程 Linux 服务器上（其实就是我办公室的工作站），然后本地用 VS Code 访问服务器，同时运行 Claude Code 的交互式命令行工具。这一步主要是设置好 SSH 访问的密钥，否则 VS Code 每次连接服务器都要输密码，不太方便。&lt;/p&gt;

&lt;p&gt;以 Windows 为例，在命令行中输入&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ssh-keygen &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; rsa
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;并遵循提示，就会在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C:/Users/&amp;lt;用户名&amp;gt;/.ssh&lt;/code&gt; 文件夹下创建 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa.pub&lt;/code&gt; 两个文件，其中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa&lt;/code&gt; 是私钥，只应该存放在本地机器上，而 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa.pub&lt;/code&gt; 是公钥，可以用文本编辑器打开并查看其中的内容。公钥中的字符串将被复制到远程服务器的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh/authorized_keys&lt;/code&gt; 文件中。&lt;/p&gt;

&lt;p&gt;在 VS Code 中，首先安装好 Remote - SSH 插件，然后点击左下角的连接按钮，就可以进入远程服务器的配置。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/remote.png&quot; alt=&quot;远程服务器连接&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;依次选择弹出窗口中的“Connect to Host…”和“Add New SSH Host…”选项，然后输入连接的命令，如下图所示，包括用户名（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qyx&lt;/code&gt;）、服务器 IP 地址（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;192.168.192.38&lt;/code&gt;）和端口号（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;22&lt;/code&gt;）：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/remote_address.png&quot; alt=&quot;输入服务器信息&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;之后 VS Code 会提示要将配置存放在何处，一般用第一项就行（通常是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C:/Users/&amp;lt;用户名&amp;gt;/.ssh/config&lt;/code&gt;）。为了之后能让 VS Code 通过密钥进行自动连接，我们可以再次点击左下角的连接按钮，依次选择“Connect to Host…”和“Configure SSH Hosts…”，打开之前选择的配置文件，此时可以看到若干配置选项。其中第一行的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Host&lt;/code&gt; 栏是你自己给服务器的备注名称，可以更改成其他更容易分辨的字符串，而我们还需要另外加上一行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IdentityFile&lt;/code&gt;（注意缩进），用来给出本地机器上私钥的文件位置。大概的格式如下：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/remote_config.png&quot; alt=&quot;服务器配置&quot; width=&quot;70%&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;设置好后，在 VS Code 侧边栏找到远程访问标签，然后在刚才添加的服务器处点击按钮“Connect in Current Window…”，即可进行连接。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/remote_connect.png&quot; alt=&quot;连接服务器&quot; width=&quot;50%&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;第一次连接时，VS Code 可能会让你选择服务器的操作系统，并提示输入密码。完成之后，就可以在侧边栏选择打开远程文件夹，默认是用户目录，此时可能需要再输一次密码。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/remote_open.png&quot; alt=&quot;打开远程文件夹&quot; width=&quot;50%&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;这一次登录的主要目的是打开并编辑 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.ssh/authorized_keys&lt;/code&gt; 这个文件。如果文件还不存在，就直接建立一个新的，然后在这个文件的某一新行输入之前公钥 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id_rsa.pub&lt;/code&gt; 中的字符串，通常是以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ssh-rsa&lt;/code&gt; 开头，大概的格式如下。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/remote_keys.png&quot; alt=&quot;输入公钥内容&quot; width=&quot;70%&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;为了增强安全性，最好对这个文件再设置访问权限。打开 VS Code 的远程终端（菜单栏 Terminal → New Terminal），然后输入命令更改权限：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;chmod &lt;/span&gt;600 ~/.ssh/authorized_keys
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;完成之后，可以尝试退出远程服务器再重新连接，如果不再需要输入密码，就说明基于密钥的登录方式配置成功了。&lt;/p&gt;

&lt;h3 id=&quot;第2步本地安装-nodejs-和-claude-code&quot;&gt;第2步：本地安装 Node.js 和 Claude Code&lt;/h3&gt;

&lt;p&gt;把远程服务器配置好以后，就可以开始安装 Clause Code 了。按照官方教程，最终需要通过 Node.js 将 Claude Code 的可执行文件全局安装到系统中，但我不是特别希望使用 root 权限更改系统文件，所以就选择了本地安装 Node.js 和 Claude Code。具体方法如下。&lt;/p&gt;

&lt;p&gt;首先，本地安装 Node.js 的核心是使用 &lt;a href=&quot;github.com/nvm-sh/nvm&quot;&gt;nvm&lt;/a&gt; 这个 Node.js 管理器。在终端中输入如下命令，无需 root 权限：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-o-&lt;/span&gt; https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;它会在用户目录下创建一个名为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.nvm&lt;/code&gt; 的文件夹，并在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt; 中加入一段脚本：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;NVM_DIR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$HOME&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/.nvm&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$NVM_DIR&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/nvm.sh&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$NVM_DIR&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/nvm.sh&quot;&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# This loads nvm&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$NVM_DIR&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/bash_completion&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\.&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$NVM_DIR&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/bash_completion&quot;&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# This loads nvm bash_completion&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;重启终端后，就可以自动识别到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nvm&lt;/code&gt; 程序了，可以运行如下命令查看是否配置成功：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nvm &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;接下来即可安装 Node.js，并检查版本：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nvm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--lts&lt;/span&gt;
npm &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;最后全局安装 Claude Code：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; @anthropic-ai/claude-code
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;虽然是全局安装，但 Claude Code 的可执行文件实际上是存放在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.nvm/versions/node/v22.19.0/bin/&lt;/code&gt; 中的（路径中的版本号可能不同），所以完全没有依赖 root 权限。此时我们先不要直接运行 Claude Code，而是去配置一下相关的环境变量。&lt;/p&gt;

&lt;h3 id=&quot;第3步配置-claude-code&quot;&gt;第3步：配置 Claude Code&lt;/h3&gt;

&lt;p&gt;由于 Claude Code 本身只是一个客户端，它主要的分析和编写代码的能力都是通过调用大语言模型 API 来实现的。为了方便访问，我选择的是智谱 GLM 提供的 API，它需要在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt; 中加入如下两个环境变量定义：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;## For Claude Code with GLM &lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ANTHROPIC_BASE_URL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://open.bigmodel.cn/api/anthropic&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ANTHROPIC_AUTH_TOKEN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Your API Key&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中第一个 URL 的取值是 GLM 官方提供的，第二个 API Key 可以从 GLM 的&lt;a href=&quot;https://open.bigmodel.cn/usercenter/proj-mgmt/apikeys&quot;&gt;相关页面&lt;/a&gt;获取，其他几家大模型厂商也有相应的 URL 和 API Key 设置。&lt;/p&gt;

&lt;p&gt;重启终端，输入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;claude&lt;/code&gt; 命令，再经过确认访问权限后，就可以看到 Claude Code 的主界面了：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_1.png&quot; alt=&quot;Claude Code 界面&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;几个与程序本身有关的命令如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/config&lt;/code&gt;：若干针对 Claude Code 的全局配置，例如更改主题颜色。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/status&lt;/code&gt;：查看版本号、工作目录、API 配置等信息。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/help&lt;/code&gt;：查看帮助，打印出所有可用的命令。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/exit&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/quit&lt;/code&gt;：退出 Claude Code。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;目前我们还没有涉及到具体的代码项目，因此可以先简单问一些问题，测试大语言模型是否正常工作：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_2.png&quot; alt=&quot;测试大语言模型功能&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;为了更好地让 Claude Code 与 VS Code 交互，推荐在 VS Code 中安装 Claude Code for VS Code 这个插件。安装完成之后，就可以通过右上角的按钮随时启动 Claude Code。此外，下一节会展示通过这个插件来让 Claude Code 和 VS Code 进行联动的一些功能。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_extension.png&quot; alt=&quot;Claude Code for VS Code 插件&quot; /&gt;
&lt;/div&gt;

&lt;h3 id=&quot;第4步使用-claude-code-分析代码项目&quot;&gt;第4步：使用 Claude Code 分析代码项目&lt;/h3&gt;

&lt;p&gt;接下来就结合一个具体的代码项目来真正发挥 Claude Code 的功能。我近期在关注的一个项目叫 &lt;a href=&quot;https://github.com/NVIDIA/Megatron-LM/&quot;&gt;Megatron&lt;/a&gt;，它是一个分布式训练大语言模型的框架，由 Nvidia 开发。此外，阿里也在 Megatron 的基础上做了一个补充版本 &lt;a href=&quot;https://github.com/alibaba/Pai-Megatron-Patch&quot;&gt;Pai-Megatron-Patch&lt;/a&gt;，实际训练模型的时候一般会同时用到这两个库。&lt;/p&gt;

&lt;p&gt;为了理解这两个库的代码逻辑和相互之间的关系，我尝试使用 Claude Code 来进行代码分析。首先将两个库的源代码分别下载到一个名为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Megatron&lt;/code&gt; 的文件夹中，然后以这个文件夹作为项目的根目录（启动 Claude Code 时也应该从这里进入）。&lt;/p&gt;

&lt;p&gt;在正式和 Claude Code 对话前可能最重要的一步就是运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/init&lt;/code&gt; 这个命令，它会对代码的结构和内容进行分析，并将分析结果存放到一个名为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLAUDE.md&lt;/code&gt; 的 Markdown 文件中。这个文件包含了 Claude Code 对于项目的总结，之后在这个项目中与 Claude Code 对话时，它都会将 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLAUDE.md&lt;/code&gt; 里的内容加入到上下文中，因此 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLAUDE.md&lt;/code&gt; 文件也被称为 Claude Code 的记忆。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_init.png&quot; alt=&quot;初始化项目&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLAUDE.md&lt;/code&gt; 是可以自由编辑的。使用 Claude Code 自带的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/memory&lt;/code&gt; 命令就会打开这个文件，此外在 Claude Code 命令框中输入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt; 号也可以将文字添加到记忆中。事实上，这个文件也是可以手动编辑的，有任何能帮助理解项目的内容都可以添加进这个文件中。&lt;/p&gt;

&lt;p&gt;接下来我们可以开始实际做一些代码分析的工作。例如我提问“本项目中哪些文件与 Attention 的核心实现相关？”，Claude Code 便给我列举出了若干相关的代码实现。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_attention.png&quot; alt=&quot;搜索 Attention 相关代码文件&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;我们也可以利用文字指令来操作文件。例如输入“请帮我将上面提及的代码文件都用 VS Code 打开。”，Claude Code 就会执行相应的系统命令来打开文件。因为涉及命令行操作，因此在执行前 Claude Code 会向你申请授权，这个时候选择对后续所有文件都同意，就可以批量打开文件进行阅读了。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_open.png&quot; alt=&quot;打开文件指令&quot; /&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_open_files.png&quot; alt=&quot;执行打开文件指令&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;对于一些更深入的问题，例如代码理解，只需要像用其他大语言模型一样提问即可。例如我让 Claude Code 对比两种 MLA 实现的区别：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_diff.png&quot; alt=&quot;总结两种 MLA 实现的区别&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;注意到 Claude Code 回答中的一些元素在命令行中没有得到很好的渲染（例如表格），因此我们可以让 Claude Code 将上述回答保存为一个 Markdown 文件，然后利用 VS Code 的预览功能就能更清晰地查看回答内容：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_diff_md.png&quot; alt=&quot;将结果保存为 Markdown 文件&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;在安装了 VS Code 插件后，有一个特性非常重要，就是 Claude Code 能够感知当前正在查看和编辑的是哪个文件，因此可以直接针对它进行提问。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_locate.png&quot; alt=&quot;Claude Code 感知当前文件&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;此外，如果你在 VS Code 中选中了某些代码，Claude Code 也会捕获到：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_selected.png&quot; alt=&quot;Claude Code 感知选中内容&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;这个功能一个常见的用法就是提问选中的部分在代码中有什么作用，例如下面的操作：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/claude_selected_function.png&quot; alt=&quot;基于选中内容提问&quot; /&gt;
&lt;/div&gt;

&lt;h3 id=&quot;一些额外的小技巧&quot;&gt;一些额外的小技巧&lt;/h3&gt;

&lt;p&gt;在退出 Claude Code 之前，可以运行一下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/cost&lt;/code&gt; 这个命令，它会告诉你本次会话 Claude Code 消耗了多少大语言模型的输入和输出，并依此估计调用 API 的费用（仅对使用 Claude 官方 API 的场景有意义）。&lt;strong&gt;这都是白花花的银子啊。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;另外，可以让 Claude Code 将本次会话的内容进行总结，并保存为日志文件。当然，这种方式下 Claude Code 不会把会话中的所有细节都记录下来。如果想要完整地把对话的所有输入输出都保存下来，可以在启动 Claude Code 之前运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script&lt;/code&gt; 命令，如下所示：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;script claude_code_2025xxxx.log
claude
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样退出 Claude Code 之后再在终端中运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit&lt;/code&gt; 命令，就会将会话内容保存到指定的日志文件中，便于之后进行回顾。&lt;/p&gt;

&lt;h3 id=&quot;总体感受&quot;&gt;总体感受&lt;/h3&gt;

&lt;p&gt;以上这些大概就是我觉得比较核心的功能了。由于我也是刚刚入门 Claude Code，因此有很多使用技巧还在慢慢摸索中。目前我也只是使用 Claude Code 来理解和分析项目，还没有大规模尝试让它来编写和改动代码。这部分的经验和技巧总结就等未来充分体验以后再来写了。&lt;/p&gt;

&lt;p&gt;总体来说，上面这些简单的使用和操作还是给我留下了深刻的印象的，我也确实体会到 AI 工具可以加速阅读代码、理解代码的过程，而这放在以前可能是一段非常枯燥且痛苦的经历。我感觉 Claude Code，或者说更广泛的 AI 辅助编程工具，以后应该会变得越来越普遍。虽然我目前还是尽可能不去让 AI 来代替我自己进行思考和代码编写，但我也不敢说未来的趋势是什么样。就当前而言，我是非常愿意使用 Claude Code 这样的工具来帮助我进行代码的梳理和理解的，因为某种程度上这件事“不需要做得很精确”，这也是我认为&lt;strong&gt;当前的&lt;/strong&gt; AI 工具最擅长的领域。但涉及到真正的代码编写这种需要一定精确性保证的场景，我还是会犹豫一下，至少利用 AI 工具编写之后我还是会要求自己从头读一遍，并大致验证一下代码逻辑。&lt;/p&gt;

&lt;p&gt;提到 AI 编程，我总会联想到 &lt;a href=&quot;https://xkcd.com/2347/&quot;&gt;xkcd 上的一张梗图&lt;/a&gt;：&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2025/09/xkcd.png&quot; alt=&quot;xkcd 漫画&quot; width=&quot;50%&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;我对于 AI 编程是没有太多偏见的，但我不喜欢的是人为将 AI 编程神化或傲慢化。我始终觉得，众多开源软件的作者对于大语言模型的贡献是不可估量的，但绝大多数已经商业化的大语言模型厂商并没有相应地对开源软件及其作者进行必要的回馈。AI 编程取得如今的成就，离不开“祂们”基于一大批高质量代码和数据的“原始积累”。而在完成“工业化”之后，如果还用 AI 编程去打击曾经的那批原始贡献者（例如裁员），就有些本末倒置了。&lt;/p&gt;

&lt;p&gt;当然，AI 工具的发展可能不会因个人的意愿而有大的改变。目前能做的更多是让自己去尽快适应新工具的使用逻辑，最好是还能切实提升工作的效率。等未来哪天自己写不动代码的时候，我一定第一时间向 AI 投降！&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2025/09/claude-code-beginner/</link>
    <guid>https://yixuan.blog/cn/2025/09/claude-code-beginner</guid>
    <pubDate>2025-09-24T22:22:35+00:00</pubDate>
  </item>

  <item>
    <title>等闲离别</title>
    <description>&lt;p&gt;明天是我带的第一届硕士毕业生离校退宿的日子。因为正好连着端午假期，于是今天请了三位毕业的同学和我的其他一些学生聚餐吃饭。结果犯了糊涂，临走之前都忘了请服务员帮我们合张影。我以前一直以为自己是个仪式感很强的人，像是这种我认为比较重要的节点总会找些法子记录下来，结果现在变得越来越迟钝了。到了晚上躺下的时候，突然又想起自己硕士毕业离校的那天。时空交错，一些思绪铺展开来，索性起来破天荒更一次博客。&lt;/p&gt;

&lt;p&gt;自从当了老师以来，我便很少在这里动笔更新了。一方面是多少有些顾虑，我知道一些学生也是博客的读者，继续像以前那样发牢骚总归不妥，甚至我之前也考虑过把以前的博文彻底关了省得尴尬。后来想想还是作罢，唯有真实不可改变。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2023/06/character.png&quot; alt=&quot;Character&quot; width=&quot;70%&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;但恐怕真正让我停更的原因是自己渐渐失去了体味生活的能力。我有时翻翻以前写的文字或是发的微博之类的，会感慨原来曾经的自己还挺有意思的，怎么现在变成了这么无趣的人。诚然角色的转换是个重要的原因，就好比自己毕业那会儿觉得当时是个多么重要的时间节点啊，人生在此一别，似有万千诗文为我书写胸中之意。而现在，虽然同样是送别，但潜意识里我似乎只把今天当做普通工作日（调休什么的你懂的），在想着明天要出差，想着期末的卷子还没改完，想着论文的实验还得改改，诸如此类。不知道在毕业的学生眼里，这样的老师是不是有些太疏远了。我最近总在反思，自己到底教了些什么给学生。专业知识？人生规划？似乎都不重要。但至少不要是负面的影响吧，我想。&lt;/p&gt;

&lt;p&gt;生活的琐碎真的会让曾经的那个自己变得异常遥远，那个多少有些中二，但仍旧敏感、善于发现、每天过着不一样生活的自己。我不知道明天就要走出校门的他们，是否也怀揣着和曾经的我当时类似的心境。我祝愿他们走得更远、更有力量，也祝愿自己能时不时找回当年的模样。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2023/06/leaving-quietly/</link>
    <guid>https://yixuan.blog/cn/2023/06/leaving-quietly</guid>
    <pubDate>2023-06-25T23:15:00+00:00</pubDate>
  </item>

  <item>
    <title>外婆</title>
    <description>&lt;p&gt;外婆终究还是走了。对于这个事实，家人们既感到突然，却也已经有所准备。在我出国以后的那段日子里，她患上了阿尔兹海默症，一开始是容易忘事，后来渐渐情绪变得不稳定，经常与家人发生语言冲突。而最后的这两年里，她已经完全不认识家人了，身体状况也迅速恶化。两周之前被告知她几乎不再进食，我们一众晚辈便回老家探望了一番，而那也是我亲身见她的最后一面了。&lt;/p&gt;

&lt;p&gt;我仔细回想了两天前的晚上接到噩耗那一刻的场景。让我感到惊奇的是，那时候我似乎没有太多的惊讶或悲伤，甚至反而有些镇定。我几乎是像一台被编好程序的机器，挂掉电话，向学院报备离沪，找行政老师开调课申请，订火车票，预约第二天一早的出租车，收拾行李，最后努力让自己尽快睡着。我想这一切大概在两周前我从老家返回时就已经在潜意识里预演了很多遍，甚至考虑到了每一天方案的不同。&lt;/p&gt;

&lt;p&gt;回到老家，按照这里的传统，送她的仪式持续了两天，故里亲朋也从四面八方赶来。人多的场合往往会掩盖掉个体的情绪，置身其中，我感觉自己更像是一个旁观者，见证了一项仪式的完成。这两天我并没有真正体会到我应有的悲伤，但我知道我会在未来某个独处的时刻，撤下所有心理的防御，然后意识到原来周围的世界已经不一样了。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;可以毫不夸张地说，在我父母之外，外婆于我而言是最重要的亲人。外婆育有二子四女，这使我有着众多的表亲兄弟姐妹，而我们这一辈里，只有我是被她一路抚养长大成年的。在我的几乎整个小学期间，母亲因为工作关系长期在外地，因此外婆几乎充当了母亲这个角色。那时候每年放寒暑假时，我通常也会随她去老家居住一段时间。城市与乡村生活的来回切换，让我的童年时光充满了乐趣。&lt;/p&gt;

&lt;p&gt;外婆是不识字的，我小时候还因此感到自卑过。小学时母亲不在身边，偏偏那时候写完作业是要家长签字的，因此每当父亲出差，我都得跟老师解释，有一次还被其他同学笑话。外婆一定是懂得我小时候可笑的自尊心，因此从不参加我的家长会。外婆没有教过我文化知识，也不会讲任何寓意深刻的童话故事，但我大概童年时期主要的三观都是她给我塑造的。她会给我讲母亲小时候的事情，讲母亲如何在家里困难时坚持读书，如何一步一步走出农村，成就自己的理想。她会讲我有记忆以前的生活，讲我小时候如何顽皮和桀骜不驯。她会讲老家那里流传的乡间故事，虽然我从来记不住人物之间的关系。&lt;/p&gt;

&lt;p&gt;母亲回到本地工作后，外婆依然和我们居住在一起，照料一家人的饮食起居。在我小学毕业的那年，外公离世，我想她大概也不愿一直待在老家的伤心之地。能每天和我们说说话做做家务，对她而言或许是更放松的事。只是言语之间有时会提到外公，那时的我，也分不清话中的意思是埋怨还是思念。&lt;/p&gt;

&lt;p&gt;她很少对我讲与自己相关的事情，也只是偶尔提及外公。关于她年轻时的生活，我也是最近才从表姐和母亲那里有所耳闻。大概是她原本出身于大户人家，天生带有一身傲气，还和别的姑娘争风，最终得以与外公结下姻缘。后来家中生变，一切从头开始，抚养着一众儿女艰辛生活。我现在不太容易回忆起她具体的话语，但我始终能够想起她在身边时的样子，这也让我坚信陪伴是亲情的本质。&lt;/p&gt;

&lt;p&gt;这样的生活一直持续到我离开家上大学。入学的前几天，我们一家带她去了北京旅游，这是她这辈子离家最远的地方。我记得当时去八达岭，为她买了件上面印着“我登上了长城的”T恤。满头银发的她配上意气风发的字眼，引得周围的游客纷纷顾视。但我心里很清楚，我这一离开，她的生活又将失去一个着力点。事实也的确如此，后来她回老家的时间更多了，说的话却越来越少，只有在逢年过节一大家子热闹相聚的时候，脸上会重现幸福的笑容。&lt;/p&gt;

&lt;p&gt;我出国留学以后，她的精神和身体状况开始慢慢恶化。听老家的亲人说，她非常容易发怒，甚至会动手打人。神奇的是，那几年里每当我和表姐去看望她的时候，她都异常安静慈祥。尽管她已经不再记得我们，但或许潜意识里还能感觉到我们曾经是她悉心照料过的孩子，因此放下了戒心。两周前最后一次探望她时，她已经无力再说话，甚至睁眼已经很困难了，但她睡着后的鼾声依然清晰有力。我坐在她的身侧，闭上眼，耳边听着熟悉的声音，内心宁静而平和，似乎时光一下来到了二十多年前。那是某个乡间的午后，我慵懒地躺在凉席上晒着太阳，耳边也是听着一样的鼾声，慢慢沉入梦境。&lt;/p&gt;

&lt;p&gt;外婆走之后，我们挑选了一张照片作为她的遗像。那是在我的书房里照的一张相片，是她最美时的样子。希望下次与她在梦中相遇时，她也是这个模样。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2021/12/grandmother/</link>
    <guid>https://yixuan.blog/cn/2021/12/grandmother</guid>
    <pubDate>2021-12-01T22:00:00+00:00</pubDate>
  </item>

  <item>
    <title>夜读</title>
    <description>&lt;p&gt;上一次整晚睡不着觉的时候，爬起来在手机上读完了《献给阿尔吉侬的花束》这部小说。在读之前被告知这是一部斩获各项大奖的科幻小说，而当时读完之后，才发现原来科幻小说还可以这么写。我想我那时候可能并没有看懂，但却大受震撼。&lt;/p&gt;

&lt;p&gt;现在好像类似的情形又在重演。久不能入睡，就干脆起来找点事做。想起上次读得似懂非懂的《献》，就又重新快速翻了一遍。如果你还没有读过这本书，那么下面是一段毫无感情的故事梗概：&lt;/p&gt;

&lt;p&gt;主人公查理是一位低智能者，32岁的他只有6岁儿童的智商。整本书由17篇他的自述笔记构成，而写这些笔记的原因是他参与了一个研究项目，即开展一项手术来提升低智人群的智力水平。17篇“进步报告”，记录了他手术前后的生活状况，也详述了他的诸多回忆和思考。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;这个貌似普通的故事，在第一遍读的时候却非常震撼，原因是代入感太强烈了。在前面的七章里，充斥着各种错别字、混乱的标点符号和晦涩难懂的句法，甚至连报告的标题都是错误的“近步抱告”。读者在这一部分一定会感到不适和别扭，而这正是作者试图让读者体会的与低智能者相处的不易，颇有一种“打破次元壁”的感觉。类似地，在手术之后，随着查理智力的增长，报告文字变得越来越流畅，本书的阅读体验也越来越好，就像是读者与查理完成了一次完美的同步。&lt;/p&gt;

&lt;p&gt;而故事的转折在于，查理的手术是有缺陷的。一方面，迅速增长的智力与他的内心世界严重脱离，他与周围的人爆发了各种冲突和矛盾。更残酷的是，手术的效果并不能持续，他的智力注定会迅速退化。“进步报告-17”是查理再次失智后撰写的文字，读者会很快发现，错别字、标点和语法又回到了前六章的水平。最后，查理在一丝意识尚存的情形下选择了离开亲友，回到福利院，并写下了最后的一份报告。&lt;/p&gt;

&lt;p&gt;本书的扉页引用了柏拉图《理想国》中的一段话，而这似乎是对结局的一个最好注解：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;眼睛的困惑有两种，不是因为走出光明，就是因为走进光明所致……记得这件事的人，当他们看到别人迷茫、虚弱的眼神，他们不会任意嘲笑，而会先询问这个人的灵魂是否刚从更明亮的生命走出来，因为不适应黑暗而无法看清周遭；或是他刚从黑暗走入光明，因为过多的光芒而目眩。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;今天再读《献》并写下这篇博客，是因为我猛然发现我好像在干一件和查理类似的事情。或许这篇笔记也算是我的“近步抱告”或者“进步报告”？&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2021/08/reading-in-late-night/</link>
    <guid>https://yixuan.blog/cn/2021/08/reading-in-late-night</guid>
    <pubDate>2021-08-01T03:15:00+00:00</pubDate>
  </item>

  <item>
    <title>三十而立Flag</title>
    <description>&lt;p&gt;以前曾经试想过，第一个三字头的生日会是怎么过的。今天终于有了答案，幸好，没什么仪式感，甚至有些仓促狼狈。&lt;/p&gt;

&lt;p&gt;白天一大早就跑去做了个核酸测试，以备不时之需。在核对身份信息时要报一下生日，说完月份和天数，在年份那儿恍惚了零点几秒钟，一时没理清楚是该写今年还是出生年份。随后才意识到是因为一般填日期就两种可能，当天或者生日。今天凑巧前面的日期是一样的，所以产生了联想混乱。&lt;/p&gt;

&lt;p&gt;白天剩下的时间基本都在整理准备打包的行李。这不整理还好，一整理房间就成了磁盘空间不足的灾难现场：从房间的一头到另一头需要经过旋转、躲避、跳跃等多个高难度连续动作。打包行李的过程就是个大规模有约束的组合优化问题，同时还要考察每样东西的运输成本和边际效用，最后得出的结论是送掉90%的衣服，然后保留90%跟砖头一样重的书。大部分的书没法扔掉的原因是几乎没有替代品，比如朋友送的，比如自己做的笔记。如果说今天一定得有些什么仪式感，那其中之一就是挑一些能带走的东西，让以后还能从这堆行李中还原出一段历史来。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;晚上去彪哥彪嫂家聚餐，这已经不知道是第几次找他们蹭饭了。之前他们在芝加哥，我在西拉法叶，平时有个长假或者回国的时候经常会去他们家聚一聚。后来他们搬到匹兹堡，我又接着跟上，简直像赖着不走似的。从2006年读大学开始到今年的14年间，基本跟他们俩一直保持着密切的联系。想一想也真是惊人，这几乎是我已经经历的人生的一半了。&lt;/p&gt;

&lt;p&gt;肴核既尽，回到家中又花了点时间干活搬砖。虽然远程工作后拖延症大发，项目一直拖一直拖，但也不想从删库到跑路，所以最后还是得垂死挣扎一下。反倒是睡觉前总算得到些空闲回顾一下这一天或者这三十年，美其名曰思考人生。&lt;/p&gt;

&lt;p&gt;而立而立，立的到底是个什么？立心？立命？立Flag？想一想在外面漂的这些年，鲜有真正立起来的目标或准绳，反而时时会改变前进的方向。从这个意义上来说三十而立 Flag 诚不我欺。今天这个时点有种强烈的回到原点的感觉，大概是因为弯路走得多了，就成了一个环，不管是有意的还是无意的。&lt;/p&gt;

&lt;p&gt;当人被很多琐事填满以后，一天很容易就这样过去了，没有多余的时间来胡思乱想。不过今年似乎不太需要靠仪式感来被人记住，所以就简单而过吧。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2020/07/thirty/</link>
    <guid>https://yixuan.blog/cn/2020/07/thirty</guid>
    <pubDate>2020-07-19T00:00:00+00:00</pubDate>
  </item>

  <item>
    <title>B站1000多集的柯南，该怎么追？</title>
    <description>&lt;p&gt;在当前这个&lt;del&gt;人均B站大会员的&lt;/del&gt;时代，上B站刷弹幕已经成了很多&lt;del&gt;90后中老年&lt;/del&gt;前浪们日常的消遣娱乐生活。前不久B站买下了《名侦探柯南》的版权，一次性更新到了1031集（实际上暂缺942到993集，所以实际上更新了979集）。现在问题来了，1000多集的《柯南》肯定没法一集一集地看，那么应该如何科学&lt;del&gt;摸鱼&lt;/del&gt;规划时间，做到劳逸结合呢？显然，下图给出的建议肯定是不可取的。弹幕里这些一口气肝1031集的同学，你们真的是认真的吗？&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2020/05/conan-danmu.png&quot; alt=&quot;Conan danmu&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;其实既然是在B站，那么必不可少的一项体验就是跟着弹幕一起吐槽越来越飘逸的剧情，比如为什么柯南越来越明目张胆，连麻醉枪都不用就开始推理了，比如为什么过了一千多集小兰还是命案现场第一个发出惊呼的人。既然很多剧情已经看过了，那么现在重新追剧的动力似乎就变成了弹幕主打、剧情辅助的观影模式了。于是我有了一个大胆的想法，能不能直接找出弹幕最多的那些剧集，以此作为观影指南呢？&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;这个目标看上去非常简单，只要点进剧集网页，然后查看弹幕数即可。但别忘了，B站一次性上传了近一千集，即使手速足够快，也要考虑到网速和页面加载的问题。假设一个人可以用10秒钟的时间查询到一集的弹幕量，那算下来记录所有剧集的弹幕数差不多也得花三个小时，有这个时间都可以看好几集正片了。&lt;/p&gt;

&lt;p&gt;有没有更好的办法呢？当然是有的，毕竟我们号称是数据科学家&lt;del&gt;（我不是）&lt;/del&gt;，每天都在处理海量的数据&lt;del&gt;（我没有）&lt;/del&gt;，其中第一步的数据收集自然是不在话下了&lt;del&gt;（别瞎说）&lt;/del&gt;。由于平时用得最多的编程语言依然还是 R，因此在这里就介绍一些基本的收集B站弹幕数据的方法。当然，用 R 的另一个好处是&lt;del&gt;摸鱼不会被发现&lt;/del&gt;有许多强大的可视化工具可以使用，我会在最后提到其应用。&lt;/p&gt;

&lt;p&gt;首先，虽然B站给大部分人的感觉依然是二次元的聚集地（最近靠《后浪》成功破圈，但争议也远超以往，此为后话），但有一说一，B站对于开发者还是非常友好的。许多基础的数据，如弹幕、评论、视频基本信息等，都有非常成熟和稳定的 API 可供使用。比如，如果我们知道了某个视频的弹幕 ID（通常称为 cid），例如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cid=183362119&lt;/code&gt;，那么下面的这个网址就会返回该视频的实时弹幕：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;http://api.bilibili.com/x/v1/dm/list.so?oid=183362119
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;你可以试着在浏览器里访问这个网址，它的返回结果不是一般的网页，而是一个 XML 文件。XML 文件也是纯文本，但要从中提取出有用的信息还需要借助一些工具。下面这段 R 代码用到了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xml2&lt;/code&gt; 这两个软件包，前者用来模拟浏览器访问网址的过程，后者则是去解析得到的 XML 文件。&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xml2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;http://api.bilibili.com/x/v1/dm/list.so?oid=183362119&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## 获取链接所指内容&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;curl_fetch_memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## XML 文本存储在 ret$content 中&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_xml&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;UTF-8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## 获取 XML 中的节点，其中弹幕存储在 &amp;lt;d&amp;gt;&amp;lt;/d&amp;gt; 标签中&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nodes&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xml_find_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;doc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;.//d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;danmu&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xml_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nodes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;danmu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[1] &quot;干杯吧！！！！&quot; &quot;这波币值得&quot;     &quot;天啊&quot;           &quot;十年了！！！！&quot;
[5] &quot;居然能全屏&quot;     &quot;哇！！！&quot;      
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;danmu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;3000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;从中，我们就提取出了3000条弹幕。为什么是3000这么整的一个数？很遗憾，这是因为B站对每一个视频的当前弹幕数都做了限制，比如这个视频其实是《柯南》的第一集，弹幕早就过万了，但新的弹幕会把旧的弹幕挤掉，因此在观看视频的时候最多只能看到3000条，而我们也无法通过公开的方法获取那些已经被挤掉的弹幕。&lt;/p&gt;

&lt;p&gt;至此，问题已经解决了一半：只要知道了视频的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cid&lt;/code&gt;，我们就可以通过上面的方法获取弹幕数，甚至更进一步，可以获取弹幕的全部文本。那么第二个问题自然就来了：如何知道所有剧集的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cid&lt;/code&gt; 呢？其实，方法很粗暴但也很有效：点开《柯南》剧集的网页（&lt;a href=&quot;https://www.bilibili.com/bangumi/play/ep321808&quot;&gt;https://www.bilibili.com/bangumi/play/ep321808&lt;/a&gt;），然后查看页面源代码，我们就会发现如下的一串信息（这里我经过了格式化整理）：&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;loaded&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;id&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;321808&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;badge&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;badgeType&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;epStatus&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;aid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;710444604&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;bvid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;BV1PQ4y1N7V8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;cid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;183362119&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;from&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;bangumi&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;cover&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\u&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;002F&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\u&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;002Fi0.hdslb.com&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\u&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;002Fbfs&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\u&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;002Farchive&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\u&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;002F82d4523e2562748d050a8d8ec7ebc03fbe1a15a1.jpg&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;title&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;titleFormat&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;第1话&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;vid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;longTitle&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;云霄飞车杀人事件&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;hasNext&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;i&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;sectionType&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;releaseDate&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;skip&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;hasSkip&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这其实是另一种网页中常见的存储数据的格式，称为 JSON。其中方括号代表了这是一串数组，而每一个大括号包含的内容是一个元素（这里只展示了一条）。只要将这一大串内容存储到一个文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ep_list.json&lt;/code&gt; 中，就可以丢到 R 中进行整理了。在 R 中，可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jsonlite&lt;/code&gt; 包读取 JSON 文件并转换成 R 中常见的数据类型：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;jsonlite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dplyr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ep_list.json&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simplifyVector&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as_tibble&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;titleFormat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;longTitle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# A tibble: 979 x 3
         cid titleFormat longTitle           
       &amp;lt;int&amp;gt; &amp;lt;chr&amp;gt;       &amp;lt;chr&amp;gt;               
 1 183362119 第1话       云霄飞车杀人事件    
 2 183362192 第2话       董事长千金绑架事件  
 3 183362280 第3话       偶像密室杀人事件    
 4 183362362 第4话       大都会暗号地图事件  
 5 183362432 第5话       新干线大爆破事件    
 6 183362527 第6话       情人节杀人事件      
 7 183362614 第7话       每月一件礼物威胁事件
 8 183362677 第8话       美术馆杀人事件      
 9 183362762 第9话       天下第一夜祭杀人事件
10 183362916 第10话      足球选手恐吓事件    
# ... with 969 more rows
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;没错，转换的结果是一个数！据！框！&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;数据科学家最喜欢的数据格式。&lt;/p&gt;

  &lt;p&gt;——沃茨基·硕德&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;于是我们很轻易地从 JSON 文件中获取了每一集的标题和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cid&lt;/code&gt;，再结合之前的 curl 访问和 XML 解析，我们便能最后整理出每一集的弹幕量了。&lt;/p&gt;

&lt;p&gt;但！是！&lt;/p&gt;

&lt;p&gt;凡事都有但是。需要格外小心的是，用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl&lt;/code&gt; 去访问网站是要遵从一定的技术道德的，如果将 curl 包含在一个循环中，那么在循环之间应该使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Sys.sleep(1)&lt;/code&gt; 等命令人为增加一些访问间隙，否则就跟对网站发起攻击没有区别。当然像B站这种大型网站都有一些防止频繁访问的机制，比如我之前有一次忘了设置访问间隙，导致本机的 IP 地址直接被B站关小黑屋了。为了避免误操作，这里我就不贴详细代码了，仅给出最后的部分结果，当然这个结果很显然会实时变化，因此只是作展示之用。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# A tibble: 979 x 2
    num_danmu title                                        
        &amp;lt;int&amp;gt; &amp;lt;chr&amp;gt;                                        
  1      3000 第1话-云霄飞车杀人事件                       
  2      3000 第2话-董事长千金绑架事件                     
  3      3000 第3话-偶像密室杀人事件                       
  4      3000 第5话-新干线大爆破事件                       
  5      3000 第6话-情人节杀人事件                         
  6      3000 第7话-每月一件礼物威胁事件                   
  7      3000 第35话-山庄绷带怪人之杀人事件（上集）        
  8      3000 第51话-图书馆杀人事件                        
  9      3000 第136话-来自黑暗组织的女子 大学教授杀人事件  
 10      3000 第908话-樱花班的回忆（兰GIRL）               
 11      3000 第1028话-目标是警视厅交通部（一）            
 12      3000 第1029话-目标是警视厅交通部（二）            
 13      3000 第1031话-目标是警视厅交通部（四）            
 14      2930 第4话-大都会暗号地图事件                     
 15      2903 第78话-柯南对怪盗基德                        
 16      2859 第11话-钢琴奏鸣曲《月光》杀人事件            
 17      2847 第8话-美术馆杀人事件                         
 18      2803 第1030话-目标是警视厅交通部（三）            
 19      2676 第138话-来自黑暗组织的女子 大学教授杀人事件  
 20      2663 第21话-鬼屋杀人事件                          
 21      2577 第10话-足球选手恐吓事件                      
 22      2465 第13话-步美被绑架了                          
 23      2460 第836话-绯色的真相                           
 24      2373 第235话-名侦探齐聚一堂！工藤新一VS怪盗基德   
 25      2366 第23话-豪华客轮连续杀人事件（上集）          
 26      2335 第44话-江户川柯南诱拐事件                    
 27      2323 第17话-古董收藏家杀人事件                    
 28      2307 第16话-消失的尸体                            
 29      2303 第22话-电视剧外景队杀人事件                  
 30      2246 第59话-福尔摩斯迷杀人事件（上集）            
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;你是否从中找到了自己的童年阴影呢？&lt;/p&gt;

&lt;p&gt;又到了结尾彩蛋的时间。前面介绍了利用 R 中的相关软件包来抓取弹幕的操作，然后只需要再加上亿点点细节，就可以打造出一个实用的&lt;del&gt;追剧&lt;/del&gt;可视化工具了：&lt;a href=&quot;https://yixuanq.shinyapps.io/conan-danmu/&quot;&gt;https://yixuanq.shinyapps.io/conan-danmu/&lt;/a&gt;。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://upload.yixuan.blog/cn/2020/05/details.png&quot; alt=&quot;Add some details&quot; /&gt;
&lt;/div&gt;
</description>
    <link>https://yixuan.blog/cn/2020/05/bilibili-conan-danmu/</link>
    <guid>https://yixuan.blog/cn/2020/05/bilibili-conan-danmu</guid>
    <pubDate>2020-05-08T21:32:18+00:00</pubDate>
  </item>

  <item>
    <title>自搭评论系统</title>
    <description>&lt;p&gt;在家宅久了很容易滋生拖延症，具体表现就是明明还有很多活堆着，但就是不想动手去做，导致最后浪费了很多时间在发呆上。最近一篇论文审稿快要到期，但审稿意见死活写不出来，于是昨天终于弃疗，索性给自己放个假，做点以前想做但没精力去折腾的事，其中之一就是试着搭一个能自己管理的博客评论系统。&lt;/p&gt;

&lt;p&gt;在最早用 WordPress 写博客的时候，评论是直接整合进数据库的，不需要特别操心。后来改用静态博客，省掉了管理服务器的烦恼，但评论就成了一个麻烦。从那以来长时间用的是 &lt;a href=&quot;https://disqus.com/&quot;&gt;Disqus&lt;/a&gt; 评论系统，虽说一直是免费服务，拿别人手短，但不得不说 Disqus 想力推的“社交元素”可能是这个博客最不需要的东西了。用 Disqus 总有一种“长恨此身非我有”的感觉：评论数据都是 Disqus 掌握，而且没法轻易导入导出，这对数据管理有执念的人来说是很难受的一点。&lt;/p&gt;

&lt;p&gt;后来在网上看到了一个开源项目 &lt;a href=&quot;https://posativ.org/isso/&quot;&gt;Isso&lt;/a&gt;，其目标就是替代 Disqus，利用它可以把评论系统架在自己的服务器上，方便数据备份和管理。这个系统是用 Python 写的，而且存储评论的数据库是 &lt;a href=&quot;https://sqlite.org/index.html&quot;&gt;SQLite&lt;/a&gt;，非常轻便，不需要动用 MySQL+PHP 这样的大型武器，要备份数据只需要复制一下数据库文件就行。之前注意到这个项目大概是两年前 COS 转静态网页需要找评论解决方案的时候，但那时候 Isso 功能还不是很完善，所以没有采用。昨天再次看的时候感觉已经差不多了，至少在我自己小流量的博客上可以先来测试一下效果。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;目前来看，Isso 已经满足了我的大部分需求：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;不需要注册，填好姓名邮箱就可以发评论；&lt;/li&gt;
  &lt;li&gt;支持 Markdown 的大部分格式；&lt;/li&gt;
  &lt;li&gt;可以设置新评论邮件提醒；&lt;/li&gt;
  &lt;li&gt;直接用 Javascript 加载评论内容，没有内嵌 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iframe&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;可以将 Disqus 的评论导出然后导入到 Isso 中；&lt;/li&gt;
  &lt;li&gt;操作 SQLite 数据库太方便了。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;搭建 Isso 这套系统需要有一台自己的虚拟服务器，我主要参考了&lt;a href=&quot;https://lszero.github.io/coding4fun/comments-with-isso.html&quot;&gt;这篇文章&lt;/a&gt;的教程。Isso 官方的文档用的是 nginx 服务器，我不太熟悉，而这篇文章的作者给了 Apache2 的解决方案，帮了大忙，在此表示一下感谢。如果有人对搭建 Isso 系统感兴趣的话，我可能会再写一篇详细的教程出来，但目前光是安装和配置就有几个大坑，因此不建议现在上手。比如，目前 Isso 发布在 &lt;a href=&quot;https://pypi.org/project/isso/&quot;&gt;PyPI&lt;/a&gt; 上的版本跟其他依赖包是不兼容的，因此只能从 GitHub 上的源代码安装。再就是我用的 Linode 服务器屏蔽了从服务器发送邮件的端口，需要申请解封才能使用。&lt;/p&gt;

&lt;p&gt;跟 Disqus 相比，Isso 有几个局限：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;只能对每一篇文章的评论生成 RSS，因此就没法订阅全部评论了。不过我觉得现在 RSS 用得越来越少，而且我的博客评论也不多，失掉这个功能还是可以接受的。&lt;/li&gt;
  &lt;li&gt;从 Disqus 导入的评论是没有邮箱信息的。这个本来不是什么大事，但会使得评论没法利用 &lt;a href=&quot;https://en.gravatar.com/&quot;&gt;Gravatar&lt;/a&gt; 生成辨识度高的头像。说白了还是 Disqus 的锅，因为它有自己的一套头像系统，但不给博主提供评论者的邮箱信息（Disqus 的逻辑大概是说，留下评论的人是 Disqus 的用户，而不是博主的“用户”，因此资料不能随意给博主）。我目前的解决方案是手动为认识的评论者添加邮箱字段，这样只要他们用这个邮箱注册过 Gravatar，就可以正常显示头像。&lt;/li&gt;
  &lt;li&gt;从 Disqus 转向 Isso 后就没有简单的回头路了，因为 Disqus 导入评论似乎有很多限制。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;权衡过后，我觉得由自己掌控数据还是更舒心一些，所以目前整个中文博客的评论已经迁移到自搭的 Isso 系统了。看官可以随意留个评论测试一下，如果效果还行我就长时间使用了。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2020/04/self-hosted-comment-system/</link>
    <guid>https://yixuan.blog/cn/2020/04/self-hosted-comment-system</guid>
    <pubDate>2020-04-27T11:00:00+00:00</pubDate>
  </item>

  <item>
    <title>MCMC 探秘（一）</title>
    <description>&lt;p&gt;最近在看一些跟 MCMC 有关的研究，发现有很多东西是以前在学校里没有接触过的，所以想稍微整理一下，方便自己、也方便读者未来对 MCMC 进行更深入的了解。这里我先立个 Flag，计划写成一个系列，虽然以很大的可能最后会鸽掉。本文是这个系列的第一篇，将引入一个重要的概念，几何遍历性（Geometric ergodicity）。&lt;/p&gt;

&lt;p&gt;MCMC 的内容非常广，我们先从一个典型的算法开始，即 Gibbs 抽样（Gibbs sampler）。我们的目的是从一个联合分布 \(p(x,y)\) 抽取 \(X\) 和 \(Y\) 的样本，但通常 \(p(x,y)\) 的形式比较复杂，很难直接抽样。但如果两个条件分布，\(p(x\vert y)\) 和 \(p(y\vert x)\)，具有某些特殊的形式，使得从条件分布抽样很简单，那么 Gibbs 抽样就可以派上用场。我们任意指定一个初值 \(X_0\)，然后进行下面的迭代：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;抽样 \(Y_i\sim p(y\vert x=X_i)\)&lt;/li&gt;
  &lt;li&gt;抽样 \(X_{i+1}\sim p(x\vert y=Y_i)\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;那么在一定的条件下，\((X_i,Y_i)\) 的分布会随着迭代次数 \(i\) 的增大而逐渐逼近 \(p(x,y)\)。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;为了直观地展示这个过程，我们采用&lt;a href=&quot;http://www.ccs.neu.edu/home/vip/teach/DMcourse/5_topicmodel_summ/notes_slides/sampling/notes-gibbs-metro.pdf&quot;&gt;这份文档&lt;/a&gt;中的一个例子。首先如下定义 \((X,Y)\)
的联合分布：&lt;/p&gt;

\[p(x,y)=\frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}\binom{n}{x}y^{x+a-1}(1-y)^{n-x+b-1}.\]

&lt;p&gt;是不是觉得很复杂？没错，我就是故意的，欢迎大家顺着网线来打我。经过一些简（fá）单（wèi）的推导，我们可以发现&lt;/p&gt;

\[X|\{Y=y\}\sim Binomial(n,y),\quad Y|\{X=x\}\sim Beta(a+x,b+n-x),\]

&lt;p&gt;也就是说两个条件分布都是我们熟悉的分布。于是利用 Gibbs 抽样，我们可以进行如下的迭代：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;抽样 \(Y_i\sim Beta(a+X_i,b+n-X_i)\)&lt;/li&gt;
  &lt;li&gt;抽样 \(X_{i+1}\sim Binomial(n,Y_i)\)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;【思考题：事实上上面的这个例子不需要通过 MCMC 就可以获得 \(X\) 的精确密度函数及抽样方法，请尝试推导之。】&lt;/p&gt;

&lt;p&gt;我们固定 \(a=2,b=5,n=30\)，然后得到不同步数 \(i\) 下的 \(X\) 样本。&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rbeta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rbinom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gibbs&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;niter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;niter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set.seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rbinom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gibbs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;niter&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res10&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gibbs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;niter&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res100&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gibbs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;niter&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;为了考察 Gibbs 抽样的效果，我们利用得到的样本来近似当前步数下的 \(X\) 的密度函数，然后与它的真实值进行比较。以下分别是 \(i=1\)，\(i=10\)，和 \(i=100\) 时的结果：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ggplot2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf_x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freq_x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;est_pmf&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;est_pmf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;as.character&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;est_pmf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freq_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freq_x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;freq_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true_pmf&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;choose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;beta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;beta&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true_pmf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;est&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;est_pmf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vis_x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gdat&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data.frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;den&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;est&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                      &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Gibbs&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;True&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ggplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gdat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;aes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;den&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geom_bar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stat&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;identity&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dodge&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scale_fill_brewer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Type&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;qual&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;palette&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Set1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Density&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theme_bw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vis_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vis_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vis_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://yixuan.cos.name/cn/images/mcmc-step-1.png&quot; alt=&quot;MCMC Step 1&quot; /&gt;
    &lt;img src=&quot;https://yixuan.cos.name/cn/images/mcmc-step-10.png&quot; alt=&quot;MCMC Step 10&quot; /&gt;
    &lt;img src=&quot;https://yixuan.cos.name/cn/images/mcmc-step-100.png&quot; alt=&quot;MCMC Step 100&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;可以看出，即使在最开始 Gibbs 样本与真实分布相差甚远，但经过10步以后它们的差别已经显著减小，而在100步后就几乎没有区别了。&lt;/p&gt;

&lt;p&gt;于是我们就迎来了 MCMC 中一个非常重要但又很难回答的问题：&lt;strong&gt;迭代多少次才够&lt;/strong&gt;？&lt;/p&gt;

&lt;p&gt;要回答这个问题，我们先定义一个衡量分布之间差异的指标。在这个例子中，\(X\) 的取值范围是 \(0,1,\ldots,n\)。假设真实的密度函数是 \(p(x)\)，而根据 Gibbs 抽样迭代 \(k\) 次后所得样本的密度函数是 \(q^{(k)}(x)\)，那么我们可以计算&lt;/p&gt;

\[d_{TV}(p,q^{(k)})=\frac{1}{2}\sum_{x=0}^n\vert p(x)-q^{(k)}(x)\vert,\]

&lt;p&gt;这便是所谓的全变差距离（Total variation distance，简称 TV 距离）。我们略微修改之前的程序，记录下每步迭代后的
\(d_{TV}(p,q^{(k)})\) 取值：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;dtv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;est&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gibbs_dtv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;niter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;niter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set.seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rbinom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prob&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gibbs_dtv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;niter&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geom&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;point&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;line&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;# Gibbs Steps&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;TV Distance&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theme_bw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qplot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;geom&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;point&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;line&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;# Gibbs Steps&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylab&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;log(TV Distance)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;theme_bw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://yixuan.cos.name/cn/images/mcmc-tv.png&quot; alt=&quot;TV distance&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;这里左图是 \(d_{TV}(p,q^{(k)})\) 随 \(k\) 的变化，右图是对 \(d_{TV}(p,q^{(k)})\) 取对数后的结果。可以看出，右图的散点在前期基本处在一条直线上，说明 Gibbs 样本与真实分布的 TV 距离几乎是随着迭代步数而&lt;strong&gt;指数&lt;/strong&gt;下降的。后期保持一个接近于零的常数是因为 \(q^{(k)}(x)\) 是用样本的经验分布逼近的，因此会有一些跟样本量有关的随机误差，它不会随着迭代步数的增大而消失。&lt;/p&gt;

&lt;p&gt;我们以后会发现，这一误差呈指数衰减的性质会在 MCMC 的分析中占据非常重要的位置。用更正式的语言来说，如果一个 MCMC 算法经过有限步迭代后样本的分布与真实分布之间的 TV 距离（\(d_{TV}(p,q^{(k)})\)）随迭代步数（\(k\)）呈指数性的衰减，即存在常数 \(C&amp;gt;0\) 和 \(\rho&amp;gt;0\) 使得&lt;/p&gt;

\[d_{TV}(p,q^{(k)})\le Ce^{-\rho k},\]

&lt;p&gt;那么我们就称这个 MCMC 算法是几何遍历（Geometric ergodic）的。几何遍历性之所以重要，是因为它代表了&lt;strong&gt;快速&lt;/strong&gt;收敛的特点：具有几何遍历性的 MCMC 算法通常只需很少的步数就可以近似真实的分布，正如前面的例子展示的那样。事实上，对于大部分我们使用的 MCMC 算法，几何遍历性都是成立的，但要严格进行证明却不是一件简单事。关于这一点，就放到以后再说了。&lt;/p&gt;

&lt;p&gt;欲知后事如何，且听不知道有没有下回的下回分解。&lt;/p&gt;

&lt;p&gt;参考文献：Sean Meyn and Richard Tweedie (1993). Markov Chains and Stochastic Stability, Springer.&lt;/p&gt;

&lt;p&gt;附：【思考题】答案&lt;/p&gt;

&lt;p&gt;经过推导，可以发现&lt;/p&gt;

\[Y\sim Beta(a,b),\quad X|\{Y=y\}\sim Binomial(n,y),\]

&lt;p&gt;换言之，\(Y\) 服从一个 Beta 分布，取值范围是 \((0,1)\)，而给定 \(Y=y\)，\(X\) 服从一个概率为 \(y\) 的二项分布。\(X\) 精确的密度函数为&lt;/p&gt;

\[p(x)=\binom{n}{x}\frac{B(a+x,b+n-x)}{B(a,b)},\]

&lt;p&gt;其中 \(B(a,b)=\Gamma(a)\Gamma(b)/\Gamma(a+b)\) 是 Beta 函数。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2019/12/mcmc-notes-1/</link>
    <guid>https://yixuan.blog/cn/2019/12/mcmc-notes-1</guid>
    <pubDate>2019-12-28T08:16:46+00:00</pubDate>
  </item>

  <item>
    <title>尬聊</title>
    <description>&lt;p&gt;今天机缘巧合下和一位刚认识不久的朋友约了个午饭，其间他说看过我的中文博客，于是我心血来潮上来一看，发现博客今天停更了整好两年。仔细想想这两年，其实发生了很多事，也有很多东西应该记录下来的，但最后都默默沉了下去，一半是这期间确实没时间没精力，而另一半则是刻意保持了某种沉默。其中细节暂且不提了。&lt;/p&gt;

&lt;p&gt;但今天决定上来补一篇，是我发现最近跟一个动词一直扯上关系，就是标题所说，尬聊。我回顾读博的经历，发现有一项技能是我觉得非常必要但一直没学会的，就是跟陌生人尬聊的能力。如果说得更通俗一点，就是在一个相对陌生的场合跟周围的人打开第一个话题的能力。之所以想到这个，是因为今年在外面跑了好几个会，而在这些场合你都有一段时间被圈在一个封闭的空间里（比如餐桌、游船、报告厅等），然后你不得不跟周围的人产生一些互动。但我是属于那种没有特别明显的共同兴趣就聊不下去的人，所以简单的问候过后就突然失去了话题，而且更糟的是这个世界大概存在某种尬聊第一定律——你尬聊不下去的人，对你也有尬聊不下去的反作用。当你经历过几次尬聊之后，大脑就给你释放出一个保护讯号：与其尬聊不下去，不如压根就别聊了，所以最后的结果就是干脆也别打开话匣子，这样好歹免去了进一步的尴尬。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;至今有件事依然给我留下了很深的烙印。2016年在芝加哥的 JSM 统计大会上，导师为我要到了一张谷歌举办活动的入场券，那是谷歌邀请北美的统计教授和学生来一艘游船上参加晚宴，一方面是让不同学校的人互相了解，另一方面当然也是物色学生为将来招聘作储备。导师让我参加这个活动，也是鼓励我跟同行的人多聊聊，扩展一下社交圈，所以一开始我还挺期待的。游船开动之后，大家拿了食物就开始互相问候起来，简单打了些招呼，我发现受邀来的基本都是统计全美排名前十的名校，他们彼此之间或多或少都有一些交集，而来自众生大学的只有我一个。这个时候一种无形的威压逼迫而来，然后一种隐秘的自卑感开始作祟，越聊越不敢聊，到后来几乎就是自己坐在一个角落里喝饮料。这趟行程后我很少跟人提及这次经历，但也是这时候我意识到自己尬聊的能力急需增强。当时真的有种恨铁不成钢的感觉，直到现在我还保留着这次活动的船票，以作自勉警醒之用。&lt;/p&gt;

&lt;p&gt;后来开始反思，尬聊之所以难开口，是因为你没有去发掘你跟周围人的共同之处。人跟人之间的第一句交流，往往就是一个聚类算法，是校友啊，是老乡啊，诸如此类。前文说的那位刚认识不久的朋友，是上周在一个会上认识的。当时是他先给我打的招呼，因为他在出发的飞机上就发现了我，说明我们至少都是从匹村出发，也都是做统计的。相互来往几句，发现交集越来越多：他是匹大生统系的博士生，而我长年的同学彪哥马上就要去那里任教了；他说好像见过我的名字，因为他读过&lt;a href=&quot;https://cosx.org/&quot;&gt;统计之都&lt;/a&gt;上的一些文章。今天我们吃饭聊起这事，他说他也费了很大力气才决定开第一句口的，我说这么看来你还是比我强，我恐怕还做不到呢。&lt;/p&gt;

&lt;p&gt;有些人不管是天生的能力还是后天的锻炼，会很敏锐地抓住人与人之间的联系，比如对一个圈子的八卦和人物关系如数家珍。我自知自己还做不到这一点，一个很典型的例子就是我记名字的能力非常差，往往当时没话说，几天之后突然想起来，欸，这不就是当时谁谁谁提过的那谁谁谁吗，其实还是有一些交集的，怎么那时候就没意识到呢。所以我给自己先定了一个阶段性目标：有些场合自己没什么参与感，尬聊难度本身就很大，所以就不给自己增加压力，随意就好；当有一些展示自己的机会时，比如作了一场报告，或是对别人的报告提了问题，就逼着自己去跟场景中还不认识的人打招呼——这个难度其实已经降了很多，而且效果往往都还不错。大概尴尬是构成脸皮的某种基本物质，尬聊次数多了，后面反而会更轻松些。&lt;/p&gt;

&lt;p&gt;不过话又说回来，想要跟人有交集，至少在统计这个圈子，说白了还是要自己有干货才行。要么仔细研究过别人的工作，要么做出了让别人有印象的工作，尬聊一分钟，背后一年功。想到此处，便赶紧 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git commit&lt;/code&gt;，继续默默搬砖去了。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2019/08/networking/</link>
    <guid>https://yixuan.blog/cn/2019/08/networking</guid>
    <pubDate>2019-08-22T22:00:00+00:00</pubDate>
  </item>

  <item>
    <title>为什么统计学家也应该学学TensorFlow</title>
    <description>&lt;p&gt;（先啰嗦一句：本文的标题和内容牵涉到 TensorFlow，只是因为它是可用的工具&lt;strong&gt;之一&lt;/strong&gt;，我相信很多其他的框架都可以做到文中我想要实现的功能。我自己并没有工具上的偏好，所以就当是拿 TensorFlow 举一个例子。）&lt;/p&gt;

&lt;p&gt;对于学统计做统计的人来说，这可能是最好的时代，也可能是最坏的时代。好的地方我就不多说了，基本上关键词包括“大数据”、“数据科学”等，自行搜索可以列举出无数多激动人心的字眼。为什么会坏呢？因为统计的很多传统优势正在逐渐被众多机器学习算法取代，因此许多统计的核心元素，要么因为适应不了新形式的数据而被边缘化，要么因为在机器学习中有广泛应用而被有意无意地同化到了机器学习当中。&lt;/p&gt;

&lt;p&gt;面对机器学习，或者进一步缩小范围，面对当前火热的深度学习，我陆陆续续找了一些做统计的老师和朋友聊过这个话题，发现有两类观点比较明显。第一类是深度学习“威胁论”，主要的意思是统计学被蚕食得非常严重，很多原来统计的阵地，不管是方法论上的还是现实案例中的，都被深度学习取代了。另一类是“否定论”，认为深度学习大部分是在炒作，在一些特定的行业（例如制药），传统统计依然处于核心地位，因此无需过于担心。&lt;/p&gt;

&lt;p&gt;每次聊完之后我都会觉得“哇，对啊对啊，好有道理”，但转念一想总觉得哪里不对劲——为什么没有一种“学习论”呢？我觉得在很多情况下，人们对于新生的事物都有一种天然的抵触感，但如今面对深度学习的火热趋势，你可以支持它，也可以批判它，但惟一做不到的就是去&lt;strong&gt;忽视&lt;/strong&gt;它。相反，当你试着去了解这些新的事物，你会发现它给你带来的好处可能要远远高于其坏处。&lt;/p&gt;

&lt;p&gt;当然，这篇文章并不想过多讨论这类高层次的问题。之所以写这一篇，是想以一些自己的经历和体验来说明为什么我们应该去尝试了解和学习一些新生的事物。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;暑假的时候，利用一些空余的时间看了看 TensorFlow 的文档。在我以前的印象中，TensorFlow 是为深度学习而设计的，如果我的研究或工作不涉及深度学习，我就应该永远也用不到它。就是因为有这种刻板印象，一直以来我对于这类软件和工具都处于一种旁观的态度。&lt;/p&gt;

&lt;p&gt;然而当你实际迈出了第一步时，你就会发现很多事情并不是之前想像的那样。比如对于 TensorFlow，你会发现其&lt;strong&gt;核心组件&lt;/strong&gt;跟深度学习&lt;strong&gt;没有半毛钱关系&lt;/strong&gt;。如果使用得当，你完全可以把 TensorFlow 打造成一个强大的帮助你进行统计&lt;strong&gt;建模&lt;/strong&gt;和&lt;strong&gt;求解&lt;/strong&gt;的工具。这是我在统计这个大湖里泡了蛮久，然后又在新的水源中找到一片活水时所收获的惊喜。&lt;/p&gt;

&lt;p&gt;上个学期参加了一个讨论班，组织的老师吐槽说，我们学统计的，重心大都在模型的估计和推断上，却忽视了建模的过程。其后果就是，为了估计和推断能有比较简单的形式，往往会在建模上进行简化，这就造成了很多统计模型在实际中并不实用。如果有一种办法，能让统计学家更专注于模型的搭建，而让计算机去完成大部分的求解工作，那么统计应该就会有更广阔的应用空间。&lt;/p&gt;

&lt;p&gt;于是在我接触了 TensorFlow 之后，我发现它正好对上面的这个疑问提供了一个回应。TensorFlow 的核心部件，大部分是一些针对向量和矩阵（或者更一般地说是张量，也就是 TensorFlow 名称中的 Tensor）的&lt;strong&gt;运算符&lt;/strong&gt;。这些运算符本质上是从输入变量到输出变量的一个映射，比如计算均值的运算符，其输入一个向量，就可以得到一个单一数值的输出，又或者矩阵乘法运算符，输入两个矩阵，输出一个新矩阵。更重要的是，这些运算符包含了输入输出间导数（或者梯度）的信息，因此给定输入的数值，程序可以计算出输出对于输入的梯度，反之亦然。这样一来的结果就是，借助&lt;a href=&quot;https://en.wikipedia.org/wiki/Automatic_differentiation&quot;&gt;自动微分&lt;/a&gt;的技术，当你定义了一连串的运算后，计算机就可以自动帮你计算整个输入输出间的梯度。而我们知道，许多统计模型最后都依赖于极大似然估计，而对极大似然的最优化，最经典的办法就是梯度下降法，因此当梯度计算出来了，模型的求解就完成了一大半。&lt;/p&gt;

&lt;p&gt;当统计学家从繁复的导数计算中解放出来了，他们就可以用更多的时间来愉快地&lt;del&gt;加班&lt;/del&gt;建模了。&lt;/p&gt;

&lt;p&gt;说了这么多，该用一个实际的例子来展示一下被释放的&lt;del&gt;小宇宙&lt;/del&gt;生产力了。我们来考虑一个简单但非常实用的统计模型：混合分布的参数估计。下面这段 Python 代码生成了一组服从 \(0.3\cdot N(0,1)+0.7\cdot N(5,1.5^2)\) 混合分布的模拟数据。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;numpy&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Parameters
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.3&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;p2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.7&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mu1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;mu2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5.0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sigma1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sigma2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.5&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Simulate data
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binomial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;astype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;bool_&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sigma1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logical_not&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sigma2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Histogram
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bins&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div align=&quot;center&quot;&gt;
    &lt;img src=&quot;http://i.imgur.com/oEXEagA.png&quot; alt=&quot;Histogram&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;假设我们用的是“真实”的模型，也就是 \(p_1 N(\mu_1,\sigma_1^2)+p_2 N(\mu_2,\sigma_2^2)\)，那么接下来的任务就是用极大似然的方法来估计这些未知参数的取值。&lt;/p&gt;

&lt;p&gt;看到这里，你可能已经意识到，这是一个高斯混合模型（GMM），通常的解法是 EM 算法。但在这里，作为展示我们依然采用梯度的办法，而且另外一个好处是可以方便地扩展到其他的分布。利用 TensorFlow，我们可以用如下的代码来进行模型估计：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;TensorFlow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;TensorFlow.contrib.distributions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Define data
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Define parameters
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_p1_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t_p1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softplus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_p1_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t_mu1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t_mu2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t_sigma1_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t_sigma1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softplus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_sigma1_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t_sigma2_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Variable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t_sigma2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;softplus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_sigma2_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Define model and objective function
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_gm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Mixture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Categorical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;probs&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_p1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_p1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;components&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_mu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_sigma1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Normal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_mu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_sigma2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;t_ll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce_mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_gm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log_prob&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Optimization
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GradientDescentOptimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;minimize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_ll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Run
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;global_variables_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;Estimated values:&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t_p1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_mu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_mu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_sigma1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t_sigma2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&apos;True values:&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sigma1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sigma2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其输出结果为&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Estimated values: [0.29020751, -0.078560337, 5.0137568, 0.95754921, 1.4157518]
True values: [0.3, 0.0, 5.0, 1.0, 1.5]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以看出来，估计的结果和真值是非常接近的。&lt;/p&gt;

&lt;p&gt;在这里稍微做一些解释，更详细的介绍可以去查阅 TensorFlow 的&lt;a href=&quot;https://www.TensorFlow.org/get_started/get_started&quot;&gt;官方文档&lt;/a&gt;。这段 TensorFlow 程序其实非常贴合统计建模的思路。首先，我们定义一个结构 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_x&lt;/code&gt; 用来表示观测到的数据，这里 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.placeholder&lt;/code&gt; 的意思是当前该变量还没有取值，但会在之后予以提供。后面有一连串的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.Variable&lt;/code&gt;，是 TensorFlow 中用来表达待估参数的一种方式，括号中的第一个数是我们提供的初值。其中我们还用到了一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;softplus&lt;/code&gt; 运算符，这是为了照顾那些恒正的参数而做的一个变换，等价于函数 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;log(exp(x) + 1)&lt;/code&gt;。在这之后就是定义我们的混合模型，可以看到该模型的参数都是之前定义的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tf.Variable&lt;/code&gt; 对象，而我们的目标就是该混合分布在数据上的似然函数值，我们把它存储在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ll&lt;/code&gt; 对象中。&lt;/p&gt;

&lt;p&gt;定义好这个模型后，TensorFlow 就可以自动计算 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_ll&lt;/code&gt; 相对于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_p1_&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_mu1&lt;/code&gt; 等参数的导数，而利用 TensorFlow 提供的最优化工具，我们只需要反复迭代更新参数，直到满足收敛条件就可以了。&lt;/p&gt;

&lt;p&gt;整个过程中，我们几乎只需要把我们脑海中的模型用 TensorFlow 内置的运算符表达出来，其余的工作都可以自动化或半自动化地完成。由于导数也是由自动微分完成的，所以我们甚至连公式的推导都可以进一步简化。此外，如果你想把混合分布的成分改成 t-分布，或是想把其中的某个参数改成其他协变量的线性函数，你只需要改变一下参数和模型的定义，几乎没有多少额外的工作，这可以说是对统计建模一次巨大的&lt;strong&gt;改革&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;说了这么多，无非就是想告诉大伙儿，他山之石，可以攻玉。在面对层出不穷的新技术时，我当时也是拒绝的，但只有亲身尝试了之后，才能够切实对这些技术做出一些更为客观的评价。写这篇文章，也是希望能给身在统计圈的各位提供一些启发。数据科学也好，深度学习也好，事实是这些新名词已经诞生了，并且还附带了一系列的并发效应。除了吹与黑，每个人都有的第三种选择就是去主动拥抱这些新的知识和技术，然后去思考如何利用他们来为自己的研究和工作提供便利。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2017/08/tensorflow-for-statisticians/</link>
    <guid>https://yixuan.blog/cn/2017/08/tensorflow-for-statisticians</guid>
    <pubDate>2017-08-22T19:42:41+00:00</pubDate>
  </item>

  <item>
    <title>清白之年</title>
    <description>&lt;p&gt;朴树出新歌了。我并不属于那种“等了14年”的歌迷，只是遇到有才情的歌曲就会喜欢上。这次的新专辑主打的是《清白之年》，简单的歌词和旋律，但是有才情。&lt;/p&gt;

&lt;p&gt;专辑在网易云上首发，只要15元，非常良心了。结果 APP 准备好后告诉我海外无法支付，真是想花也花不掉。Youtube 上听了试听版，下次回去一定把欠下的补上。&lt;/p&gt;

&lt;p&gt;清白之年，每个人都有自己的解读。或许是清澈的面庞，与飘飘的白衣，又或许是江上之清风，与山间之白月。但无论如何，清白总是一张刚铺开的画布，留下无数种可能。&lt;/p&gt;

&lt;p&gt;时间给一个人留下的可怕痕迹，就是从前他会想“我将得到什么”，而未来他会想“我还拥有什么”。进大学之前常常会幻想，将来的几年里会遇到哪些有趣的人，有趣的事，而现在十年过去，操心的事怎么就成了如何精打细算，把简历上的每一项指标兑换成找工作的筹码。清白之年里的一缕缕期许，不知如今往后可还找寻得到？&lt;/p&gt;

&lt;p&gt;今年的主要目标就两个字，毕业。但是越接近这个目标，就会越发感到一种无力感。并不是说担心毕不了业，而是觉得自己有愧于从前的自己。害怕有一天，我渐渐停止各种不切实际的幻想，开始计算自己的极限所在，慢慢变得精明而无趣。我向来自认为是一个悲观的乐观主义者，记录下类似的这种文字，就是想时不时提醒自己，路可能越走越窄，但无论如何也要在画布中留下几处清白。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;人随风飘荡&lt;/p&gt;

  &lt;p&gt;天各自一方&lt;/p&gt;

  &lt;p&gt;在风尘中遗忘的清白脸庞&lt;/p&gt;

  &lt;p&gt;此生多勉强&lt;/p&gt;

  &lt;p&gt;此身越重洋&lt;/p&gt;

  &lt;p&gt;轻描时光漫长&lt;/p&gt;

  &lt;p&gt;低唱语焉不详&lt;/p&gt;
&lt;/blockquote&gt;

&lt;!-- more --&gt;
</description>
    <link>https://yixuan.blog/cn/2017/05/uncharted/</link>
    <guid>https://yixuan.blog/cn/2017/05/uncharted</guid>
    <pubDate>2017-05-01T01:15:40+00:00</pubDate>
  </item>

  <item>
    <title>李斯与 GitHub</title>
    <description>&lt;p&gt;抱歉，我又标题党了。&lt;/p&gt;

&lt;p&gt;我可以很负责任地说，李斯没有用过 GitHub。取这个标题，是因为昨天在 &lt;a href=&quot;https://news.ycombinator.com/item?id=14182203&quot;&gt;Hacker News&lt;/a&gt;
上看到一个歪果仁写的关于中国文字演变的系列文章（&lt;a href=&quot;https://al3x.svbtle.com/on-chinese-writing-1&quot;&gt;一&lt;/a&gt;，&lt;a href=&quot;https://al3x.svbtle.com/on-chinese-writing-2&quot;&gt;二&lt;/a&gt;，&lt;a href=&quot;https://al3x.svbtle.com/on-chinese-writing-when-mao-reinvents-writing&quot;&gt;三&lt;/a&gt;），觉得很有意思。其中第二篇里讲到秦始皇时期的统一文字，用了这样一个类比（因为暂时无法联系到作者授权，所以就不贴原文了，大意如下）：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;因为中国这个项目太大，所以每个诸侯国都从文字的总库进行了分流（Fork），导致文字库变得异常混乱。秦始皇下令说我们要有一个统一的主线，于是把这事交给了项目经理李斯丞相。李斯于是就开始处理各种合并请求（Pull request），负责各分支的合并（Merge）事项。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;嘿，被他这么一说还挺像那么回事。&lt;/p&gt;

&lt;p&gt;我一口气看完了这位歪果仁（很可能是一位发果仁）写的三篇文章，发现他的认识着实不简单，而且很多吐槽写得深得我心。比如第一篇写文字的诞生，一种推测是原始部族为了占卜的需要而有了甲骨文，然后他就吐槽说这个故事太不浪漫了，文字的诞生居然是基于官僚的目的。如果让他来设计一段历史，他会隐秘地希望文字的诞生是为了记录李白“水中捞月，醉酒溺亡”这个传说（当然这是野史）。&lt;/p&gt;

&lt;p&gt;然后讲到汉字的构字法，他先列举了一些典型的形声构字（晴，清，睛），说这种造字法多么有逻辑，然后幸灾乐祸地说，呵呵，那是你们太天真了，所有的中文课本都会在第一课给你展示这些字，但随着你往后学，你会发现越来越多的特例，而学到最后你就会发现我去原来最初学的那些完美的构字才是特例啊。&lt;/p&gt;

&lt;p&gt;最让我会心一笑的是第三篇中关于简体中文和繁体中文的对比。谈到简体使用者和繁体使用者之间的交流，他说中国大陆在学校只教简体，直到……卡拉OK被发明出来，那时中国学生可能每天要花一个小时来接受繁体中文的训练。我当时看到这段,心想我勒个去你一个歪果仁怎么这么懂这些梗啊……&lt;/p&gt;

&lt;!-- more --&gt;
</description>
    <link>https://yixuan.blog/cn/2017/04/lisi-and-github/</link>
    <guid>https://yixuan.blog/cn/2017/04/lisi-and-github</guid>
    <pubDate>2017-04-26T12:58:25+00:00</pubDate>
  </item>

  <item>
    <title>震惊！时隔两年八阿哥居然回来了！</title>
    <description>&lt;p&gt;不用看了，&lt;a href=&quot;https://www.zhihu.com/question/55517991&quot;&gt;UC 震惊部&lt;/a&gt;已经不招人了。&lt;/p&gt;

&lt;p&gt;这里也没有什么八阿哥，只有一堆 Bug。&lt;/p&gt;

&lt;p&gt;事情是这样的，昨天收到了一封邮件，是 R Core 之一的 Martin Maechler 通过 R 的 Bug 汇报系统给我的回复。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://yixuan.blog/cn/images/r_bug.png&quot; alt=&quot;R Bug&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;两年前在写一个 R 包的时候发现 R 的报警系统（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R CMD check&lt;/code&gt;）误判了我写的几个函数，于是追根溯源发现是 R 的源代码中有个函数用错了，于是就发了一个 Bug 汇报。结果 R 开发者那边一直没有回音，拖到现在我都基本快忘掉这回事了，直到 Martin 不知道怎么就逛到了这个 Bug 然后给了一个隔空两年的对话。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;&lt;del&gt;终于见到比谢大还能耐得住性子&lt;a href=&quot;https://yihui.name/cn/2017/04/r-markdown-office/&quot;&gt;等啊等啊两年后再给你回复&lt;/a&gt;的人了。这句得掐掉。&lt;/del&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2017/04/bug-after-two-years/</link>
    <guid>https://yixuan.blog/cn/2017/04/bug-after-two-years</guid>
    <pubDate>2017-04-06T20:29:01+00:00</pubDate>
  </item>

  <item>
    <title>寂寞沙洲冷</title>
    <description>&lt;p&gt;前几周的时候放春假，跟几位小伙伴自驾出去玩。开车坐车的无聊时间自然是要放歌【插播一条段子，当某小伙伴对司机喊了声“放歌吧”的时候，我附和了句“但我不能放歌，悄悄是别离的笙箫”，然后全车人开始刹不住车背起诗来了……】，途中某个时候放了周传雄的《寂寞沙洲冷》。&lt;/p&gt;

&lt;p&gt;我突然意识到，虽然我知道这句话是来自苏轼的一首词，但其实我对词的全文已经完全没印象了。之后突然又想起这事，于是就上网搜了搜。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;缺月挂疏桐，漏断人初静。谁见幽人独往来，缥缈孤鸿影。&lt;/p&gt;

  &lt;p&gt;惊起却回头，有恨无人省。拣尽寒枝不肯栖，寂寞沙洲冷。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;这首词的意境很容易让人想起元好问的《摸鱼儿·雁丘词》，都是以鸿雁的故事为旨，咏其心志之高。从故事的结尾来看，似乎还不如雁丘词之悲壮，然而令我感到惊奇的是，这首词相传还有一段序：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;惠州有温都监女，颇有色。年十六，不肯嫁人。闻坡至，甚喜。每夜闻坡讽咏，则徘徊窗下，坡觉而推窗，则其女逾墙而去。坡从而物色之曰：“当呼王郎，与之子为姻。”未几，而坡过海，女遂卒，葬于沙滩侧。坡回惠，为赋此词。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;此序的真实性尚有疑问，但如果真有这段往事，那么和这首《卜算子》合起来，顿时觉得其文字的重量多了好几分。苏轼啊苏轼，你到底是想写孤鸿，还是奇女，还是你自己呢？或许作为读者，多少都有一份私心，希望这个故事是真的吧。&lt;/p&gt;

&lt;p&gt;但不管怎样，词人似乎都想要回答，或者可能已经回答了一个问题：雁冷沙洲，到底值是不值？温氏女郁郁而终，到底值是不值？自己不愿与世同流，到底值是不值？&lt;/p&gt;

&lt;p&gt;有些事情可能永远无法衡量得失，只能说有选择便有代价，有些人付不起，有些人担得下，有些人赢生前事，有些人得身后名，仅此而已。&lt;/p&gt;

&lt;p&gt;又及：写这篇博客的时候几乎立马定了这个标题，但立刻就发愁英文短链该怎么写，想着总不能直译吧。后来突然想起来在央视的一个节目《郎读者》里有一位许渊冲老先生，节目介绍说他翻译了许多唐诗宋词，于是抱着试一试的心态去网上搜了搜他的译作，其中果然有这首《卜算子》：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;From a sparse plane tree hangs the waning moon&lt;/p&gt;

  &lt;p&gt;The water clock is still and hushed is man&lt;/p&gt;

  &lt;p&gt;Who sees a hermit pacing up and down alone?&lt;/p&gt;

  &lt;p&gt;Is it the shadow of a swan?&lt;/p&gt;

  &lt;p&gt;Startled, he turns his head&lt;/p&gt;

  &lt;p&gt;With a grief none behold&lt;/p&gt;

  &lt;p&gt;Looking all over, he won’t perch on branches dead&lt;/p&gt;

  &lt;p&gt;But on the lonely sandbank cold&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;绕了一大圈，结果回到了起点。&lt;/p&gt;

&lt;!-- more --&gt;
</description>
    <link>https://yixuan.blog/cn/2017/03/lonely-sandbank-cold/</link>
    <guid>https://yixuan.blog/cn/2017/03/lonely-sandbank-cold</guid>
    <pubDate>2017-03-31T20:50:23+00:00</pubDate>
  </item>

  <item>
    <title>现学现卖</title>
    <description>&lt;p&gt;之前统计之都一直在酝酿一系列的网站改版，但因为种种原因中断了一些时间，直到最近才又把改版计划提上了日程。其中&lt;a href=&quot;https://github.com/cosname/cosx.org&quot;&gt;主站的部分&lt;/a&gt;由谢大牵头，目前已经召集了一支&lt;a href=&quot;https://github.com/orgs/cosname/teams/editors&quot;&gt;精锐部队&lt;/a&gt;大刀阔斧地在 Github 上开干了，而论坛的部分则是由我接盘，准备把当前有些老旧的 bbPress 系统迁移到 &lt;a href=&quot;http://flarum.org/&quot;&gt;Flarum&lt;/a&gt; 上去。&lt;/p&gt;

&lt;p&gt;俗话说得好，不写 SQL 的前端工程师不是好的 PHP 程序员。我前阵子决定接论坛的盘是因为上一次论坛搬家的时候费力研究过 bbPress 的数据库结构，所以在看过 Flarum 的后台后觉得这个可以有，然而实际开始搬的时候才发现掉进了天坑里，而这个锅主要得甩给&lt;a href=&quot;http://blog.schauderhaft.de/2010/02/15/why-sql-sucks/&quot;&gt;反人类的 SQL&lt;/a&gt;。就这么说吧，假设所有的表都存成数据框（Data frame），在 R 里10分钟能搞定的活用 SQL 就非得耗掉你一小时，其中30分钟是在搜需要的函数，而这30分钟里面的25分钟是在确认并不存在我需要的那个函数。依我看如果科技公司要招数据库程序员，技术面试的问题都可以免了，直接给个论坛的数据带回家，一周之内成功转到另一个系统就算通过。这个考验的不是懂多少 SQL，而是考察这个人有没有足够的耐性写一千行枯燥的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt; 而不至于暴走。&lt;/p&gt;

&lt;p&gt;就在我天真地以为数据库导好后就万事大吉时，我才发现万里长征只迈出了第一脚。原因是当前的 Flarum 系统还不成熟，许多功能核心开发者还没有打算实现，比如我发现它的密码验证机制跟当前论坛的不一样，还有默认的编辑器不支持数学公式等。怎么办？没办法，只好参照&lt;a href=&quot;http://flarum.org/docs/extend/&quot;&gt;官方文档&lt;/a&gt;的描述，给论坛写些扩展的插件了。可是论坛的后端是用&lt;a href=&quot;https://www.zhihu.com/question/26133313&quot;&gt;最好的编程语言&lt;/a&gt;写的，还是基于什么高大上的 &lt;a href=&quot;https://laravel.com/&quot;&gt;Laravel&lt;/a&gt; 框架，我一个平时用 R 做统计计算的，真的办不到啊。&lt;/p&gt;

&lt;p&gt;所以最后能做的就是依葫芦画瓢，现学现卖了。找了几个官方出品的插件，模仿着它们的结构边查文档边改代码，最终也算是把程序都调通了。&lt;a href=&quot;https://github.com/cosname&quot;&gt;统计之都的 Github&lt;/a&gt; 上一堆 flarum 开头的项目都是这时期的产物。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://yixuan.blog/cn/images/flarum.png&quot; alt=&quot;Flarum插件&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;说实话，论坛这种上古遗物可能确实已经有些跟不上时代了，或许只有我们这种前朝遗老还愿意花时间和精力把数据库里面一行行的记录搬来搬去，视若珍宝。不过新论坛现在看上去还是挺简洁美观的，新增的功能也都非常实用，各位大侠不妨点开看看这个&lt;a href=&quot;https://cos.name/flarum/&quot;&gt;预览版&lt;/a&gt;，然后到&lt;a href=&quot;https://cos.name/cn/topic/418468/&quot;&gt;这个帖子&lt;/a&gt;去提些意见和建议。在此先行谢过。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2017/03/doing-while-learning/</link>
    <guid>https://yixuan.blog/cn/2017/03/doing-while-learning</guid>
    <pubDate>2017-03-28T22:38:09+00:00</pubDate>
  </item>

  <item>
    <title>段子手苏东坡</title>
    <description>&lt;p&gt;晚上失眠，起来翻几页书来读，翻到苏轼的词选，结果这老家伙害得我更睡不着，活生生笑清醒了。此仇不报难解心中之痒，所以分享出来给诸君放毒。&lt;/p&gt;

&lt;p&gt;事情是这样的，熙宁七年九月，苏老爷子，啊不对，当年他也才37，是个大叔，苏大叔路过湖州，当时的湖州知州叫李公择，正巧生了个儿子，第三天的时候宴请宾客，请苏大叔写首词助助兴。然后苏大叔就写了下面这首《减字木兰花》给他：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;惟熊佳梦，释氏老君亲抱送。壮气横秋，未满三朝已食牛。&lt;/p&gt;

  &lt;p&gt;犀钱玉果，利市平分沾四座。多谢无功，此事如何着得侬？&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;前半阙其实还比较正常，是化用自杜甫的诗《徐卿二子歌》，里面有这么几句：“徐卿二子生绝奇，感应吉梦相追随。孔子释氏亲抱送，并是天上麒麟儿。大儿九龄色清澈，秋水为神玉为骨。小儿五岁气食牛，满堂宾客皆回头。”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;但是，&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;苏大叔你怎么抄得这么不走心啊！&lt;img src=&quot;https://i.imgur.com/byZg5ia.gif&quot; alt=&quot;[扶额]&quot; /&gt;&lt;/p&gt;

&lt;p&gt;杜老爷子说人家小孩是孔子和佛祖抱着来的，到了你这儿倒好，变成了佛祖和老君抱来的，&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;不知道他们俩在一块会打起来啊？！&lt;/strong&gt;&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;然后杜甫吹吹牛，说徐家小孩儿厉害啊，五岁就有食牛的气势了，结果苏大叔把这牛吹大了六百倍，说老李家的儿砸三天就够了啊。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;我……勒……个……去……&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;人家只是个未满三天的孩子啊，你以为是哪吒吗？&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/R87Ec8m.jpg&quot; alt=&quot;[十冷哪吒]&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;然后到了下阙，&lt;strong&gt;高能来了&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;犀钱玉果，是洗儿会（一种洗礼）上用的洗儿钱和洗儿果，把他们分发给宾客，相当于发红包。然后苏大叔说，哈哈，多谢多谢，无功受禄，真是不好意思啊。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;什么？你说你没看懂笑点在哪儿？&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;那是因为最后这一句又是一个典故。相传晋元帝生了皇子，宴请百官，赐绫罗绸缎。大臣殷洪乔叩谢皇上，说“臣无功受禄，甚觉惶恐”，然后晋元帝说，“这事哪容得你有功啊？”于是百官笑惨……&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/yzsej8y.gif&quot; alt=&quot;[捶地]&quot; /&gt;
&lt;img src=&quot;https://i.imgur.com/yVAtVnK.gif&quot; alt=&quot;[捶地]&quot; /&gt;
&lt;img src=&quot;https://i.imgur.com/VHtil3w.gif&quot; alt=&quot;[捶地]&quot; /&gt;
&lt;img src=&quot;https://i.imgur.com/mtLPatE.gif&quot; alt=&quot;[捶地]&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;苏轼你个老流氓……&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;苏大叔的另一则趣闻发生在同年的十月，那时他在润州，太守设宴款待，并让名叫郑容和高莹的两名歌女陪酒。两位姑娘颇得苏大叔欢心，席间她们便请求苏大叔为她们求情脱籍（不当歌女，嫁人），苏大叔立马答应了，但却一直没有把这事跟太守说。一直到了他快要离开的时候，两位姑娘跑到苏大叔的船上，再次恳求他帮忙。苏大叔便说，“你们拿我写的这首词给太守，他看了就明白了。”&lt;/p&gt;

&lt;p&gt;这首词便是另一首《减字木兰花》：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;郑庄好客，容我尊前先堕帻。落笔生风，籍籍声名不负公。&lt;/p&gt;

  &lt;p&gt;高山白老，莹骨冰肌那堪老。从此南徐，良夜清风月满湖。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;二女不解词中之意，只觉是在赞颂主人好客，风月美好。然而当她们把这首词递上去之后，太守果然批准了她们落籍从良。其中玄妙，就在于这是首藏头词，八句首字，便是“郑容落籍，高莹从良”。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;苏东坡，老狐狸啊。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;但这狐狸着实让人觉得可爱。要知道歌女在当时的社会地位地下，苏东坡能以一己之才华，解歌女之忧苦，感人间之冷暖，实属不易。看似戏谑玩世，实则赤心一片。&lt;/p&gt;

&lt;p&gt;林语堂评价苏轼说：“苏东坡是一个无可救药的乐天派、一个伟大的人道主义者、一个百姓的朋友、一个大文豪……苏东坡比中国其他的诗人更具有多面性天才的丰富感、变化感和幽默感，智能优异，心灵却像天真的小孩——这种混合等于耶稣所谓蛇的智慧加上鸽子的温文。”&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/7iPNSWo.jpg&quot; alt=&quot;[苏东坡]&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;参考书目：《苏轼全集校注》第九册&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2016/06/dongpo-anecdotes/</link>
    <guid>https://yixuan.blog/cn/2016/06/dongpo-anecdotes</guid>
    <pubDate>2016-06-25T07:58:54+00:00</pubDate>
  </item>

  <item>
    <title>标题党统计学</title>
    <description>&lt;p&gt;如果你是被这个标题骗进来的，那么说明标题党的存在的确是有原因的。在网络高度发达（以及“大数据”泛滥）的今天，数据动不动就是以 GB 和 TB 的级别存储，然而相比之下，人类接受信息的速度却慢得可怕（参见大刘《乡村教师》）。试想一下，你一分钟能阅读多少文字？一千？五千？总之是在 KB 的量级。所以可以说，人们对文字的“下载速度”基本上就是 1~10KB/min。如果拿这个速度去上网的话你还能忍？&lt;/p&gt;

&lt;p&gt;既然如此，每天网上有成千上万的新闻、报告、文章和八卦，怎么看得过来呢？没办法，只能先对正文进行一次粗略的筛选——看标题。俗话说得好，这是一个看脸的世界。于是乎，文章的作者为了吸引读者，就要取个足够博眼球的标题，而所谓标题党便是充分利用这种心理，用各种颇具创意的标题来吸引读者的注意。&lt;/p&gt;

&lt;p&gt;好了，既然看官已经看到了这里，我就可以承认本文其实也是标题党了。这篇小文并不是要讨论标题党的前世今生，而是研究一个与此有关的统计问题：怎样的标题会更加吸引读者的关注？&lt;/p&gt;

&lt;p&gt;这个问题有点太大了，所以我们缩小一下范围。既然是统计问题，就拿自家的一个例子下手吧：做统计学研究的，都得读各种各样的统计论文，那么论文的标题是否会对这篇文章的阅读量产生影响呢？巧的是，&lt;a href=&quot;http://www.tandfonline.com/loi/uasa20&quot;&gt;美国统计协会期刊（JASA）的网站&lt;/a&gt;上正好提供了该期刊旗下文章的下载访问量，所以我们可以以此做一个小分析，来研究一下标题与文章阅读量之间的关系。&lt;/p&gt;

&lt;p&gt;可能有读者要问，为什么要使用文章的访问量，而不是引用率呢？这是因为 JASA 在其网站上说明，访问量数值是指从 JASA 官网下载的统计量，不包括从其他途径（比如购买的论文数据库）的来源。在 JASA 网站上，下载文章之前读者能获取到的主要是文章的标题和作者信息，所以访问量的主要驱动因素就是读者在阅读标题和作者之后产生的好奇感，从而减少了数据中的噪音。相反，引用一篇文章，通常是对文章有了充分理解之后产生的行为，这时候标题的作用可能就非常微弱了。总而言之，JASA 文章的下载量可以较好地代表读者在获取了文章的基本信息后对它感兴趣的程度。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/DLTqSwi.png&quot; alt=&quot;JASA论文&quot; /&gt;
&lt;/div&gt;

&lt;!-- more --&gt;

&lt;p&gt;那么怎么研究文章标题与下载量之间的关系呢？我们知道，标题是由一个个的单词组成的，如果忽略掉单词之间的语法关系，我们可以以标题“是否包含某个单词”作为自变量，来看哪些单词与因变量（文章下载量）之间的关系最为显著。除此之外，我们还需要加上一些其他的自变量，从而更好地解释因变量的变化。本文中我们要考察的变量还包括：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;作者信息，即文章是否会因为某些大牛是其作者而有非常高的访问量？&lt;/li&gt;
  &lt;li&gt;文章所属的类别，例如是应用类（Applications and Case Studies）的文章更受欢迎还是方法类（Theory and Methods）的阅读量更大？&lt;/li&gt;
  &lt;li&gt;文章所属期数（Issue Number），即更新的文章是否会有更高/低的访问量？&lt;/li&gt;
  &lt;li&gt;文章在页面中的次序，即是否靠前的文章会倾向于有更高的访问量？（联想到竞价排名……）&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;有了这些数据，我们就可以开始我们的分析了，大体过程如下：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;截取最近十年的 JASA 文章，获取其标题、作者和下载量等信息；&lt;/li&gt;
  &lt;li&gt;将文章的标题进行分词，构造词频矩阵；&lt;/li&gt;
  &lt;li&gt;类似地，构建文章-作者矩阵；&lt;/li&gt;
  &lt;li&gt;以文章下载量为因变量，对标题、作者和其他自变量作回归；&lt;/li&gt;
  &lt;li&gt;提取重要的变量，考察其系数，以此作为重要性的指标。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;下面就对以上过程进行一些更详细的描述。&lt;/p&gt;

&lt;h3 id=&quot;数据获取&quot;&gt;数据获取&lt;/h3&gt;

&lt;p&gt;最近十年 JASA 共出刊 41 期（Issue 473 ~ Issue 513），文章大概有 1700 多篇。我们先获取每一期的的网址，例如
&lt;a href=&quot;http://www.tandfonline.com/toc/uasa20/101/473&quot;&gt;http://www.tandfonline.com/toc/uasa20/101/473&lt;/a&gt;，然后从网页中提取出每篇文章的标题，作者，下载量，文章所属类别，以及每篇文章所在的期数和在该页中的次序。文章最后给出了抓取网页的代码和获取到的数据。&lt;/p&gt;

&lt;h3 id=&quot;构造词频矩阵&quot;&gt;构造词频矩阵&lt;/h3&gt;

&lt;p&gt;由于是英文期刊，我们基本上可以省掉分词的步骤。对文章标题进行简单的处理，如转换小写，删除停用词之后，就可以构造词频矩阵了。我们首先构造一个所有文章标题词项的字典，标记为 Term 1 到 Term p，然后词频矩阵的每一行代表一篇文章，每一列代表这个词项在各标题中的词频。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/QLepnBa.png&quot; alt=&quot;词频矩阵&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;在本文的分析中，我们定义一个词项可以是1~3个连续单词的组合，比如如果文章标题是 &lt;em&gt;Applied linear regression&lt;/em&gt;，那么由此生成的词项就有 applied，linear，regression，applied_linear，linear_regression，applied_linear_regression 共六个。这么做的原因在于，一个单词的意义有时会依照其上下文而有所变化，例如同样是 estimation，point estimation 和 interval estimation 就代表了两种风格的文章，所以我们希望考虑到这些变化。&lt;/p&gt;

&lt;p&gt;此外，我们希望选出来的词项具有一定的代表性，所以在构造词频矩阵的时候，我们要求每一个词项在文章集合中至少出现过两次。这样就避免了一些特殊的词语搭配因为出现在某一篇访问量极高的文章中而被挑选出来。&lt;/p&gt;

&lt;p&gt;在 R 中，可以使用 &lt;a href=&quot;https://cran.r-project.org/web/packages/text2vec/index.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;text2vec&lt;/code&gt;包&lt;/a&gt;很方便地完成这一过程，代码如下：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text2vec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;article_dat&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read.csv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;jasa.csv&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stringsAsFactors&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;article_dat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tolower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_tokenizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sw&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;of&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;for&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;and&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;in&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;with&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;the&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;to&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;on&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;an&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;by&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;its&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itoken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title_voc&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_vocabulary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ngram&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stopwords&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prune_vocabulary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;term_count_min&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vectorizer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vocab_vectorizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title_voc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;itoken&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokens&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title_mat&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create_dtm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;it&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vectorizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;最终该矩阵包括 1753 篇文章和 2552 个词项。&lt;/p&gt;

&lt;h3 id=&quot;构造文章-作者矩阵&quot;&gt;构造文章-作者矩阵&lt;/h3&gt;

&lt;p&gt;文章-作者矩阵与词频矩阵非常相似，第 i 行第 j 列的元素表示第 j 个人是否是第 i 篇文章的作者，取值为 1 或 0。同样，为了避免单独一篇文章过大的影响，我们只包括最近十年至少在 JASA 上发过两篇文章的作者。最终该矩阵包括 1753 篇文章和 615 位作者。&lt;/p&gt;

&lt;h3 id=&quot;lasso-回归建模&quot;&gt;Lasso 回归建模&lt;/h3&gt;

&lt;p&gt;加上类别、期数和页内次序的信息后，我们共有 1753 个观测和 3180 个自变量。由于变量数大于观测数，我们将采用 Lasso 来对自变量进行筛选。此外，因变量的分布严重右偏，所以我们预先对其进行对数变换后再进行建模。&lt;/p&gt;

&lt;p&gt;在 Lasso 回归中，我们选取罚参数使得有30个非零变量被选出，然后将它们按照系数大小做成下面这张图。注意到图中没有包括截距项，而且其中一个变量，文章所在的期数（IssueNO.），因为系数太大影响图片美观，所以对它的柱形进行了截断，其真实的数值标注在柱形上。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/YLZCWWU.png&quot; alt=&quot;结果展示&quot; /&gt;
&lt;/div&gt;

&lt;h3 id=&quot;结果分析&quot;&gt;结果分析&lt;/h3&gt;

&lt;p&gt;下面就这结果简单解释一下，大体上是我自己的理解，所以不一定靠谱，各位看官就当游戏好了。&lt;/p&gt;

&lt;p&gt;用 Lasso 筛选出的变量可以分成下面这几类：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;文章的期刊属性：IssueNO. 系数为正且非常大，说明读者更喜欢阅读最新的文章；PageOrder 系数为负，说明越靠后的文章，阅读量通常越小。（竞价排名现象有木有！）&lt;/li&gt;
  &lt;li&gt;文章所属的类别：正系数排第二的是文章类别 Theory and Methods。JASA 的文章主要有两个类别，应用类（Applications and Case Studies）和方法类（Theory and Methods），由此可以看出 JASA 的读者更关心统计模型和方法，这和 JASA 的风格是比较吻合的。但需要注意的是，这两个类别是2009年之后才有的，之前统一标记为”原创文章“，不加区分，所以这个结果会有一些误差存在。除此之外，评论类（通常是书籍评论）的文章具有负的系数，说明读者对于这一部分的文章通常不关心。&lt;/li&gt;
  &lt;li&gt;文章作者：结果中特别标注出了两位统计学家：Michael Jordan 和 Tilmann Gneiting。Jordan 是公认的统计机器学习大牛，Gneiting 是概率和预测理论的专家，这些说明了他们的领军效应。图中还有一项空白的作者，系数为负，这通常是评论类的文章，在网页上没有标明作者，所以其结果与上一点中的评论类是类似的。&lt;/li&gt;
  &lt;li&gt;文章标题：这算是本文的正题了。先来看看“黑名单”：阅读量较小的文章通常是讨论（discussion），修正（correction），评论（comment）和反驳（rejoinder），它们是其他文章的附属品，所以关注度自然也小。而“红名单”中的词项基本上反映了过去十年统计学研究的流行趋势，例如高维问题（high dimensional, principal components），变量选择（regression, selection, lasso, sparse, Bayesian variable selection），非独立型数据（correlation），Dirichlet 过程（Dirichlet processes）等。由于这些词语的曝光率很高，所以相应地，以这些词作为标题的文章也倾向于有更高的下载量。还有一个相对特殊的 racial 因为涉及敏感的种族问题，所以也比较吸引眼球。&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;最后的话&quot;&gt;最后的话&lt;/h3&gt;

&lt;p&gt;不论上述解释是否靠谱，通过本文的统计分析，我们还是发现了一些有趣的结果。当然，在有些方面本文做了不少简化，所以结果也还有提升的空间。下面我简单列举了一些，供感兴趣的读者进一步分析之用：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;构建词项字典的时候，没有考虑词性变化，比如单复数和时态等。在文本分析中，合并相同词根的过程通常叫 Stemming，在 R 中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SnowballC&lt;/code&gt; 等软件包提供了相应的函数，但并不十分完美。&lt;/li&gt;
  &lt;li&gt;网站的下载量统计是从2011年6月25日开始的，这意味着早期的文章有一些下载量没有统计到，所以会造成一些偏差。期数（IssueNO.）具有非常大的系数也与此有关。&lt;/li&gt;
  &lt;li&gt;从数据来源上说，还可以考虑加上文章的摘要，因为它提供了更多的文本信息。但另一方面，摘要的眼球效应就不如标题，所以用摘要中的词频来解释文章的阅读量，效果会打一些折扣。&lt;/li&gt;
  &lt;li&gt;本文的建模用的是线性模型（Lasso）。要衡量变量的重要性，还可以考虑使用一些非线性的模型，如随机森林中的变量重要性指标等。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;最后的最后，我们通过文章标题这个小窗口瞥见了统计学过去十年的一个缩影。下一个十年又会是怎样的光景呢？我们拭目以待。&lt;/p&gt;

&lt;p&gt;附：文中数据和代码都放在 &lt;a href=&quot;https://github.com/yixuan/COS-article/tree/master/statistics-on-article-titles/&quot;&gt;Github&lt;/a&gt; 上。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2016/06/statistics-on-article-titles/</link>
    <guid>https://yixuan.blog/cn/2016/06/statistics-on-article-titles</guid>
    <pubDate>2016-06-08T08:44:33+00:00</pubDate>
  </item>

  <item>
    <title>标准正态分布函数的快速计算方法</title>
    <description>&lt;p&gt;标准正态分布的分布函数 \(\Phi(x)\) 可以说是统计计算中非常重要的一个函数，基本上有正态分布的地方都或多或少会用上它。在一些特定的问题中，我们需要大量多次地计算这个函数的取值，比如我经常需要算正态分布与另一个随机变量之和的分布，这时候就需要用到数值积分，而被积函数就包含 \(\Phi(x)\)。如果 \(Z\sim N(0,1), X\sim f(x)\)，\(f\) 是 \(X\) 的密度函数，那么 \(Z+X\) 的分布函数就是&lt;/p&gt;

\[P(Z+X\le t)=\int_{-\infty}^{+\infty} \Phi(t-x)f(x)\mathrm{d}x\]

&lt;p&gt;我们知道，\(\Phi(x)\) 没有简单的显式表达式，所以它需要用一定的数值方法进行计算。在大部分的科学计算软件中，计算的精度往往是第一位的，因此其算法一般会比较复杂。当这个函数需要被计算成千上万次的时候，速度可能就成为了一个瓶颈。&lt;/p&gt;

&lt;p&gt;当然有问题就会有对策，一种常见的做法是略微放弃一些精度，以换取更简单的计算。在大部分实际应用中，一个合理的误差大小，例如 \(10^{-7}\)，一般就足够了。在这篇文章中，给大家介绍两种简单的方法，它们都比R中自带的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pnorm()&lt;/code&gt; 更快，且误差都控制在 \(10^{-7}\) 的级别。&lt;/p&gt;

&lt;p&gt;第一种办法来自于经典参考书 &lt;em&gt;Abramowitz and Stegun: Handbook of Mathematical Functions&lt;/em&gt;
的&lt;a href=&quot;http://people.math.sfu.ca/~cbm/aands/page_932.htm&quot;&gt;公式 26.2.17&lt;/a&gt;。其基本思想是把 \(\Phi(x)\) 表达成正态密度函数 \(\phi(x)\) 和一个有理函数的乘积。这种办法可以保证误差小于 \(7.5\times 10^{-8}\)，一段C++实现可以在&lt;a href=&quot;http://www.johndcook.com/blog/cpp_phi/&quot;&gt;这里&lt;/a&gt;找到。（代码中的常数与书中的略有区别，是因为代码是针对误差函数 \(\mathrm{erf}(x)\) 编写的，它与 \(\Phi(x)\) 相差一些常数）&lt;/p&gt;

&lt;p&gt;我们来对比一下这种方法与R中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pnorm()&lt;/code&gt; 的速度，并验证其精度。&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Rcpp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sourceCpp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;test_as26217.cpp&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system.time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## user  system elapsed&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## 1.049   0.000   1.048&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system.time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asy&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r_as26217ncdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## user  system elapsed&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## 0.293   0.019   0.311&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## [1] 6.968772e-08&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以看出，A&amp;amp;S 26.2.17 的速度大约是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pnorm()&lt;/code&gt; 的三倍，且误差也在预定的范围里，是对计算效率的一次巨大提升。&lt;/p&gt;

&lt;p&gt;那么还有没有可能更快呢？答案是肯定的，而且你其实已经多次使用过这种方法了。怎么，不相信？看看下面这张图，你就明白了。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/buX1QWo.png&quot; alt=&quot;正态分布概率表&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;没错，这种更快的方法其实就是两个字：查表。它的基本想法是，我们预先计算好一系列的函数取值 \((x_i,\Phi(x_i))\)，然后当我们需要计算某个点 \(x_0\) 时，就找到离它最近的两个点 \(x_k\) 和 \(x_{k+1}\)，再用线性插值的方法得到 \(\Phi(x_0)\) 的近似取值：&lt;/p&gt;

\[\Phi(x_0)\approx \frac{x_0-x_k}{x_{k+1}-x_k}\Phi(x_{i+1})+\frac{x_{k+1}-x_0}{x_{k+1}-x_k}\Phi(x_i)\]

&lt;p&gt;什么？觉得这个方法太简单了？先别急，这里面还有不少学问。之前我们说了，我们需要保证这种方法的误差不超过 \(\epsilon=10^{-7}\)，因此就需要合理地选择预先计算的点。由于 \(\Phi(-x)=1-\Phi(x)\)，我们暂且只需要考虑 \(x\) 为正的情况。如果让 \(x_i = ih,i=0,1,\ldots,N\)，那么对函数 \(f\) 进行线性插值的误差将不超过（&lt;a href=&quot;http://pages.cs.wisc.edu/~amos/412/lecture-notes/lecture09.pdf&quot;&gt;来源&lt;/a&gt;）&lt;/p&gt;

\[E(x)\le \frac{1}{8}\Vert f&apos;&apos;\Vert_{\infty}h^2\]

&lt;p&gt;其中 \(\Vert f’’ \Vert_{\infty}\) 是函数二阶导绝对值的最大值。对于正态分布函数来说，它等于 \(\phi(1)\approx 0.242\)。于是令 \(E(x)=10^{-7}\)，我们就可以解出 \(h\approx 0.001818\)。最后，只要 \(x_N&amp;gt;5.199\)，即 \(N\ge 2860\) 并另所有 \(x&amp;gt;x_N\) 的取值等于1，就可以保证整个实数域上 \(\Phi(x)\) 的近似误差都不超过 \(10^{-7}\)。&lt;/p&gt;

&lt;p&gt;这种简单方法的实现我放在了 &lt;a href=&quot;https://github.com/yixuan/fastncdf&quot;&gt;Github 上&lt;/a&gt;，源程序和测试代码也可以在文章最后找到。下面给出它的表现：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Rcpp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sourceCpp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;test_fastncdf.cpp&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;by&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system.time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fasty&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r_fastncdf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## user  system elapsed&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## 0.043   0.024   0.066&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fasty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;## [1] 9.99999e-08&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;与之前的结果相比，相当于速度是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pnorm()&lt;/code&gt; 的15倍！&lt;/p&gt;

&lt;p&gt;我们似乎一直以为，在计算机和统计软件普及以后，一些传统的做法就会慢慢被淘汰，例如现在除了考试，或许大部分的时间我们都是在用软件而不是正态概率表。从教学与实际应用的角度来看，这种做法是&lt;a href=&quot;http://yihui.name/cn/2009/04/how-and-what-to-teach-in-statistics/&quot;&gt;应该进行推广和普及的&lt;/a&gt;，但这也不妨碍我们从一些“旧知识”中汲取营养。关于这种大巧若拙的做法的故事还有很多，比如广为流传的&lt;a href=&quot;http://www.matrix67.com/blog/archives/362&quot;&gt;这一则&lt;/a&gt;。在计算资源匮乏的年代，科学家们想出了各种巧妙的办法来解决他们遇到的各种问题。现如今计算机的性能已经远不是当年可以媲迹，但前人的很多智慧却依然穿透了时间来为现在的我们提供帮助，不得不说这也是一种缘分吧。&lt;/p&gt;

&lt;p&gt;附：&lt;a href=&quot;http://yixuan.cos.name/cn/files/2016/01/normal_cdf.zip&quot;&gt;实现和测试源程序&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2016/01/fast-normal-cdf/</link>
    <guid>https://yixuan.blog/cn/2016/01/fast-normal-cdf</guid>
    <pubDate>2016-01-30T04:00:41+00:00</pubDate>
  </item>

  <item>
    <title>周记（四）</title>
    <description>&lt;p&gt;上周的周记缺了一次，原因是期末最后的这两周基本上是在各种死线中度过的，于是没有精力也没有心情去写博客了。&lt;/p&gt;

&lt;p&gt;其实上上周有好几条突如其来的消息。谢老大之前身体不适，前两周一直在医院检查，直到这周一做了一晚上手术，向师姐了解情况后，得知最后问题不是太大，应该能很快恢复。也是两周之前，瑶总滑雪摔伤，经过前前后后的诊断，需要一个来月的时间恢复静养，其中滋味必然不好受，希望伤痛能尽快退去，新年无忧无虞。还有一条挺沉重的消息，是两周前陈博士在微信群里告诉我们的。当年和我们一起入学的有位瑞典小哥，他在13年暑假的时候去南美旅游，结果被报失踪，再也没有回来。陈博士偶然在浏览相关报道的时候发现今年5月份有更新的消息，说是他的遗骸被找到了，与当地的黑帮有关。这种在电影里面出现的情节就发生在自己身边的人，一时还难以接受。&lt;/p&gt;

&lt;p&gt;负能量过后，生活依然还在继续，忙碌了两周，把剩下的期末报告都写完之后，今天就算是进入假期了。&lt;/p&gt;

&lt;p&gt;周二和导师见面，闲聊的时候不经意提到了主动性的问题，他说读书期间要学会主动一点，如果有机会就应该多把自己做过的东西展示出来，之前十月份的时候他让我参加泛华统计协会的Poster比赛就是这个原因。他说这种类似的比赛有很多，如果有一些成果都可以投出去试试，拿不拿奖是其次，关键是要学会怎么让别人更多地了解你。&lt;/p&gt;

&lt;p&gt;在这一点上，我确实得反思一下。很多时候我会觉得宣传自己是一件没有必要而且有些难以启齿的事，所以很少会主动跟别人介绍自己的能力或是成果，为此身边的朋友都或多或少觉得我过于低调和谦卑了。任何事情推到极致都是有问题的，关键是如何达到一个平衡，导师和朋友的劝诫我都记下了。&lt;/p&gt;

&lt;p&gt;说起这事是因为和另一个话题有关。之前谢老大跟我提起过好几回参加&lt;a href=&quot;http://stat-computing.org/awards/jmc/&quot;&gt;John Chambers奖比赛&lt;/a&gt;的事，而我一直觉得没有什么值得参赛的作品，所以并没有太上心。当天听了导师一席话后，觉得确实得有所行动，所以就决定拿暑假写的&lt;a href=&quot;https://github.com/yixuan/spectra&quot;&gt;一个软件包&lt;/a&gt;参加比赛。虽然我对结果并不抱什么希望，但我发现给自己人为地加一个截止日期，不管有意无意，都能极大提高工作的效率。事实上比赛当天就要截止，于是我一整晚都在写各种文档，完善一些细节，最终在截止前一个小时把申请投了出去。&lt;/p&gt;

&lt;p&gt;参加这个比赛给我带来的另一个收获是它促使我做了一些之前一直想做但就是没有动力去做的事，比如给写的这个软件做一个网站。因为申请材料的篇幅有限，所以很大程度上得依靠材料之外的媒介对参赛作品进行描述，而网站一般是首选。由于前一阵为了做&lt;a href=&quot;http://stat2016.china-r.org/&quot;&gt;R会议的网站&lt;/a&gt;搭过一个简单的框架，所以这次上手也很快，我对&lt;a href=&quot;http://yixuan.cos.name/spectra/&quot;&gt;做出来的效果&lt;/a&gt;也比较满意。让我有些吃惊的是，谢老大在&lt;a href=&quot;http://yixuan.cos.name/cn/2015/11/weekly-journal-2/&quot;&gt;第二篇周记&lt;/a&gt;里留过言，说“做一个好看的网站，看起来不务正业，却是有实在产出并对将来能产生影响的事情，所以是看起来没用但重要的事情”，不想这么快预言就成真了。&lt;/p&gt;

&lt;p&gt;忙完这许多事情，算是可以稍微休息一阵了。今年村里的冬天很反常，气象温和，阳光常在，下午坐在书桌前，发现窗台前的花草在卧室墙上投出了一道映像。素影参差，年光催度，希望亲朋好友都能在年底有个完满的收官，把不如意之事放在旧年，给马上到来的2016一个崭新的开端。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/jS9f785.jpg&quot; alt=&quot;影&quot; /&gt;
&lt;/div&gt;
</description>
    <link>https://yixuan.blog/cn/2015/12/weekly-journal-4/</link>
    <guid>https://yixuan.blog/cn/2015/12/weekly-journal-4</guid>
    <pubDate>2015-12-19T09:39:56+00:00</pubDate>
  </item>

  <item>
    <title>周记（三）</title>
    <description>&lt;p&gt;周日在朋友家吃火锅，其间Z同学喊了句“藕下了啊”，我便调侃了句“这话在好多年前的 BBS 上就是我要下线了的意思”，然后W同学评论说暴露年龄了，于是接下来又互相聊出了更多暴露年龄的典故，像是多图杀猫，九城社区，东北人都是活雷锋，小小，Showgood 之类。这么列举一下，发现2000年初的网络文化其中很重要的一支就是 Flash 动画，而这其中 Showgood 毫无疑问是当时中国 Flash 界的主力军之一。&lt;/p&gt;

&lt;p&gt;第一次听说 Showgood 我记得是初中的时候去P姑娘家玩，她推荐了大话三国系列中的某一部作品给我看，当时我就被那部动画的画风和搞笑台词惊艳到了，后来家里能拨号上网，就自己去 Showgood 的网站上看，现在依然还有印象的就是三集左右的《千里走单骑》，还有后来的《小兵的故事》。下午回到公寓后特意又搜了下，发现 Showgood 这个网站居然还在，只是已经成了一家做互联网儿童教育的创业公司。尽管已经改头换面，但进网站后迎面那句“这些年，你们还好吗？”还是让人唏嘘不已。&lt;/p&gt;

&lt;p&gt;但其实在我心目中真正的 Flash 神作还是小小系列，也就是当时风靡整个 Flash 界的火柴人动画。我至今还记得小小的域名是 xiaoxiaomovie.com，用 whois 查了下，并没有被注销掉，只是网站已经无法访问了。小小的影响力之大，我清楚地记得曾经玩过一个英文的 Flash 格斗游戏，里面可选的角色是各类美国的超级英雄和日本的动漫角色，但列表中赫然有着 Xiao Xiao 的名字。可惜后来再想找到这个游戏，就如同大海捞针了。少年时的记忆就像是擦肩而过的路人，偶尔留下一抹难忘的影子，又很快地融入到黑压压的人群中，再不见踪迹。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;object width=&quot;600&quot; height=&quot;338&quot; type=&quot;application/x-shockwave-flash&quot; data=&quot;http://flash.ngfiles.com/video_player/videoplayer.swf&quot; id=&quot;video_embed&quot; style=&quot;visibility: visible;&quot;&gt;
&lt;param name=&quot;wmode&quot; value=&quot;direct&quot; /&gt;
&lt;param name=&quot;allowscriptaccess&quot; value=&quot;never&quot; /&gt;
&lt;param name=&quot;allowfullscreeninteractive&quot; value=&quot;true&quot; /&gt;
&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;
&lt;param name=&quot;play&quot; value=&quot;false&quot; /&gt;
&lt;param name=&quot;flashvars&quot; value=&quot;NewgroundsAPI_PublisherID=1&amp;amp;NewgroundsAPI_SandboxID=5663073e76acc&amp;amp;NewgroundsAPI_SessionID=null&amp;amp;NewgroundsAPI_UserName=%26lt%3Bdeleted%26gt%3B&amp;amp;NewgroundsAPI_UserID=0&amp;amp;ng_username=%26lt%3Bdeleted%26gt%3B&amp;amp;url=http%3A//uploads.ungrounded.net/alternate/8000/8601_alternate_24378.mp4%3F-62169966000&amp;amp;title=null&amp;amp;max_resolution=1080&amp;amp;promo_url=http%3A//www.newgrounds.com/portal/exitroll/42307&quot; /&gt;
&lt;/object&gt;

&lt;p&gt;初中那会儿大概就是受众多 Flash 动画的影响，曾经痴迷过好一阵的 Flash 动画制作，那时每次去书店都会瞄一眼有没有关于 Flash 的新书，有合适的就买回去学习里面的例子。这种痴迷当然是在某个时候戛然而止，然而万万没有想到它对我的影响却几乎一直存在。大一下学期，计算机公共课上老师讲了几节课的扩展话题，其中之一就是 Flash，还留了作业，让大家自己做一个 Flash 动画，上传到作业系统中，而且互相可以投票，选出最受欢迎的十件作品。当时这个作业几乎重燃了我初中时学做 Flash 的所有热情，虽然技术一直很渣，但却乐此不疲，拿鼠标一点一点画背景画角色画时间轴，简直就像是从当年穿越过来一样。后来我提交的动画拿了个第二名，我确信它至今还保存在我某个移动硬盘中，只是一时半会儿也找不出来了。&lt;/p&gt;

&lt;p&gt;现在回想一下 &lt;a href=&quot;http://yixuan.cos.name/cn/2013/12/story-of-r2swf-2/&quot;&gt;R2SWF 诞生记&lt;/a&gt;，虽然当时 Flash 早就开始没落，快被 HTML5 取而代之，但依然花了这么多时间来维护这个包，是不是潜意识里也在向那个辉煌的 Flash 时代致敬呢？&lt;/p&gt;

&lt;p&gt;怀旧结束。这周干了一件正事，就是在传海的课上讲了一下 Spark 上 &lt;a href=&quot;http://joegaotao.github.io/cn/2014/02/admm/&quot;&gt;ADMM 算法&lt;/a&gt; 的&lt;a href=&quot;https://github.com/yixuan/ADMM-Spark&quot;&gt;实现&lt;/a&gt;，幻灯片放在了&lt;a href=&quot;http://statr.me/files/ADMM-Spark.html&quot;&gt;这里&lt;/a&gt;。周日是感恩节假期最后一天，下午收到邮件说让我们第二天讲，我和队友Z同学顿时就吓傻了，因为当时幻灯片还没做。幸好10月份的时候秉帅让我在南昌R会上做了一个关于 ADMM 的报告，大部分的片子都可以直接翻译一下拿来用，所以秉帅真是救了我一命呢。当然到了周一的时候，有另一组的三位大神救场，我们的报告被安排在了周三，讲出来效果还不错。&lt;/p&gt;

&lt;p&gt;这年头技术更新太快，但大部分都是知易行难。像是 Spark 和 Scala 这些，在各种报告中幻灯片中看到的是一回事，实际做起来就是另一回事。没有亲自尝试一遍，完全无法理解各种细节上的琐碎。现在来看 Scala/Spark 上许多重要的库还不是非常完善，但其实只要有需求，有活跃的开发者和社区，相信短板都会慢慢补上。如今的江湖风起云涌，各大门派雄据一方，且看未来将如何变幻。&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2015/12/weekly-journal-3/</link>
    <guid>https://yixuan.blog/cn/2015/12/weekly-journal-3</guid>
    <pubDate>2015-12-05T09:44:28+00:00</pubDate>
  </item>

  <item>
    <title>周记（二）</title>
    <description>&lt;p&gt;上周周六，入冬以来的第一场雪，气温骤降，让人猝不及防。突然从清爽中还带着一丝明媚的“深秋”时光变到压抑晦暗的冬天，一时还有些不习惯。&lt;/p&gt;

&lt;p&gt;不过冬天依然是个好季节，很多事情都是在冬天的时候想通的。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/k1UxgBI.jpg&quot; alt=&quot;初雪&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;周日做了一次大胆的尝试，从早上八点到晚上八点，把手机一直保持在关机状态，看能不能存活下来。后来事实证明不但成功地活了下来，还大大提高了做事的效率，先是把该干的活干完了，还抽空读掉了一本小说，就是上周因为写周记而没有读的那本，2014年诺奖得主帕特里克•莫迪亚诺的《暗店街》。&lt;/p&gt;

&lt;p&gt;书是室友的，当时看封面内页的介绍，有些侦探小说的感觉，于是好奇就借来看了。书很薄，不到200页，总的故事走向也很简单，就是在二战后的巴黎，主角因为遗失记忆而四处寻找线索，希望能揭开自己的身世之谜。然而在读完最后一章后发现根本就没有真相大白的一幕，直到最后，主角还在寻找、怀疑和挣扎，依靠一些微弱的线索去拼凑曾经的自己。从理性的角度讲，主角是可以彻底与过去割裂开的，然而寻找本源似乎是所有人与生俱来的一种天性，以至于作者借另一个角色之口说出了“在生活中重要的不是未来，而是过去”。我非常喜欢书里面的一个比喻，他说我们都是一种 &lt;strong&gt;海滩人&lt;/strong&gt;，我们无数次地出现在海滩度假照片中的一角或背景中，出现在欢快的人群中，但谁也叫不出我们的名字，说也说不清我们为何在那儿。“沙子只把我们的脚印保留几秒钟”，当我们离开沙滩以后，与这个世界的联系就仅剩许许多多互不相关的碎片而已。&lt;/p&gt;

&lt;p&gt;豆瓣上一条评论说这本书得在一个阳光明媚的午后拿来读，否则容易想多，我觉得是这么回事。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;这周是感恩节假期，从周三一直放到周日，但因为并没有出行的计划，而且期末作业还欠了一屁股债，所以直到今天都一直待在家里。本来理想的计划是用两天时间把作业做完，但因为拖延症作祟，今天才刚开始动手。说到拖延症，就不得不提一下下面这本书，《拖延症的艺术》。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/jgZs8Cn.jpg&quot; alt=&quot;拖延症的艺术&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;这本书是很早以前益辉推荐给我们看的，我是最近才买了一本。书很薄，用词也不晦涩，之后有时间我准备写个简单的书评放到博客上。书的核心思想就是，对于拖延症“患者”来说，他们对优先级排在第一位的任务往往具有很强的抵触心理，能拖则拖，但这并不意味着他们就是懒惰的人。相反，在对优先任务拖延的过程当中，他们经常能以很高的效率和热情去完成一些优先级并不高的事情，作者把这种行为称作“结构化的拖延”。这本书可以说解决了我多年以来的一个困惑，我一直觉得自己有些“不务正业”，因为我往往会对那些本应是一段时间里“最重要”的事动力不足，反而是一些当时看来无关紧要的杂事更吸引我的注意。比如以前临近期末的时候，明明还有几天就要考试，我却根本无心复习，反而是开始去读一些一直想找机会去读的闲书。又比如这几天明明得优先做完期末的作业，但放假前两天我却在研究怎么搭一个美观一些的网站，给明年的R会议作宣传用。这样的例子比比皆是，而书的作者就是想告诉我们，通过合理的安排，懒癌患者们可以通过“不干正事”来达到“完成许多其他事”的效果。&lt;/p&gt;

&lt;p&gt;书中给的许多建议都很有意思，但我感觉效果也是有好有坏。好处是能让自己减少很多负罪感，坏处是助长了自己不专注的毛病。我一直非常欣赏或者说仰慕的一种品质就是专注，更具体地说，就是能说服自己去做一些本身并不愿意去做的事。其实就这个话题我和许多人聊过，最后得到的结论都是知易行难，大概这就是能把人和人区分开来的一个准则吧。&lt;/p&gt;

&lt;p&gt;既然是感恩节，我想也不能免俗了。虽说这个节日有它自己的背景和渊源，但重温一下感恩之心，也未尝不是一件好事。感谢一直以来亲人和朋友的陪伴，让漫漫冬日中还有温热可以呼吸；感谢生活中经历的欢乐和苦痛，那是三千世界里留过的痕迹。&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2015/11/weekly-journal-2/</link>
    <guid>https://yixuan.blog/cn/2015/11/weekly-journal-2</guid>
    <pubDate>2015-11-27T13:52:51+00:00</pubDate>
  </item>

  <item>
    <title>周记</title>
    <description>&lt;p&gt;今天晚上突然有个疯狂的想法：想开始写周记。&lt;/p&gt;

&lt;p&gt;之所以我觉得疯狂，是因为别说周记了，月记都难得写一篇，上一篇博客更新还是半年多以前。但真的不知道是什么原因，让我突然想有规律地记录一些东西下来。&lt;/p&gt;

&lt;p&gt;回想了一下之前写的博客，基本上是按主题成文的，比如有了些新的想法，自然就会写篇博客出来。但当你接触的东西越来越多，新鲜的想法反而会越来越少，所以更新的进度也越来越慢。如果要求自己按一定的时间间隔写一些东西出来，或许还能把思路放开一些，少一些拘束。这大概是潜意识里准备写周记的一个原因。&lt;/p&gt;

&lt;p&gt;另一件事情我发现让我尤其苦恼，就是很多时候当我确实想写博客的时候，我会在脑子里快速地构思一遍要写的内容，但这个过程结束后，我也就失去了把想法写下来的动力。就在二十分钟前，我还准备用一晚上的时间读掉一本小说，但有了写周记的冲动后，我便立马开了机，生怕又像以往那样，计划构思完了就又把它们抛在脑后了。&lt;/p&gt;

&lt;p&gt;回到写周记这事，最早应该是在初中的时候语文老师让我们写的，目的大概是锻炼写作能力之类的，而且内容通常都是写一件小事，然后在文末点题，抒发一下人生感悟什么的。用的作业本是那种半张A4纸那么大，画了方格的，每周老师要收上去改，偶尔还会写些评语。现在想想，当真是幼稚又温馨。如果不出意外的话，家里某个角落里应该还能翻出以前用过的课本，写过的作业，考过的试卷一类，说不定还能找到些十几岁时的周记本。记得某次暑假回家，在杂物堆里赫然发现了&amp;lt;10岁时画过的画，当时激动得真的快要哭出来。当时的心情，是不是就是十几年后再看这篇博客时的心情呢？时间真是一件让人着迷的东西。&lt;/p&gt;

&lt;p&gt;这周的事情说多不多，说少不少，挑三件来写一写。&lt;/p&gt;

&lt;p&gt;周二的时候，系里举办感恩节聚餐（当然真正过节是下周），每个人带一道菜，拼在一起吃。我做了烤排骨，早上把菜送到教务秘书那儿就听课去了。等下课到就餐的地方，排骨已经没了一半，于是拖着餐盘小心夹走三块不声张，深藏功与名。我座位对面是我现在待的统计咨询中心的首席指导教授，寒暄了几句发现不知道聊啥，于是便默默吃饭（他边上还坐着系主任）。几分钟后边上的美国同学开始聊体育，各种橄榄球棒球，教授便滔滔不绝和他以及另外一个美国教授攀谈起来。我只好在心里默默摊一摊手，深感文化隔阂一直有，融也融不进去。&lt;/p&gt;

&lt;p&gt;周三听了威斯康星大学Ming Yuan教授的报告，并注册了和他吃午饭。他的名字可以经常在各大统计期刊的理论文章中见到，而我对纯理论的研究一向有些成见，认为他们不关注实际问题。但接触了袁教授后我的这种观点有了180度的转变，他的演讲在开头用了足足十多分钟的时间来介绍一个具体的问题，然后一步一步进行归纳总结，最后提炼抽象出核心的理论部分，招式精妙，内功深厚，俨然一派大师风范。由此觉得艰深的内容不一定就出不了精彩的呈现，关键是看演讲者本身的功力是否足够。&lt;/p&gt;

&lt;p&gt;今天上午上刘传海教授的大数据计算课，收获颇丰。跟刘教授有关的故事太多，一篇周记放不下，以后得慢慢写。总而言之一句话，他是我接触过的最具黑客精神的统计学教授。据他说他最近几个月几乎把 Email 给关了，他的一些同事朋友因为联系不上他还特意打电话询问情况，实际上他只在干一件事：写！程！序！要知道他可是正教授啊，还是统计学的正教授，就算是统计系的学生，据我所知也很少有这么投入的。那么他在写些什么呢？他在写R！的！源！代！码！几个月前他就提到他在研究R的核心，当时我以为他只是说笑，万万没想到居然是真的。那么他研究R的源代码是要干嘛呢？答案居然是要写一个多！线！程！的！R！平时大家最恨R的哪点？不就是它单线程吗？那么为什么没有人把R改成多线程的呢？那是因为这要触碰到R的源代码，而这些底层的东西过于艰深，很少有人有能力也有精力去做这方面的研究。目前来说，R里面实现并行的方式基本都是要通过创建新的 R &lt;strong&gt;进程&lt;/strong&gt; 来实现，而他今天给我们展示的结果，是让R可以通过直接创建 &lt;strong&gt;线程&lt;/strong&gt; 来并行地执行R代码。关键是，今天的演示还成！功！了！当这个功能稳定以后，R在并行计算中的可扩展性将大大提高。上完课以后，有种多年未能攻破的难题终被化解了一样，此事以后详谈。&lt;/p&gt;

&lt;p&gt;写到这儿发现已经两个小时过去了，小说是看不成了，以后也不知道能不能坚持写下去，先就此收尾吧。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2015/11/weekly-journal/</link>
    <guid>https://yixuan.blog/cn/2015/11/weekly-journal</guid>
    <pubDate>2015-11-20T21:18:04+00:00</pubDate>
  </item>

  <item>
    <title>showtext 新版本发布</title>
    <description>&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/7dmcchI.png&quot; alt=&quot;quick_example&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;前两天把 &lt;a href=&quot;http://cran.r-project.org/web/packages/showtext/index.html&quot;&gt;showtext&lt;/a&gt; 包更新到了 0.4-1 版本。因为这次更新的内容比较多，所以简单写篇博客介绍下。&lt;/p&gt;

&lt;p&gt;第一个比较重大的改变是现在可以在图形设备中自动使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt; 了，用法比以前更为简单。在这个版本之前，需要每次都打开图形设备，调用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.begin()&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.end()&lt;/code&gt;，然后关闭图形设备。这样反复调用函数显得非常麻烦。在新版本中，只要在最开始调用一下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.auto()&lt;/code&gt;，之后所有的图形设备就会自动使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt; 来进行渲染。这对窗口设备尤其重要，因为我们平常用 R 画图时，很少手动去调用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;windows()&lt;/code&gt; 或是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x11()&lt;/code&gt;，而是直接使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plot()&lt;/code&gt; 函数让 R 自动打开一个窗口。调用了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.auto()&lt;/code&gt; 之后，窗口设备也可以自动使用载入的字体了，这对于预览图形效果会很有帮助。&lt;/p&gt;

&lt;p&gt;自动调用的另一个好处是能在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggsave()&lt;/code&gt; 中使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt; 了。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggsave()&lt;/code&gt; 本身会自动打开和关闭图形设备，所以在以前的版本中我们无法在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggsave()&lt;/code&gt; 中插入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.begin()&lt;/code&gt;。现在自动化以后，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggsave()&lt;/code&gt; 也可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt; 的字体库了。&lt;/p&gt;

&lt;p&gt;要关闭自动调用功能，只需执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.auto(FALSE)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;第二个比较显著的改进是更好地支持了位图图形。在之前的版本中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt; 主要用于矢量图，比如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pdf()&lt;/code&gt; 或者 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;svg()&lt;/code&gt;。如果在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;png()&lt;/code&gt; 或是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jpeg()&lt;/code&gt; 中使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;，你会发现画出的字体非常难看。这是因为那些位图设备没有较好的抗锯齿支持。而现在，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt; 可以为那些设备绘制好平滑后的字形，所以图片质量也会得到提升。唯一需要注意的是要保持图形设备的 DPI 与 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt; 的一致，比如用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;png()&lt;/code&gt; 设备时，应该手动设置一下想要的分辨率（此处 DPI 为120）&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;showtext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;showtext.opts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dpi&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;png&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;支持位图图形也就意味着支持了窗口设备，比如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;windows()&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x11()&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;最后一个改进其实是之前 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sysfonts&lt;/code&gt; 包的更新内容，就是目前在国内无法直接连接 Google 的字体库，所以在使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;font.add.google()&lt;/code&gt; 时，可以使用 360 提供的&lt;a href=&quot;http://libs.useso.com/&quot;&gt;代理&lt;/a&gt;。使用方法是加一个参数，比如&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;showtext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font.add.google&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Gochi Hand&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;gochi&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;repo&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;useso&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其他的例子和说明都可以参见 Github 上的&lt;a href=&quot;https://github.com/yixuan/showtext/blob/master/README.md&quot;&gt;介绍&lt;/a&gt;。&lt;/p&gt;

&lt;!-- more --&gt;

</description>
    <link>https://yixuan.blog/cn/2015/04/showtext-new-version/</link>
    <guid>https://yixuan.blog/cn/2015/04/showtext-new-version</guid>
    <pubDate>2015-04-29T22:25:04+00:00</pubDate>
  </item>

  <item>
    <title>极简 Spark 入门笔记——安装和第一个回归程序</title>
    <description>&lt;p&gt;现在的各种数据处理技术更新换代太快，新的名词和工具层出不穷，像是 Hadoop 和 Spark 这些，最近几年着实火了一把，但自己一直没精力和时间去尝试和学习。特别是听说这些工具配置起来比较复杂，就更懒得去折腾。在这一点上，果然是不如从前了。&lt;/p&gt;

&lt;p&gt;然而绝知此事要躬行。即使将来不一定会花大功夫在这上面，但对它们有些基本的了解总是好的。听说 Spark 有一段时间了，但一直是只闻其名不见其实，今天就来简单记录一下初学 Spark 的若干点滴。&lt;/p&gt;

&lt;h3 id=&quot;spark-是什么&quot;&gt;Spark 是什么&lt;/h3&gt;

&lt;p&gt;按照 &lt;a href=&quot;http://spark.apache.org/&quot;&gt;Spark 官方的说法&lt;/a&gt;，Spark 是一个快速的集群运算平台，以及一系列处理大型数据集的工具包。用通俗的话说，Spark 与 R 一样是一套用于数据处理的软件和平台，但它最显著的特点就是处理大型数据（我就是不说大数据）的能力。&lt;/p&gt;

&lt;h3 id=&quot;极简安装&quot;&gt;极简安装&lt;/h3&gt;

&lt;p&gt;Spark 本身面向的是大规模的分布式计算，但对学习和测试来说，利用单机的多核 CPU 就已经足够了，所以作为入门，我并没有打算去涉及多台计算机相连的情形。在这个基础上，第一件出乎我意料的事情就是，Spark 的安装和配置其实可以是&lt;strong&gt;异常简单&lt;/strong&gt;的。&lt;/p&gt;

&lt;p&gt;在网上出现的各种资料中，Spark 经常与 Hadoop 和 Scala 这两个名词一起出现。前者也是一个大型分布式计算的框架，诞生得比 Spark 更早；后者是 Spark 主要使用的一种编程语言。这就给我造成了一种印象，好像要使用 Spark 的话就得先安装配置好 Hadoop 和 Scala，而要安装它们又得有更多的软件依赖。但实际上，要在单机上使用 Spark，真正需要的只有下面两样：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;一个 Linux 操作系统&lt;/li&gt;
  &lt;li&gt;Java 开发环境 JDK&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;这两样可以说是大部分计算环境的标配，如果系统还没有安装 JDK，那么一般都可以用系统的包管理工具，比如 Fedora 下是&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo yum install java-1.8.0-openjdk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Ubuntu 下是&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo apt-get install openjdk-7-jdk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;有了上面的开发环境，安装 Spark 就非常容易了，基本上只要下载预编译包，解压缩，然后添加系统路径即可。首先，到 &lt;a href=&quot;https://spark.apache.org/downloads.html&quot;&gt;https://spark.apache.org/downloads.html&lt;/a&gt; 选择最新的 Spark 版本和 Hadoop 版本（实际上我们暂时用不上 Hadoop，所以任何版本都行），然后下载压缩包。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/2IbWNhI.png&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;完毕后，将其中的文件夹解压到某个特定的位置，比如，我将解压出的文件夹命名为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spark&lt;/code&gt;，并放在我的主文件夹 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/home/qyx&lt;/code&gt; 里，这样我就可以执行&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/home/qyx/spark/bin/spark-shell
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;来运行 Spark 的终端了。为了避免每次打开 Spark 都要输入很长一串的路径，可以将 Spark 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bin&lt;/code&gt; 目录加入到系统路径中，例如我在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bashrc&lt;/code&gt; 文件中写入了&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export PATH=$PATH:/home/qyx/spark/bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;!-- more --&gt;

&lt;p&gt;于是安装过程就这么愉快地结束了。&lt;/p&gt;

&lt;h3 id=&quot;spark-终端&quot;&gt;Spark 终端&lt;/h3&gt;

&lt;p&gt;我觉得 Spark 非常亲民的一点是它提供了一个交互式的命令行终端，这样用户就可以快速地测试一些命令和语句，而无需每次都保存代码脚本然后调用执行，这对于 R 和 Python 用户来说是非常顺心的一件事。如果已经将 Spark 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bin&lt;/code&gt; 目录加入到了系统路径，那么在系统命令行里输入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spark-shell&lt;/code&gt; 就可以进入 Spark 的交互式终端了。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/WXuIyFf.png&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;如果出现了像图中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&amp;gt;&lt;/code&gt; 这样的提示符，就说明 Spark 安装成功。这里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala&lt;/code&gt; 指的是 Scala 编程语言。前面说了，Spark 主要使用 Scala 来进行开发，这意味着要最大程度地发挥 Spark 的性能，还需要再多学一门编程语言（Spark 还支持 Java 和 Python 的接口，但 Java 的语法没有 Scala 简洁，Python 的性能没有 Scala 的高）。虽然这需要花费一些额外的时间，但好在 Scala 的语法非常直观，基本上通过例子就可以模仿写出自己的程序来。&lt;/p&gt;

&lt;p&gt;如果对 Scala 语言感兴趣，可以参考&lt;a href=&quot;http://people.cis.ksu.edu/~schmidt/705a/Scala/scala_tutorial.pdf&quot;&gt;这份教程&lt;/a&gt;来了解其基本的语法。但在这里我们将直接进入正题，用 Spark 来跑一个回归的例子。&lt;/p&gt;

&lt;h3 id=&quot;spark-例子回归模型&quot;&gt;Spark 例子：回归模型&lt;/h3&gt;

&lt;p&gt;Spark 的数据分析功能包含在一个称为 MLlib 的组件当中，顾名思义，这是 Spark 的机器学习库，而回归是它支持的模型之一。为了演示例子，我们首先用 R 生成一组模拟的数据（是不是感觉怪怪的，主要是我还没用熟 Scala）：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;set.seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1e6&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%*%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data.frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write.table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;reg.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row.names&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col.names&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;我们将数据保存为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reg.txt&lt;/code&gt; 文件，它共有一百万行，每一行有6个数，用空格分隔，其中第一个数代表因变量，其余的为自变量。&lt;/p&gt;

&lt;p&gt;下面就是一段用 Scala 实现的 Spark 算回归的程序，其中包括了读取数据，拟合回归，计算回归系数，进行模型预测以及计算 $R^2$ 的过程。将这段程序复制到 Spark 的终端里，就可以迅速查看输出结果，体验 Spark 的基本功能了。&lt;/p&gt;

&lt;div class=&quot;language-scala highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.mllib.linalg.Vectors&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.mllib.regression.LabeledPoint&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.mllib.regression.LinearRegressionWithSGD&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.spark.mllib.stat.Statistics&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;raw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;sc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;textFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;reg.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;parsed&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;({&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;line&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;split&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;line&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;&apos; &apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 字符串按空格切分&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;toDouble&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// 第一个数转成Double型，是因变量&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;tail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;toDouble&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 其余的转成自变量向量&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;LabeledPoint&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Vectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;dense&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// 把因变量和自变量打包&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}).&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;cache&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;parsed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;LinearRegressionWithSGD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;setNumIterations&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;setIntercept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;tstart&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parsed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;tend&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;currentTimeMillis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Model training time: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tend&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tstart&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1000.0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot; secs&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;intercept&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;weights&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pred&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;parsed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;features&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Statistics&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;corr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;parsed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;label&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;R-square = &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;下面我们来解释一下程序中每一部分的含义。开头1到4行的是一系列的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import&lt;/code&gt; 语句，目的是使用一些已经封装好的类，与 R 中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library()&lt;/code&gt; 和 Python 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;import&lt;/code&gt; 语句类似。另外，相信不少读者立刻就能看出这是 Java 风格的导入语句。事实上，Scala 正是基于 Java 而开发的，因此其语法也大多脱胎于 Java。&lt;/p&gt;

&lt;p&gt;第6行是读取数据，并将结果赋值给一个变量 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;raw&lt;/code&gt;。特别需要指出的是，这条语句实际上并没有开始读取文件，而只是建立了数据与程序之间的一种连接。这一点是与 R 中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read.table()&lt;/code&gt; 最大的不同。&lt;/p&gt;

&lt;p&gt;第8到第13行其实包含了两条操作，第一是调用了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;raw&lt;/code&gt; 对象的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; 方法，之后连着的是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cache()&lt;/code&gt; 方法。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; 相当于 R 中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply()&lt;/code&gt;，意思是对读进来文件的每一行进行一次变换，然后将结果返回，组成一个新的向量。之所以需要这么做，是因为 Spark 读取文本文件时把每一行当作了一个字符串，因此我们需要从这个字符串中解析出我们需要的数据来。语句中被大括号包括的部分其实就是定义了这样一个变换函数，其输入是参数 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;line&lt;/code&gt;，代表传入函数的那个字符串，而输出是一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LabeledPoint&lt;/code&gt; 对象，它是 MLlib 中定义的一个数据结构，用来代表一个因变量-自变量的配对观测。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map()&lt;/code&gt; 方法返回的结果，就是一个长度为一百万，每个元素为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LabeledPoint&lt;/code&gt; 类型的向量。&lt;/p&gt;

&lt;p&gt;接下来的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cache()&lt;/code&gt; 方法是 Spark 非常独到的一个操作，它是为了告诉 Spark，只要内存足够（限额可以通过 Spark 中的配置文件设置），就将数据放到内存里，以加快后续程序的运算速度。如果内存放不下，就依然保存到硬盘中。这样的好处在于，一方面避免了 R 把所有对象都往内存放的操作，另一方面避免了 Hadoop 这种重度依赖硬盘，以至于效率低下的情形。&lt;/p&gt;

&lt;p&gt;而有趣的是，执行完这一句后，数据其实还没有进行真正的读取。这是因为 Spark 采用了一种“延迟运行”的机制，意思是数据只有在真正用到的地方才开始运算，其理念就是，“&lt;strong&gt;只要老师不检查作业，我就暂时不写&lt;/strong&gt;”。基于这个原因，数据只有到了下面 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parsed.count()&lt;/code&gt; 这句需要计算样本量时才真正开始进行读取和变换。&lt;/p&gt;

&lt;p&gt;接下来的第17到26行就是真正拟合回归模型的时候了。MLlib 里拟合回归采用的是随机梯度下降法（SGD），选用这种算法的原因，一是因为它是一种迭代算法，可以通过设置迭代次数来随时终止计算（当然同时会损失一部分精度），这对于大规模的数据是非常关键的，因为通常在这些场合下我们不要求结果非常精确，但却希望计算能在规定的时间里完成。而另一个原因就在于这种算法可以比较容易地进行并行，扩展性较好。&lt;/p&gt;

&lt;p&gt;第17行中，我们先建立模型对象，然后在19、20行设置最大迭代次数以及告诉模型应该包括截距项。22和24行插入了两句获取时间的函数，是为了评估模型训练（23行）花费的时间。&lt;/p&gt;

&lt;p&gt;在第31行中，我们用拟合出的模型对训练集本身进行了预测。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parsed.map(_.features)&lt;/code&gt; 的目的是取出训练集中的自变量部分，而 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;predict()&lt;/code&gt; 方法返回的结果就是因变量的预测值向量。&lt;/p&gt;

&lt;p&gt;最后的第33行，我们利用 MLlib 为我们封装好的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;corr()&lt;/code&gt; 函数计算了预测值与真实值之间的相关系数（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parsed.map(_.label)&lt;/code&gt; 与 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parsed.map(_.features)&lt;/code&gt; 相对，是取出训练集中的因变量），将它平方一下，就是模型的 $R^2$ 值了。&lt;/p&gt;

&lt;h3 id=&quot;整体印象&quot;&gt;整体印象&lt;/h3&gt;

&lt;p&gt;总体感觉，Spark 还是挺好上手的，唯一麻烦的地方大概就是要对 Scala 语言有所了解。不过好在 Spark 也提供了 Python 和 Java 的接口，所以如果只是想尝尝鲜，也完全可以用其他的语言来加以操作。另外一个好消息是，从2015年4月起，Spark 官方已经开始提供 R 语言的接口，大约在2015年夏季发布 Spark 1.4 版本时，R 用户就可以使用原生的 Spark 接口了。&lt;/p&gt;

&lt;h3 id=&quot;学习资源&quot;&gt;学习资源&lt;/h3&gt;

&lt;p&gt;下面几个链接我觉得对于 Spark 入门是非常有帮助的：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;http://ampcamp.berkeley.edu/big-data-mini-course/index.html&quot;&gt;伯克利的 Spark 迷你课程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://spark.apache.org/docs/latest/quick-start.html&quot;&gt;Spark 官方快速入门教程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://spark.apache.org/docs/latest/mllib-guide.html&quot;&gt;MLlib 官方文档&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
</description>
    <link>https://yixuan.blog/cn/2015/04/spark-beginner-1/</link>
    <guid>https://yixuan.blog/cn/2015/04/spark-beginner-1</guid>
    <pubDate>2015-04-19T14:42:50+00:00</pubDate>
  </item>

  <item>
    <title>风乍起</title>
    <description>&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/AqLRhpp.jpg&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;村里开始回暖了。今天基本上是整个冬天以来第一次把阳台上的门长时间开着透气，外面四处堆的雪堆在路上融成了小河。趁着五点钟依然明媚的太阳出去跑了一圈，终于不是在闭塞的健身房和僵硬的跑步机上了。公寓边上的小湖依然冻着，但草地里面已经全是雪水糊起的泥巴，走在上面鞋底都能感觉到是湿的。小风微冷，草木尚芜，电线杆上的鸟儿却已经开始咿咿呀呀地叫个不停。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;昨天晚上去超市补货，车才开出去几十米就发现不对。白天连着下了几阵小雨，傍晚时分就开始起雾了。车灯所及之处，全是白茫茫一片，能见度不及五米。头一次遇到这种雾天，硬着头皮保持低速慢慢地开着，神奇的是到路口拐弯的时候顿时雾气全无，空气透明得像是刚换了一副眼镜。然而回程的时候，同一个路口，却是完全相反的景象，红绿灯都变得模糊不清了。这一明一朦之间，倒是觉得靖节先生的世外桃源有几分确信了呢。&lt;/p&gt;

&lt;p&gt;其实天气一转暖，身体会很敏感地捕捉到环境中细微的变化，进而影响心情心绪。前人有才，取了个好听的名字，叫做春愁。愁则必有诗，诗必有曲。说起来昨晚睡觉的时候手机里的应用推荐了一个歌单，叫“当诗遇上歌”。点开一看，大多牵强，唯独发现羽泉有首老歌叫《辛弃疾》，顿时觉得有趣。歌词化用稼轩平生词作，名句在耳，不失豪迈之感。而世间之巧，更是让人捉摸不透。今天在电脑上随机放歌，发现曲目中有一首汪苏泷的《李清照》，同样是用生平词写生平事，相照之下，一刚一柔，一唱一叹，甚是有趣。要知道，从诗词改编的现代歌曲并不少见，但这种以词集句，且歌名就是词人本身的我倒是真不知道还有其他的。不知道我们的&lt;a href=&quot;http://www.tudou.com/home/_88324353/&quot;&gt;音乐家&lt;/a&gt;是否有兴趣作上一两首，让我们开开眼界呢？&lt;/p&gt;

&lt;p&gt;上周室友在看新版的神雕侠侣，我跟着一起看了几集。室友说是冲着郭襄登场才看的，我不知道这句话的意思是这一版的小龙女太惊艳，只好看郭襄，还是说以前看神雕只注意了侠侣，而这一回有了新的感受。神雕的小说我之前还没看过，直到上一次回家时在飞机上看了小一半，落地后又放下了。现在又捡起来，倒是真想找个时间把余下的都给读完了。今天这部剧大结局，大家对编剧的评价估计会是出奇的一致，唯独我觉得最后几集里面对金轮和郭襄“师徒情分”的演绎是编剧花了心思的。下面这一幕，会不会是编剧故意安排的呢？&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/ik9YKoc.png&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;金老先生给了神雕最好的结局，却把执念留在了经年以后。倚天屠龙记里面写道，峨嵋派的第二任掌门法号曰风陵师太，只因“风陵渡口初相遇，一见杨过误终身”。后来还看到网上一个帖子，说笑傲江湖里华山上的思过崖也是郭襄所名，那是她最后一次见到杨过的地方。这说法自然牵强，但也算是一种品读了吧。网上有一首流传甚广的小诗，每每读之，总有怅惘之感。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;我走过山时，山不说话&lt;/p&gt;

  &lt;p&gt;我路过海时，海不说话&lt;/p&gt;

  &lt;p&gt;小毛驴滴滴答答，倚天剑伴我走天涯&lt;/p&gt;

  &lt;p&gt;大家都说我因为爱着杨过大侠&lt;/p&gt;

  &lt;p&gt;才在峨嵋山上出了家&lt;/p&gt;

  &lt;p&gt;其实我只是爱上了峨嵋山上的云和霞&lt;/p&gt;

  &lt;p&gt;像极了十六岁那年的烟花&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;其时明月在天，清风吹叶，树巅乌鸦啊啊而鸣。相思相见知何日，此时此夜难为情。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2015/03/breezing/</link>
    <guid>https://yixuan.blog/cn/2015/03/breezing</guid>
    <pubDate>2015-03-12T00:14:15+00:00</pubDate>
  </item>

  <item>
    <title>寂难永劫</title>
    <description>&lt;p&gt;这个旮旯已经很久没有来过了，回头一看发现今年居然还写了两篇博客，已然很震惊。在这个微博和微信都已经开始衰落的年代，博客感觉已经成了一件老古董，用来记录互联网的变迁史。&lt;/p&gt;

&lt;p&gt;今天跑到这儿来，实在是因为书看不进代码写不进游戏不想玩电影不想看，索性放点音乐，随便写点什么，总好过在网上漫无目的浪费一整晚。&lt;/p&gt;

&lt;p&gt;上个月家里买了电视，可以连上电脑看电影。因为地域的原因，能看的中文片更新都很滞后，到现在只看了《催眠大师》《后会无期》和《绣春刀》三部。一般来说写影评好像挺累的，而且也不清楚贵圈的规则，所以只是瞎扯扯罢了。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;催眠大师很明显是在向盗梦空间致敬。说到这个题材，就想起以前在群众大学的时候某姓名都是领导人姓氏的老师吐槽，说你们怎么老是把催眠和放倒挂钩呢？当然为了剧情需要，放倒还是必须的，好在剧情整体都很严密，接受了这个设定后故事进展也是跌宕起伏，合情合理。电影最后两分钟主角们在清醒状态下的对话很有意思，表达的观点在其他的一些小说或是影视作品中也出现过，但放在这个故事里更加契合。你是无法做到一件事呢，还是不愿做到这件事？你是不能得到解脱呢，还是根本就不想被宽恕？&lt;/p&gt;

&lt;p&gt;后会无期，忘了最开始是怎么知道这部电影的，但我估计是在微博上。某一次跟家里打电话的时候老爹说他们也看了这电影，直呼是史上最烂的片，我说你这么想太正常啦，你回忆一下，电影院里面观众都是多大年纪的？我没有告诉他的是，这戏内的电影，远没有戏外国民岳父发的那些微博（及其评论）有意思啊。电影本身的笑点、亮点和槽点都十分明显，网上各种解读也是足够多了，我也无需多言。朴树的《平凡之路》显然是这部片子的一个大杀器，当时曲子公布出来的时候是我第一次目睹微博和微信上连续几十条消息转发同一内容的壮观景象。借用网上一句对这首歌的评论，十年前你让我生如夏花，十年后你说平凡才是唯一的答案，这十年都发生了什么？&lt;/p&gt;

&lt;p&gt;最后看的绣春刀，网上一边倒地说是一部被票房低估（不是票房被低估）了的武侠片。我对明朝的历史不熟，所以对故事背景没有什么代入感，但从武侠的方面来看，至少觉得很畅快。我现在脑子里还有画面感的武侠场面，就只剩下张艺谋的《英雄》和动画的《秦时明月》了，可惜《英雄》已经成了古董，《秦时明月》改走玄幻，那叫一个痛心疾首。片中难得的一个笑点“要加钱”，立马想到了后会无期里的苏米，风格统一得那叫一个诡异。最后电影放片尾曲的时候，我十分确定我之前听过，但就是想不起来在哪儿听过，为此还纠结了很久。&lt;/p&gt;

&lt;p&gt;最近一两周更是连电影都懒得看了，开始进入一个死循环。写文章毫无进展，也找不到能提起兴趣的事来做，最后的结果就是正事没做好，连闲事也荒废了。这是我最担心的生活。发现自己的弱点不可怕，感觉到疼了说明还好好活着。可怕的是发现自己的弱点还心安理得地视而不见，这不是麻木是什么？只能先洗洗睡了，看明天是不是会更疼一些。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/IxuaD62.jpg&quot; /&gt;
&lt;/div&gt;

</description>
    <link>https://yixuan.blog/cn/2014/09/kalpa/</link>
    <guid>https://yixuan.blog/cn/2014/09/kalpa</guid>
    <pubDate>2014-09-25T22:24:18+00:00</pubDate>
  </item>

  <item>
    <title>RcppParallel 和 R6</title>
    <description>&lt;p&gt;最近 RStudio 的几位大佬频发大招，JJ 写了一个 &lt;a href=&quot;https://github.com/RcppCore/RcppParallel&quot;&gt;RcppParallel&lt;/a&gt;，用来在 Rcpp 中支持并行计算，而 Winston 发布了 &lt;a href=&quot;http://cran.r-project.org/web/packages/R6/index.html&quot;&gt;R6&lt;/a&gt;，意在紧随“R5”的步伐，将 R 的面向对象编程（OOP）向前再推一把。&lt;/p&gt;

&lt;p&gt;RcppParallel 是 Rcpp 核心组件的新成员，目前还没有发布到 CRAN，但可以从 github 安装。我感觉它最大的亮点是给类 UNIX 系统和 Windows 系统提供了一个统一的接口，因为 R 中大部分支持并行的包都没法在 Windows 上工作。&lt;/p&gt;

&lt;p&gt;当然了，像其他 Rcpp 组件一样，RcppParallel 主要是用来写 C++ 代码的，因此对大部分的 R 用户来说其作用依然有限。RcppParallel 中最重要的一个函数是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parallelFor&lt;/code&gt;，下面给一个简单的例子，来说明其用法。假设我们想并行计算 R 中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qf&lt;/code&gt; 函数（计算非中心化 F 分布的分位数），我们可以写下面的 R 代码（C++ 代码被放到了字符串中，供 R 调用）：&lt;/p&gt;

&lt;!-- more --&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;includes&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;
using namespace Rcpp;
using namespace RcppParallel;

struct Qf : public Worker
{
   const double *x;
   double df1;
   double df2;
   double ncp;
   double *result;
   
   Qf(const NumericVector &amp;amp;x_, double df1_, double df2_, double ncp_,
      NumericVector &amp;amp;result_) :
       x(x_.begin()), df1(df1_), df2(df2_), ncp(ncp_),
       result(result_.begin()) {}
   
   void operator()(std::size_t begin, std::size_t end) {
       for(std::size_t i = begin; i &amp;lt; end; i++)
           result[i] = R::qnf(x[i], df1, df2, ncp, 1, 0);
   }
};
&apos;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;
NumericVector parqf(NumericVector x, double df1, double df2, double ncp)
{
    NumericVector result(x.length());
    Qf fun(x, df1, df2, ncp, result);
    parallelFor(0, x.length(), fun);
    
    return result;
}
&apos;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cppFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;depends&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;RcppParallel&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;includes&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;includes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中有几点需要解释一下。我们先看主程序 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parqf&lt;/code&gt;，它调用了之前提到的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parallelFor&lt;/code&gt; 函数。顾名思义，这一句指的是一个并行的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; 循环。那么循环体在哪里呢？这就要看里面的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fun&lt;/code&gt; 参数。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fun&lt;/code&gt; 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Qf&lt;/code&gt; 的一个对象，而 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Qf&lt;/code&gt; 又继承自 RcppParallel 中定义好的一个类 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Worker&lt;/code&gt;。这个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Worker&lt;/code&gt; 类描述了并行计算中每个线程需要干的活，而其核心部分在于那个运算符重载 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator()&lt;/code&gt;。这又是什么意思？&lt;/p&gt;

&lt;p&gt;我们先假设，我们要算一个循环，循环变量从 0 一直到 99，那么要并行计算时，我们将 0 到 49 的部分给第一个线程，而 50 到 99 给第二个线程。也就是说，对于第一个线程，它接收到了两个参数，0 和 49，这就对应了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator()&lt;/code&gt; 中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;begin&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end&lt;/code&gt; （事实上 49 对应的是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end - 1&lt;/code&gt;）。有了这个信息，线程 1 就可以开始干活了，在这里就是计算 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;result[begin]&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;result[begin + 1]&lt;/code&gt;，一直到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;result[end - 1]&lt;/code&gt; 的取值。对于线程 2，它接收到了另一组 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;begin&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end&lt;/code&gt; 的取值，但干的活是差不多的，唯一的区别是下标不同。所以说，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;operator()&lt;/code&gt; 就代表了每个线程需要完成的那部分循环任务。&lt;/p&gt;

&lt;p&gt;说到这里，可能已经把一些看官说晕了。但没办法，C++ 的语法实在是比 R 复杂太多，这也是为什么 R 的简洁性很难被取代。要获取效率，就只能在语法的简洁性上进行牺牲了。&lt;/p&gt;

&lt;p&gt;还是直接来看效率的对比，我们分别计算 R 中串行的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qf&lt;/code&gt; 还有我们实现的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parqf&lt;/code&gt;，结果如下：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set.seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;123&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runif&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system.time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;qf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3.4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
   &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;elapsed&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;8.010&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;8.013&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system.time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parqf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3.4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
   &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;elapsed&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
 &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;12.010&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.001&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;   &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3.017&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;identical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;我的机器开了 4 个线程，运行时间从 8 秒减少到了 3 秒。&lt;/p&gt;

&lt;p&gt;总的来说，效果还是很显著的，但为了实现这一并行的功能，我们多写了几十行 C++ 的代码，而且为了写这几十行 C++ 代码，我们可能还得去读几百页的 C++ 的书。至于值不值得，就是见仁见智了。&lt;/p&gt;

&lt;p&gt;另一个要提一下的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R6&lt;/code&gt; 则要轻松得多了，完全是 R 本身的东西。作者在介绍文档中特意解释了这个名字的来历：R 中早期的 OOP 系统包括 S3 和 S4，几年前做了一个 Reference Class 出来，大家戏称 R5，但官方一直没承认这个名字。这下好了，R6 的名字就这样被占了，R core 们就是想用回 R5 这个名字，估计也会在心里暗叫不爽吧——总感觉包作者是有意想调戏一下诸位老顽固们。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R6&lt;/code&gt; 说白了，就是想模仿 C++ 那样来在 R 中定义类，它相对于 Reference Class 的改进在于实现了公有成员和私有成员的区分。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R6&lt;/code&gt; 的&lt;a href=&quot;http://cran.r-project.org/web/packages/R6/vignettes/Introduction.html&quot;&gt;说明文档&lt;/a&gt;很清晰，在这里就不多说了。&lt;/p&gt;

&lt;p&gt;最后要说，这两个包都给人眼前一亮的感觉，但相比之下，作为它们基础部分的 R，其本身的功能已经停滞不前很久了。益辉之前分享了一篇文章，&lt;a href=&quot;http://outspeaking.com/words-of-technology/why-perl-didnt-win.html&quot;&gt;为什么 Perl 输了&lt;/a&gt;，说的就是当创新不再，就离游戏结束不远了。这话还适用于学问，事业，人生，等等。&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2014/07/rcppparallel-and-r6/</link>
    <guid>https://yixuan.blog/cn/2014/07/rcppparallel-and-r6</guid>
    <pubDate>2014-07-18T18:20:31+00:00</pubDate>
  </item>

  <item>
    <title>R绘图字体解决方案</title>
    <description>&lt;p&gt;&lt;a href=&quot;http://cos.name/cn/topic/147359&quot;&gt;COS论坛&lt;/a&gt;里面经常会遇到的一个问题就是绘图时中文字体怎么解决。最初，一个流行的方法是使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;family = &quot;GB1&quot;&lt;/code&gt;，但一般这样做出来的图比较难看，而且并没有完全解决问题。后来发现了&lt;a href=&quot;http://cran.r-project.org/web/packages/Cairo/index.html&quot;&gt;Cairo&lt;/a&gt;包，&lt;a href=&quot;http://cos.name/cn/topic/101521&quot;&gt;喜大普奔&lt;/a&gt;，但后来又发现它选字体的时候不太明确，也就是说我不知道怎样才能选中我想要的那个字体。再然后，&lt;a href=&quot;https://github.com/wch&quot;&gt;Winston Chang&lt;/a&gt;写了&lt;a href=&quot;https://github.com/wch/extrafont&quot;&gt;extrafont&lt;/a&gt;包，功能很完善，但需要系统里面安装Ghostscript。我是懒人，不想花时间在配置Ghostscript上，于是写了一个叫做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;的包。（好像哪里不对）&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
  &lt;img src=&quot;https://i.imgur.com/IdFtOLP.png&quot; /&gt;
&lt;/div&gt;

&lt;!-- more --&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;详细的说明写在我的&lt;a href=&quot;http://statr.me/2014/01/using-system-fonts-in-r-graphs/&quot;&gt;英文博客里面&lt;/a&gt;，四级的词汇量，很好懂的。这里就简单说明一下用法，因为用法挺简单的。&lt;/p&gt;

&lt;p&gt;上面那幅图所有的代码如下，当然要事先将其中用到的四个&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/showtext-example-fonts.zip&quot;&gt;字体文件&lt;/a&gt;放到工作目录下。&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;showtext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cairo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font.add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;newrocker&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;newrocker.ttf&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font.add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;aerofoil&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;aerofoil.ttf&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font.add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;xkcd&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;xkcd.ttf&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font.add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;nextdoor&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;thegirlnextdoor.woff&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CairoPNG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;showtext.png&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;640&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;400&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;showtext.begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;family&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;xkcd&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cex.axis&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cex.lab&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;showtext 0.2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cex&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;family&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;nextdoor&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;#F8766D&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;has been released!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cex&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;family&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;newrocker&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;col&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;#00BFC4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Free to use fonts&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;family&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;aerofoil&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cex.main&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;showtext.end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dev.off&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其实也就两步：先用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;font.add()&lt;/code&gt;加载字体，然后用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.begin()&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.end()&lt;/code&gt;把绘图语句包含进来。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;font.add()&lt;/code&gt;的第一个参数是绘图时字体的系列名（family），也就是告诉R如何在绘图时引用这个字体。第二个参数是字体的路径，如果字体在工作目录下就直接打字体名。当然，操作系统一般都会有一些默认的字体安装位置，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;（实际上是其依赖的&lt;a href=&quot;https://github.com/yixuan/sysfonts/&quot;&gt;sysfonts&lt;/a&gt;包）会尝试搜索这些路径。在Windows下，直接输入&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;font.add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;songti&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;simsun.ttc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;就可以加载宋体。&lt;/p&gt;

&lt;p&gt;加载完了字体，R本身还不知道如何使用它们，所以，绘图的语句需要包含在一对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.begin()&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.end()&lt;/code&gt;中间。这时候只需要指定合适的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;family&lt;/code&gt;参数就可以使用这些字体了。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;自带了&lt;a href=&quot;http://wenq.org/wqy2/index.cgi?MicroHei&quot;&gt;文泉驿微米黑&lt;/a&gt;字体，如果只是想在图中使用中文，只需要加载&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;，然后使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.begin()&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext.end()&lt;/code&gt;即可。&lt;/p&gt;

&lt;p&gt;注意&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;只对当前打开的图形设备有效，如果当前没有图形设备，或是设备已关闭，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;都是不起作用的。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;showtext&lt;/code&gt;的开发页面在&lt;a href=&quot;https://github.com/yixuan/showtext&quot;&gt;https://github.com/yixuan/showtext&lt;/a&gt;，欢迎到那里拍砖捉虫。在此留言亦可，重谢。&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2014/01/fonts-in-r-graphs/</link>
    <guid>https://yixuan.blog/cn/2014/01/fonts-in-r-graphs</guid>
    <pubDate>2014-01-01T17:01:57+00:00</pubDate>
  </item>

  <item>
    <title>我的2013，和我</title>
    <description>&lt;p&gt;2013最后一天，回头翻了翻一年以来的微博，发现挺多都是配了图的。于是索性把这些瞬间串了起来，权当作年终总结了。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;div class=&quot;deco-line&quot;&gt;&lt;/div&gt;

&lt;p&gt;2012年12月21日，失约的世界末日，旧金山街头。流浪歌手带着他的音乐和朋友，静静地看着这个重生的世界。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/PUNjt3t.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年1月4日，西拉法叶McCormick Place公寓。早上七点半仍是一片黛蓝，新的一年以早八点的考试作为序幕。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/MDZyNeP.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年1月25日，普渡大学HAAS楼。哈佛大学孟晓犁教授来访，送上统计之都印制的主题扑克。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/Ap3M00X.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年2月9日，除夕，McCormick Place公寓。小伙伴们的新年聚餐。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/3gMy19A.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年2月24日，元宵，McCormick Place公寓。聊寄乡思。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/H89NcIM.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年3月12日，华盛顿DC。如城堡一般的Smithsonian学会，仿佛回到了几个世纪以前。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/A7pL136.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年3月14日，纽约。帝国顶端的遥望。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/sfnBjDv.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年4月8日，McCormick公寓。慵懒的傍晚一朵慵懒的云。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/Xy2C3m7.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年4月12日，普渡大学Loeb剧场。一场亲临现场的TED演讲。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/t4kDI6u.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年5月4日，北京，小石灶餐厅。再相聚。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/PtE8HOq.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年5月15日，南昌，家。归故园。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/FPKtDR5.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年5月18日，北京，人民大学明德楼第六届R语言会议。逢盛友。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/FWjXCuQ.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年5月26日，西安，古城墙。梦长安。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/eHRmu3T.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年6月5日，芝加哥大学，Cathey Learning Center。再一次远离故乡，踏上求学的旅途。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/1Lc74lE.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年7月9日，西拉法叶。一次黄昏的漫步。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/wNQHD5I.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年8月1日，Santa Monica海滩。与此相连，便是茫茫太平洋。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/xJ3m72r.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年9月19日，中秋，西拉法叶。明月如霜，夜凉如洗。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/fi3tICJ.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年10月12日，芝加哥。有朋自远方来。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/yQCob6P.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年11月28日，芝加哥。人大统计聚会。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/R7DFwab.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;2013年12月22日，冬至，西拉法叶。为什么四季要歌唱？&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/9Y0IXAc.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;deco-line&quot;&gt;&lt;/div&gt;

&lt;p&gt;2014年，又会有怎样的期待，失望，快乐和苦痛？我一齐等待着。&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2013/12/my-2013/</link>
    <guid>https://yixuan.blog/cn/2013/12/my-2013</guid>
    <pubDate>2013-12-31T01:03:48+00:00</pubDate>
  </item>

  <item>
    <title>R2SWF诞生记（二）</title>
    <description>&lt;p&gt;哦，你问我后来怎么样了？其实就是下面这个计划：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/MTV2Kbf.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/MTV2Kbf.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;换句话说，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R2SWF&lt;/code&gt; 包就在这个时候诞生了。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;后面的过程并非一帆风顺。当时发现了另外一个生成 SWF 的程序 &lt;a href=&quot;http://swfmill.org/&quot;&gt;swfmill&lt;/a&gt;，觉得它的用法更简单，而且支持 SVG 图形，于是曾试图把 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R2SWF&lt;/code&gt; 构建在它上面。但后来磕磕绊绊，带来的麻烦远大于带来的好处，只好再老老实实切换回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;libming&lt;/code&gt;，直到现在没再改过。&lt;/p&gt;

&lt;p&gt;而这之后的故事，最惊心动魄的莫过于与 CRAN 之间沟通，摩擦，妥协，最后欣然接受的过程了。&lt;/p&gt;

&lt;p&gt;事情的起因是提交0.5版的时候，我像以前那样给 CRAN 发邮件，结果被告知不合要求，比如邮件的标题要有特定的格式，邮件里面不能有 HTML ，R包的检查更加严格了等等。这时候距我上次提交R包已经有一年多，完全没有意识到现在的标准已经严格了太多。来来回回几次修改和邮件沟通后，终于把包放到 CRAN 上了，结果第二天，就收到了来自 CRAN 的通知，作者是R包开发者都必定刻骨铭心的那位大人。&lt;/p&gt;

&lt;p&gt;邮件的大意是这个包被 CRAN 撤了，原因有好几个，首先是因为安全原因不能包含 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zlib&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;libpng&lt;/code&gt; 的源代码，其次是我被怀疑造假（意思是我包含了其他软件的源代码但没有署名，尽管那些代码都是开源和 GPL 的），再次是发现以前的版本也有这个问题，所以不能留你了。总之言辞比较激烈，让你根本不想读第二遍。&lt;/p&gt;

&lt;p&gt;当时我心里面那个郁闷啊，而且特别不舒服的是 CRAN 上还留有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R2SWF&lt;/code&gt; 的页面，只不过上面写的是该包违背 CRAN 政策被撤了，原因是“不诚实的署名”。那感觉就像是被钉在了耻辱柱上，怎么看怎么觉得不爽。不过既然有问题，那就改吧。本来我在软件包里面加入所有源代码就是为了让 Windows 和其他系统一样可以方便地编译，现在不让这么做，只好又走回很久以前的老路，对 Windows “特别关照”一下。作者署名和版权信息，往R包里加就是，列了一长串的名字和一长串的 Copyright(C) xxxx-xxxx。之后来来回回又是几通邮件，一会儿说是我把版权和许可证的概念弄混了，一会儿说是包含的许可证类型不兼容，总之为了达到 CRAN 的标准，我把那些版权信息和开源许可证研究了个遍，最后花在文字上的功夫比花在代码上的时间多多了。&lt;/p&gt;

&lt;p&gt;折腾了这许久吧，也不是没有收获。首先还是很高兴新版的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R2SWF&lt;/code&gt; 上线了，当前 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R2SWF&lt;/code&gt; 包最大的一个更新就是加入了原生的 SWF 设备支持。之前用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R2SWF&lt;/code&gt; 生成动画，本质上是将其他类型的图片一幅一幅接起来，转换成 Flash 动画。这样很麻烦，而且效率不高，同时在转换的过程中还容易出错。而现在的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R2SWF&lt;/code&gt; 提供了跟其他图形设备一样的接口，于是你可以像生成 PDF 文件一样来创建动画。比如下面的代码&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;R2SWF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;swf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dev.off&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;会创建一个两帧的 Flash 动画，每一个新的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plot()&lt;/code&gt; 语句就代表了新的一帧。此外，这个 SWF 设备还加入了完整的字体支持，意思是你可以使用任何一个 ttf/ttc 类型的字体来绘制文字，包括对中文字体的支持，就像下面这样。&lt;/p&gt;

&lt;div align=&quot;center&quot;&gt;
 &lt;embed width=&quot;350&quot; height=&quot;210&quot; name=&quot;plugin&quot; src=&quot;http://yixuan.cos.name/cn/files/2013/12/R2SWF_example.swf&quot; type=&quot;application/x-shockwave-flash&quot; /&gt; 
&lt;/div&gt;

&lt;p&gt;关于这些 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R2SWF&lt;/code&gt; 的新特性和用法，我想会另写一篇博客来说明。&lt;/p&gt;

&lt;p&gt;最后说一下对当前 CRAN 这种更严格的政策的想法，我觉得是有利有弊吧。弊处不用多说，门槛提高会让开发者的积极性大打折扣，而且会花很多时间在非代码的工作之上，我自己的经历就是例子。但平心而论，更严格的标准意味着对代码和整个软件包的质量有了更高的要求，现在不再是写完论文后用R实现一下算法就能往 CRAN 上发了。此外 CRAN 严格的署名制度会让你在使用其他人的成果时更加小心，这也是对知识产权的尊重。&lt;/p&gt;

&lt;p&gt;最后是一些关于版权和许可证的经验教训，如果想写R包发到 CRAN 上，但使用了其他人写的代码，有几点需要注意：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一定要在 DESCRIPTION 文件的 Author 栏加上那些代码的作者。如果人太多，可以写在一个名为 AUTHORS 的文件里，塞到源码包的 inst 文件夹下。&lt;/li&gt;
  &lt;li&gt;如果那些代码包含版权信息，应该把它们写在一个名为 COPYRIGHTS 的文件里，同样塞到 inst 文件夹下。&lt;/li&gt;
  &lt;li&gt;注意许可证的兼容性。引用了 GPL 的代码，就必须把整个包变成 GPL 的。如果包含的代码是 GPL-2 的，整个包的许可证就必须是 GPL-2，不能是 GPL-3。许可证为 GPL-2 的软件包不能包含 Apache 2.0 的代码，但 GPL-3 可以。许可证为 Apache 2.0 的包不能包含 GPL 的代码，这意味着，你的软件包不能同时包含 GPL-2 和 Apache 2.0 的代码。你已经绕晕了对不对？其实我也是，都是被逼的。&lt;/li&gt;
&lt;/ul&gt;

</description>
    <link>https://yixuan.blog/cn/2013/12/story-of-r2swf-2/</link>
    <guid>https://yixuan.blog/cn/2013/12/story-of-r2swf-2</guid>
    <pubDate>2013-12-08T12:37:50+00:00</pubDate>
  </item>

  <item>
    <title>R2SWF诞生记（一）</title>
    <description>&lt;p&gt;刚刚提交了 &lt;a href=&quot;http://cran.r-project.org/web/packages/R2SWF/index.html&quot;&gt;R2SWF&lt;/a&gt; 0.7 版。&lt;/p&gt;

&lt;p&gt;五年以前，差不多就是这个时候，北京召开了&lt;a href=&quot;http://cos.name/2008/12/1st-chinese-r-conference-summary/&quot;&gt;第一届R语言会议&lt;/a&gt;，那时候天还很蓝（←_←够了）。&lt;/p&gt;

&lt;p&gt;那时候还不认识初代目火影谢老大，当时他做了一个R语言实现动画的报告，我坐在下面听。因为当时我好像正好在捣腾 Flash 动画，于是报告结束后上去搭讪，问了三个问题：“这个动画的原理跟 Flash 很像啊。不知道现在有没有什么自动生成 Flash 的程序？这个功能以后能不能加到 animation 包中去？”初代目答曰：“是啊，不清楚，可能吧。”（注：此处有夸张）&lt;/p&gt;

&lt;p&gt;后来 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animation&lt;/code&gt; 包真的加入了一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;saveSWF()&lt;/code&gt; 函数，其原理是调用了一个叫 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SWFTools&lt;/code&gt; 的程序，它可以把 PNG, JPEG 和 PDF 格式的图片转换成 SWF 格式的 Flash 动画。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;saveSWF()&lt;/code&gt; 所做的就是将程序调用的命令行封装成函数，供用户使用。但这里面有一个麻烦事，就是需要系统里面装好 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SWFTools&lt;/code&gt;，【而且】，把可执行文件加到系统 PATH。后面这件事对于 Windows 用户简直就是灾难，所以初代目一直想把 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SWFTools&lt;/code&gt; 打包到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animation&lt;/code&gt; 里面。我记得当时做了很多尝试，比如把 exe 文件直接塞到软件包中，但因为 CRAN 不允许软件包包含可执行文件，所以此路不通。还有用函数自动下载安装文件，然后读取注册表获取文件位置什么的，但好像也是因为太过复杂而放弃。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;然后过了一年，到了&lt;a href=&quot;http://cos.name/2009/12/2nd-chinese-r-conference-summary/&quot;&gt;第二届R语言会议&lt;/a&gt;，那时候天依旧很蓝（戳开链接有图为证）。&lt;/p&gt;

&lt;p&gt;这中间的一年里开始折腾各种新奇的玩意，比如 Linux，比如开始学C语言，比如学着自己编译一些开源软件，等等。可能也是因为会议的演讲中有宫雨老师关于R与C交互的报告，所以当时就萌生了把 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SWFTools&lt;/code&gt; 打包进 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animation&lt;/code&gt; 的想法。于是那时候便开始研究 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SWFTools&lt;/code&gt; 的源代码结构，然后试着与R进行结合。终于，天可怜见（误），在某个夜黑风高的夜里，居然真的被我编译通了，为此还兴奋地&lt;a href=&quot;http://yixuan.cos.name/cn/2009/12/enhanced-animation-package/&quot;&gt;发了篇博客&lt;/a&gt;。只可惜，那时候还太年轻（？），根本不知道里面的水有多深。要知道，开源社区的代码风格是以最大可能兼容各种不同的平台，所以你需要 configure 文件，需要 Makefile，需要 config.h.in，等等等等。等我意识到这些，才知道已经掉到坑里出不来了，参见&lt;a href=&quot;http://yixuan.cos.name/cn/2009/12/r-conf-animation-security-and-others/&quot;&gt;那时的吐槽&lt;/a&gt;。因为这些问题，程序没办法在 Windows 上编译，所以只好潜心修学，苦练内功，等待他日再战。&lt;/p&gt;

&lt;p&gt;【警告：后面要变文风了】&lt;/p&gt;

&lt;p&gt;于是一年又一年，这次是 2010 年的 12 月 3 日，距初代目潜入美帝已经过了两载。这段时间略有一些反常，因为原本初代目会定期将一些情报资料发送到我们组织的加密邮箱，但最近已经有两周没有通信了。我推测信件是因为不明的原因被拦截，因为即使经过256位加密，当前的情报传输也并非总是可靠的。如果是这样，今后的通信可能会面临很大的风险。但无论如何，这一天我必须将两个压缩文件包发送到网络通道的另一端，以在约定的时间里完成组织的任务。不，不对，这不是组织的任务，而是私下里与初代目构思出的一个计划。即使是在组织内部，也很少有人听说过这个计划。&lt;/p&gt;

&lt;p&gt;这一天的天气本应不错，但我故意调低了房间的亮度，因为我觉得太强的光线会让我放松警惕。在确定周围没有其他人之后，我像往常一样打开邮件的页面，同时从抽屉的最深处拿出一个闪存盘，将其中的两个文件上传到附件中。我反复检查了邮件的内容，使其看上去更像是日常的对话，以免露出破绽。我不停地告诉自己，这一步迟早要来的，就算是失败了也不会对我产生本质上的影响，但我还是迟迟没有办法点下发送的按钮。就这样犹豫了一分多钟，手上的汗已经沾到了鼠标和键盘上。终于，门外一阵陌生的声响，让我警觉地迅速点下按钮，决绝地忘记了其中的任何细节。&lt;/p&gt;

&lt;p&gt;我原本以为，这封邮件会石沉大海，或至少要等若干天才收到回复，谁知道仅仅过了四个小时，我便收到了来自初代目的指示。大意非常明确，“时机成熟，独立出来，开始行动”。&lt;/p&gt;

&lt;p&gt;【待续】&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2013/12/story-of-r2swf-1/</link>
    <guid>https://yixuan.blog/cn/2013/12/story-of-r2swf-1</guid>
    <pubDate>2013-12-07T17:35:16+00:00</pubDate>
  </item>

  <item>
    <title>学习Julia（二）</title>
    <description>&lt;p&gt;接&lt;a href=&quot;http://yixuan.cos.name/cn/2013/11/learning-julia-1&quot;&gt;上一篇&lt;/a&gt;。本次包括以下一些新的内容：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;条件判断&lt;/li&gt;
  &lt;li&gt;异常处理&lt;/li&gt;
  &lt;li&gt;表达式（黑魔法）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这一次我们的线索依然是t检验的程序，我们准备进一步完善它。作为比较，我们依然先把R的输出结果贴出来：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;t.test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.768&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.4622&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alternative&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hypothesis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;95&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;confidence&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;interval&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
 &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-0.6509637&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.3201667&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimates&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.3346015&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上一篇中t检验的程序有几个缺陷：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;没有对数据进行测试，因为t检验样本量至少为2&lt;/li&gt;
  &lt;li&gt;没有显示置信区间（这本质上与下面的几行代码是一样的，这里是偷懒没把它加上）&lt;/li&gt;
  &lt;li&gt;输出结果中小数点位数太多&lt;/li&gt;
  &lt;li&gt;没有显示第一行的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data:  rnorm(10)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;下面我们一一解决，照例先把代码贴上：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/iG2ttVy.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/iG2ttVy.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;在Julia中产生错误信息的函数与R一样，叫做 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error()&lt;/code&gt;。程序第7到第9行是判断 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; 的长度，如果不足2，则产生错误信息，退出函数。可以看出，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; 语句采用的同样是缩进风格，并需要用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end&lt;/code&gt; 来关闭，但省略了R中的圆括号和大括号。&lt;/p&gt;

&lt;p&gt;第15行到第17行是计算置信区间的上下界，其中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quantile()&lt;/code&gt; 进一步展示了分布函数的操作，这是取分布的分位数，相当于R中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; 系函数，如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qt()&lt;/code&gt; 等。&lt;/p&gt;

&lt;p&gt;在输出部分中，与R一样，四舍五入的函数是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;round()&lt;/code&gt;，在后面我们对要输出的数值都进行了舍入处理。&lt;/p&gt;

&lt;p&gt;最后，本篇中最大的坑来了，R中那行诡异的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data:  rnorm(10)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;可能你会觉得，这行输出与其他的语句没什么区别，按理就是一些字符串操作，但实际情况要复杂得多。让我们先做一个实验。在R里面，如果你运行&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;t.test(rnorm(100))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;那么输出结果中的第一行就是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data:  rnorm(100)&lt;/code&gt;。如果你输入的是&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;t.test(1:10)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出结果就是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data:  1:10&lt;/code&gt;。如果函数里面是一个已经定义好的变量 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;，那输出结果就是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data:  x&lt;/code&gt;。换言之，Ｒ会把你输入的表达式原封不动地打印出来。也就是说，当表达式传递给函数时，表达式本身并不会先求值，而是把表达式整体传递给函数。当函数要用到表达式的值时，才会运行这段表达式。这种特性在Ｒ中称为“惰性求值”（lazy evaluation）。很晕对不对，确实，让我们看一个简单的例子来帮助理解。&lt;/p&gt;

&lt;p&gt;请分别运行下面两段程序：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输出结果应该是这样的：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; f1(print(1));
NULL
&amp;gt; f2(print(1));
[1] 1
NULL
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看到区别了吗？运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f1(print(1))&lt;/code&gt; 的时候这个1并没有被打印出来，而在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f2(print(1))&lt;/code&gt; 中却会。这是因为，在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f1()&lt;/code&gt; 中，我们根本就没有用到函数的参数 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;，所以表达式传递给函数后没有再被使用，R也就不会计算它的取值。而在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f2()&lt;/code&gt; 中，我们需要把 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; 的值赋给另一个变量，这样就必须对其进行计算。在R中，我们可以通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;substitute()&lt;/code&gt; 函数来捕获参数本身的表达式，然后将其转换成字符串，输出到屏幕上。&lt;/p&gt;

&lt;p&gt;然而在Julia中，非常遗憾，所有的参数在传递之前都要计算取值，这是&lt;a href=&quot;https://groups.google.com/forum/#!topic/julia-dev/lDI2E1UjjbA&quot;&gt;在Julia设计之初就定下的规矩&lt;/a&gt;，所以我们无法知道数据是通过何种表达式生成的，也就无法输出类似于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data:  rnorm(10)&lt;/code&gt; 这样的结果。&lt;/p&gt;

&lt;p&gt;当然，我说的是无法实现自动的惰性求值，如果只是要实现表达式的操作，Julia也是可以的。Julia用冒号和括号来表示表达式，比如&lt;/p&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当表达式只包含一个元素时，它的类型就变成了符号（Symbol），比如上面的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:x&lt;/code&gt;。你甚至可以先把表达式存下来，然后对其取值，或是转换成字符串。&lt;/p&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;eval&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;总结一下，惰性求值是R中非常独特的一个功能（“作者你够了，这篇文章应该是写Julia的吧！”），最常见的两个例子，一个是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library(MASS)&lt;/code&gt; （或其他任何一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library()&lt;/code&gt; 语句），一个是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plot(rnorm(10), rnorm(10))&lt;/code&gt;。不知道你有没有发现惰性求值存在的痕迹呢？&lt;/p&gt;

&lt;p&gt;当然，惰性求值基本属于黑魔法范畴。Julia虽然不支持这一特性，但对于大部分的应用来说，并不会造成太大的问题——它基本属于锦上添花一类，而不是必需的。&lt;/p&gt;

&lt;p&gt;最后依然是涉及到的一些R与Julia的对比。&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;R&lt;/th&gt;
      &lt;th&gt;Julia&lt;/th&gt;
      &lt;th&gt;作用&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;error()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;生成错误信息&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qt(0.975, df)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quantile(TDist(df), 0.975)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;t分布分位数&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;round(x, 3)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;round(x, 3)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;四舍五入&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;substitute()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;捕获表达式&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expression(x + 1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:(x + 1)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;生成表达式&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eval(expr)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eval(expr)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;表达式求值&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;as.character(x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;string(x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;强制转换为字符串&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
</description>
    <link>https://yixuan.blog/cn/2013/11/learning-julia-2/</link>
    <guid>https://yixuan.blog/cn/2013/11/learning-julia-2</guid>
    <pubDate>2013-11-21T20:37:00+00:00</pubDate>
  </item>

  <item>
    <title>学习Julia（一）</title>
    <description>&lt;p&gt;&lt;a href=&quot;http://julialang.org/&quot;&gt;Julia&lt;/a&gt; 0.2 发布了。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://yixuan.cos.name/cn/2012/02/julia-comes/&quot;&gt;很早以前&lt;/a&gt;写过一篇博客简单地介绍了一下这门新兴的语言，现在第二个稳定版本发布，看来可以有计划地学习一下了。&lt;/p&gt;

&lt;p&gt;作为本系列（设想中，如果烂尾了请人艰不拆）的第一篇，我想先编写一个简单的单样本 t 检验程序，来展示一下Julia的一些基本语法和特性。当然，我是从一个R用户的角度来学习Julia的，所以并不会从最基本的操作开始，而是侧重于统计计算和与R的比较。这次的内容大概有下面这些：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;基本的向量操作&lt;/li&gt;
  &lt;li&gt;定义函数，以及默认值的使用&lt;/li&gt;
  &lt;li&gt;打印字符串&lt;/li&gt;
  &lt;li&gt;附加包的安装和使用&lt;/li&gt;
  &lt;li&gt;Julia中的分布函数&lt;/li&gt;
  &lt;li&gt;查看/设定工作空间，以及运行脚本&lt;/li&gt;
&lt;/ul&gt;

&lt;!-- more --&gt;

&lt;p&gt;作为一个演示的例子，我们模仿R中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t.test&lt;/code&gt;函数实现一个单样本的t检验。（R的输出如下）&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;t.test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;One&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sample&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.768&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;df&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.4622&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alternative&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hypothesis&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;not&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;equal&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;95&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;percent&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;confidence&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;interval&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
 &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-0.6509637&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.3201667&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sample&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;estimates&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.3346015&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;接下来就开门见山，直接上Julia代码。&lt;/p&gt;

&lt;div class=&quot;language-julia highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Pkg.add(&quot;Distributions&quot;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Pkg.installed()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Distributions&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt; t_test&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;meanx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;stdx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tstat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meanx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stdx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tdist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TDist&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;pvalue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ccdf&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tdist&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tstat&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;        One Sample t-test&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;t = &quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tstat&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;, df = &quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;, p-value = &quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pvalue&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;alternative hypothesis: true mean is &quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;print_with_color&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;not equal&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot; to &quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mu&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;mean of x&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meanx&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;由于现在博客的代码环境暂时不支持行号，所以我再贴一张图上来：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/OFDdr5e.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/OFDdr5e.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;逐行解释一下这段代码。&lt;/p&gt;

&lt;p&gt;第1行的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pkg.add()&lt;/code&gt; 是Julia中安装软件包的命令，相当于R中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;install.packages()&lt;/code&gt;。同样地，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pkg.installed()&lt;/code&gt; 是查看已安装的软件包，在R中相当于不带参数的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library()&lt;/code&gt;。此外可以看出，Julia的注释方法与R一样，是用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt;号。要使用安装好的软件包，需要使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;using&lt;/code&gt; 语句（第3行），类似于R中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library(*package*)&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Distributions&lt;/code&gt; 是Julia中处理分布函数的软件包，后面我们将用它来计算t检验的p值。详细的说明文档在&lt;a href=&quot;http://juliastats.github.io/Distributions.jl/index.html&quot;&gt;这里&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;从第5行开始我们就在定义函数了。Julia定义函数的方法与Matlab相似，是用缩进风格。此外，Julia不允许用点作为变量名，所以我们的函数名改为了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_test&lt;/code&gt;。参数列表与R中差不多，其中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mu = 0&lt;/code&gt; 声明了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mu&lt;/code&gt; 的默认值。&lt;/p&gt;

&lt;p&gt;代码的6~9行有强烈的R既视感，除了把 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sd()&lt;/code&gt; 改成了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std()&lt;/code&gt;。第10和第11行是Julia中使用分布函数的方法，我们要先创建一个t分布的对象（创建时需要指定自由度），然后调用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ccdf()&lt;/code&gt; 函数计算生存函数。&lt;/p&gt;

&lt;p&gt;再接下来就是字符串的输出。简单地来说，Julia中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print()&lt;/code&gt; 就相当于R中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat(..., sep = &quot;&quot;)&lt;/code&gt;，而 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println()&lt;/code&gt; 则是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat(..., &quot;\n&quot;, sep = &quot;&quot;, )&lt;/code&gt;。额外的福利是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print_with_color()&lt;/code&gt; 函数，用它可以来输出彩色的文字。&lt;/p&gt;

&lt;p&gt;像R中一样，Julia的代码有两种运行方式，存成脚本，或在控制台里运行。我们先把上述代码保存为脚本文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t_test.jl&lt;/code&gt;，然后展示控制台中的操作。&lt;/p&gt;

&lt;p&gt;依然与R中一样，在Julia中我们有工作空间的概念。在R中查看和设定工作空间的函数是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getwd()&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setwd()&lt;/code&gt;，在Julia中则对应为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pwd()&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cd()&lt;/code&gt;。接下来我们用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;include()&lt;/code&gt; 函数加载这个文件（相当于R中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source()&lt;/code&gt;），然后做一个简单的测试：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/hWZiNKG.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/hWZiNKG.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;上面的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;randn()&lt;/code&gt; 是生成标准正态随机数的函数，用分号结尾是避免其取值被打印出来（这两点都源自Matlab）。最后我们用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit()&lt;/code&gt; 来退出Julia。&lt;/p&gt;

&lt;p&gt;总结一下，Julia借鉴并融合了一些其他科学计算语言的特性，我相信R和Matlab的用户都会对它的语法感到很舒服。下面是本文涉及的R和Julia的函数对比：&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;R&lt;/th&gt;
      &lt;th&gt;Julia&lt;/th&gt;
      &lt;th&gt;作用&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;install.packages(&quot;pkg&quot;)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pkg.add(&quot;pkg&quot;)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;安装软件包&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pkg.installed()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;查看已安装包&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;library(pkg)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;using pkg&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;加载软件包&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length(x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length(x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;向量长度&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mean(x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mean(x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;求均值&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sd(x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;std(x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;求标准差&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1 - pt(x, df = n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ccdf(TDist(n), x)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;t分布&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat(..., sep = &quot;&quot;)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;打印字符串&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat(..., &quot;\n&quot;, sep = &quot;&quot;)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;println()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;打印一行&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt; &lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;print_with_color()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;彩色打印&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getwd()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pwd()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;查看工作空间&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setwd()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cd()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;设定工作空间&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;source()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;include()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;运行代码文件&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rnorm(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;randn(n)&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;标准正态随机数&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit()&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;退出&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
</description>
    <link>https://yixuan.blog/cn/2013/11/learning-julia-1/</link>
    <guid>https://yixuan.blog/cn/2013/11/learning-julia-1</guid>
    <pubDate>2013-11-18T10:54:00+00:00</pubDate>
  </item>

  <item>
    <title>闲扯一下博客</title>
    <description>&lt;p&gt;博客又快荒废得长毛了。按现在这样写博客的速度，过不了多久半衰期就可以按年计了。当然我可以很幸灾乐祸地说页面右边那一列人也好不到哪去，哈哈哈哈哈（顿时一排砖头飞过来）。怎么，不服？来战！&lt;/p&gt;

&lt;p&gt;我挑了友情链接里面数据比较好抓的几位大人的博客，然后计算每月的博客数，如下图所示：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/N1bBFGg.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/N1bBFGg.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;画出图以后彻底被吓呆了：原来一直以为yihui是博客狂人，现在才知道liyun原来这么高产！那两个日均超过一篇的月份（2008.11，2009.7）是什么情况？！关键在于，liyun的更新频率几乎没有明显的衰减趋势。关于这一点，即使是博客基数很大的谢老师（稍后解释这个称呼）自从世界末日以后也没再更新过（中文）博客了。&lt;/p&gt;

&lt;p&gt;相比上面两位大侠，sizhe，yishuo和我的博客就显得稀疏得多了，不过sizhe的跨度长，yishuo最近一直在更新，如果能坚持下去也是相当难得的。&lt;/p&gt;

&lt;p&gt;最有意思的是yihui的博客变化趋势。图形很明显地分成了三段：2008年以前，2008年到2010年，2010年以后。我们看到2007年年中以前博客数基本上都是0，但实际上不是的。那时候yihui把博客放在&lt;a href=&quot;http://xieyihui.bokee.com/&quot;&gt;博客网上面&lt;/a&gt;，我认为这一段时期是这种生活+技术博客的巅峰。回应前面挖的那个坑，这段时期我觉得可以叫做“&lt;a href=&quot;http://xieyihui.bokee.com/1535388.html&quot;&gt;谢老师&lt;/a&gt;时期”，整个博客就是一部活脱脱的群众大学统计学院史记，当然视角人物是谢老师了。&lt;/p&gt;

&lt;p&gt;2008年到2010年，是所谓的“谢老大时期”。这时候谢老大刚去美帝，但同时也是COS迅速发展的时期。这期间博客的话题非常多，R会议的诞生，美帝的见闻和吐槽，R和COS论坛的那些事，或冷或热的娇客，等等。这段时间应该是作者和读者都最为活跃的时期，有的时候&lt;a href=&quot;http://yihui.name/cn/2009/12/cos-in-2010/&quot;&gt;文章后面的评论堆得比原文还高&lt;/a&gt;，非常壮观。&lt;/p&gt;

&lt;p&gt;2010年以后，技术文章大量出现，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animation&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;formatR&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LyX&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cranvas&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;knitr&lt;/code&gt;等等都有很大的篇幅。如果要给这段时间加一个名字，干脆就叫“谢博士时期”吧。= =&lt;/p&gt;

&lt;p&gt;当然玩笑归玩笑，事实还是不太乐观的，博客相比以往已然没落了许多。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/UfUjjUh.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/UfUjjUh.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;我们可以列举出一些杀死博客的罪魁祸首，当然这里我应该限定一下范围，大概指的是COS各位老大这种生活+技术的个人博客。首当其冲的自然是各种“短平快”风格的微博工具。为什么？因为写博客累啊，洋洋洒洒数千字，太短了都不好意思发出来，尤其是偏技术的文章。博客往往是主题驱动，而微博则是事件驱动。事件常有，而主题不常有。&lt;/p&gt;

&lt;p&gt;另一个潜在的原因我感觉是Google Reader等工具的没落以及消亡。至少对于我来说，在有Google Reader的时候，博客是通过Reader聚合在一起的。我很少挨个点开各位大人的主页，而往往是在Reader里面逐条阅读，然后评论感兴趣的话题。对于博主来说，这也相当于有了稳定的读者群体。自从Google Reader挥一挥衣袖后，我的这种习惯遭到了极大的改变，尽管有类似的替代品，但总有一种不太适应的感觉。我相信很多人不再用阅读器关注各种博客，然后这些博客的人气也渐渐降低（访问量减少，评论和回复减少等），相应地作者也慢慢失去了写作的动力。&lt;/p&gt;

&lt;p&gt;再回到前面说的微博。微博其实是帮你做了Google Reader原来在做的事情，就是把许许多多的信息聚合在一起，让你有一种“我已经掌握了所有我应该掌握的信息”的错觉，所以你会很少再主动地去关注其他形式的信息聚合，博客自然就更不受待见了。&lt;/p&gt;

&lt;p&gt;但不管怎么说，我认为微博至今没法超越博客的一点是，每次看以前的微博，我都有一种把当时的自己摁死的冲动，用某电视剧里的话说，“××就是矫情”。但翻以前的博客，你觉得那就是你当时思考后的结果，是当时生活状态的一种记录。无事可写，说明生活中缺少了惊奇，缺少了探索未知的乐趣。技术文也是同样，只有保持学习的动力，才会不断发现有意思的话题，才会乐于把自己的发现分享给其他人。&lt;/p&gt;

&lt;p&gt;这样算是凑够一篇博客了，争取后面多写一些。&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2013/11/about-blog/</link>
    <guid>https://yixuan.blog/cn/2013/11/about-blog</guid>
    <pubDate>2013-11-02T21:49:20+00:00</pubDate>
  </item>

  <item>
    <title>数据科学家访谈：Hadley Wickham</title>
    <description>&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/EPgIMLi.jpg&quot; class=&quot;alignleft&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Hadley Wickham 是 RStudio 的首席科学家以及 Rice University 统计系的助理教授。他是著名图形可视化软件包 
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggplot2&lt;/code&gt; 的开发者，以及其他许多被广泛使用的软件包的作者，代表作品如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plyr&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reshape2&lt;/code&gt; 等。2013年9月13日
Hadley Wickham 在普渡&lt;del&gt;众生&lt;/del&gt;大学进行了一场报告，之后小编（Yixuan）对他（Hadley）进行了一次简短的采访，
谈及了他在图形可视化、数据整理和R编程等诸多方面的工作。
本文同时是&lt;a href=&quot;http://cos.name/&quot;&gt;统计之都&lt;/a&gt;主站访谈系列文章的其中一篇。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;!-- more --&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：是否可以给我们介绍一下，你是如何进入统计学，或者说数据科学这一领域的？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：我的第一个学位是在一个医科学校获得的，事实上我差点就成为一名医生了（笑）。但是后来我意识到我并不想从医，所以就重新找回我在高中时的兴趣，开始学习计算机和统计学。我尤其喜欢编程和统计，后来便在美国读博士，研究的方向是数据可视化和多元数据分析。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：你如何定义数据科学家？和统计学家有区别吗？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：我觉得他们基本上是相同的。或许数据科学家会更关心数据库和编程等方面，但他们试图做的是同一件事。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：所以你觉得数据科学家会涉及更多实际的数据？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：是的。传统的统计学非常注重数理的那一方面，一个好的数学背景与一个好的计算机背景同样重要。在某种程度上，只要你所做的东西方向是对的，你并不需要知道如何用计算机去实现。当然了，另一种可能是你并不知道你在做什么，但只要让计算机算一些东西出来，问题也不大。（笑）&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：在你的工作中，最令人激动的和最具挑战性的分别是什么？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：现阶段最令我兴奋的工作是下一代的 &lt;a href=&quot;http://ggplot2.org/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggplot2&lt;/code&gt;&lt;/a&gt;，叫做 &lt;a href=&quot;https://github.com/rstudio/ggvis&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggvis&lt;/code&gt;&lt;/a&gt;，它试图在图形中加入交互的元素。目前我正与 Winston Chang（他也在 RStudio 工作）一起着手  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggvis&lt;/code&gt; 的开发，希望能在年底以前发布一些可供展示的例子。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：所以这是一个针对 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggplot2&lt;/code&gt; 的长期开发计划？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：是的。很明显的一个事实是，目前的数据可视化工作，你会希望让它在网页上实现，因为所有人都拥有一个网页浏览器。另一个原因是，各大浏览器的开发者都将大部分的时间投入在如何让图形既快速又能跨平台的问题之上。现在有很多浏览器之间的测试和比赛，例如比较到底是 Firefox 更快还是 Chrome 更快。这些技术的进步都使得交互式统计图形的快速绘制成为可能，而在过去这是一件比较困难的事。要向图形中方便地加入交互元素，速度是一个很重要的原则。有了交互式图形，你可以在图形中添加一个滑杆，让它调节直方图的窗宽或是Loess曲线的平滑参数。这些都是很有趣的工作。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：那最具挑战性的工作呢？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：目前我在做的另一件事是 &lt;a href=&quot;https://github.com/hadley/dplyr&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dplyr&lt;/code&gt;&lt;/a&gt; 的开发，它是 &lt;a href=&quot;http://plyr.had.co.nz/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plyr&lt;/code&gt;&lt;/a&gt; 的下一代产品。在这个过程中，我需要学习如何编写高效率的 SQL 代码。如果你在两星期前问我，我对 SQL 了解多少，或许我会说“75%吧”。然而现在，自从我真的开始使用它之后，我才意识到我可能只了解它的25%。SQL 远比我想像的要更复杂、更精深。这些工作可以说是挑战与乐趣并存。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：就我看来，在之前你最著名的作品是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggplot2&lt;/code&gt;，而现在，我们可以看到你正试图将更多的精力放在数据整理的工具之上，例如你编写的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plyr&lt;/code&gt; 包和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reshape2&lt;/code&gt; 包。同时，你也写过一些利用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rcpp&lt;/code&gt; 进行高性能计算的教程。你觉得这些技术都是如何关联在一起的？数据可视化，数据整理，以及高性能计算？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：我感兴趣的是如何让数据分析更简便、更快速。所以你只需要考虑一下你在数据分析中各环节所用的时间。如果你用了8小时进行数据清理和数据整理，而只用了2小时进行建模，那么很明显，你希望了解如何将数据清理和整理的时间尽可能缩短。正如我今天的报告提及的那样，我们会发现现在数据分析有两个瓶颈，一是我们的目标是什么，二是我们如何用计算机去实现。我现有的很多作品，如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggplot2&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plyr&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reshape2&lt;/code&gt;，更关注的是如何更简单地表达你的目标，而不是如何让计算机算得更快。现在做这些事情都变得相对简单了，而瓶颈就成了如何去进行实际的计算。我现在正在学习的就是如何编写快速的代码，如何用R调用C++以获得更快的速度等。这就像是一个往复循环的过程：如果瓶颈在这儿，你就去修复这个问题，直到它消耗更短的时间；然后瓶颈又转移到那儿，你便去着手那一个问题。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：所以你试图缩短这两方面的时间，表达数据，以及进行计算。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：是的。而另一个我感兴趣的话题是……很明显，我不可能逐一去编写人们需要的每一个R包，所以我应该如何让其他人编写优质的代码，以及优质的R包呢？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：那便是 &lt;a href=&quot;https://github.com/hadley/devtools&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;devtools&lt;/code&gt;&lt;/a&gt; 包试图做的？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：对，这个包就是希望能让其他人使用R的过程更加简便，然后鼓励他们去为社区做贡献。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：能否为我们介绍一下你的数据分析工具箱？例如使用的软件、编程语言等。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：我现在是 RStudio 的忠实用户（笑）。我曾经在 Mac 上用 Sublime Text，但在过去几个月转而使用 RStudio 了，现在它在处理函数方面非常便捷。我的工作是分析数据，所以我把90%的时间投入在R中。此外，我也在试图回答一些其他的问题，例如“其他用户都在做什么”，“他们会在哪方面遇到困难”，“如何用R更简洁地表达这个想法”，以及“我如何能将代码变得更加高效”。所以尽管我主要用的是R，我依然会用C++来写一些能快速运行的代码。这其中最大的挑战在于，你可以（用C++）让代码变得非常快，但同时你需要耗费更多的时间来编写。如果我代码中有错误，它很有可能使得R崩溃，然后你就必须从头开始。当然，对现在的 RStudio，如果R崩溃了，它会自动重启R而不致强行退出。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：很多我们网站（&lt;a href=&quot;http://cos.name/&quot;&gt;统计之都&lt;/a&gt;）的访客都对动态图形很感兴趣。他们想知道你是否有在下一代的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggplot2&lt;/code&gt; 中整合 R 和 &lt;a href=&quot;http://d3js.org/&quot;&gt;D3&lt;/a&gt; 的计划？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggvis&lt;/code&gt;（下一代的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggplot2&lt;/code&gt;）的工作原理是生成 &lt;a href=&quot;http://trifacta.github.io/vega/&quot;&gt;Vega&lt;/a&gt; 代码，而 Vega 是一个基于 D3 的软件库。所以在这个意义上，可以说 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggvis&lt;/code&gt; 是基于D3的，它支持动态的、可交互的图形。我可以给你演示一段&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggvis&lt;/code&gt;的示例。（示例演示中……）&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：这几年以来，对于软件开发，我们能看到的一个显著变化是社会化编程变得更加流行了，许多开发者都有了一个 &lt;a href=&quot;https://github.com/‎&quot;&gt;Github&lt;/a&gt; 帐号。你觉得这种现象是否会改变R以及R包的开发模式？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：我认为是的。我发现从我创立一个 Github 项目（repository）到我收到第一个提交请求（pull request）的时间变得越来越短了。最近我刚新建了一个项目而且没有告诉任何一个人，但我发现4小时之后就有人向我发送提交请求了。我认为这是社会化编程非常酷的一个方面，作者会更加有动力投入到他们的项目中，因为人们不仅是在用你的软件，而且是在参与你的开发过程。我和我的同事曾经在讨论如何利用 &lt;a href=&quot;https://gist.github.com/&quot;&gt;Gist&lt;/a&gt;，其中一个例子便是 &lt;a href=&quot;http://rpubs.com/&quot;&gt;RPubs&lt;/a&gt;。它应该基于 Gist 构建，这样你便可以在上面 fork 其他人的作品，然后添加一些修改。如果原作者愿意，他们会将修改反过来应用上去。我们有很多关于这方面的想法。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：作为 &lt;a href=&quot;http://www.rstudio.com/&quot;&gt;RStudio&lt;/a&gt; 的首席科学家，你对 RStudio 有什么未来的计划？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：我希望未来的某一天会有更多的科学家参与进来（笑）。我想当我们开始盈利的时候，我们会基于R的社区进行投资。其中一件可能会做的事就是让R成为一门高效的编程语言。&lt;/p&gt;

&lt;p&gt;我同时还对统计学习非常感兴趣，我希望它能成为一整套的统计建模工具，将它们整合在一起，形成一套模型的语法。正如图形语法能将现有的图形部件进行组合从而形成新的图形一样，你应该也能利用已有的模型来组合成新的模型。我认为那样会使建模的过程更加简单。例如，你可以用一种方式来拟合线性模型，用另一种方式来拟合随机森林，然后在一个统一的框架下进行建模。&lt;/p&gt;

&lt;p&gt;另一个我正在思考的问题是 Lasso 类的统计方法。在我上过的一门课中，我想告诉学生，现在你应该尝试一些稳健的回归方法，以及类似 Lasso 的方法。目前大概有13个关于 Lasso 的R包，我全都试了一遍，但发现其中的每一个都会因为某种原因而失效。例如，有些不能处理缺失值，有些不支持分类变量，有些不能做预测或计算标准误，或者有些不能自动选 $\lambda$ 参数等等。或许这是因为这些软件包的作者对理论更感兴趣，而不是想提供一个实际的数据分析的工具。所以，我想将它们整合在一起，形成一套既高效、又能良好运行的工具。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：我们的团队翻译了你的ggplot2书，而且 Winston 的 R Graphics Cookbook 也正在翻译中。你能否给我们介绍一下你的下一本著作，如果我没有说错的话，应该叫做《R高级编程》（&lt;a href=&quot;http://adv-r.had.co.nz/&quot;&gt;Advanced R Programming&lt;/a&gt;）？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：《R高级编程》的目标是让人们成为更好的R程序员。目前绝大部分关于R的书籍都是教你如何用R来做统计分析的，而很少是介绍编程本身的。Matloff 的&lt;a href=&quot;http://cos.name/2013/05/the-art-r-programming/&quot;&gt;《R语言编程艺术》&lt;/a&gt;（&lt;a href=&quot;http://nostarch.com/artofr.htm&quot;&gt;Art of R Programming&lt;/a&gt;）是一本很好的入门和进阶水平的书，而我想要介绍的是那些我认为很酷很强大的R语言特性。为了了解如何使用这些特性，我需要阅读大量的文档，并且我自己也做了很多实验来弄清楚其中的机理。我感兴趣的是如何帮助人们理解并编写高效的、表达力强的R代码。&lt;/p&gt;

&lt;p&gt;R作为一门编程语言似乎名声不太好，但我觉得那不是事实。我认为R的核心是一门优雅的编程语言，只是因为绝大部分的R用户都不是程序员，所以伴随着这个优雅的核心的，是大量冗长乏味的代码。R其实和 Javascript 很像，有一本书叫做《JavaScript语言精粹》（&lt;a href=&quot;http://shop.oreilly.com/product/9780596517748.do&quot;&gt;JavaScript: The Good Parts&lt;/a&gt;），它试图提炼出 Javascript 优雅的一面。而我的书与此类似，不仅是如何教会人们如何编写优雅的R代码，而且试图让问题解决的过程更加简单。当然，这需要介绍一些R背后的机制和理论。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：最后一个问题：在你的闲暇时间里，都有哪些爱好？&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hadley&lt;/strong&gt;：我喜欢烹饪。最近我正在学美式烧烤，我还喜欢调鸡尾酒。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yixuan&lt;/strong&gt;：好的，非常感谢你给我这次采访的机会！&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/ICvLmEQ.jpg&quot; class=&quot;aligncenter&quot; /&gt;&lt;/p&gt;

&lt;p&gt;（Hadley Wickham 和统计之都翻译的&lt;a href=&quot;http://cos.name/2013/05/ggplot2/&quot;&gt;《ggplot2：数据分析与图形艺术》&lt;/a&gt;）&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2013/09/a-conversation-with-hadley-wickham/</link>
    <guid>https://yixuan.blog/cn/2013/09/a-conversation-with-hadley-wickham</guid>
    <pubDate>2013-09-27T21:45:20+00:00</pubDate>
  </item>

  <item>
    <title>R并行计算的相关资料</title>
    <description>&lt;p&gt;R 从 2.14.0 开始加入了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parallel&lt;/code&gt; 包，用以支持 R 中的并行计算。但是在这方面，相关的文档和书籍还是不是很多，我目前知道的有下面几本：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Q. Ethan McCallum 和 Stephen Weston 的 &lt;a href=&quot;http://shop.oreilly.com/product/0636920021421.do&quot;&gt;&lt;em&gt;Parallel R&lt;/em&gt;&lt;/a&gt;，是一本专门讲解 R 并行的书，内容较深，包括了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;snow&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;multicore&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parallel&lt;/code&gt; 几个包和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hadoop&lt;/code&gt; 的内容。&lt;/li&gt;
  &lt;li&gt;Norman Matloff 老爷子的 &lt;a href=&quot;http://nostarch.com/artofr.htm&quot;&gt;&lt;em&gt;Art of R Programming&lt;/em&gt;&lt;/a&gt;，里面的第16章标题就是“Parallel R”。这本书已经有了&lt;a href=&quot;http://book.jd.com/11243704.html&quot;&gt;中文版&lt;/a&gt;，嗯，这是广告。&lt;/li&gt;
  &lt;li&gt;还是 Norman Matloff 老爷子写的，书名叫 &lt;a href=&quot;http://heather.cs.ucdavis.edu/parprocbook&quot;&gt;Programming on Parallel Machines&lt;/a&gt;，其中的第10章是讲 R 的并行，是对上面编程艺术那本书的补充。这本书很有意思，是一本“开源”的书，可以免费下载得到。&lt;/li&gt;
  &lt;li&gt;Joseph Adler 的 &lt;a href=&quot;http://shop.oreilly.com/product/0636920022008.do&quot;&gt;R in a Nutshell, 2nd Edition&lt;/a&gt;，其中第26章讲的是R和Hadoop。&lt;a href=&quot;http://www.bjt.name/&quot;&gt;思喆&lt;/a&gt;，&lt;a href=&quot;http://jliblog.com/&quot;&gt;舰哥&lt;/a&gt;和&lt;a href=&quot;http://yishuo.org/&quot;&gt;一硕&lt;/a&gt;正在翻译此书。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parallel&lt;/code&gt; 包自带的小品文（vignette），介绍了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parallel&lt;/code&gt; 包的基本用法和注意事项。其他类似的还有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foreach&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doMC&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doParallel&lt;/code&gt; 等一系列并行包的小品文，这些文档前前后后加起来也不少了。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;中文文档方面，目前为止成型的书可能只有翻译好的编程艺术一本，其他的主要还是一些技术文档，比如阿稳之前博客上的一系列文章（目前暂时无法访问，之后加上链接）。&lt;/p&gt;

&lt;p&gt;在看 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parallel&lt;/code&gt; 包的小品文时，觉得这篇文档可以作为并行 R 的入门参考读物，与编程艺术中的章节有很多对应的地方，所以就花时间把它翻译成了中文，放在了&lt;a href=&quot;https://github.com/yixuan/parallel-translation&quot;&gt;github&lt;/a&gt;上，供各位参考和修正。后续如果有精力准备继续翻译 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foreach&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doMC&lt;/code&gt; 等包的小品文，如果客官愿意加入进来也可以直接在 github 中一起协作完成。&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2013/06/documentation-on-parallel-R/</link>
    <guid>https://yixuan.blog/cn/2013/06/documentation-on-parallel-R</guid>
    <pubDate>2013-06-16T10:45:40+00:00</pubDate>
  </item>

  <item>
    <title>六届众生——第六届R语言会议见闻</title>
    <description>&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/RBJ6itu.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/RBJ6itu.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;第六届中国R语言会议是前天闭幕的，整整两天的日程，又是一次规模空前的盛会。根据后台统计，报名的人数超过了600，而实际到会也有400以上，
再加上强大的演讲者阵容，以及相当拉风的国学馆会场，可以说是力超以往各届。&lt;/p&gt;

&lt;p&gt;参加这次会议，基本上一半是学习，一半是访友。而访到的朋友，又一半是故交，一半是新识。按以往的传统，似乎应该按报告先后记录一下会议的流程，
但这回好几场报告没有听完全，所以干脆把本纪改成列传，盘点一下我印象中的英雄谱吧。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h3 id=&quot;谢老大&quot;&gt;谢老大&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/spJlm75.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/spJlm75.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
跟&lt;a href=&quot;http://yihui.name/&quot;&gt;头目&lt;/a&gt;又是一年半没见，不知不觉谢老大都已经要博士毕业了，先预祝一下填坑顺利。
会议的前一天，我和老大、教主、魔王一起去汴京大学听 Maindonald 教授的讲座，
其间教主拉着我问，“他真的是谢老大吗？怎么跟我大一时候见的一点变化也没有啊？”直到现在我回想起这句话，才觉得教主此言有深意，恍然大悟
这真的不是在&lt;a href=&quot;https://i.imgur.com/NFpOldB.jpg&quot;&gt;黑PhD&lt;/a&gt;吗……&lt;/p&gt;

&lt;p&gt;老大的报告是这几年编写R包的经验总结，从最初的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animation&lt;/code&gt;这块敲门砖，到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;knitr&lt;/code&gt;这个彻底改变了很多人写作习惯的神器，总结出一句话，好玩是第一生产力。
而至于宣传，老大则用“善易者不卜”这句话来作结。此言一出，被林师兄化用一番，成了本次会议的另一个亮点。&lt;/p&gt;

&lt;p&gt;哦，对了，在跟 Maindonald 教授聊天的时候，谢老大终于给我们展示了那个 RStudio 的“&lt;a href=&quot;http://www.weibo.com/2514669664/zlDxL0kdk&quot;&gt;前无古人的新功能&lt;/a&gt;”，
但是我不能说，唉，恨我吧。&lt;/p&gt;

&lt;h3 id=&quot;魔王驾临&quot;&gt;魔王驾临&lt;/h3&gt;
&lt;p&gt;这次很难得地见到了未曾谋面的魔王（&lt;a href=&quot;http://weibo.com/u/1635976784&quot;&gt;@严酷的魔王&lt;/a&gt;），本来他给我们也准备了一个报告的，但当时我们没有及时回复，结果错失了一场演讲，在此先道个歉。
魔王不远千里，从yao都来到di都，一身凌厉的极客范瞬间就把老中（请自觉对号入座）两代黑客给征服，并互称极友（益辉语）。
后来我被问到是怎么把魔王给勾搭上的，我回想了一下，那时候果壳让我写文章，
我翻了翻归档，发现有几篇关于统计的文章都是“严酷的魔王”这个ID发布的，于是在新浪微博里搜到相同的ID，那时候魔王才大三吧。后来又找到魔王的博客，也让他进驻了COS主站，
总之是一步一步诱使魔王入圈，成为我统中人。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/Y0pUMxY.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/Y0pUMxY.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;走时匆匆没能道别，成了一大遗憾。&lt;/p&gt;

&lt;h3 id=&quot;武林萌主&quot;&gt;武林萌主&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/P6hp9XB.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/P6hp9XB.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;去年mo都，萌主（&lt;a href=&quot;http://weibo.com/zhouyummy&quot;&gt;@Yummy_zhou&lt;/a&gt;）横空出世，一时江湖上多少英雄豪杰都被他萌得一脸是血。今年驾临di都，
因段子众多，被益辉唤作段王爷。会议前一天晚上的沙龙上，一串串眩目的JS交互图形震惊四座，博得全场喝彩。&lt;/p&gt;

&lt;p&gt;萌主教育我们，要做一名有情怀的R程序员，而更难能可贵的是，萌主能把前端做得如此之美观，着实让人敬佩。然后的然后，我不小心想起了下面的场景：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/NPgHnOT.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/NPgHnOT.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;涛声依旧&quot;&gt;涛声依旧&lt;/h3&gt;
&lt;p&gt;从第二届R会议的初露锋芒，到如今成为会议的首脑级人物，涛涛（&lt;a href=&quot;http://weibo.com/u/1869812215&quot;&gt;@三水成海&lt;/a&gt;）的成长真是让人欣喜。
实事求是地说，会议的规模一年比一年大，办会的难度也呈多项式级别增长。
前前后后忙了好几个月，邮件少说也有几百个来回，更不用说其他的借教室申经费约演讲者。本次会议名义上是陈昱小师弟当主席，
但其实大家都知道幕后黑手是谁。陈森师弟发微博说，幕后黑手累了，其实这已经是历届名义主席和实际主席的经典动作了，参见下图。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/iVR1phH.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/iVR1phH.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;https://i.imgur.com/4Ce2bct.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/4Ce2bct.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;走之前的聚餐上，涛涛带着倦意跟我说，“我终于能体会你当时的感觉了，参会和办会完全不一样”。&lt;/p&gt;

&lt;p&gt;几年一晃而过，来过的人走了，走过的人回了，所幸涛声依旧。&lt;/p&gt;

&lt;h3 id=&quot;楠木成林&quot;&gt;楠木成林&lt;/h3&gt;
&lt;p&gt;还记得前面说的COS的老中青三代黑客吧，当年的少年黑客如今也已硕果累累晋升中生代，成了承上启下的一个传奇了。惟一让现在中生代黑客感到惆怅的乃是“美人迟暮，
英雄发福”，聊以慰藉的是“遏制住了体重增速过快的趋势”（冤有头债有主，此为太云语，我只负责转发）。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/WjcyB5p.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/WjcyB5p.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;重回讲坛，小楠还是一贯地追求极值的美感和黑客独有的教义，正如他送我那本不明觉厉的天书证明，如果非要我用文字加以形容，可谓“身是人间客，行若天上星”。&lt;/p&gt;

&lt;p&gt;小楠的ID是&lt;a href=&quot;http://weibo.com/road2stat&quot;&gt;@road2stat&lt;/a&gt;，如今这条路上草木渐盛，楠已成林。&lt;/p&gt;

&lt;h3 id=&quot;后有来者&quot;&gt;后有来者&lt;/h3&gt;
&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/fUQyxu3.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/fUQyxu3.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
前无古人易，后有来者难。老人们纷纷离去，谁来接手可是愁等大事。这次参会，又结识了一批参与组织会议的师弟师妹（其实已经有不少“老人”混迹其中），
这也算是我拍屁股走人后感到非常欣慰的一件事了。&lt;/p&gt;

&lt;p&gt;陈妍（&lt;a href=&quot;http://weibo.com/u/2365843762&quot;&gt;@姗姗__ss&lt;/a&gt;）和晓矛（&lt;a href=&quot;http://weibo.com/u/1749040412&quot;&gt;@没有墨迹的橘子&lt;/a&gt;）自是不用说，去年到今年都是主力队员，平时跟我也是邮件来往甚多，几无隔阂。而某云更是特意考证出，
漫锜（&lt;a href=&quot;http://weibo.com/u/2291399371&quot;&gt;@小鳗鱼不想长大&lt;/a&gt;）在第三届会议就已经参与其中，
如今更是被我等教众奉为教主，以至于第一天晚上吃饭时喊口号“为谢老大干杯”，益辉明显犹豫了一下是否说的是自己。至于晚餐期间各种花边新闻，我觉得很有必要总纟&lt;/p&gt;

&lt;p&gt;陈昱（&lt;a href=&quot;http://weibo.com/u/2798096702&quot;&gt;@陈昱RUC&lt;/a&gt;）师弟，本节主席和下届干活主席，乃是统计学院历来未曾有过的计算机高手。在我回国之前就听太云反复说起师弟的事迹，见过真人之后更是膜拜得一塌糊涂。开会休息的时候，
我拿起我的Nexus玩了一个名叫Lazors的反射镜游戏，玩到后面难度太大过不去，连高智商游戏高手的萌主也都束手无策。师弟看过之后操起键盘编起程，一通演算过后，
活生生地把通用的算法给解出来了，而且用的程序语言是Pascal，Pascal啊！&lt;/p&gt;

&lt;p&gt;冷静（&lt;a href=&quot;http://weibo.com/angelgofly&quot;&gt;@冷静RUC&lt;/a&gt;）师妹，另一个让我震惊了很久的人物。师妹虽然现在才大二，但已然颇有大将风范。和漫锜一样，这次全程陪同了澳大利亚来的两位教授，光英语这一项，
就可以甩开我们这种（伪）留学生好几条街了。不光如此，涛涛和太云一直夸她果断干练，亲眼一见，绝非虚言，实乃我统之幸。&lt;/p&gt;

&lt;p&gt;展航（&lt;a href=&quot;http://weibo.com/xzhman&quot;&gt;@zhanhangshaw&lt;/a&gt;），我的同门师弟，毕业之前虽然见过面却并未深交，为兄之错啊。会议期间跟展航聊天的机会不多，
但任何一点言语之中都能感觉到他对统计啊R语言啊各种新技术啊等等的热情，非常有上进心的一位童鞋。明年这时候也给我们做一个报告吧，哈哈。&lt;/p&gt;

&lt;p&gt;王贺（&lt;a href=&quot;http://weibo.com/u/1961610973&quot;&gt;@王贺_RUC&lt;/a&gt;）师弟，非常厉害的R语言高手，为我们做了场R里面文本挖掘技术的报告，可谓尽得思喆和舰哥的真传。报告完之后，立马就被场下坐不住的某公司给盯上了，
这年头，劫才啊。&lt;/p&gt;

&lt;p&gt;陈森（&lt;a href=&quot;http://weibo.com/u/2665167631&quot;&gt;@ActuaryChan&lt;/a&gt;）师弟，和陈妍陈昱并称三陈（太云语），接触的时间太短，没能好好聊聊，但太云也是一直说师弟靠谱，（据说酒量也大），见面后颇有一见如故之感。
写这篇博客的时候翻了下大家传的照片，可是，可是，怎么关于师弟的照片都是下面这些啊！！&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/cgHyAJG.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/cgHyAJG.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;
&lt;a href=&quot;https://i.imgur.com/u1Gxh5S.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/u1Gxh5S.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;包括陈森，还有其他几位第一次见的师弟师妹，真希望以后能再有机会相聚一堂，把我统这种精神一直传承下去。&lt;/p&gt;

&lt;p&gt;最后还要特别感谢一下建冲（&lt;a href=&quot;http://weibo.com/710030221&quot;&gt;@苏建冲study&lt;/a&gt;）这段时间的辛勤工作，低调+踏实，我尤其敬佩的那种人。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/DGdkTj6.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/DGdkTj6.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;本来还有好多人要写的，像是三年未见的菁菁，和一直没有断过联系的太云一硕堰平思喆舰哥等等，只因常挂心上，觉得从未远离。&lt;/p&gt;

&lt;p&gt;是为记。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/qJl4gNe.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/qJl4gNe.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2013/05/6th-china-r-conference/</link>
    <guid>https://yixuan.blog/cn/2013/05/6th-china-r-conference</guid>
    <pubDate>2013-05-20T09:34:17+00:00</pubDate>
  </item>

  <item>
    <title>高频函数</title>
    <description>&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ggplot2&lt;/code&gt;的书马上就要出版了，前段时间太云让我试着做一下封面的图案，大概是分析一下书中的代码，然后生成一个函数使用频数的词云。&lt;/p&gt;

&lt;p&gt;要分析函数的使用频率，就得先把函数提取出来，这相当于是做一个语法解析。这个工作可以由R中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parse()&lt;/code&gt;函数来完成，
它可以读取文件或字符串，将它们转换成R中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expression&lt;/code&gt;对象。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expression&lt;/code&gt;对象是一个你每次用R都会见到，但很少直接对它操作的东西，我在这里把它翻译成“表达式串”。表达式串由一个或多个表达式组成，
每一个表达式在R中叫做&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;，代表了一条语句或一次函数调用。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;对象又可以互相嵌套，比如一个函数调用可以作为另一个函数调用的参数。
一个简单的例子是这样的：&lt;/p&gt;

&lt;!-- more --&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;expression&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;call&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;`+`&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ca&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;call&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;从上面可以看出，我们能用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[[]]&lt;/code&gt;来对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;对象取子集，例如上面的例子中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x + abs(y)&lt;/code&gt;是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;对象，其第一个元素是操作符（也是一个函数）&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;，
第二个元素是参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;，第三个元素是一个新的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;对象&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;abs(y)&lt;/code&gt;。我们的任务就是递归地把所有的函数/操作符提取出来，然后统计频数。&lt;/p&gt;

&lt;p&gt;提取R文件中函数名的函数是这样的：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;print.exp&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 单个元素，不可再分解&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 单个元素可能是变量、常量等，只打印函数&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inherits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;call&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;as.character&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;invisible&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 对表达式（串）中的每个元素逐个调用print.exp本身&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 一般的表达式类名为&apos;call&apos;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 赋值表达式和大括号是特殊的表达式，&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 类分别为&apos;&amp;lt;-&apos;和&apos;{&apos;，需要单独列出&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inherits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;call&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;=&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;-&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;{&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print.exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 表达式的第一个元素为函数名，如plot(x, y)，&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 在表达式列表中即为&apos;plot&apos;，&apos;x&apos;和&apos;y&apos;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 类似地，x + y的列表形式为&apos;+&apos;，&apos;x&apos;和&apos;y&apos;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;as.character&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;invisible&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;用的时候先让&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parse()&lt;/code&gt;解析文件，然后套到这个函数里面，就可以把文件里面的函数名打印出来了。为了处理方便，可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sink()&lt;/code&gt;重定向输出，例如：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warn&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;output.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print.exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;someRfile.R&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sink&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;《ggplot2：数据分析与图形艺术》中的代码托管在&lt;a href=&quot;https://github.com/cosname/ggplot2-translation/&quot;&gt;这里&lt;/a&gt;，
利用上面编写的函数配合&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordcloud&lt;/code&gt;包，最终做出的词云是这样的：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/YcPh8Tz.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/YcPh8Tz.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2013/04/frequently-used-functions/</link>
    <guid>https://yixuan.blog/cn/2013/04/frequently-used-functions</guid>
    <pubDate>2013-04-07T09:34:17+00:00</pubDate>
  </item>

  <item>
    <title>听雨声</title>
    <description>&lt;h3 id=&quot;点滴到天明&quot;&gt;点滴到天明&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/gqnvX.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/gqnvX.jpg&quot; alt=&quot;点滴到天明&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;旧金山的冬天是雨季，这是我来之前未曾料到的。连着好几天的阴雨，让原来计划的好些景点都没法去看。到了今天实在憋不住了，就冒着雨来到了市政大厅附近的亚洲艺术博物馆，一边是躲雨，一边也算是游览旧金山的一部分。&lt;/p&gt;

&lt;p&gt;一个亚洲人从亚洲来到美国，然后从美国的一个地方到另一个地方来看亚洲艺术展，乍听起来是不是有些怪怪的。不过我倒是觉得在一个陌生的地方能看见自己熟悉的一些事物，还是会有一些感触和反思的。&lt;/p&gt;

&lt;p&gt;从博物馆的正门入馆，一路向前走就是位于二楼的一个大厅，大厅的名字嘛，叫做Samsung Hall，原因你懂的。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/6I5h7.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/6I5h7.jpg&quot; alt=&quot;Samsung Hall&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;大厅很空旷，只有一口钟立在一端，我进去的时候里面一个人也没有，是个适合沉思的地方。纯粹出于好奇的原因，我用相机的全景照相功能把大厅的前半边扫了一圈，然后合成了一幅全景照。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/NmjXn.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/NmjXn.jpg&quot; alt=&quot;Samsung Hall半景&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;从Samsung Hall两侧的门可以到达不同的展厅，二楼是中日韩三国的展厅，三楼包括南亚和东南亚，还有中国的青铜器、玉石以及西藏的一些展览。而在进入展厅之前，首先吸引我注意的是走廊两侧的瓷器展。只是一眼，便看到了那个久违的熟悉的地名。一件，两件，后来发现整整一排都是景德镇的瓷器。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/89QpM.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/89QpM.jpg&quot; alt=&quot;景德镇&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;或许是因为平时生活里对景德镇的瓷器太熟悉了，所以看到一件件的展品时会觉得格外亲切。此时窗外还下着潺潺细雨，隔着玻璃后的青瓷，不禁哼唱起那熟悉的旋律。天青色等烟雨，你又在等谁？&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/wxRqF.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/wxRqF.jpg&quot; alt=&quot;青花&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;目眇眇兮&quot;&gt;目眇眇兮&lt;/h3&gt;

&lt;p&gt;而让我感到惊叹的是展品的丰富程度，每过一个展架就是一种新的风格。从青瓷到彩瓷，从物件到人神，当真可谓玲琅满目。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/nB5Xk.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/nB5Xk.jpg&quot; alt=&quot;彩瓷&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/RDz2E.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/RDz2E.jpg&quot; alt=&quot;神像&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;而最让我震惊的是下面两件。如果我告诉你它们不是竹子和木头，而是紫砂，你会相信自己的眼睛吗？&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/y7Z7S.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/y7Z7S.jpg&quot; alt=&quot;舟&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/vz8MJ.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/vz8MJ.jpg&quot; alt=&quot;椅&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;看过一件件让人兴奋的展品，又不禁有些惆怅。它们曾经都属于大洋彼岸的那个国度，如今却飘洋过海，在此静静地守候。若是它们也有灵性，是否也会有所谓的乡愁呢？就好比那尊屈原的瓷像，双目低垂，看到的却不是楚国的故土。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/CJTrV.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/CJTrV.jpg&quot; alt=&quot;屈原&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;谦谦君子&quot;&gt;谦谦君子&lt;/h3&gt;
&lt;p&gt;来到三楼的玉石展厅，又是一番晶莹的光彩。夫昔者君子比德于玉，温润而泽，仁也；缜密以栗，智也；廉而不刿，义也；垂之如坠，礼也；叩之其声清越而长，其终则诎然乐矣，瑕不掩瑜，瑜不掩瑕，忠也；孚尹旁达，信也；气如白虹，天也；精神见于山川，地也；珪璋特达，德也；天下莫不贵者，道也。如今这个典故被翻译成了英语，成为了展厅的介绍。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/r6rMm.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/r6rMm.jpg&quot; alt=&quot;Introduction to Chinese Jade&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;此外展厅的导言还提到，在中国最早的字典中，列出了一百多个用来指代玉石的文字。翻一翻那些带王字旁的字词，我在想，是不是这墙上每一件玉器都有一个对应的表达呢？&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/f7zig.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/f7zig.jpg&quot; alt=&quot;玉环&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/eNNmt.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/eNNmt.jpg&quot; alt=&quot;玲琅满目&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;舞墨&quot;&gt;舞墨&lt;/h3&gt;
&lt;p&gt;在日本馆、韩国馆、南亚和东南亚馆逛了一圈，就回到了一楼的特别展区。这个展区的主题是Out of Character: Decoding Chinese Calligraphy。再亲切不过了。在展区一侧的桌子上，主办方准备了水写纸和字帖，供游客（当然主要是“外国”游客了）模仿中国文字。我跟着凑了个热闹，但笔的感觉有些奇怪，写出来的字也特别丑。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/Z7Vp4.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/Z7Vp4.jpg&quot; alt=&quot;归&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;房间的另一侧是一幅直接印在墙上的画。游客可以写一张红字条，然后挂在画中的树上。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/uoVdB.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/uoVdB.jpg&quot; alt=&quot;画墙&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;红字条上大都是祝福的话语，但也能看到一些可爱得让人心碎的片段。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/A39Ui.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/A39Ui.jpg&quot; alt=&quot;心愿&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在这张字条的背后，又会书写着什么呢？或者，就让它成为一个谜吧。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/XGNHQ.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/XGNHQ.jpg&quot; alt=&quot;解名缰&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2012/12/listening-to-rain/</link>
    <guid>https://yixuan.blog/cn/2012/12/listening-to-rain</guid>
    <pubDate>2012-12-24T15:00:00+00:00</pubDate>
  </item>

  <item>
    <title>秋天的歌</title>
    <description>&lt;p&gt;气温降到华氏40度以下，只是一晚上的事情。&lt;/p&gt;

&lt;p&gt;来美国快一个半月了，之前一直觉得该写些什么东西记录下来，可是大多数情况下不是忙到没时间写，就是闲到无事可写。只有在现在这个不忙不闲的季节，才开始梳理起那些理不断，剪还乱的思绪。（抱歉，我又开始讲冷笑话了，因为我发现这个错位的短语特别适合形容我当时给自己理发的情形。）总之消失了一个多月，是时候把博客重新收拾一下了。&lt;/p&gt;

&lt;p&gt;刷微博，毫无悬念地又读到了纳兰容若的浣溪沙。谁念西风独自凉，萧萧黄叶闭疏窗，沉思往事立残阳。这里有西风，有黄叶，有残阳，却唯独没有往事的。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/4aPT4kS.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/4aPT4kS.jpg&quot; alt=&quot;残阳&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;!-- more --&gt;

&lt;p&gt;鲮鱼在微博上告诉我，来国外的前两个月是蜜月期，两个月之后开始会想家，再加之一年半载，就是乡愁了。我开玩笑说，我向来对未来之事有所担忧，现在就已经在操心两个月之后的事了。&lt;/p&gt;

&lt;p&gt;不过这终究是个坏毛病，大家也都给我提过很多遍了。来这里之后，也往往因为一些习惯性的（吐槽）言语被冠之以“不淡定”之名，我想应该是很贴切的了。&lt;/p&gt;

&lt;p&gt;当然说笑归说笑了。选择来这个僻静的地方读书，其中一个原因便是希望自己能在一个陌生的地方也学会变得强大起来。（好吧，明明是因为别的地方不要我……）感觉自从大一以来，每次遇到不顺的事，就会有人安慰我说“你年纪还小嘛”，“你还年轻，有的是机会”（甚至昨天我还听到了这样的话）。可是这样慢慢对自己造成了暗示，好像你有了停滞不前的理由。仔细想想，今年已经到了最二的年纪，却依然功业未成，穷困（Sleepy）潦倒，再赶上秋风拂面，简直就是悲从中来。&lt;/p&gt;

&lt;p&gt;锋哥之前有句话说得很对，一路上真是多亏了各位贵人相助。刚下飞机的那天，到公寓的时候管理员已经下班了，没有钥匙，进不了房间。当天是被好心的王大伯和林阿姨收留，才算度过了第一个夜晚。至今我钱包里还留着他们的名片，惭愧的是开学一个月以来都被各种事情折腾，否则应该再去拜访他们一次的。有意思的一件事是，他们住的庄园叫做Kingswood，正好跟他们的姓对应上了。&lt;/p&gt;

&lt;p&gt;来到这里之后，也承蒙各位师兄师姐的照顾。人大毕业的兆楠师姐在我申请的时候就帮了很多忙，上个月她和云辉师兄请我们人大过来的两个人吃饭，顿时被一种久违的归属感包围了。北大毕业的武韬师兄我是在来之前就在网上联系过，对于美国的各种习俗文化相当熟悉，我称他是Encyclopedia，周末经常蹭他的车出去，还有好多生活英语都是跟他学的，总之开学以来最要感谢的人就是他。北理毕业的奇明师兄和COS上的大牛杨灿是朋友，他也经常在COS上潜水，开学以来对我们颇为照顾，为人很热情，更重要的是他刚结婚了，妻子是我们同届的同学Yue。:D&lt;/p&gt;

&lt;p&gt;正式上课以后，开始陆续认识系里的同学，不出所料，中国学生占了国际学生的一大半。就我认识的来说，Simeng和Chunyan来自科大，Han和Chen来自山大，Hong来自吉大，Ching-wei来自台湾，Steve和Gigi是已经在这边读过书的。由于我们选的课都差不多，所以经常能见上面。除了中国学生的圈子，平时能搭上话的，有韩国的Ji-hwan，他编程很厉害，属于能量超群型的，这学期选了四门课，还有助教和英语课要上；瑞典的Philip，中文名奥影，人非常聪明，最让人震惊的是会五门语言（瑞典语，法语，英语，中文，还有一门忘了），我估计把会的编程语言加上都超不多他；美国的Zach和Patrick，他们共用一间办公室，在我的办公室对面，有时会和他们讨论作业题，其实也就这时候交流比较顺畅一些，平时生活上的闲聊，我的听力差，经常听不懂，就弄得很尴尬。不过还是觉得英语非得靠说才能练好，以后得多和他们讨教学习才是。&lt;/p&gt;

&lt;p&gt;流水账似地写了这么多，已经不知道主题是什么了，就好像一首秋天的歌，有回忆，有憧憬，有孤独，有相聚，有枫林向晚，有月高天际，有花，有木，有人心。&lt;/p&gt;

&lt;p&gt;又及：COS服务器频繁遭毒，估计过不了多久就要把博客搬家了，这或许是搬家前的最后一篇博客。&lt;/p&gt;

&lt;p&gt;又又及：今天考试，原以为不会很难，但最终还是在几个盲点上犯错了。又想起大刘的话，弱小和无知不是生存的障碍，傲慢才是。&lt;/p&gt;

&lt;p&gt;是为记。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2012/09/a-song-for-autumn/</link>
    <guid>https://yixuan.blog/cn/2012/09/a-song-for-autumn</guid>
    <pubDate>2012-09-21T11:36:00+00:00</pubDate>
  </item>

  <item>
    <title>CLT的一些事</title>
    <description>&lt;p&gt;最近在读 E. L. Lehmann 的 &lt;em&gt;Elements of Large-Sample Theory&lt;/em&gt; 一书。说起大样本和渐进理论，我一向是敬而远之，因为只要一碰到测度论和极限定理，我就不由自主地发怵。但这本书着实给了我很多惊喜，因为整本书几乎没有提到测度这两个字，简单翻阅了一下，感觉以数学分析的内容就可以看懂其中的大部分证明，于是甚感欣慰。大师的作品确实有其独到之处，读起来很少有晦涩难懂的地方，于是趁着现在时间相对宽松，也想在大样本理论这一块入个门。Lehmann也是一位令人尊敬的大师级人物，写过不少点估计和假设检验方面的经典教材，遗憾的是于2009年与世长辞。&lt;/p&gt;

&lt;p&gt;今天看了跟中心极限定理有关的一些内容，从中了解到了不少之前没有考虑过的问题，而书中也解答了一些我一直留存的疑问，所以一并记录在这里。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h3 id=&quot;平均后的变异程度&quot;&gt;平均后的变异程度&lt;/h3&gt;

&lt;p&gt;中心极限定理考察的对象是一列随机变量样本均值的分布。定理的表述有很多种，在这里只考虑最简单的情形：样本独立同分布。我们直觉会认为，样本均值会比原始的随机变量更稳定、更集中，最后成为一个方差逐渐趋于0的正态分布。经过一定的标准化处理，极限分布就是标准正态分布。但事实上，对变量进行平均，并不一定能减小分布的变异程度。例如，对于柯西分布来说，将两个独立的柯西分布进行平均，其结果仍然是一个同分布的柯西分布，所以相应地，均值的极限分布与单个的变量分布相同。&lt;/p&gt;

&lt;p&gt;此外还有“越平均越变异”的分布。书中举的例子是标准正态分布的平方倒数，即 \(X=1/Y^2\)，其中 \(Y \sim N(0,1)\)。对于这个分布来说，\(\bar{X}\) 与 \(nX_1\) 是同分布的，所以样本量越大，均值的分布越分散。&lt;/p&gt;

&lt;p&gt;下面的三组箱线图是对这三种分布的一个对比，横轴是样本量，纵轴是均值分布的箱线图。可以看出，对于正态分布，样本量越大，均值抽样分布的离散程度越小；对于柯西分布，离散程度几乎不变；对于第三种分布，很明显离散程度迅速变大。需要注意的是，绘制箱线图的数据只取了中间的一半，因此实际的分布会比箱线图所反映出的更加分散。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/KO8rPur.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/KO8rPur.png&quot; alt=&quot;正态分布均值分布&quot; /&gt;&lt;/a&gt;
&lt;a href=&quot;https://i.imgur.com/1wzCKN9.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/1wzCKN9.png&quot; alt=&quot;柯西分布均值分布&quot; /&gt;&lt;/a&gt;
&lt;a href=&quot;https://i.imgur.com/onm403J.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/onm403J.png&quot; alt=&quot;正态分布平方倒数的均值分布&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;样本量要多大才正态&quot;&gt;样本量要多大才正态？&lt;/h3&gt;

&lt;p&gt;如果我们记 \(\sqrt{n}(\bar{X}-\xi)/\sigma\) 的分布函数是 \(G_n(x)\)，那么中心极限定理说的其实就是当 \(n\rightarrow \infty\) 时，对任意的 \(x\) 有 \(G_n(x)\rightarrow \Phi(x)\)。于是这里就出现了一个问题：到底n要多大 \(G_n(x)\) 才与标准正态比较接近？&lt;/p&gt;

&lt;p&gt;我一直以为这个问题是无解的，因为我感觉没有一个普世的准则对所有的分布都有效。直到今天才看到书中引用了一个定理，对这个问题进行了解答。定理是这样说的：&lt;/p&gt;

&lt;p&gt;Berry-Esseen定理：假设 \(X_1,\ldots, X_n\) 独立同分布（分布函数为F），且具有有限的三阶矩，那么存在一个与F无关的常数C，使得对任意的x，都有&lt;/p&gt;

\[\vert G_n(x)-\Phi(x)\rvert\le \frac{C}{\sqrt{n}}\frac{E\lvert X_1-\xi\rvert^3}{\sigma^3}\]

&lt;p&gt;目前已知该定理对C=0.7975成立，对C&amp;lt;0.4097不成立，但C可能的最小值依然是未知的。这个定理最强大的地方在于它对任意的x和任意的n都是成立的，所以不管x是否在变动，以及不管n有多大，你都可以给出当前均值分布与正态分布的绝对误差！从另一个方面说，只要你指定一个允许的误差，你就能计算出满足这个误差上界所需的样本量大小。&lt;/p&gt;

&lt;h3 id=&quot;因为对称所以正态&quot;&gt;因为对称，所以正态？&lt;/h3&gt;

&lt;p&gt;益辉在&lt;a href=&quot;http://cos.name/2010/05/from-clt-simulation-to-normal-distribution/&quot;&gt;这篇文章&lt;/a&gt;中指出了一个问题，那就是很多教科书在对中心极限定理举例时都喜欢拿对称的分布来做，而一旦原始分布不对称，往往要当样本量很大时均值分布才接近正态。当时我就想，是什么原因使得对称分布有这样的优越性？同样是直到今天，我才在书中找到了对这个疑惑的一个合理的解释。&lt;/p&gt;

&lt;p&gt;注意到之前的Berry-Esseen定理中，等号右边是 \(1/\sqrt{n}\)，但既然是小于等于号，我们就不禁要问，收敛的速度有没有可能更快？要回答这个问题，我们就需要用到下面的这个定理：&lt;/p&gt;

&lt;p&gt;假设 \(X_1,\ldots, X_n\) 独立同分布（分布函数为F），具有有限的三阶矩，且不是一个格点分布，那么&lt;/p&gt;

\[G_n(x)=\Phi(x)+\frac{\mu_3}{6\sigma^3 \sqrt{n}}(1-x^2)\phi(x)+o(\frac{1}{\sqrt{n}})\]

&lt;p&gt;其中 \(mu_k=E(X-\xi)^k\) 表示k阶中心矩，上式中的第二项称为一阶Edgeworth正态近似修正。&lt;/p&gt;

&lt;p&gt;这个定理的意思是，\(G_n(x)\) 趋近于 \(\Phi(x)\) 的速度由两部分决定，一部分是 \(1/\sqrt{n}\) 的速度，另一部分是比 \(1/\sqrt{n}\) 更快的速度。而如果第一部分等于0，那么趋近的速度就可以更快。要达到这个目的，唯一的可能是让 \(\mu_3\) 等于0，而这恰恰意味着分布的偏度等于0，也就是说是对称的分布。至此，真相大白。&lt;/p&gt;

</description>
    <link>https://yixuan.blog/cn/2012/03/something-about-clt/</link>
    <guid>https://yixuan.blog/cn/2012/03/something-about-clt</guid>
    <pubDate>2012-03-05T23:11:00+00:00</pubDate>
  </item>

  <item>
    <title>Julia来了</title>
    <description>&lt;blockquote&gt;
  &lt;h2 id=&quot;in-short-because-we-are-greedy&quot;&gt;&lt;em&gt;In short, because we are greedy.&lt;/em&gt;&lt;/h2&gt;
&lt;/blockquote&gt;

&lt;p&gt;今天看到一篇文章，是&lt;a href=&quot;http://sd.csdn.net/a/20120223/312315.html&quot;&gt;介绍一门编程语言Julia的&lt;/a&gt;。文章底下的评论有些扎眼，建议移步看它的&lt;a href=&quot;http://julialang.org/blog/2012/02/why-we-created-julia/&quot;&gt;英文原文&lt;/a&gt;。文章的作者，同时也是这门语言的创始人之一，&lt;a href=&quot;http://karpinski.org/&quot;&gt;Stefan Karpinski&lt;/a&gt;，目前是加州大学圣踏芭芭拉分校的一名博士生，但曾经在若干家企业工作过。他自称是一名数据科学家和应用数学家，最近发表的一篇论文题目叫作&lt;em&gt;Non-Parametric Discrete Mixture Model Recovery via NNMF&lt;/em&gt;。看到这里我隐约感觉到，这门语言有可能是我感兴趣的。&lt;/p&gt;

&lt;p&gt;为什么又来了一种语言？作者给出的回答就是第一段的那句话——因为我们贪得无厌。我们想要C的速度，想要Python的通用性，想要R的统计，想要Matlab的代数运算，想要很多很多以及更多。Julia是否能成为这样的一门编程语言我目前还无法预知，但它目前的一些特性确实让我眼前一亮。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h3 id=&quot;与r及matlab相似的语法&quot;&gt;与R及Matlab相似的语法&lt;/h3&gt;

&lt;p&gt;Julia的官方网站是&lt;a href=&quot;http://julialang.org/&quot;&gt;http://julialang.org/&lt;/a&gt;，源代码放置在github上，地址是&lt;a href=&quot;https://github.com/JuliaLang/julia&quot;&gt;https://github.com/JuliaLang/julia&lt;/a&gt;。从中可以看到一些示例的代码，典型的Matlab风格，但好在与R的差距并不是很大，像我依葫芦画瓢也能写出一些比较简单的代码来，比如下面就是用一个暴力的二重循环来生成一个1000*1000的均匀分布随机数矩阵，并计算所有元素之和。&lt;/p&gt;

&lt;p&gt;Julia:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function fun1j()
    m = zeros(1000, 1000)
    s = 0
    for i = 1:1000
        for j = 1:1000
            m[i, j] = rand()
            s = s + m[i, j]
        end
    end
    s
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;与之等价的R程序是&lt;/p&gt;

&lt;p&gt;R:&lt;/p&gt;
&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fun1r&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runif&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;速度&quot;&gt;速度&lt;/h3&gt;

&lt;p&gt;速度应该是所有的编程语言都要考虑的内容。我们都知道R的痛点在哪儿，像上面的这个R程序照常理是要挨板子的，但我们还是看看它的运行时间。在我的电脑上（就不贴硬件参数了），运行时间大概为：&lt;/p&gt;

&lt;p&gt;R:&lt;/p&gt;
&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system.time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fun1r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
 &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;用户&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;系统&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;流逝&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5.769&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.004&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5.782&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在Julia中，&lt;/p&gt;

&lt;p&gt;Julia:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;julia&amp;gt; @elapsed fun1j()
0.09904909133911133
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;是的你没看错，只有不到0.1秒。&lt;/p&gt;

&lt;p&gt;但这并不是一次有意义的对比，因为上述过程是可以被向量化的。我们用R来把这段程序优化一下：&lt;/p&gt;

&lt;p&gt;R:&lt;/p&gt;
&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;fun1optr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;runif&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;system.time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fun1opt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
 &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;用户&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;系统&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;err&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;流逝&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.048&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.051&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;同样地，Julia也进行改写：&lt;/p&gt;

&lt;p&gt;Julia:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function fun1optj()
    m = rand(1000, 1000)
    sum(m)
end

julia&amp;gt; @elapsed fun1optj()
0.008805990219116211
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;相信结果已经很清楚了。&lt;/p&gt;

&lt;h3 id=&quot;有理数&quot;&gt;有理数&lt;/h3&gt;

&lt;p&gt;这可能只是一项很小的功能，但有些时候还是很方便的。Julia用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;//&lt;/code&gt;表示有理数除法，比如&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;julia&amp;gt; 1//2
1//2

julia&amp;gt; 11//22
1//2

julia&amp;gt; 987//654 + 12//34 * 56//78
84917//48178
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;并行&quot;&gt;并行&lt;/h3&gt;

&lt;p&gt;Julia是一门刚诞生不久的语言，因此它尽可能地采用了最新的设计理念来应对当前的问题，其中对并行的支持是很重要的一点。&lt;/p&gt;

&lt;p&gt;要让Julia使用多个CPU或多个核，需要在启动时加上相应的参数，例如下面的命令表示让Julia程序启用2个处理器：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./julia -p 2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Julia提供了许多内置的函数来支持多核的运算，在此引用Julia文档中的一个例子，具体的信息可以在&lt;a href=&quot;http://julialang.org/manual/parallel-computing/&quot;&gt;这个页面&lt;/a&gt;中获取。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;M = {rand(1000,1000) | i=1:10}
pmap(svd, M)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;以上程序就是对10个大矩阵进行并行的奇异值分解。&lt;/p&gt;

&lt;h3 id=&quot;接下来的事&quot;&gt;接下来的事&lt;/h3&gt;

&lt;p&gt;由于时间所限，这里举的都是非常基础的一些例子，而Julia的开发应该说也还是刚刚起步。目前Julia只支持类UNIX系统，如Linux和Mac，已经编译好的二进制文件可以在&lt;a href=&quot;https://github.com/JuliaLang/julia/downloads&quot;&gt;这里&lt;/a&gt;下载。&lt;/p&gt;

&lt;p&gt;Julia会不会成为R的一个良好替代，现在还都是未知，但有一点是肯定的，那就是无论一门语言多么优秀，其生命力永远是由用它的人决定的。R的成功，一部分源于其本身的设计，而更重要的是其背后存在一个强大的开源社区。这里有&lt;a href=&quot;http://groups.google.com/group/julia-dev/browse_thread/thread/9f79ed4f8334830a&quot;&gt;一个关于R与Julia的讨论&lt;/a&gt;，其中Julia的另一名开发者Viral Shah说了一句特别有意思的话：”What should julia do in order to provide an alternative to R? In my opinion, it is R’s libraries and its community that define it - rather than the language design or its implementation.”&lt;/p&gt;

&lt;p&gt;R与Julia的未来发展会如何？我们拭目以待。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2012/02/julia-comes/</link>
    <guid>https://yixuan.blog/cn/2012/02/julia-comes</guid>
    <pubDate>2012-02-28T00:18:31+00:00</pubDate>
  </item>

  <item>
    <title>用R读取Excel的新方法</title>
    <description>&lt;p&gt;还在纠结用如何用R读取Excel的同学有福了。昨天逛CRAN的时候发现了一个xlsx包，它给出的介绍是可以读取、写入Excel 2007/2003文件并支持格式的设置。简单地来说，将Excel读取为数据框，以及将数据框写入为Excel文件都不是问题，而更加强大的是它能处理Excel中的格式，比如合并单元格，设置列的宽度，设置字体和颜色等等。&lt;/p&gt;

&lt;p&gt;如果只需要基本的读取/写入操作，那么其中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read.xlsx()&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write.xlsx()&lt;/code&gt;应该就能满足大部分的需求了，其用法也很简单，看看帮助文档就了解了。此外，还有两个相应的函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read.xlsx2()&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write.xlsx2()&lt;/code&gt;，按作者的话说，这两个函数使用了不同的实现方式，效率上会更高一些。&lt;/p&gt;

&lt;p&gt;除了基本的读写操作之外，如之前所说，xlsx包还能进行格式方面的设置。下面是一个简单的例子，说明了如何创建工作簿和工作表，如何操作单元格等。感兴趣的朋友不妨运行一下下面的例子，看看最终的效果。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;link&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;http://yixuan.cos.name/cn/wp-content/uploads/2012/01/ind.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read.table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;link&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;\t&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlsx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create a new workbook&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wb&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;createWorkbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create a new sheet with a name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;createSheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;第一页&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Set the zoom ratio when you open the Excel file&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setZoom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Set the width of columns&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setColumnWidth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2.8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create rows&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;createRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create cells for each row&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cells&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;createCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;73&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Merge the first row into one cell&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addMergedRegion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;73&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create the style for title cell&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title_cell_style&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CellStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alignment&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Alignment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;horizontal&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ALIGN_CENTER&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Font&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;blue&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isBold&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Create the style for black cells&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;black_cell_style&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CellStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;border&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Border&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fill&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Fill&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foregroundColor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;black&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get the first row&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first_row&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getRows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sheet1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Get the title cell from first row&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title_cell&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getCells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;first_row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Set the value of the title cell&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setCellValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title_cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Read/Write Excel!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Set the style of the title cell&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setCellStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title_cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title_cell_style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Set the style of black cells&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setCellStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;black_cell_style&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Save the workbook into a file&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saveWorkbook&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;test.xlsx&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;总的来说，xlsx包是我目前见过的功能最全的操作Excel的R包，它只依赖于Java环境和rJava、xlsxjars两个包，在多种平台下都能运行，局限是写操作只支持Excel 2007格式（*.xlsx），对于机器上只有MS Office 2003的人来说可能会有些不便。（LibreOffice和OpenOffice.org都可以打开Excel 2007文件）&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2012/01/new-method-to-read-excel-file-in-r/</link>
    <guid>https://yixuan.blog/cn/2012/01/new-method-to-read-excel-file-in-r</guid>
    <pubDate>2012-01-18T16:16:48+00:00</pubDate>
  </item>

  <item>
    <title>手写识别，50行R代码</title>
    <description>&lt;p&gt;标题当然是唬人的，不过代码真的是50行正好（不算注释和空行啊），可以玩简单的数字识别。玩法很简单，先把附加的RData文件加载到R空间中（就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;load(&quot;train.RData&quot;)&lt;/code&gt;这句），然后调用函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;guess()&lt;/code&gt;，会打开一个空白的图形窗口。按住鼠标左键画一个0到9范围内的数字，然后右击鼠标确认，R会把猜测的结果输出出来。&lt;/p&gt;

&lt;!-- more --&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getData&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pushViewport&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;viewport&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid.rect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mousedown&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buttons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buttons&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;invisible&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eventEnv&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;onMouseMove&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mousemove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mousemove&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buttons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid.points&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mouseup&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buttons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eventEnv&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;onMouseMove&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setGraphicsEventHandlers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;onMouseDown&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mousedown&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                             &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;onMouseUp&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mouseup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eventEnv&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getGraphicsEventEnv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Click down left mouse button and drag to draw the number,
   		right click to finish.\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getGraphicsEvent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dev.off&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;length.out&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spx&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spy&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cbind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;traceCorr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# recogTrain = list();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# for(i in 1:10)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# {&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#     recogTrain[[i]] = getData();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# }&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# save(recogTrain, file = &quot;train.RData&quot;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;train.RData&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guess&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coefs&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sapply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recogTrain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;traceCorr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dat2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;which.max&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coefs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;verbose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coefs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;I guess what you have input is &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;.\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;有玩成功了的同志在评论里吼一嗓子啊（貌似4很难识别对），谢谢。&lt;/p&gt;

&lt;p&gt;下载：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/HandwritingRecognition.zip&quot;&gt;train.RData&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/12/handwriting-recognition-with-50-lines-r-code/</link>
    <guid>https://yixuan.blog/cn/2011/12/handwriting-recognition-with-50-lines-r-code</guid>
    <pubDate>2011-12-16T23:33:39+00:00</pubDate>
  </item>

  <item>
    <title>一些澄清，致歉，和广告</title>
    <description>&lt;p&gt;事情的源起是果壳的这个帖子：&lt;a href=&quot;http://www.guokr.com/post/74433&quot;&gt;http://www.guokr.com/post/74433&lt;/a&gt;。帖子中转载了我之前写的一篇博客，主要内容是对宋词进行了一些词频的统计，然后各位网友纷纷发挥了自己的创造力，利用这些高频词语来“写词”。看到大家热情这么高我当然非常高兴，因为这至少说明我做的一些东西是有意义的。我学的专业是统计和精算，平时会和各种类型的数据打交道，之前写那篇博文也是出于兴趣，想利用学到的专业知识来对一些实际的问题进行分析。&lt;/p&gt;

&lt;p&gt;而我没有意料到的是大家对这件事的关注度会这么高，以至于果壳网把那篇帖子推到了首页，甚至还有一些朋友说希望能有进一步的采访等等，这都是我始料未及的。对此我觉得有必要对其中的一些细节进行解释，以避免不必要的误会。&lt;/p&gt;

&lt;p&gt;首先，很多人肯定都会提到“自动作词机”，就比如拿生日、QQ、物理常数等套用里面的排序来“写词”。但我想说的是，这其实不是我的创意，也不是我写那篇博文的初衷。如果大家看过果壳的那篇帖子，就会发现大家开始“狂欢”是因为39楼“达芬奇的鸡蛋”的创意，而词频统计本身并没有任何特殊之处。事实上，大家可能听说过“文本挖掘”这个名词，它就是对文本数据进行分析，来得到有用的结论。文本挖掘是个很复杂的过程，牵涉到分词、词频统计、特征选择、聚类等等，如果大家对这一块内容有所了解的话，就会知道词频统计是一个很平凡的过程。&lt;/p&gt;

&lt;p&gt;关于自动作词机，这其实也是一个很早就有的概念，甚至网上流传，刘慈欣老师在90年代就编写过类似的软件。而就宋词来说，也有相应的文章进行过讨论，比如《一种宋词自动生成的遗传算法及其机器实现》，感兴趣的朋友可以到&lt;a href=&quot;http://wenku.baidu.com/view/bf7c8a00b52acfc789ebc9be.html&quot;&gt;http://wenku.baidu.com/view/bf7c8a00b52acfc789ebc9be.html&lt;/a&gt;进行浏览。&lt;/p&gt;

&lt;p&gt;第二个大家可能觉得比较新鲜的地方是利用理科的知识来研究文学的内容。我需要说的是，这个其实也很常见，而且可以追溯到更久以前，一个典型的例子是李贤平老师的《&lt;红楼梦&gt;成书新说》，浏览的地址是[http://www.docin.com/p-277121750.html](http://www.docin.com/p-277121750.html)。事实上，有很多学者都尝试过对《红楼梦》的词频进行分析，以试图找出前八十回和后四十回的差异。&lt;/红楼梦&gt;&lt;/p&gt;

&lt;p&gt;第三点我需要说的是，可能有些朋友觉得我得到宋词的词频是一件技术含量很高的活儿，但从技术层面上来讲，我做的那些东西也并无任何高级之处（当然需要有一些编程经验）。我个人对R语言比较感兴趣，所以这些分析都是用R语言实现的。但对于其它的编程语言或统计软件，要实现的话都是有章可循的。果壳的帖子中25楼的朋友就给出了一个很好的解决方案。事实上，目前已经有不少专门进行文本挖掘的软件，比如R语言的rmmseg4j软件包等，如果有对技术感兴趣的朋友，可以参考这个帖子：&lt;a href=&quot;http://cos.name/cn/topic/105321&quot;&gt;http://cos.name/cn/topic/105321&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;所以上面说的这些东西中心思想是什么呢？意思就是说，无论是想法、研究还是技术，其实很早以前就有人做过了，太阳底下并无新鲜事，大家需要知道这背后的一些事实。&lt;/p&gt;

&lt;p&gt;然后是标题的第二部分。今天有一些朋友发邮件或是给我博客留言，说希望能有一些采访，我把我的想法一并在这里说了吧。首先，真的非常感谢几位对这个话题的关注以及对我的肯定，我自己非常感激。但非常遗憾，我想我还不能接受。一方面是我之前说的，有几位朋友觉得是我弄出的自动写诗机，但实际上不是的，而且我自己也没有打算往这一方向去做。另一方面就是最近我的学业压力比较大，没有太多的精力投入在这些事情上。我这个博客的目的，是记录自己学习的过程，同时如果可能的话，希望我做的东西能引起大家对统计学和R语言的兴趣。至于其他的方面，可能并不是我考虑的初衷。&lt;/p&gt;

&lt;p&gt;如果还有朋友仍然对这一话题有兴趣，那么请看下面的广告。&lt;/p&gt;

&lt;p&gt;=======================趁着还没到2012年插播一段广告=======================&lt;/p&gt;

&lt;p&gt;好了，我不想把这篇博文弄得和紧张兮兮的新闻发布会一样，而且我也还远没到那个层次。请大家抬头看一下我博客的域名：cos.name，这个域名是属于&lt;a href=&quot;http://cos.name&quot;&gt;统计之都网站&lt;/a&gt;的。统计之都（Capital of Statistics）是一个由志愿者团队维护的非赢利网站，旨在推进国内统计学的发展和应用。我目前是统计之都的管理员之一，而在这个团队中还有很多厉害的牛人，比如网站的创始人&lt;a href=&quot;http://yihui.name/cn&quot;&gt;谢益辉大师兄&lt;/a&gt;。更多的成员可以在网站的&lt;a href=&quot;http://cos.name/about&quot;&gt;关于页面&lt;/a&gt;中找到，他们中的每一个都有自己擅长的领域，都是很棒的人。&lt;/p&gt;

&lt;p&gt;之前大家普遍转载的是我个人博客中的&lt;a href=&quot;http://localhost:4000/2011/03/text-mining-of-song-poems/&quot;&gt;《东风何处是人间》&lt;/a&gt;，但我其实对数据和结果都进行了一些修订，发在了统计之都网站上，文章的标题是&lt;a href=&quot;http://cos.name/2011/03/statistics-in-chinese-song-poem-1/&quot;&gt;《统计词话（一）》&lt;/a&gt;。在果壳的帖子里面有朋友对《全唐诗》的词频也感兴趣，而这部分也已经&lt;a href=&quot;http://yixf.name/2011/03/21/%E5%B1%B1%E4%BA%BA%E4%BD%95%E5%A4%84%E5%90%9B%E4%B8%8D%E8%A7%81%EF%BC%9F%E4%B8%9C%E9%A3%8E%E4%B8%80%E8%8A%B1%E5%80%9A%E9%98%91%E5%B9%B2%EF%BC%81-%E2%80%94%E2%80%94%E3%80%8A%E5%85%A8%E5%94%90%E8%AF%97/&quot;&gt;有网友做过了&lt;/a&gt;，就在&lt;a href=&quot;http://cos.name/2011/03/statistics-in-chinese-song-poem-1/&quot;&gt;《统计词话（一）》&lt;/a&gt;的评论中，那位网友的博客地址是&lt;a href=&quot;http://yixf.name&quot;&gt;http://yixf.name&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;统计之都中还有更多有意思的文章，比如&lt;a href=&quot;http://cos.name/2011/07/we-never-know-randomness/&quot;&gt;对上帝他老人家的一些思考&lt;/a&gt;，以及&lt;a href=&quot;http://cos.name/2011/04/exploring-renren-social-network/&quot;&gt;yinyin网的社会网络分析&lt;/a&gt;等。《统计词话》在计划中还会有续篇，我会把我一些新的想法继续发在网站上。&lt;/p&gt;

&lt;p&gt;总而言之，我相信真正有生命力也最值得关注的是统计学本身，而不仅仅是统计词频或者作诗这一个小的方面。&lt;/p&gt;

&lt;p&gt;=======================趁着还没到2012年插播一段广告=======================&lt;/p&gt;

&lt;p&gt;嗯，就这么多，最后还是要谢谢大家！&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/12/some-notices/</link>
    <guid>https://yixuan.blog/cn/2011/12/some-notices</guid>
    <pubDate>2011-12-01T14:32:42+00:00</pubDate>
  </item>

  <item>
    <title>近日见闻</title>
    <description>&lt;p&gt;最近这个月是在各种奔波忙碌中度过的。各种考试，报告，赶场，一度有抓狂的感觉。不过好在都熬过来了，现在面临的是新的战斗。至于这段时间到底值还是不值，或许只有以后才清楚。&lt;/p&gt;

&lt;p&gt;上周五给数据挖掘小组做了关于大型计算的报告，讲了讲R调用数据库、R对象缓存和并行计算等方面的内容。讲完之后发现自己演讲的能力还是有待提高。充分的准备是一方面，与观众的互动是一方面，总体来说还是要提高自身的素质。&lt;/p&gt;

&lt;p&gt;周六和云熹踏上前往上海的高铁，300km/h，音速的四分之一，一时间恍如隔梦。路上看掉半本《追风筝的人》，脑中一片眩晕，我果然还是对苦难的描写没有抵抗力。到宾馆时已经快九点，开始为第二天的报告抱佛脚。之后见到涛涛、丽云、一硕、龙哥、舰哥，一群人挤在房间里，很有家的感觉。&lt;/p&gt;

&lt;p&gt;第二天的报告，见识了中国最年轻的博导，不禁感慨人和人之间还是有差距的。当然在师姐的气场下，周老师和“老牟”都成了师姐的信徒——这当然是玩笑话。轮到我们讲时，已经快到12点，好在整体比较紧凑，没有背负太多的罪名。到了下午，会议全部结束时，所有人都松了口气，在此必须要对会务组表示由衷的感谢。因为自己经历过，所以清楚办会的辛苦。之前见到丽云时，我们都说她脸色不太好，想是好几天没有好好休息了吧。而翔兄舰兄作为历届的主席，个中辛苦也只有他们自己清楚。&lt;/p&gt;

&lt;p&gt;晚上李舰师兄和林祯舜师兄请我们去酒吧喝酒聊天，露天的座椅，微凉的夜色，酒醉微醺，谈笑风生。每每听及师兄回忆人大的旧事，心中便是一阵神往，这感觉之前也曾有过，那是在读益辉05年的博客。&lt;/p&gt;

&lt;p&gt;第二日早上睡得昏昏沉沉，却也到了再见的时分。太云说搞不好我和涛涛十年之后才能再见，我骂他这不咒我们吗。不过真的到了告别的那一刻，才发现一种失落感涌上心头。我跟师姐说，我现在特别怕听到“以后还有机会再见”之类的话，因为我知道很多机会都是在还有机会中消失的。这两天身心俱疲，以至于整日板着张脸，我也不想这样，所以只能在这里跟各位赔个不是了。有时并非不想接受朋友的好意，实是无福消受太多。&lt;/p&gt;

&lt;p&gt;大家来来往往，聚聚散散，本也是常态了。不过掐指数来，范兄半年没见了，益辉菁菁一年半，小楠两年。真心希望明年我也离开之时，大家又能齐聚一堂，指点江山。&lt;/p&gt;

&lt;p&gt;相逢的人会再相逢。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/11/recent-life/</link>
    <guid>https://yixuan.blog/cn/2011/11/recent-life</guid>
    <pubDate>2011-11-15T10:13:58+00:00</pubDate>
  </item>

  <item>
    <title>rationalfun</title>
    <description>&lt;p&gt;刚刚向CRAN提交了一个新包，名字叫&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rationalfun&lt;/code&gt;。这个包顾名思义，是处理有理函数的。毕业论文里面有一块内容是要求有理函数的积分，数学分析课本中给出的方法是部分分式，但这个在程序中不好实现；而如果用数值积分，则速度又太慢，不划算。后来费尽千辛万苦（这里是夸张的修辞手法），终于在一篇计算机的论文中找到了解决的办法，于是索性把算法写成了包，也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rationalfun&lt;/code&gt;。本来想把论文放到博客上的，&lt;a href=&quot;http://www.fanren8.com/read.php?tid=31536&quot;&gt;但听说要坐牢&lt;/a&gt;，心想还是算了。论文的信息是&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;T. N. Subramaniam, and Donald E. G. Malm, How to Integrate Rational Functions, &lt;em&gt;The American Mathematical Monthly&lt;/em&gt;, Vol. 99, No.8 (1992), 762-772.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;目前这个包还很小很天真，能进行的运算无非是加减乘除乘方导数和积分，下面的代码差不多概括了主要的用法：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rationalfun&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
 &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-------&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;simplify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                       &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-----------------------------------------------&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rationalfun&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rationalfun&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r4&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r4&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
   &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-------&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
       &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;----------------&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;deriv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
     &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;----------------&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;14&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;integral&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.142857142857143&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.0890699716941046&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.24697960371747&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.223380423562293&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;atan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.623489801858734&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.78183148246803&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.031788704850902&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.445041867912629&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.27855083205195&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;atan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.222520933956314&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.974927912181824&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.128709838271774&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1.80193773580484&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.123966782605016&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;atan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.90096886790242&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.433883739117558&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;是不是有一种在进行符号运算的错觉？&lt;/p&gt;

&lt;p&gt;另：读牛人写的包总能发现令人震惊的东西，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rationalfun&lt;/code&gt;里面很多代码都是依赖或改编自&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;polynom&lt;/code&gt;包的，里面发现了这样一个函数：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;as.function.polynomial&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as.name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;w&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as.name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;{&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;-&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;seq_along&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;-&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;+&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个函数的作用就是用一个系数向量来生成一个多项式函数，里面&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;as.name()&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call()&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;body()&lt;/code&gt;之类的黑魔法用得不亦乐乎……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/10/rationalfun/</link>
    <guid>https://yixuan.blog/cn/2011/10/rationalfun</guid>
    <pubDate>2011-10-31T21:53:26+00:00</pubDate>
  </item>

  <item>
    <title>烧香</title>
    <description>&lt;p&gt;这次考试得过啊……&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/pmh7R.gif&quot;&gt;&lt;img src=&quot;https://i.imgur.com/pmh7R.gif&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/10/bless/</link>
    <guid>https://yixuan.blog/cn/2011/10/bless</guid>
    <pubDate>2011-10-18T15:43:20+00:00</pubDate>
  </item>

  <item>
    <title>当时只道是寻常</title>
    <description>&lt;p&gt;纪念那些无忧的岁月。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/bIlSX.jpg&quot;&gt;&lt;img src=&quot;https://i.imgur.com/bIlSX.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/10/commemorate/</link>
    <guid>https://yixuan.blog/cn/2011/10/commemorate</guid>
    <pubDate>2011-10-16T22:21:38+00:00</pubDate>
  </item>

  <item>
    <title>My English site</title>
    <description>&lt;p&gt;Well, just to save my terrible English, I have to find a place to practice writing that way. Please visit my English site &lt;a href=&quot;http://statr.me/&quot;&gt;http://statr.me/&lt;/a&gt;, another place where I share my ideas and thoughts.&lt;/p&gt;

&lt;p&gt;Welcome you all!&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/10/my-english-site/</link>
    <guid>https://yixuan.blog/cn/2011/10/my-english-site</guid>
    <pubDate>2011-10-02T19:54:52+00:00</pubDate>
  </item>

  <item>
    <title>新的开始</title>
    <description>&lt;p&gt;前两天收到雅虎邮箱的一封邮件，大意是说，嘿，老伙计，你已经用我们邮件9年了，怎么还在用9年前的界面呢，我们给你换个新的吧。于是一时手滑，看了看新的主题，结果发现没法切换回老的界面了。有时候过去的事就像这样，不经意间就找不回来了。&lt;/p&gt;

&lt;p&gt;换就换吧，干脆来得更彻底些，于是把博客的模版也一并更换了一个。不过怎么说呢，现在的这个模版还是多多少少有些怀旧的意味在里面的。:-)&lt;/p&gt;

&lt;p&gt;这会是一个新的开始吗？&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/09/a-new-start/</link>
    <guid>https://yixuan.blog/cn/2011/09/a-new-start</guid>
    <pubDate>2011-09-19T08:54:54+00:00</pubDate>
  </item>

  <item>
    <title>中秋献礼——Layer图形设备</title>
    <description>&lt;p&gt;你在用R画图的时候，是否会遇到以下的麻烦：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;加图例或文字时总是对不准坐标，要花很多精力调整元素的位置；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;某个细节出错，整幅图得重新绘制；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;想要更酷的平移、拉伸、旋转操作，就好像在Gimp或Photoshop里面一样；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;想更方便地使用字体，特别是中文的显示。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;于是接下来就有一个好消息和一个坏消息。好消息是有一个软件包可以解决上面的大部分问题了，而坏消息是这个包仍然处于开发阶段，所以各种bug是难以避免的。今天恰逢中秋，我便把这个自己编写的Layer软件包介绍给大家，算是送给大家的一份小礼物。&lt;/p&gt;

&lt;p&gt;Layer顾名思义，指的是图层，而这个绘图设备正是采用了图层的思想。在你用Layer画图时，你可以将不同的图形元素放在不同的层上，彼此之间互不影响。例如，你可以将图例单独建立一个图层，当图例移动时，下层的图形并不会发生变化，再加上一定的鼠标操作，就可以方便地绘制出美观的图形。&lt;/p&gt;

&lt;p&gt;为了让大家能直观地感受Layer的操作，下面给出了一段Layer的操作演示视频。&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;embed src=&quot;http://player.youku.com/player.php/sid/XMzAzNDkyNTU2/v.swf&quot; allowfullscreen=&quot;true&quot; quality=&quot;high&quot; width=&quot;480&quot; height=&quot;400&quot; align=&quot;middle&quot; allowscriptaccess=&quot;always&quot; type=&quot;application/x-shockwave-flash&quot; /&gt;&amp;lt;/embed&amp;gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;此外，Layer有着更方便的字体支持。在打开Layer图形设备时，你可以指定一个ttf字体文件作为图形字体的来源，如果参数为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NULL&lt;/code&gt;，则图形会使用软件包自带的&lt;a href=&quot;http://wenq.org&quot;&gt;文泉驿&lt;/a&gt;微米黑字体。&lt;/p&gt;

&lt;p&gt;Layer软件包的下载地址如下。需要说明的是，Layer需要GTK+环境的支持，对于Windows用户，如果你已经安装了GTK+环境，请选择第二个下载地址；如果尚未安装，可以直接下载第三个文件（软件包中附带了GTK+）。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Layer_0.1-0.tar.gz&quot;&gt;源代码&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Layer_0.1-0.zip&quot;&gt;Windows二进制包&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Layer.zip&quot;&gt;Windows二进制包（含GTK+运行库）&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/09/layer-graphics-device/</link>
    <guid>https://yixuan.blog/cn/2011/09/layer-graphics-device</guid>
    <pubDate>2011-09-12T21:46:22+00:00</pubDate>
  </item>

  <item>
    <title>扫雷</title>
    <description>&lt;p&gt;又是一段时间没更新博客，今天拿刚编的R版扫雷凑个数。跟上一版本相比，主要是改进了用户界面，数字的大小可以根据格子的大小自动调整。&lt;/p&gt;

&lt;p style=&quot;text-align: center;&quot;&gt;Ubuntu下的扫雷&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/dKm0N.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/dKm0N.png&quot; alt=&quot;Ubuntu下的扫雷&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p style=&quot;text-align: center;&quot;&gt;Windows下的扫雷&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://i.imgur.com/SaXoK.png&quot;&gt;&lt;img src=&quot;https://i.imgur.com/SaXoK.png&quot; alt=&quot;Windows下的扫雷&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;fun包的源代码在&lt;a href=&quot;https://github.com/yihui/fun&quot;&gt;https://github.com/yihui/fun&lt;/a&gt;。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/08/mine-sweeper/</link>
    <guid>https://yixuan.blog/cn/2011/08/mine-sweeper</guid>
    <pubDate>2011-08-18T11:25:59+00:00</pubDate>
  </item>

  <item>
    <title>卷积</title>
    <description>&lt;p&gt;又是三个月没写博客了，或许是因为越来越浮躁了吧，都不知道自己应该做什么了。今天因为论坛上的&lt;a href=&quot;http://cos.name/cn/topic/104640&quot;&gt;一个帖子&lt;/a&gt;，偶然想起了前段时间看的有关卷积的一些东西，于是想着在这里整理一下。&lt;/p&gt;

&lt;p&gt;如果学习过概率论的话，应该对卷积不会陌生——求两个独立随机变量之和的密度函数，就是将这两个变量的密度函数进行卷积运算，它实际上就是一个积分。不过今天在这里谈的离散的情形，所以我们来看看下面的这个例子：&lt;/p&gt;

&lt;p&gt;设离散随机变量 \(X\) 取值为 \(0, 1, 2, \ldots, n\) 的概率分别为 \(p_0,p_1,\ldots,p_n\)，另一个离散随机变量 \(Y\) 取值为 \(0, 1, 2, \ldots, m\) 的概率分别为 \(q_0,q_1,\ldots,q_m\)，现在要求 \(Z=X+Y\) 的分布列。&lt;/p&gt;

&lt;p&gt;这个问题并不难，比如说，要使 \(Z=0\)，则一定有 \(X=0\) 且 \(Y=0\)，于是 \(P(Z=0)=p_0q_0\)。再考虑 \(Z=1\)，它分两种情况，\(X=0,Y=1\) 和 \(X=1,Y=0\)，于是 \(P(Z=1)=p_0q_1+p_1q_0\)。类似地，要求 \(P(Z=r)\)，就把那些满足 \(i+j=r\) 的 \(p_iq_j\) 相加起来即可，例如 \(P(Z=4)=p_0q_4+p_1q_3+p_2q_2+p_3q_1+p_4q_0\)。而根据 \(X\) 和 \(Y\) 的取值范围，我们知道 \(Z\) 的可能取值有 \(0,1,2,\ldots,n+m\)，不妨假设 \(Z\) 取这些值的概率分别为 \(r_0,r_1,\ldots,r_{n+m}\)，于是有&lt;/p&gt;

\[\displaystyle r_k=\sum_{i+j=k}p_iq_j,\ k=0,1,2,\ldots,n+m\]

&lt;p&gt;这就是离散情形下的卷积公式，它实际上就是对两个向量 \(p=(p_0,p_1,\ldots,p_n)’,q=(q_0,q_1,\ldots,q_m)’\) 进行运算，来得到第三个向量 \(r=(r_0,r_1,\ldots,r_{n+m})’\)。&lt;/p&gt;

&lt;p&gt;好了，那卷积有什么用呢？在概率和统计中，它最大的用处自然就是求独立随机变量和的分布，但实际上它还有许多有趣的应用。来看下面这个问题：&lt;/p&gt;

&lt;p&gt;假设有两个多项式，\(P(x)=p_nx^n+p_{n-1}x^{n-1}+\cdots+p_1x+p_0\)，\(Q(x)=q_mx^m+q_{m-1}x^{m-1}+\cdots+q_1x+q_0\)，现在想要求 \(R(x)=P(x)Q(x)\) 的表达式中 \(x^k\) 的系数。你发现了什么？没错，其结果和之前的卷积表达式是完全一样的。这也就是说，已知了两个多项式的系数向量，这两个多项式乘积的系数向量就是它们的卷积。&lt;/p&gt;

&lt;p&gt;于是开头提到的帖子中的问题就可以解决了。举个例子来说，我们要求1111的平方，首先可以将1111写为 \(1\times 10^3+1\times 10^2+1\times 10^1+1\times 10^0\)，然后令 \(P(x)=x^3+x^2+x+1\)，则有 \(1111=P(10)\)。接下来，计算 \(R(x)=P(x)P(x)\) 的系数向量，也就是对 \((1, 1, 1, 1)’\) 自身进行卷积，得到的结果是 \((1,2,3,4,3,2,1)’\)，即 \(R(x)=x^6+2x^5+3x^4+4x^3+3x^2+2x+1\)，于是我们可以很自豪地说，\(1111^2=R(10)=10^6+2\times 10^5+\cdots+2\times 10+1\)，那么1111的平方自然就等于多项式的系数向量组成的数字1234321了。&lt;/p&gt;

&lt;p&gt;可是不要高兴得太早，万一出现 \(5\times 10^3+16\times 10^2+11\times 10+1\) 这种情况怎么办？总不能说是516111吧？也不用急，我们可以从个位数起，依次将数码向高位进位：如果该位的数字小于10，那么就保留这个数，转向更高一位；如果大于10，就将它除以10的余数保留，除以10的商加到更高一位。在这个例子中，个位数等于1，保留；十位上的数除以10余1，商1加到百位；百位17除以10余7，商1加到千位；千位6，保留。因此结果最后就是6711。&lt;/p&gt;

&lt;p&gt;在R中，求卷积的函数是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;convolve()&lt;/code&gt;，如果要求两个向量 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pv&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qv&lt;/code&gt; 的卷积，用法就是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;convolve(pv, rev(qv), type = &quot;open&quot;)&lt;/code&gt;。注意函数中的第二个向量一定要反向一下，而且 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type&lt;/code&gt; 参数必须指定为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open&lt;/code&gt;。帖子中的问题，程序代码如下：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;open&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;as.integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 将double转为integer的常见做法&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%/%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
    <link>https://yixuan.blog/cn/2011/06/convolution/</link>
    <guid>https://yixuan.blog/cn/2011/06/convolution</guid>
    <pubDate>2011-06-29T15:17:20+00:00</pubDate>
  </item>

  <item>
    <title>Processing JS+Wordpress整合！</title>
    <description>&lt;p&gt;Processing有Javascript版本了！虽然这已经是个&lt;a href=&quot;http://processingjs.org/&quot;&gt;很老很老的新闻&lt;/a&gt;，但今天突然看到有款&lt;a href=&quot;http://www.keyvan.net/code/processing-js/&quot;&gt;Wordpress插件&lt;/a&gt;，可以直接在Wordpress里写Processing程序，于是果断装上试试。下面是一段示例（Processing的JS库比较大，需要等它下载完毕，所以可能得过一段时间才出现）：&lt;/p&gt;

&lt;div style=&quot;text-align: center;&quot;&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;//yixuan.blog/cn/assets/themes/delicate/js/processing-1.4.1.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;application/processing&quot;&gt;
//Info: http://processingjs.org/reference
float x=60,y=240,z=-120;
float x2=61,y2=240,z2=-120;
int sigma=120,rho=336,beta=32;
float vx=sigma*(y-x),vy=x*(rho-z)-y,vz=x*y-beta*z;
float vx2=vx,vy2=vy,vz2=vz;
float deltat=0.0005;

void setup() {
  size(500, 500);
  background(0);
}
void draw() {
  frameRate(60);
  dot1();
  dot2();
}
void dot1()
{
  smooth();
  stroke(0,0);
  fill(255,0,0,100);
  ellipse(x+250,y+220,5,5);
  x+=vx*deltat;
  y+=vy*deltat;
  z+=vz*deltat;
  vx=sigma*(y-x);
  vy=x*(rho-z)-y;
  vz=x*y-beta*z;
}
void dot2()
{
  smooth();
  stroke(0,0);
  fill(0,0,255,100);
  ellipse(x2+250,y2+220,5,5);
  x2+=vx2*deltat;
  y2+=vy2*deltat;
  z2+=vz2*deltat;
  vx2=sigma*(y2-x2);
  vy2=x2*(rho-z2)-y2;
  vz2=x2*y2-beta*z2;
}
&lt;/script&gt;
&lt;canvas width=&quot;500&quot; height=&quot;500&quot;&gt;&lt;/canvas&gt;
&lt;/div&gt;

&lt;p&gt;效果与Java版本相比几乎没有区别！&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/03/processing-js-and-integration-with-wordpress/</link>
    <guid>https://yixuan.blog/cn/2011/03/processing-js-and-integration-with-wordpress</guid>
    <pubDate>2011-03-21T21:24:35+00:00</pubDate>
  </item>

  <item>
    <title>东风何处是人间</title>
    <description>&lt;p&gt;再次神经短路，突然想看看宋词里面什么样的意象是最常见的，比如可以做个频率分析什么的。当然文本挖掘需要分词，我没法在其中花太多时间，于是想出了一个土办法。宋词的句子都很短，如果穷举可能的字的组合的话并不是太多，况且最常见的词语一般是两三个字，这样可能的组合就更少了。比如“犹解嫁东风”这句话，可能的二字组合是“犹解”“解嫁”“嫁东”“东风”，三字组合是“犹解嫁”“解嫁东”“嫁东风”，词的字数越多，可能的组合就越少。如果把每句话可能的字的组合都列举出来，就可以整体统计频率了。&lt;/p&gt;

&lt;p&gt;当然里面会有很多无意义的字的组合，不过这类“词语”本身的出现就是一个偶然，所以可以预期的是它们整体的频数会很低，是入不了“大雅之堂”的。话不多说，直接上代码和结果。&lt;/p&gt;

&lt;p&gt;数据：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Ci.zip&quot;&gt;《全宋词》文本&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;代码：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Ci.txt&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;character&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sep&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;\n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l.len&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 某些行是作者和标题，所以选取长度大于10的行；&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 另外这个文本文件不太规整，有些网址什么的，&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 所以也要排除那些长度太长的。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ci&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l.len&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;l.len&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 句子用标点符号分割。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strsplit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;，|。|！|？|、&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unlist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s.len&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 单句太长了说明有可能是错误的字符，去除掉。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s.len&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s.len&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nchar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 暴力挨个拆分，比如“犹解嫁东风”的所有二字组合为&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# “犹解”“解嫁”“嫁东”“东风”，&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# 无意义的词其频数自然就落在后面了。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;splitwords&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x.len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;substring&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x.len&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x.len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mapply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;splitwords&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sentences&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s.len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SIMPLIFY&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;USE.NAMES&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unlist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words.freq&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words.freq&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words.freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decreasing&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data.frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Word&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words.freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Freq&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;as.integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words.freq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;结果（排在第一的是无效字符，这跟数据源有关）：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;   Word Freq           Word Freq
1       1485        51  天上  368 
2   东风 1382        52  杨柳  362 
3   何处 1230        53  西湖  356 
4   人间 1202        54  桃花  354 
5   风流  857        55  扁舟  353 
6   归去  812        56  消息  351 
7   春风  802        57  憔悴  344 
8   西风  779        58  何事  339 
9   归来  771        59  芙蓉  338 
10  江南  765        60  神仙  334 
11  相思  753        61  一片  334 
12  梅花  732        62  桃李  333 
13  千里  676        63  人生  332 
14  回首  656        64  十分  331 
15  明月  651        65  心事  329 
16  多少  648        66  黄花  328 
17  如今  642        67  一声  325 
18  阑干  630        68  佳人  324 
19  年年  613        69  长安  321 
20  万里  590        70  东君  319 
21  一笑  582        71  断肠  316 
22  黄昏  550        72  而今  315 
23  当年  542        73  鸳鸯  314 
24  天涯  537        74  为谁  313 
25  相逢  528        75  十年  310 
26  芳草  527        76  去年  309 
27  尊前  516        77  少年  308 
28  一枝  512        78  海棠  307 
29  风雨  505        79  寂寞  306 
30  流水  472        80  无情  306 
31  依旧  472        81  不是  305 
32  风吹  471        82  时候  304 
33  风月  461        83  肠断  303 
34  多情  457        84  富贵  303 
35  故人  451        85  蓬莱  303 
36  当时  450        86  昨夜  303 
37  无人  445        87  行人  302 
38  斜阳  438        88  今夜  301 
39  不知  430        89  谁知  300 
40  不见  429        90  不似  299 
41  深处  422        91  江上  298 
42  时节  403        92  悠悠  296 
43  平生  398        93  几度  295 
44  凄凉  398        94  青山  295 
45  春色  394        95  何时  294 
46  匆匆  383        96  天气  293 
47  功名  383        97  惟有  293 
48  一点  378        98  一曲  291 
49  无限  377        99  月明  291 
50  今日  369        100 往事  290
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;不知各位看官看到上面这些既熟悉又悠远的话语又将作何感想？或许，她们就是我们千百年来的精神寄托吧。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2011/03/text-mining-of-song-poems/</link>
    <guid>https://yixuan.blog/cn/2011/03/text-mining-of-song-poems</guid>
    <pubDate>2011-03-02T00:29:37+00:00</pubDate>
  </item>

  <item>
    <title>向琐碎开战</title>
    <description>&lt;p&gt;翻翻“过往”才发现，三个月没写博客了。离二月结束还剩20分钟，得抓紧了。&lt;/p&gt;

&lt;p&gt;很多时候，不怕一个巨大的困难摆在你面前，而是怕无数小的麻烦不断蚕食你的时间。在家里安逸地待了一阵子，刚来学校突然就不适应了。貌似周围的许多同志们也出现间歇性不规则情绪低落，同病相怜啊。&lt;/p&gt;

&lt;p&gt;之前一直在找寻克制琐碎的法门，但一直无果，看来这百毒不侵之身不是这么容易练就的啊。不过这几天倒是寻到一条邪道用以克制琐碎，那就是——做更多琐碎的事……&lt;/p&gt;

&lt;p&gt;于是趁着还没到三月份把今天特意制造的一些琐碎罗列如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;向R core报告了HTML帮助中的一个bug，看看是否会有回应；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;成功编译了gotoblas2，而且居然支持多核计算，甚好甚强大（&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/gotoblas2.zip&quot;&gt;下载&lt;/a&gt;）；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;不小心皈依了Emacs教，不过是否虔诚还有待考证；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;在预测精度和变量显著性之间纠结了一阵子，然后我笑了；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;成功地在23:59之前点了右上角的发布按钮。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

</description>
    <link>https://yixuan.blog/cn/2011/02/fire-to-chaos/</link>
    <guid>https://yixuan.blog/cn/2011/02/fire-to-chaos</guid>
    <pubDate>2011-02-28T23:59:39+00:00</pubDate>
  </item>

  <item>
    <title>更新一下Rhelper-RGtk2</title>
    <description>&lt;p&gt;前两天做了一个稍微有些艰难的决定，把&lt;a href=&quot;http://yixuan.github.com/cn/2010/08/rhelper-rgtk2/&quot;&gt;Rhelper-RGtk2&lt;/a&gt;更新了一下。主要更新的地方有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Rd文档的解析函数全部重写，应该速度比之前的快些；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Rd文档格式的支持更加完整，但目前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\tabular&lt;/code&gt;标签的排版还是个很麻烦的问题；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;新增包选择功能，可以从下拉框中选择软件包，包中的函数会自动列出；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;新增过滤器功能，在输入框中键入字符串后按回车，函数列表会根据键入的内容进行匹配，相当于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apropos()&lt;/code&gt;。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;测试环境是Windows XP 32位及RGtk2 2.20.3版本，其它环境尚未测试过。&lt;/p&gt;

&lt;p&gt;示例：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;library(RGtk2);
Rhelper(&quot;help&quot;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;截图：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/11/rhelper-update.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/11/rhelper-update.png&quot; alt=&quot;Rhelper-RGtk2&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;下载：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Rhelper_RGtk2_20101117_uft8.txt&quot;&gt;Rhelper-RGtk2 2010.11.17更新&lt;/a&gt;（注：必须在UTF-8编码下打开）&lt;/p&gt;

&lt;p&gt;目前程序还不是特别稳定，暂时仍处于测试状态。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/11/rhelper-rgtk2-updated/</link>
    <guid>https://yixuan.blog/cn/2010/11/rhelper-rgtk2-updated</guid>
    <pubDate>2010-11-17T19:53:45+00:00</pubDate>
  </item>

  <item>
    <title>专业精神</title>
    <description>&lt;p&gt;专业精神，好大的一个题目，准确的定义是什么？我不知道。不过今天遇到的一件小事，不妨拿出来说说。&lt;/p&gt;

&lt;p&gt;现在住的寝室是七个人一个套间，可是楼下信箱的钥匙只有一把。今天没别的事，便准备去配钥匙。便民市场一进门就是一家五金店，开店的是位老太太，我拿钥匙给她，她微微摇了摇头，说这种小钥匙特别难配。十来分钟之后，总共配了六把，我交完钱准备走，老太太把我叫住，说是有问题就再回来找她。&lt;/p&gt;

&lt;p&gt;回到宿舍，我依次试了试，结果都开不了。没办法，只好回头再请老太太看看。到了店里，老太太一听情况也不多说，便拿了把锉刀在钥匙上磨了一阵。一会儿工夫，老太太把磨好的钥匙交给我，让我再试试。&lt;/p&gt;

&lt;p&gt;回来之后我又依次试了一遍，结果非常令人失望，依旧全都打不开。这时候我开始有点怀疑她的技术了，可是也没办法，只好再跑一趟。
第三次走进店里，老太太一听就急了，赶忙又拿锉刀磨了磨，一边问我住哪儿，远不远。我说就住边上，走五分钟就到。她点了点头，跟我说：“带我过去看看吧，我去那儿跟你弄。”我本来不想再继续折腾，配不了也就算了，而且看她年纪也大，走路多少有些不方便，就出言婉拒了。可是她却不同意，坚持要过去看看，一边拿起工具一边说着，“保证给你配好”。
我和她走出门的时候，门边一个修车的师傅半开玩笑半“幸灾乐祸”地冲着老太太说道，“开不了！开不了！”边上一起聊天的几个人也一并附和着。我猜那个修车师傅已经知道，老太太一出门就意味着她配的钥匙打不开锁，要亲自去看看了。而这显然已经不是第一次。&lt;/p&gt;

&lt;p&gt;到了我宿舍楼底下，老太太依次扳动钥匙，然后拿锉刀去磨，不一会儿有三把钥匙已经可以开了，但剩下的三把却一直没有办法。又过了一阵，他突然转向我，笑着说道：“我知道要怎么弄了，你今天找个时间到我那儿去取剩下的三把，我晚上七点才走。”我想着已经有四把钥匙，大家可以共用，就跟她解释说不必麻烦，已经足够了。可她还是那句话：“差一点都不行，我保证给你配好。”&lt;/p&gt;

&lt;p&gt;晚上去吃饭的时候，顺便去老太太那儿取钥匙，她满脸红光地说：“这次保证没有问题了！”。我回来一试，果然都行了。跟之前的几把对比了一下，发现唯一的不同是新的三把在钥匙背上刻了一道很浅的凹痕。&lt;/p&gt;

&lt;p&gt;之前还有一段小插曲。跟她过来的路上，我看她腿脚挺灵活的，便问她多大年纪了，她说今年七十一了。我又问她，是不是一直待在人大，做配钥匙的活儿做了多久了，她微微点头，笑着说：“四十年”。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/11/professional-spirit/</link>
    <guid>https://yixuan.blog/cn/2010/11/professional-spirit</guid>
    <pubDate>2010-11-06T20:38:04+00:00</pubDate>
  </item>

  <item>
    <title>小心！</title>
    <description>&lt;p&gt;客官请听题：话说有两个随机变量，\(X\sim N(\mu_1,\sigma_1^2),Y\sim N(\mu_2,\sigma_2^2)\)，而且这两个正态随机变量的相关系数是 \(\rho\)，问，\((X, Y)\) 的联合分布是什么？&lt;/p&gt;

&lt;p&gt;如果你给出的答案是&lt;/p&gt;

\[\left(\begin{array}{c}X\\Y\end{array}\right)\sim N\left(\left(\begin{array}{c}\mu_{1}\\\mu_{2}\end{array}\right),\left(\begin{array}{cc}\sigma_{1}^{2}&amp;amp;\rho\sigma_{1}\sigma_{2}\\\rho\sigma_{2}\sigma_{1}&amp;amp;\sigma_{2}^{2}\end{array}\right)\right)\]

&lt;p&gt;那么请打开你的R，然后运行一下下面的代码：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;set.seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ifelse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shapiro.test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hist&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;什么？还没缓过神来？请看仔细了，&lt;strong&gt;y和z的符号可是相同的&lt;/strong&gt;！那么这说明什么了呢……&lt;/p&gt;

&lt;p&gt;这是今天复习高等统计学的时候看到书上写的，看完之后突然感觉自己的IQ和EQ都受到了严重的摧残，后果基本上和&lt;a href=&quot;http://yixuan.github.com/cn/2009/10/probability-played-a-trick-on-me/&quot;&gt;这个时候&lt;/a&gt;差不多。难道我每隔一年就要被这样打击一次？&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/10/be-careful/</link>
    <guid>https://yixuan.blog/cn/2010/10/be-careful</guid>
    <pubDate>2010-10-20T18:32:47+00:00</pubDate>
  </item>

  <item>
    <title>吟枫舞墨</title>
    <description>&lt;p&gt;曾经买过一本英语阅读的小册子，像我买过的所有英语读物一样，看完不到一半，就被我随手搁在了书架上，过着暗无天日的日子。而这本小册子略微有些不同，有时我心情烦闷，便会取下小册子翻一翻，但有些讽刺的是，我所看的不是其中的英文美文，而是书中每一小节前的一段中文小诗。&lt;/p&gt;

&lt;p&gt;书中的第一首诗我印象最深刻，反复吟诵，却也记了下来：&lt;/p&gt;

&lt;p style=&quot;text-align: center;&quot;&gt;或许是一盏清亮的灯&lt;br /&gt;
或许是竹杖芒鞋醉春风&lt;br /&gt;
或许是宝马雕车&lt;br /&gt;
载起凌云的心情&lt;br /&gt;
红尘猎猎&lt;br /&gt;
岁月无边&lt;br /&gt;
要打点起所有美景&lt;br /&gt;
装饰这段华年&lt;br /&gt;
可以水波潋滟&lt;br /&gt;
可以大风回旋&lt;br /&gt;
只为快乐地走在自己前面&lt;br /&gt;
快乐地站在下一个季节前&lt;br /&gt;
左手花枝&lt;br /&gt;
右手月圆&lt;/p&gt;

&lt;p&gt;秋风拂面，总是引人无数遐想，心中之斑斓，一如枫之翩舞，一如花之馨菲，一如月之皎白。可惜秋来秋往，胜景终究不能长在，惟有指间毫锥，墨色如一。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/10/calligraphy.jpg&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/10/calligraphy.jpg&quot; alt=&quot;竹里馆&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/10/maple-singing-ink-waving/</link>
    <guid>https://yixuan.blog/cn/2010/10/maple-singing-ink-waving</guid>
    <pubDate>2010-10-07T21:41:29+00:00</pubDate>
  </item>

  <item>
    <title>Linux下支持自动完成的R编辑器——SciTE</title>
    <description>&lt;p&gt;之前谢老大写了一篇&lt;a href=&quot;http://yihui.name/en/2010/08/auto-completion-in-notepad-for-r-script/&quot;&gt;在Notepad++中配置R函数自动完成的文章&lt;/a&gt;，甚好甚强大，不过可惜的是Notepad++只能在Windows下用，没有Linux的版本。当然了，Linux下的编辑器也不少，拿Ubuntu来说，简单的有Gedit，高级的有Kate，神级的有Emacs、Vim等等，但本文介绍的SciTE绝对是一款甚小甚强大的编辑器软件。&lt;/p&gt;

&lt;p&gt;Ubuntu下安装的方法就不用说了，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt-get install scite&lt;/code&gt; 就可以搞定。软件非常小，但其功能绝对不容小觑，只要你有耐心设置好配置文件，几乎任何一种语言都可以实现语法高亮、代码折叠和自动完成等功能。下面是编辑R脚本时的截图。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/09/scite-1.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/09/scite-1.png&quot; alt=&quot;SciTE自动完成&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/09/scite-2.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/09/scite-2.png&quot; alt=&quot;SciTE函数提示&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;要实现上面的效果，需要一些额外的配置文件，可以到下面下载。&lt;/p&gt;

&lt;p&gt;下载：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/SciTE_config.zip&quot;&gt;SciTE配置文件&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;配置方法：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;用管理员权限打开/usr/share/scite/SciTEGlobal.properties（具体的位置视系统而定），搜索其中的“import r”语句，将其前面的井号去掉；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;将附件中的r.api用管理员权限复制到/usr/share/scite中；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;将附件中的.SciTEUser.properties复制用户目录下，即/home/&lt;用户名&gt;。&lt;/用户名&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;一些主要的配置选项都放在.SciTEUser.properties中，其中大都加了注释，可以按需要进行修改。修改时可以参照&lt;a href=&quot;http://www.scintilla.org/SciTEDoc.html&quot;&gt;SciTE的官方文档&lt;/a&gt;获得完整的说明，没错，那里有上百个选项可以进行自定义，只要你有足够的耐心……&lt;/p&gt;

&lt;p&gt;事实上，SciTE是一个跨平台的编辑器，其配置文件也具有良好的兼容性，只需对配置文件做些许修改，在Windows下同样可以完成相同的功能。这里有个Windows版的SciTE，体积是小到令人发指的600K，要使用自动完成功能的话，需要把SciTEGlobal.properties的最后一行改成正确的路径。&lt;/p&gt;

&lt;p&gt;下载：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/SciTE_win.zip&quot;&gt;Windows版SciTE&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/09/r-script-editor-supporting-autocompletion-in-linux-scite/</link>
    <guid>https://yixuan.blog/cn/2010/09/r-script-editor-supporting-autocompletion-in-linux-scite</guid>
    <pubDate>2010-09-19T11:06:01+00:00</pubDate>
  </item>

  <item>
    <title>R的原生并行2——修改BLAS</title>
    <description>&lt;p&gt;BLAS的全称是Basic Linear Algebra Subprograms，它是用FORTRAN语言编写的一套跟线性代数运算有关的函数，包括向量与向量的运算、向量与矩阵的运算以及矩阵与矩阵的运算三个层次。R的很多底层运算都是基于BLAS来完成的，比如常见的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%*%&lt;/code&gt;，用来完成矩阵的乘法。而要让R支持原生的并行计算，一个很重要的方面就是让BLAS本身可以通过并行计算来完成。幸运的是，BLAS中很多运算都是比较规整的循环语句，而这些语句恰恰可以通过OpenMP来实现并行。由于BLAS中的函数非常多，在此为了简便起见，我只修改了其中的dgemm.f文件，该文件主要是实现矩阵乘法运算。下面是我用修改后的BLAS源代码编译出的Rblas.dll文件，用它替换R的bin目录下的Rblas.dll（同时需要把压缩包中其它两个dll文件也复制到bin目录下），就可以对R的并行矩阵运算进行测试（注：程序的有效性可能会受CPU型号和架构的影响）。当然了，说是说修改源代码，其实就是参照&lt;a href=&quot;http://rajorshi.net/blog/2009/05/programming-for-multicore-introduction-openmp-gcc/&quot;&gt;这篇文章&lt;/a&gt;对一些循环语句进行了并行化的声明。&lt;/p&gt;

&lt;p&gt;下载：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Rblas_OpenMP.zip&quot;&gt;Rblas.dll&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其中dgemm.f是修改后的程序，完整的BLAS源代码可以到&lt;a href=&quot;http://www.netlib.org/blas/blas.tgz&quot;&gt;这里&lt;/a&gt;下载。&lt;/p&gt;

&lt;p&gt;下面是我电脑上的一些测试。
测试环境：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;操作系统——32位 Windows XP Home Edition SP3&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;CPU——Intel Core Duo T2300 1.66GHz（双核）&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;内存——1.00GB DDR2&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;测试程序：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set.seed(123);
a = matrix(rnorm(4000000), 2000);
system.time(a %*% a);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;测试结果：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;用R自带的Rblas.dll：&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; system.time(a %*% a);
  用户  系统   流逝
25.97  0.05 26.05
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;用附件中的Rblas.dll：&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; system.time(a %*% a);
  用户  系统   流逝
32.76  0.06 16.85
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;用&lt;a href=&quot;http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/ATLAS/&quot;&gt;ATLAS优化的Rblas.dll&lt;/a&gt;：&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; system.time(a %*% a);
  用户  系统   流逝
11.46  0.01 11.50
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中第一项和第三项测试CPU都只能使用单核。&lt;/p&gt;

&lt;p&gt;从结果可以看出，并行计算的时间几乎比一般的串行计算少了一半，但因为程序没有经过优化，所以还不如ATLAS优化过的BLAS。但是，如果CPU核的数量很多，ATLAS优化的BLAS依然只能使用一个核，性能将不会有明显的提升，但并行的BLAS则可以进一步提高性能。&lt;/p&gt;

&lt;p&gt;如果要进一步实现R的并行计算功能，我觉得有两方面的工作要做，一是将BLAS中的其它函数都进行相应的修改，而是将并行与ATLAS优化结合起来，进一步提高运算效率。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/09/the-road-to-paralleled-r-2-modify-blas/</link>
    <guid>https://yixuan.blog/cn/2010/09/the-road-to-paralleled-r-2-modify-blas</guid>
    <pubDate>2010-09-18T13:32:21+00:00</pubDate>
  </item>

  <item>
    <title>R的原生并行——一个尝试</title>
    <description>&lt;p&gt;一直以来，R的并行计算都是一件令人头疼的问题。尽管有各种各样的包可以辅助地实现并行计算，但R本身依然只能使用一个核，这对于逐渐普及的多核CPU来说确实是一种资源的浪费。而对于我们普通用户来说，并行计算的编程和实现似乎是一件遥不可及的事，但昨天偶然在图书馆翻到的一本书却迅速改变了我的这种观点。下面先把图和真相贴出来。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/09/serial.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/09/serial.png&quot; alt=&quot;串行计算&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/09/parallel.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/09/parallel.png&quot; alt=&quot;并行计算&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;上面两幅图展示了同一个函数（矩阵乘法）的两种实现途径，第一种是普通的计算，从图中可以看出，只有一个CPU核处在完全使用状态，总的CPU利用率是50%；而第二种方法采用了并行计算，两个CPU核都是100%的使用率。从时间上来看，第二种方法几乎为第一种的一半，与CPU的利用率相吻合。&lt;/p&gt;

&lt;p&gt;可能你会觉得，第二个程序会比第一个程序复杂很多，但实际上，在它们的C程序源代码中，两段程序只有&lt;strong&gt;一行&lt;/strong&gt;是不同的！程序的C源代码、编译出的dll文件以及R源代码可以在&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/test_parallel.zip&quot;&gt;这里&lt;/a&gt;下载得到，其主要原理就是使用了OpenMP这个支持并行计算的库。一个简单但很有启发性的介绍可以看&lt;a href=&quot;http://rajorshi.net/blog/2009/05/programming-for-multicore-introduction-openmp-gcc/&quot;&gt;这里&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;事实上，R的很多代数运算都是基于BLAS系统的，而从网上的一些介绍来看，它也有支持并行计算的版本，如果可以把它直接链接到R中，那就有好戏看了……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/09/the-road-to-paralleled-r-1-a-first-trial/</link>
    <guid>https://yixuan.blog/cn/2010/09/the-road-to-paralleled-r-1-a-first-trial</guid>
    <pubDate>2010-09-17T00:23:34+00:00</pubDate>
  </item>

  <item>
    <title>Rhelper-RGtk2版本</title>
    <description>&lt;p&gt;寒假的时候用wxWidgets程序库写过一个&lt;a href=&quot;http://localhost:4000/2010/01/please-help-me-test-a-program-rhelper/&quot;&gt;Rhelper小程序&lt;/a&gt;，初衷是在chm类型的帮助文档被取消后，能提供一个类似的查看帮助的环境。当时编的那个程序基本实现了这一目标，但它有两个比较大的缺点：一是程序与R相脱离，调用起来不方便；二是程序只能在Windows下运行，对操作系统有依赖。由于当时能力和时间有限，所以也就没再多加考虑。&lt;/p&gt;

&lt;p&gt;之后，从谢老大那里得知了有gWidgets包的存在，顿时兴奋不已，因为这个包可以用简单的语句生成一系列的图形界面，而且不依赖于操作系统，甚至不依赖于图形界面库（GTK+，Java，tcl/tk等）。谢老大也以这个包为主题在&lt;a href=&quot;http://cos.name/2010/06/3rd-china-r-beijing-summary/&quot;&gt;第三届中国R语言会议&lt;/a&gt;上做了&lt;a href=&quot;http://yihui.name/cn/wp-content/uploads/2010/06/3rd-ChinaR-gWidgets-Yihui-Xie.pdf&quot;&gt;报告&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;当然了，越是方便的工具，其功能也越容易受到限制。就Rhelper来说，有两个问题必须要得到解决，一是要实现文档格式化（比如以HTML的形式展现），二是要实现超链接跳转。这两个要求在wxWidgets中都有现成的程序库可以解决，可是在gWidgets中都很难实现。&lt;a href=&quot;http://www.omegahat.org/&quot;&gt;Omegahat&lt;/a&gt;曾经开发了一个&lt;a href=&quot;http://www.omegahat.org/RwxWidgets/&quot;&gt;RwxWidgets包&lt;/a&gt;，用来在R中调用wxWidgets，可是这个包很久没有更新，而且似乎在Windows下很不稳定。最后综合各方面的因素，决定用比较底层的RGtk2包来实现Rhelper的功能。&lt;/p&gt;

&lt;p&gt;由于RGtk2包没有直接解析HTML的控件，所以文档的格式化成了程序中最大的一个难题。目前我是直接从Rd文档中获取文字和相应的标签，然后对不同的标签应用不同的格式。下面是两张效果图，分别是Windows下和Ubuntu下的运行界面：&lt;/p&gt;

&lt;p style=&quot;text-align: center;&quot;&gt;Windows下运行界面&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/08/rhelper-windows.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/08/rhelper-windows.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p style=&quot;text-align: center;&quot;&gt;Ubuntu下运行界面&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/08/rhelper-ubuntu.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/08/rhelper-ubuntu.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其中蓝色的链接都是可以点击的（光标不会变成手形），不过目前Rd文档的解析仍然是个大工程，有很多函数的文档会出现格式混乱的现象，这个还需要不断修补。总之这个程序还只是一个初步的框架，很多细节仍有待完善。&lt;/p&gt;

&lt;p&gt;附件：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Rhelper-RGtk2_dev.txt&quot;&gt;程序代码下载&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;用的时候输入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rhelper(&quot;&amp;amp;&amp;amp;&quot;)&lt;/code&gt; 之类的就可以了。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/08/rhelper-rgtk2/</link>
    <guid>https://yixuan.blog/cn/2010/08/rhelper-rgtk2</guid>
    <pubDate>2010-08-09T08:49:13+00:00</pubDate>
  </item>

  <item>
    <title>再遇挫折</title>
    <description>&lt;p&gt;离会议只有两三天，又出现了诸多突发状况，实在是让人心神不宁啊。之前宫雨老师已经说不能出席了，今天又接到王剑师兄和一硕的消息，说是会议当天也来不了了，于是三场精彩的演讲就这样遗憾地无法展现在观众面前。文化衫的图案删了改，改了删，想要满足所有人的口味也是难上加难。观众带着期待而来，会不会留下遗憾而去？不敢多想。又一次遇到挫折，又一次不淡定了。或许我得想些办法调整一下心情了。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/06/setback-again/</link>
    <guid>https://yixuan.blog/cn/2010/06/setback-again</guid>
    <pubDate>2010-06-12T00:34:49+00:00</pubDate>
  </item>

  <item>
    <title>用R处理照片打造铅笔画效果</title>
    <description>&lt;p&gt;之前看过一篇文章，介绍的是如何在GIMP中对照片进行处理，产生一种铅笔画的效果。原文在&lt;a href=&quot;http://the-double-0-project.net/blog/?p=58&quot;&gt;这里&lt;/a&gt;，步骤主要分四步：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;将照片去色；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;复制图层，将图层反色；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;对上层图层进行高斯模糊；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;将上层图层设置为减淡属性；&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;之前写了一篇&lt;a href=&quot;http://yixuan.github.com/cn/2010/02/blur-and-smoothing/&quot;&gt;跟高斯模糊有关的文章&lt;/a&gt;，里面的代码可以直接用，于是简单改了改，就可以实现上面的铅笔画效果了。代码如下：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;blur&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernelf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;grid should be great than or equal to 3!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%%&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;grid should be odd number!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sapply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernelf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newx&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nrow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colSums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ncol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colSums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernel.gauss&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blur.gauss&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernel.gauss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                                            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rimage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read.jpeg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;demo.jpg&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rgb2grey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blur.gauss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pencil&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# For R before 2.11.0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imagematrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pencil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# For R 2.11.0 and after, may be faster&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pencil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pencil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mar&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlim&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylim&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;n&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asp&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
     &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;axes&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlab&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ylab&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rasterImage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pencil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Use biOps package to write JPEG&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;biOps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeJpeg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;output.jpg&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imagedata&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pencil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;示例图片如下（点击看大图）:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/05/sketch-raw.jpg&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/05/sketch-raw.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;效果图如下（点击看大图）：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/05/sketch.jpg&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/05/sketch.jpg&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;biOps包到&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/biOps.zip&quot;&gt;这里&lt;/a&gt;下载（包含了依赖文件）。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/05/processing-pictures-with-a-pencil-sketch-effect-using-r/</link>
    <guid>https://yixuan.blog/cn/2010/05/processing-pictures-with-a-pencil-sketch-effect-using-r</guid>
    <pubDate>2010-05-17T13:32:53+00:00</pubDate>
  </item>

  <item>
    <title>硬盘安装Ubuntu 10.04</title>
    <description>&lt;p&gt;整个四月一篇博客也没写，今天来充个数好了，内容非原创，只不过是把Ubuntu硬盘安装的方式记录一下，免得到时候又上网到处找。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;第一步，下载GRUB4DOS，把其中的grub.exe和grldr解压到C盘根目录。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;第二步，创建menu.lst，内容如下：&lt;/p&gt;

    &lt;pre&gt;title Install Ubuntu
root (hd0,1)
kernel (hd0,1)/vmlinuz boot=casper iso-scan/filename=/ubuntu-10.04-desktop-i386.iso ro quiet
splash locale=zh_CN.UTF-8
initrd (hd0,1)/initrd.lz&lt;/pre&gt;
    &lt;p&gt;理论上Windows的C盘应该是(hd0,0)的，但不知为何我这儿成了(hd0,1)。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;第三步，修改Windows启动项，加上语句&lt;/p&gt;

    &lt;pre&gt;C:\grldr=&quot;Install Ubuntu&quot;&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;第四步，把光盘镜像复制到C盘根目录，并解压其中的vmlinuz和initrd.lz。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;第五步，重启，进入Ubuntu光盘。其中要注意，安装之前需要在终端执行&lt;/p&gt;

    &lt;pre&gt;sudo umount -l /isodevice&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;我下的这个版本，安装完Ubuntu后Windows XP的启动菜单会消失，这时需要在终端下执行&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo update-grub
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;但据说官方会修复这个问题，把光盘重新打包。&lt;/p&gt;

&lt;p&gt;另：关闭按钮放在左边确实有些别扭……&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/04/ubuntu-recursion.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/04/ubuntu-recursion.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/04/install-ubuntu-10-04-from-hard-disk/</link>
    <guid>https://yixuan.blog/cn/2010/04/install-ubuntu-10-04-from-hard-disk</guid>
    <pubDate>2010-04-30T18:24:11+00:00</pubDate>
  </item>

  <item>
    <title>Processing R，你看见了什么？</title>
    <description>&lt;p&gt;标题里的Processing不是一个动名词，而是一个纯粹的名词。很早以前谢老大就曾&lt;a href=&quot;http://cos.name/cn/topic/15015&quot;&gt;在COS论坛中推荐过Processing&lt;/a&gt;，特别是在“统计图形与数据可视化兴趣小组”成立之后。&lt;/p&gt;

&lt;p&gt;简单来说，Processing（主页&lt;a href=&quot;http://processing.org&quot;&gt;http://processing.org&lt;/a&gt;）是一门定位于数据可视化的编程语言，它是用Java编写的，本身也可以跟Java互通，但Processing专注于图形方面的开发，所以在图形的绘制上（2D及3D）Processing具有更加简洁的语法，画出来的图也很漂亮。&lt;/p&gt;

&lt;p&gt;用Processing编好的程序，会被编译成Java中的类，同时还可以根据需要生成相应的可执行文件，在具有Java环境的系统中运行。因为是基于Java，所以Processing也是跨平台的（同时也是开源的）。&lt;/p&gt;

&lt;p&gt;可以先简单看看下面的这张截图，演示的是洛仑兹混沌理论的“蝴蝶效应”，动态运行的程序和源代码放在附件的Lorenz文件夹中（文章最后），运行需要Java环境支持，如果没有的话可以到&lt;a href=&quot;http://www.java.com/&quot;&gt;http://www.java.com/&lt;/a&gt;下载。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/03/processing-butterfly.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/03/processing-butterfly.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Processing确实很艺术，不过对于数据的理解，光靠画图还是不够的。老大之前说过，就目前而言，Processing基本上跟统计没什么关系，因此它的作用更像是画图而不是数据可视化。如果想要结合统计分析与图形表达，一个很自然的想法就是让Processing这样的绘图工具与R这样的统计计算软件结合起来。R也有很强大的绘图功能，但是有一个小问题就是，对于动画这种表现形式，在R中会受到一些制约，这主要体现在R加载图形时会有一小段时间的缓冲，因此每一秒钟加载图形的数量不能太多，这使得图形的运动不够平滑，而且也较难表现高速的变化。Processing正好可以弥补这点不足，它默认的帧速是60帧/秒，这对于一般的视觉体验来说是足够了。&lt;/p&gt;

&lt;p&gt;幸运的是，Processing与Java的结合非常紧密，而R与Java的沟通又可以通过rJava包来实现，所以在Processing中使用R的函数也就成为了可能。下面再给一个演示布朗运动的例子，程序和源代码放在附件的Brown_trace文件夹中。以下两张图是两次布朗运动的“轨迹”实现，其中需要用到的随机数都是通过R来生成的。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/03/brown-1.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/03/brown-1.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/03/brown-2.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/03/brown-2.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;（大家来找茬：通过以上两幅图，你能找出两次“轨迹”实现中&lt;strong&gt;共同的起点&lt;/strong&gt;吗？）&lt;/p&gt;

&lt;p&gt;要在Processing中调用R的函数，需要做以下几件事：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;把R的bin目录放到系统PATH中；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;在R中装好rJava包；&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;rJava包下面有一个jri文件夹，把这个文件夹的路径也放到系统PATH中。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;在购建Processing的项目时，点击Sketch-&amp;gt;Add File…添加之前jri文件夹中的JRI.jar文件，然后参照Brown_trace的例子写一些导入库的语句以及TextConsole类的声明。后面就可以通过Rengine和REXP等定义好的类型去调用R的函数，这一部分可以参照jri文件夹下的examples。&lt;/p&gt;

&lt;p&gt;附件：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Processing.zip&quot;&gt;点击这里下载&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;说明：每个例子都包含三个文件夹，格式是application.*，代表了不同的操作系统。再里面有一个source文件夹，其中.pde文件是Processing的源文件，.java文件是将.pde文件转换成的java源文件。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/03/processing-and-r/</link>
    <guid>https://yixuan.blog/cn/2010/03/processing-and-r</guid>
    <pubDate>2010-03-26T15:25:51+00:00</pubDate>
  </item>

  <item>
    <title>广告时间</title>
    <description>&lt;p&gt;又有两位重量级COS老大入驻cos.name，大家速速前去围观！&lt;/p&gt;

&lt;p&gt;一硕：&lt;a href=&quot;http://yishuo.cos.name&quot;&gt;My Blog: Statistics, Literature, And Fantasy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;冰大：&lt;a href=&quot;http://bing.cos.name/&quot;&gt;青蓝冰水&lt;/a&gt;&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/03/advertisement-time/</link>
    <guid>https://yixuan.blog/cn/2010/03/advertisement-time</guid>
    <pubDate>2010-03-22T18:12:13+00:00</pubDate>
  </item>

  <item>
    <title>Copula，奇妙并困惑着</title>
    <description>&lt;p&gt;毕业论文想写一些跟 Copula 有关的东西。之前在寒假的时候，已经阅读了一些文献，算是大概弄明白了 Copula 是什么。简单来说，它可以看作是一种连接函数，即把随机变量的联合分布与其各自的边缘分布连接起来。用一个简单的式子表达，就是 \(F_{XY}(x,y)=C(F_X(x),F_Y(y))\)，其中的 \(C(\cdot,\cdot)\) 就是传说中的 Copula。&lt;/p&gt;

&lt;p&gt;进一步来说，Copula 有一个更有意思的含义。我们知道，如果 \(X\sim F_X(x)\)，那么 \(U=F_X(X)\sim U(0,1)\)，即将一个随机变量的分布函数作用到这个随机变量上时，它将变成一个 \((0,1)\) 上的均匀分布。类似地，也有 \(V=F_Y(Y)\sim U(0,1)\)，于是乎，&lt;/p&gt;

\[\begin{array}{ll}P(U\le u, V\le v)&amp;amp;=P(F_X(X)\le u, F_Y(Y)\le v)=P(X\le F_X^{-1}(u),Y\le F_Y^{-1}(v))\\&amp;amp;=F_{XY}(F_X^{-1}(u),F_Y^{-1}(v))=C(u,v)\end{array}\]

&lt;p&gt;这就说明，式中的 Copula 函数正是U和V的联合分布。在这个意义上，两个随机变量的分布函数可以被巧妙地拆成三个部分：两个边缘分布，以及另外一个联合分布。这条性质使得分布的建模更具灵活性。在很久很久以前，我们为了描述 \((X,Y)\) 的联合分布，一般都只用一个单一的分布族，而且这个分布族的结构也不会太复杂，否则写出来的公式会让人难以忍受。此外，限定了联合分布就相当于隐性地限定了边缘分布，这有时候不符合实际。而用 Copula 的分解公式，我们就可以分别指定边缘分布和联合结构的形式，使得模型的解释力更强。此奇妙之一也。&lt;/p&gt;

&lt;p&gt;关于 Copula 函数，有一个重要的不等式，即对任意的一个 Copula 函数 \(C(\cdot,\cdot)\) 以及任意满足定义域的实数 \(u\) 和 \(v\)，有&lt;/p&gt;

\[\max\{u+v-1,0\}\le C(u,v)\le \min\{u,v\}\]

&lt;p&gt;特别地，不等式的左右两端也是两个 Copula 函数，分别称为下界 Copula 和上界 Copula。当两个随机变量的 Copula 函数达到上界时，这两个随机变量将是完全的“正相关”；反之，达到下界时将是完全的“负相关”。在 Copula 中，完全“正相关”是这样定义的：假设有一个随机向量 \((X, Y)\)，\((x1, y1)\) 和 \((x2, y2)\) 是它的两个实现值，那么 \((x1 - x2)\) 与 \((y1 - y2)\) 符号相同的概率为1；类似地，完全“负相关”就是它们符号相反的概率为1。任何一个 Copula 函数，都一致地被上界 Copula 和下界 Copula 所包围，所以 Copula 函数是对随机变量之间相关性或依赖性的一种度量。此奇妙之二也。&lt;/p&gt;

&lt;p&gt;如果上面的这些都不够让你兴奋的话，那么可以看看下面这条性质。这一条说的是，如果随机向量 \((X, Y)\) 的 Copula 函数是 \(C(u, v)\) 的话，那么对于两个单调不减的函数 \(f\) 和 \(g\)，随机向量 \((f(X), g(Y))\) 的 Copula 函数将依然是 \(C(u, v)\)！这一点很容易让人联想到 Spearman 秩相关系数，它对于这种单调变换也可以保持相关系数不变，但 Spearman 秩相关系数只是利用了数据秩的信息，而 Copula 则是在分布的级别上保持变量依存关系的不变性。此奇妙之三也。&lt;/p&gt;

&lt;p&gt;好了，奇妙说到这里，就谈谈我的一些困惑了。简单说来是四个字：干嘛用的？由于我才疏学浅，我总感觉使用 Copula 的主要目的依然是对分布进行估计和拟合，那么它相对于传统的一些方法而言，优势在哪儿呢？如果说灵活性是其最大的优势，那我不仅要说，传统的估计方法只是对于一个分布有模型误设的风险，现在凭空多出的两个待估计分布，其增加的风险会不会大于灵活性所减少的风险呢？再进一步说，如果采用的是非参数的密度估计方法，那么传统方法与 Copula 方法都没有模型误设的问题（我们一般认为非参密度估计没有模型误差，只有估计误差），但 Copula 方法多出了两个待估计的分布，给人的感觉就是误差一定会比直接估计的要大，这是不是说非参数密度估计在 Copula 中就完全不适用了呢？可是实际问题中参数方法总是有很大的局限，特别是样本量很大时非参方法明显具有很大的优势，如果这样就被 Copula 拒之门外是不是又太可惜了呢？不解啊，不解。&lt;/p&gt;

&lt;p&gt;路漫漫其修远兮，吾将上下而求索……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/03/copula-marvelous-and-confusing/</link>
    <guid>https://yixuan.blog/cn/2010/03/copula-marvelous-and-confusing</guid>
    <pubDate>2010-03-18T18:36:01+00:00</pubDate>
  </item>

  <item>
    <title>天啊，真开了？！</title>
    <description>&lt;p&gt;以前在博客里写过几句话，说是我们其实每天都在享受开源作者的劳动果实，比如“正宗宫保鸡丁”的配料。结果今天看到一则新闻，倒是把这个不靠谱的论断给证实了，&lt;a href=&quot;http://www.networkworld.com/community/node/57846&quot;&gt;真相在此&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;不过还是那句话啊，给我源码也没用啊，谁叫我没有编译器呢……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/02/oh-it-really-opens/</link>
    <guid>https://yixuan.blog/cn/2010/02/oh-it-really-opens</guid>
    <pubDate>2010-02-25T14:00:33+00:00</pubDate>
  </item>

  <item>
    <title>模糊和平滑</title>
    <description>&lt;p&gt;用过Photoshop或者GIMP的朋友应该都对高斯模糊这个工具比较熟悉。我虽然对作图不怎么了解，但隐约感觉投影效果中的阴影就是用高斯模糊做成的。下面是一个简单的高斯模糊的示例（&lt;a href=&quot;http://www.zcool.com.cn/gfx/ZNzMyNjQ=.html&quot;&gt;图片来源&lt;/a&gt;），还有一个真相请看&lt;a href=&quot;http://yihui.name/cn/2010/02/a-big-thank-you-to-linlin-yan/&quot;&gt;这里&lt;/a&gt;：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/02/blur.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/02/blur.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其中右半部分应用了高斯模糊的效果，看起来就像是在画面上盖了一层毛玻璃。那么这种效果是怎么实现的呢？其实啊，看到高斯这个名号，就不由地让人警觉起来——他的渗透力实在太大了。&lt;/p&gt;

&lt;p&gt;在网上找了几篇介绍高斯模糊的文章——&lt;a href=&quot;http://zh.wikipedia.org/wiki/%E9%AB%98%E6%96%AF%E6%A8%A1%E7%B3%8A&quot;&gt;维基&lt;/a&gt;、&lt;a href=&quot;http://www.cnblogs.com/hoodlum1980/archive/2008/03/03/1088567.html&quot;&gt;博客一&lt;/a&gt;、&lt;a href=&quot;http://blog.sina.com.cn/s/blog_4b97ab670100aa3a.html&quot;&gt;博客二&lt;/a&gt;，看着里面的几个公式那叫一个亲切啊，虽然世间“高斯”的含义有很多，但此处的高斯自然就是统计中最熟悉不过的正态分布了。维基中对高斯模糊的解释挺吓人，“图像的高斯模糊过程就是图像与正态分布做卷积”，像我这种数学根基不好的看到这里便肃然起敬，只好通过后面博客中的例子来理解了。&lt;/p&gt;

&lt;p&gt;看来看去，终于看到了一些熟悉的东西，原来高斯模糊和平滑技术中的核函数回归是一回事。先看下面这两张图（从&lt;em&gt;&lt;a href=&quot;http://www-stat.stanford.edu/~hastie/local.ftp/Springer/ESLII_print3.pdf&quot;&gt;The Elements of Statistical Learning&lt;/a&gt;&lt;/em&gt;中截的）：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/02/smoothing.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/02/smoothing.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;其中散点图代表了y与x之间的对应关系。可以看出，随着x的增大，y的取值会上下起伏，变动比较大；而图中的蓝线非常光滑，同时又体现了散点图的大致走向，因此可以认为蓝线是这堆散点的一种规律性描述。事实上，图中的散点就是在蓝线的基础上加以随机误差而生成的。在大多数的情况下，我们能够获得的信息就是图中的散点，而蓝线则是我们想要得到的规律，于是我们面临的问题就是用这些已知的数据去估计未知的结构。图中的绿线做的就是这件事，左图用的是最近邻法，右图是用的是Epanechnikov核函数法。具体的计算细节在此不多加讨论，但总结起来它们都做了同样一件事：为了估计某个点x0处y的取值y0，先在x0处开一扇窗子，让某些点进入窗子，其它的排除在外；这样一来，窗子内的每个点xi都有其对应的y的取值yi，然后用某种权重将这些yi组合起来，作为y0的估计。举个例子来说，如果要估计x=0.5时y的取值，先构造一扇窗子(0,1)，那么只有x取值在这个范围内的点才能进入；假定进入窗子的点有(0.2, 1)(0.4, 0.5)(0.8, 0.2)三个，其权重假定为0.2、0.6和0.2，那么x=0.5时y取值的估计就是0.2&lt;em&gt;1+0.6&lt;/em&gt;0.5+0.2*0.2=0.54。在实际计算的时候，窗子的宽度怎么定，窗内点的权重怎么定，是主要要讨论的内容。上面那幅图左边的最近邻法比较简单，就是对离x0最近的k个点取算术平均，而右边的则相对复杂些，如果感兴趣的话可以看看那本书的第193页。&lt;/p&gt;

&lt;p&gt;回到正题，在高斯平滑方法中，也基本上是遵循了这条套路，但它的窗子取的是整个实数集，也就是说，图中的所有点都将参与运算，这样一来就只剩下如何确定权重这个问题了。关于这一点，我们应该会有一种直观的感觉，那就是，离我越近的点，我就应该越相信它，因此这个权重很自然地就会取成是两点横坐标之差的函数，而且它应该有两条性质：一是应该关于y轴对称，即在我左边和在我右边是同等待遇；二是离我越远，其权重就应该越小，即满足“亲疏原则”。很显然，正态分布的密度函数就满足这两个条件。以标准正态分布的密度函数φ(x)为例，假设有三个点，离我的距离分别是1，2和3，那么它们权利的大小就分别是φ(1)=0.242，φ(2)=0.054和φ(3)=0.004，相应的权重分别为0.242/(0.242+0.054+0.004)=0.807，0.054/0.3=0.18和0.004/0.3=0.013。我们知道正态分布有两个参数，μ和σ^2，根据前面的对称性原则，μ必须要为0，而σ则可以根据实际情况来变化。一般来说，σ越大，密度曲线就越平坦，等权的倾向也就越大。下面的公式是估计值的表达式，其中的K(,)就是核函数，相当于是不同点“决定权”大小的度量，图中的三条曲线是三种不同的核。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/02/kernel.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/02/kernel.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;让人感到惊奇的是，这种平滑技术在视觉上的效果就是图像的模糊变化。电脑屏幕上所看到的图像都是由像素点构成的，而颜色则是由一个三维向量(R, G, B)来表示，其中三个分量分别代表了红色、绿色和蓝色的亮度。简单一点，如果只考虑黑白色系，那么颜色就由其“黑的程度”来表达，定义黑为0，白为1，于是任何一种灰色都可以对应到(0, 1)之中的一个实数。想像一下，电脑上的一幅黑白照片其实可以看作是三维空间中的一幅散点图，x轴和y轴分别对应了某个像素点在电脑屏幕上的水平和垂直坐标，z轴则是这个像素点颜色的深浅。要对这幅三维散点图进行平滑，步骤完全一样，只是这时决定权重的函数变成了二元正态分布的密度函数。一般来说，其中的ρ参数都会取成0，即两个分量是相互独立的，而两个方向上的σ则是所谓的X方向和Y方向上的模糊半径。&lt;/p&gt;

&lt;p&gt;前面说到，用正态分布来进行平滑，所有的点都要参与运算，这在实际操作中运算量是相当大的。但我们知道，正态密度曲线衰减的速度是比较快的，3σ之后与0相差无几，因此在计算时可以人为地划定一个框（比如3×3或5×5等），只对框内的点进行加权。下面就给出一段R语言程序，用来在R中实现高斯模糊的效果。需要说明的是，这段程序只适用于灰度的jpg或jpeg图片，而且算法没有经过优化，速度可能会&lt;strong&gt;非常慢&lt;/strong&gt;。rimage包和示例图片到&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Gaussian-Blur.zip&quot;&gt;这里&lt;/a&gt;下载。&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#生成“框”中各个元素的权重，grid是框的边长。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gaussm&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#加载rimage包。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rimage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#指定框的边长和sigma的取值。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#生成框。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gaussm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#读取图片，这段程序中需要灰度的jpg或jpeg图片。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pic&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read.jpeg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;demo.jpg&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#把图片转成像素矩阵。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;picm&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newm&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;picm&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#对像素点逐一进行转换，为了简便起见没有包括图片边缘上的点。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ncol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;picm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nrow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;picm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;picm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#画出原来的图片。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pic&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;#新开一个窗口，画出高斯模糊后的图片。&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imagematrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;更新后的代码&lt;/strong&gt;，速度应该更快，而且支持彩色图片，可以更换核函数（参数kernelf），感谢Linlin Yan：&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;blur&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernelf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;grid should be great than or equal to 3!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%%&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;grid should be odd number!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sapply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernelf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newx&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is.grey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nrow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is.grey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colSums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colSums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ncol&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is.grey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colSums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;colSums&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[,(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernel.gauss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dnorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blur.gauss&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blur&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;grid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kernel.gauss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rimage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read.jpeg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;demo.jpg&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blur.gauss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;imagematrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
    <link>https://yixuan.blog/cn/2010/02/blur-and-smoothing/</link>
    <guid>https://yixuan.blog/cn/2010/02/blur-and-smoothing</guid>
    <pubDate>2010-02-24T10:36:56+00:00</pubDate>
  </item>

  <item>
    <title>请大家帮忙测试一个小程序Rhelper</title>
    <description>&lt;p&gt;昨天bjt大哥写了&lt;a href=&quot;http://www.bjt.name/2010/01/chinese-earthquake-visualization/&quot;&gt;一篇很有启发性的文章&lt;/a&gt;，然后在评论中问曰：“yixuan 最近没怎么更新博客啊！”答曰：“寒假回家堕落了……”&lt;/p&gt;

&lt;p&gt;这是真话。一回到家，就彻底没了上进的念头，什么东西都不想学，也什么东西都不想写了。经大哥这一提醒，才发现日子确实不能这么过啊，总得逼着自己找点事做。突然想起回家前在学校折腾着编了一个小程序，但还没有完成，于是今天就花了些时间把它完善了一下，想请大家帮忙测试测试，找点BUG什么的。:)&lt;/p&gt;

&lt;p&gt;程序暂且取名叫Rhelper，其作用是让2.10.0以上的R重新焕发chm的风采。自从R升级到2.10.0后，其帮助文件都是动态生成的，原来的chm格式不再支持，这使得很多useRs一时难以适应。纯文本的帮助没有函数之间的超链接，因此在See Also的时候会比较麻烦，得重新去找；html帮助是一个不错的选择，但也有朋友说不如chm的直观，因为后者在左侧列出了包中的所有函数，可以快速查看相关的内容。&lt;/p&gt;

&lt;p&gt;鉴于此，我之前便萌生了一个想法，就是把帮助文件动态生成的特性与chm的优点结合起来，为2.10.0以上版本的R做一个简便易用的帮助系统。下面是一幅程序运行的截图：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/01/rhelper.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/01/rhelper.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;程序主要由两部分组成，一部分是外部程序，用以打开动态生成的html文件，另一部分是R中的函数，用来调用这个外部程序。程序可以从&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Rhelper_bin_20100130.zip&quot;&gt;这里&lt;/a&gt;下载，“安装”的办法是把压缩包里的Rhelper.exe和libgcc_s_dw2-1.dll复制到R安装目录的bin文件夹中，然后在R中运行Rhelper.R中的函数。如果准备经常用的话，可以把这个函数加入到etc/Rprofile.site中，这样每次打开R的时候程序就会自动把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rhelper()&lt;/code&gt;这个函数载入，用的时候只需&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rhelper(topic)&lt;/code&gt;，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rhelper(par)&lt;/code&gt;就可以了。&lt;/p&gt;

&lt;p&gt;目前这个程序已经实现了帮助文件中函数超链接的跳转，但一些外部的链接（比如链接到网站）和最底下的索引页链接还暂时无法实现。此外一个问题是这个程序的打开速度可能会比R自己的帮助慢零点几秒，因为这个程序调用了R中的一些函数，在用这些函数时相当于要新打开一个R进程。&lt;/p&gt;

&lt;p&gt;我不太确定这个程序在其它机器上能不能正常运行，如果没有什么大问题的话，我准备发到&lt;a href=&quot;http://cos.name/bbs&quot;&gt;COS&lt;/a&gt;上去，看看大家的意见。如果程序有BUG，敬请提出，谢谢！&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;感谢lanfeng：&lt;/strong&gt;之前我还不知道，原来图片是用绝对路径放到程序中的，所以之前的程序在其它机器上没办法显示两个图标。现在还不知道怎么把图片编译到程序中，总感觉放两张图太累赘……&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;20100129更新：&lt;/strong&gt;图标的问题搞定了，现在无需那两张png就可以显示图片了。lanfeng碰到的问题我暂时没有遇到，所以不知道应该怎样修改程序，但我想确定一下，你的那些包是不是最新版本的？如果是比较老的，还带有chm帮助的，可能会有些问题。此事还有待查证。&lt;/p&gt;

&lt;p&gt;回谢老大的问题：默认的加载页面是我胡乱设定的，因为是Rhelper嘛，所以就选择&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;help()&lt;/code&gt;函数的帮助了。:)
生成html的方法是用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tools&lt;/code&gt;包里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tools:::fetchRdDB()&lt;/code&gt;函数和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rd2HTML()&lt;/code&gt;函数，前者从每个包的help/*.rdb数据库中抓出Rd对象，后者把这个Rd对象转成html。函数的使用方法可以看R源码包src/library/utils/R/help.R的第237行。
函数跳转就是读取超链接地址中提供的包名和Rd文档名，然后用类似的方法生成html。不过有时候包名好像有问题，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;graphics&lt;/code&gt;包里链到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grDevices&lt;/code&gt;包的函数，其路径还是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;graphics&lt;/code&gt;，遇到这种情况时只能再用一次&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;help()&lt;/code&gt;来搜索。
这个程序是用开源的GUI库wxWidgets写的，里面提供了显示html的控件，也正是因为这个我才有打算编出这个程序。不知道wxWidgets跟gWidgets有没有关系，如果有联系的话可能移植起来会方便些。&lt;/p&gt;

&lt;p&gt;源代码在&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/Rhelper_src_20100130.zip&quot;&gt;这里&lt;/a&gt;下载。（纯业余，不知道怎么生成标准的源码结构，就把Code::Blocks的工程文件发上来了。工程可以直接用Code::Blocks打开，但里面的一些依赖库的路径得改。）&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/01/please-help-me-test-a-program-rhelper/</link>
    <guid>https://yixuan.blog/cn/2010/01/please-help-me-test-a-program-rhelper</guid>
    <pubDate>2010-01-28T17:44:38+00:00</pubDate>
  </item>

  <item>
    <title>VIM、文泉驿、器与道</title>
    <description>&lt;p&gt;先引一则Joke，来自于&lt;em&gt;Autoconf&lt;/em&gt;手册的第一章：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A physicist, an engineer, and a computer scientist were discussing the nature of God.&lt;/p&gt;

  &lt;p&gt;“Surely a Physicist,” said the physicist, “because early in the Creation, God made Light; and you know, Maxwell’s equations, the dual nature of electromagnetic waves, the relativistic consequences…”&lt;/p&gt;

  &lt;p&gt;“An Engineer!,” said the engineer, “because before making Light, God split the Chaos into Land and Water; it takes a hell of an engineer to handle that big amount of mud, and orderly separation of solids from liquids…”&lt;/p&gt;

  &lt;p&gt;The computer scientist shouted: “And the Chaos, where do you think it was coming from, hmm?”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;最近又在瞎折腾东西。不知是哪天脑子突然短路，决定去碰一碰传说中的上古神器Vim，于是怀着无比敬畏的心情敲下了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo apt-get install vim&lt;/code&gt;。一阵云烟飘过，便来到了神器面前。跟着里面自带的30分钟入门教程还有网上的一些讲解，好歹算是把右脚踏进了门里面。左h右l上k下j，O前o后g头G尾，不得不说用Vim写东西真的跟弹琴一样。&lt;/p&gt;

&lt;p&gt;在Ubuntu下玩了一阵子，觉得Vim确实是一个Very Interesting Modifier，于是准备在Windows下也培养培养感情，要不然拿着一个记事本也不好意思见人啊。 在网上下好了相关的文件，打开Vim，感觉默认的字体不怎么好看（应该就是记事本的那种字体），而相比之下Ubuntu下的字体却很清爽，于是准备把字体给换了。上网搜了些教程，也下载试了一些别人推荐的字体，但要么是来历不明（比如各种版本的YaHei），要么是对中文无效，总之折腾了半天还是很乱。此外一个很头疼的问题是等宽字体，英文字体要等宽不是什么难事，但中文就麻烦了，如果要让中文的宽度等于两倍的英文，那么要么是英文太瘦，要么是中文太宽，总之很难协调好。&lt;/p&gt;

&lt;p&gt;后来想起Ubuntu下默认的是文泉驿，于是准备在Windows下也试试。看了官网的一些介绍，最后下载的是矢量的微米黑字体。按照官网的介绍，这款字体“很小很强大”，小是因为它的体积只有2M多，解开也不过5M多，但其功能却决不含糊。下面是我配置好字体后Vim的一个截图：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2010/01/wqy-vim.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2010/01/wqy-vim.png&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;可以看出字体的边缘都很平滑，视觉效果还是不错的。此外这个字体对于中文的等宽处理是让中文之间留出一个空档，避免中文太胖，但每个中文字的左侧都是与英文相对齐的。&lt;/p&gt;

&lt;p&gt;说到&lt;a href=&quot;http://wenq.org/&quot;&gt;文泉驿&lt;/a&gt;感觉确实应该大赞一下这个项目，按照网上流传的一句话，有了文泉驿，终于不用到font文件夹中偷偷拷字体出来了。而我对于文泉驿另外一个欣赏之处是它到处都充满了中国文字的气息，从那句“开彼源兮，斯流永继”，到它的一系列中文&lt;a href=&quot;http://zh.wikipedia.org/wiki/%E6%96%87%E6%B3%89%E9%A9%BF&quot;&gt;开发代号&lt;/a&gt;（盘古、夸父、炎黄、尧舜、战国、赢政等等），感觉就是一部现代科技下的文字史书。&lt;/p&gt;

&lt;p&gt;把字体配置好了之后，又在网上逛了逛，看到了&lt;a href=&quot;http://www.cnblogs.com/Henrya2/archive/2009/02/21/1395591.html&quot;&gt;一段操作Vim的截图动画&lt;/a&gt;，当时的第一感觉就是，人类的科技已经发达到了这种地步……当然我目前是用不着这些高科技了，所以也没再研究，只是又把一些细节配置了一下。&lt;/p&gt;

&lt;p&gt;折腾了半天，似乎感觉到了一种满足感，但仔细想想，花了一整天时间在这上面是不是值得？就算学会甚至精通了Vim，对你是不是真的有很大帮助？就算Vim真的是神器，可它终究也只是一个器啊。如若道已不存，器又有何用呢？&lt;/p&gt;

&lt;p&gt;仔细审视一下自己大四以来的生活，发现自己已经很久没有用心读过诗书，也很久没有认真学过知识了，整天好像都是在被一些“器”所诱惑着。这就好比江湖有传闻说某某神兵降世，夺得之后便可无敌于天下，于是各路豪杰无心钻研武艺，只知向神兵进发。而结果如何？众武侠小说已经为我们说明了一切。&lt;/p&gt;

&lt;p&gt;“一箪食，一瓢饮，在陋巷”，真正对于道的追求，是要放下很多执念，也是要抵制很多诱惑的。看来我还差得很远很远啊。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2010/01/vim-wenquanyi-tool-and-tao/</link>
    <guid>https://yixuan.blog/cn/2010/01/vim-wenquanyi-tool-and-tao</guid>
    <pubDate>2010-01-04T23:05:20+00:00</pubDate>
  </item>

  <item>
    <title>其实微软挺幽默的</title>
    <description>&lt;p&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/12/ms1.png&quot; alt=&quot;ms1&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/12/ms2.png&quot; alt=&quot;ms2&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/12/ms3.png&quot; alt=&quot;ms3&quot; /&gt;&lt;/p&gt;

&lt;p&gt;算是年底给大家找点乐子吧。:-)&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/12/in-fact-ms-is-very-humorous/</link>
    <guid>https://yixuan.blog/cn/2009/12/in-fact-ms-is-very-humorous</guid>
    <pubDate>2009-12-31T20:29:05+00:00</pubDate>
  </item>

  <item>
    <title>原来R里面的很多函数是可以直接调用的</title>
    <description>&lt;p&gt;以前一直只知道用R可以调用C程序，昨天猛然一试发现C也可以调用R的。大家不妨看看R目录include子目录里的Rmath.h看看，里面定义了一系列跟分布有关的函数，也就是我们常用的rdpq系列。但是这个头文件中只有定义，这些函数的实现已经被编译到bin目录下的R.dll中了（这里都是指Windows系统）。下面就简单演示一下怎么在C中调用这些函数。&lt;/p&gt;

&lt;p&gt;首先，建立一个能让程序安身立命的文件夹（其实是废话……）；&lt;/p&gt;

&lt;p&gt;其次，配置一下能让程序安身立命的环境，主要是把R的bin文件夹添加到系统路径，然后需要有GCC编译器（Rtools中自带了）；&lt;/p&gt;

&lt;p&gt;第三，把R目录下的include文件夹以及bin文件夹中的R.dll拷贝到刚才那个安身立命的文件夹中，并把R.dll重命名为libR.dll；&lt;/p&gt;

&lt;p&gt;第四，编写C语言文件，示例如下：&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;Rmath.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Rf_dnorm4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%f&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Rf_dnorm4()&lt;/code&gt;是求正态分布密度的函数，参数的意义见R中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dnorm()&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;第五，假设上述C程序保存为test.c，那么现在总的文件夹中将有如下的东西：一个include文件夹，一个libR.dll以及一个test.c。打开cmd，cd进这个文件夹，然后输入下面的命令：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gcc -o test test.c -I./include -L. -lR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;不出意外的话文件夹中应该已经生成了一个test.exe文件。在刚才的cmd中输入test，屏幕上应该就会打印出0.398942，这就是标准正态分布的密度函数在0点的取值。&lt;/p&gt;

&lt;p&gt;解释一下gcc命令的几个参数。-o以及后面的test是指定生成的程序的名称；-I./include是让gcc程序在当前目录的include文件夹中搜索头文件（这就是为什么之前要把include文件夹拷过来），因为我们用到了Rmath.h；-L.（注意有个点！）是让gcc程序在当前目录中搜索库文件（比如本例中的dll文件），-lR是指定要载入的库文件的名称（这个比较诡异，比如-l后面如果跟的是abc，那么它要载入的就是libabc.dll，也就是说库文件的开头一定要有lib这个字眼，这也是为什么之前要把R.dll改成libR.dll）。&lt;/p&gt;

&lt;p&gt;另外要注意的是，为了保证程序能正确运行，一定要让程序能找到R目录bin文件夹中的几个dll，而且缺一不可。如果你没有将bin目录添加到系统路径，那么就应该将那几个dll拷贝到程序的同一目录下。&lt;/p&gt;

&lt;p&gt;嗯，暂时没了。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/12/many-functions-can-be-called-from-r/</link>
    <guid>https://yixuan.blog/cn/2009/12/many-functions-can-be-called-from-r</guid>
    <pubDate>2009-12-29T13:28:57+00:00</pubDate>
  </item>

  <item>
    <title>统计版《青花瓷》</title>
    <description>&lt;p&gt;嗯嗯，灵感来源于&lt;a href=&quot;http://www.cos.name/bbs/read.php?tid=2664&quot;&gt;这里&lt;/a&gt;和&lt;a href=&quot;http://tieba.baidu.com/f?kz=610323497&quot;&gt;这里&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;写这个东西有两个原因，一是向ypchen、fan和jingjing赔罪，那天你们请我去唱歌，但因为那天下午有会，再加上本人实在是五音不全，所以嗓子唱不了只好用手唱一首，聊表歉意。&lt;/p&gt;

&lt;p&gt;其二是考研马上就要到了，在此向所有奋斗在考研一线的同志们表示最衷心的祝福，愿你们在紧张的复习之余也能偶尔轻松一下。:-)&lt;/p&gt;

&lt;p style=&quot;text-align: center;&quot;&gt;统计版《青花瓷》&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;高斯的钟/轻划破/每一个晨曦&lt;br /&gt;
布朗的桥/游走时/连着我和你&lt;br /&gt;
泊松到来/你却在/痴痴地等待&lt;br /&gt;
马氏链/在轮回中/悄无忆&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;谁打断了/百年前/启蒙的赌局&lt;br /&gt;
谁把置信/水平定/为廿分之一&lt;br /&gt;
是谁在测度论里&lt;br /&gt;
测着我测不了的概率&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;山千重水万条&lt;br /&gt;
追寻你足迹&lt;br /&gt;
只因我笃信你&lt;br /&gt;
大数的定律&lt;br /&gt;
每遇见/你的身影/绝世而独立&lt;br /&gt;
我就向远方刻下印记&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;山千重水万条&lt;br /&gt;
追寻你足迹&lt;br /&gt;
只缘我沉醉你&lt;br /&gt;
时序的遍历&lt;br /&gt;
用时光/换来红尘/滚滚的唯一&lt;br /&gt;
你眼带笑意&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;回归判别/列联表/方差的分析&lt;br /&gt;
多元世界/隐藏着/太多的秘密&lt;br /&gt;
我用无限/追寻着/一致的估计&lt;br /&gt;
非参数/还这乾坤/秩与序&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;走遍了你/平滑后/每一条轨迹&lt;br /&gt;
看遍了你/挖掘后/数据的瑰丽&lt;br /&gt;
在随机森林树里&lt;br /&gt;
主成分的投影被隐去&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;山千重水万条&lt;br /&gt;
追寻你足迹&lt;br /&gt;
只因我笃信你&lt;br /&gt;
大数的定律&lt;br /&gt;
每遇见/你的身影/绝世而独立&lt;br /&gt;
我就向远方刻下印记&lt;/p&gt;
&lt;p style=&quot;text-align: center;&quot;&gt;山千重水万条&lt;br /&gt;
追寻你足迹&lt;br /&gt;
只缘我沉醉你&lt;br /&gt;
时序的遍历&lt;br /&gt;
用时光/换来红尘/滚滚的唯一&lt;br /&gt;
你眼带笑意……&lt;/p&gt;

&lt;p&gt;嗯，纯属娱乐，文字拙劣也是没办法的事……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/12/statistics-version-of-blue-and-white-porcelain/</link>
    <guid>https://yixuan.blog/cn/2009/12/statistics-version-of-blue-and-white-porcelain</guid>
    <pubDate>2009-12-17T22:27:33+00:00</pubDate>
  </item>

  <item>
    <title>R会议、animation、安全及其他</title>
    <description>&lt;p&gt;第二届中国R语言会议上海会场已经在昨天顺利闭幕，虽然我无法亲临现场，而且现在也还没有看到相关的照片和报道，但我相信一定是extRemely wondeRful的。&lt;/p&gt;

&lt;p&gt;“两会”的召开确实让我们扩展了自己的视野，直接的和间接的受益都非常多。下面说说我的一些意外收获吧。
在北京会场闭幕、上海会场开幕之前的这段时间里，除了整理会后的材料，我还花了些时间研究了一下把应用程序嵌到R中的方法。这源于宫雨老师的演讲以及谢老大为上海会场演讲写的一个幻灯片，里面提到animation包中输出动画的功能依赖于ImageMagick和SWF Tools这两个软件。但因为实际用到的功能只是这两个软件的很小一部分，而且这两个软件的体积都不小，所以希望能从中提取出animation包需要用到的少数几个函数。换句话说，就是把软件打个包，放到animation中去。&lt;/p&gt;

&lt;p&gt;理论上要打包的话，可以直接把那两个软件中要用到的几个exe拷贝到包的目录中，然后在包中写函数调用。但如果这样做，一来是跨平台的特性会大打折扣，二来牵涉到GPL协议中公开源代码的问题，三来是R CMD check中那句checking for executable files着实让人发怵……所以能做的基本上就是把那两个软件的源代码找来，然后分离出有用的源代码来编译。&lt;/p&gt;

&lt;p&gt;有了这个想法后，我就开始拿SWF Tools来做实验。我一开始觉得应该很容易完成，因为无非就是拿源代码来编译嘛，此外为了以防万一，我先不管R，而是直接编译成exe文件试试。可是刚在Code::Blocks（一个开源的C/C++开发集成环境）中按下编译按钮就傻眼了，老是说缺少config.h这个头文件，于是找呀找，却只找到个config.h.in文件。后来才知道，原来这些开源软件大部分都需要先运行configure程序，以解决环境的依赖问题。config.h.in里面有很多“开关”，configure程序就是依照当前环境的各种指标，把这些“开关”打开或关上，然后生成当前环境下的config.h文件。为了明白这一点着实不容易，我先后绝望了好几次，把程序装了又删，删了又装，好在没有完全放弃，否则可能永远也搞不明白了。&lt;/p&gt;

&lt;p&gt;把这个搞明白了只是第一步，后面依然老是出错。兜了几个弯子，才知道编译SWF Tools需要另外两个库，就是JPEG的库和Zlib库。于是又上网搜源代码，调试，杀虫，总之折腾了两三天，好歹能编译成功了，但是接下来又有问题了。SWF Tools的源代码解压开有10M多，加上JPEG库3M，Zlib库2M，如果都放到R包中去也未免太臃肿，所以必须要提取出有用的成份。但是这个过程就太痛苦了，因为你直接看的话很难搞清楚其中的依赖关系，有的时候删一个文件就有几百个编译错误，关系太复杂。好在Code::Blocks提供了一些查询和搜索的工具，又折腾了两天，删掉了N个文件，注释掉了M个函数，最终精简成了现在的2M左右的大小。&lt;/p&gt;

&lt;p&gt;好不容易自信满满准备把程序嵌到R中，才发现灾难又来临了。R是一个跨平台的软件，编写的程序包也应该尽量满足这个特性。但是我经常发现Windows下配置好的源代码放到Linux下无法编译，Linux下配置好了Windows又不行了，所以没办法，还得把configure脚本学一学，于是上网搜了半天，知道要先用autoscan生成configure.ac，然后用auroheader生成config.h.in，最后用autoconf生成configure什么的。而直到现在，依然有很多问题没有解决，比如R需要另外生成一个configure.win文件，用来在Windows下进行配置，但是我在网上没有找到什么文献，所以只能作罢。&lt;/p&gt;

&lt;p&gt;记得老大之前说过一句话，就是程序员和网页设计者最抓狂的事就是要面向不同的平台编写不同的代码，这几天也实在是被跨平台的问题折磨得不轻。不过好在这几天趁机把C语言又学习了一番，而且还了解到更多的与GNU有关的内容，算是意外的收获吧。目前的成果是把SWF Tools中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jpeg2swf&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;png2swf&lt;/code&gt;两个程序放到了animation包中，在R中是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sth2swf()&lt;/code&gt;这个函数，当然还没写说明文档什么的（&lt;a href=&quot;http://cos.name/bbs/read.php?tid=17472&quot;&gt;老大要笑了&lt;/a&gt;:)），可以到&lt;a href=&quot;http://yixuan.cos.name/cn/2009/12/enhanced-animation-package/&quot;&gt;http://yixuan.cos.name/cn/2009/12/enhanced-animation-package/&lt;/a&gt;去下载，里面给了示例的代码。当然这个东西还非常初级，需要很多进一步的工作，等&lt;a href=&quot;http://yanlinlin82.vicp.net/blog/&quot;&gt;yanlinlin&lt;/a&gt;忙完了一定要好好跟他取取经。如果真的能把这块东西搞定了，那老大的animation包的实用性肯定会有很大提升的。&lt;/p&gt;

&lt;p&gt;而做到这一步我又不禁想起&lt;a href=&quot;http://www.road2stat.com/&quot;&gt;肖楠&lt;/a&gt;对于R环境安全的演讲。试想，既然这些工具可以嵌入到R中，那么恶意程序岂不是也可以进行类似的操作？而且把恶意代码写成C并编译的话，就更加难以被人发现了，毕竟读一段C代码一般要比读一段R代码更难。前两天COS被一些不速之客光临过了，这也让我们不得不审视一下当前网络的安全问题，攻防之战依然在继续啊。&lt;/p&gt;

&lt;p&gt;附：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sth2swf()&lt;/code&gt;的说明。&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sth2swf(from, cmdargs)&lt;/code&gt;有两个参数，from可以取“jpeg”和“png”，说明你是要将什么格式的文件转成swf；cmdargs是命令行参数，参见SWF Tools的&lt;a href=&quot;http://wiki.swftools.org/index.php/Main_Page&quot;&gt;官方说明&lt;/a&gt;。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/12/r-conf-animation-security-and-others/</link>
    <guid>https://yixuan.blog/cn/2009/12/r-conf-animation-security-and-others</guid>
    <pubDate>2009-12-14T12:36:33+00:00</pubDate>
  </item>

  <item>
    <title>增强版animation包</title>
    <description>&lt;p&gt;本来想把东西整理一下再发上来的，但是忍不住要跟大家分享一下。今天太累了，先把东西放上吧，等明天再把相关的说明加上。&lt;/p&gt;

&lt;p&gt;源码包：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/animation_1.0-10.tar.gz&quot;&gt;下载&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;编译包：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/animation_1.0-10.zip&quot;&gt;下载&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;示例代码：&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/rcode.7z&quot;&gt;下载&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;补记：相关说明参见&lt;a href=&quot;http://yixuan.cos.name/cn/2009/12/r-conf-animation-security-and-others/&quot;&gt;这里&lt;/a&gt;。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/12/enhanced-animation-package/</link>
    <guid>https://yixuan.blog/cn/2009/12/enhanced-animation-package</guid>
    <pubDate>2009-12-14T00:12:24+00:00</pubDate>
  </item>

  <item>
    <title>社区和火炬</title>
    <description>&lt;p&gt;原本以为今天可以倒头便睡，让过热的大脑充分休息一下，结果最后竟然有点失眠，无奈之下打开电脑，把今天的一些感想记录下来。&lt;/p&gt;

&lt;p&gt;今天和不少高人都有一些交谈，其中有两个话题是我印象非常深刻的。首先是早上的牟先生，牟先生非常热心，他原本是去上海那边听报告的，这几天正好到北京办事，第一天下午就赶到了会场听演讲。特别值得一提的是，在当初统计之都进行捐赠的时候，正是他的一笔援助填满了1500元的捐赠目标。&lt;/p&gt;

&lt;p&gt;今天牟先生依然来到了会场，我们聊到了R会议的组织问题。牟先生谈及了自己的一些经验，他说他在上海经常参加一些Linux社区的活动，形式上跟R会议非常类似，都属于开源软件使用者自发举行的集会，而且他还说这种现象在开源软件中非常普遍，而商业软件则较少有这种自发的活动。&lt;/p&gt;

&lt;p&gt;但是他也提及了这种会议一个常见的问题，那就是第一届第二届都好办，而越往后，办成功的难度就会越大。牟先生的建议就是说，这种会议最好要有一个相对固定的组织者和领导者，因为这样可以既保证“老人”不流失，又可以吸引更多的新人；如果每次都换一个班子，那么原来建立起来的联系就有可能断裂，甚至流失的速度还要快于流入的速度。他还举了一个例子，说是有个社区的领头人，本身有自己的工作，而且非常忙，但是主持会议坚持了十年，非常不容易。&lt;/p&gt;

&lt;p&gt;其实这也是一个很现实的问题。就拿第一届与第二届的对比而言，第一届是报名130人，实到150人，而这次是报名150人，实到不足130人。要知道，第一届的很多人可是慕名而来（当时我们班几个人去那儿也有这方面的因素），而这一次则缺少了这方面的优势，这是我们原来一直担心的问题。&lt;/p&gt;

&lt;p&gt;此外我记得很早的时候COS上有个帖子，是有人问老大，如果你毕业了，工作了，还有热情去维护这个网站吗？这一问其实是很犀利的。就我们这群人而言，很多东西确实是靠热情来维持的，但为了R会议以及COS的长远发展，我们似乎也应该有所反思，去构建一条更加牢固的、能连结更多人的纽带。现在想想，老大让我们读《管子·形势第二》，读管理学，实在是很有远见的。&lt;/p&gt;

&lt;p&gt;而另一个话题是与刘思喆师兄展开的，我们聊到了一种学术氛围的传承问题。依然是一个不容乐观的现象，中南的三位师弟远道而来为我么讲经，但反观人大统计，虽然报名不少，但实际到场的却只有少数几人（当然这几位师弟师妹据我所知都是非常优秀的）。刘师兄跟我说，我们能创造出像R语言会议这样的一种氛围和环境是相当不容易的，但是我们本院的同学却似乎没有意识到这一点，这是非常可惜的。我与太云聊天的时候也说，中南的三位师弟非常有“革命精神”，而人大则过得太安逸了。&lt;/p&gt;

&lt;p&gt;居安思危。这次R会议三位大二的中南师弟给我留下了非常深的印象。回想我们当时大二的情况，感觉就是天壤之别。当我们还在沉迷于书本的时候，他们三位已经成了富有热情和志向的学术青年；当我们还在对分数和考试耿耿于怀的时候，他们已经对这些东西有了完全不一样的心态；当我们还在为未来踌躇不已的时候，他们已经看到了更为广阔的一片天空。&lt;/p&gt;

&lt;p&gt;我想谢老大和刘师兄之所以被我们所尊敬，很大一部分程度上是因为他们时刻在想着怎样为后来的人提供一个更好的平台，怎样把自己的经验和教训一代代传递下去。这种责任感和使命感就是一支火炬，不断启蒙（enlighten）着一代又一代的人。&lt;/p&gt;

&lt;p&gt;火炬既然已经传到了我们的手中，就不再有停下的理由。三位师弟临走的时候，我送了一本《女士品茶》给他们，并在扉页上写着：“愿知识的火炬，代代相传，生生不息！”&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/12/community-and-torch/</link>
    <guid>https://yixuan.blog/cn/2009/12/community-and-torch</guid>
    <pubDate>2009-12-07T09:45:48+00:00</pubDate>
  </item>

  <item>
    <title>一点随感</title>
    <description>&lt;p&gt;不容易，真不容易，说真的，我都不知道怎么形容现在的心情了。&lt;/p&gt;

&lt;p&gt;可能大家不相信，今天开完会回到寝室，看到&lt;a href=&quot;http://yihui.name/cn/2009/12/snow-in-ames-and-2nd-chinese-r-conference/&quot;&gt;谢老大写的日志&lt;/a&gt;，我真的快哭出来了。不怕大家笑话，我在打这些字的时候眼睛都是湿的。
感激，除了感激还是感激。&lt;/p&gt;

&lt;p&gt;在刚才过去的两分钟内，我对我自己说，如果没有去年的第一届R会议，我可能永远也不会结识老大、太云、陈堰平师兄、刘思喆师兄和奚潭这些人，永远不会加入到COS这个团体中，永远不会接触到因为这些人和COS而认识的范师兄、钟老师、胡大哥、颜林林、中南三剑客、陈丽云，等等等等。如果是这样，我现在可能还躲在某个灰暗的角落，想着一些毫无意义却又萦绕身心的困惑。
知遇之恩，此生难报。&lt;/p&gt;

&lt;p&gt;只是当初一个小小的选择，竟让我完全踏进了另一个世界。一年，只是一年，一年可以让一个人改变太多。今天和颜林林、钟老师、胡大哥、刘师兄的相见，完全像是遇到了久识的朋友，丝毫没有意识到那不过是几个月前的事。&lt;/p&gt;

&lt;p&gt;几位一起组织会议的师兄和同学可能觉得我总是紧张兮兮的。我承认这是我的弱点，但我没有理由让自己不这样做。我想做任何事，只要有种力量会推动你一直前进，你也必然不会给自己找一个休息的理由。欧·亨利的小说中有一句话，&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;When one loves one’s Art, no service seems too hard.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;对于老大、R和COS的感恩，已然不是我自己所能控制的了。&lt;/p&gt;

&lt;p&gt;准备会议确实很累，但是回想起和中南师弟的拥抱，回想起和钟老师的握手，回想起和大家的谈笑，我真的没有任何理由再去抱怨，我坚信我所得到的要比我所付出的多出许多。&lt;/p&gt;

&lt;p&gt;要感谢所有前来参会的朋友，尤其要感谢大洋彼岸的吴老师和谢老大。他们留给我们的，真的远不止是一门语言和一届会议而已。正如陈师兄在今天致辞时所提，吃水不忘挖井人，没有这些先驱们的杰出贡献，何来我们今天的欢聚一堂？&lt;/p&gt;

&lt;p&gt;出去散步回来，稍有缺憾的月色投下一丝伤感。顿时有种感觉，像是在挂念远在他乡的兄长。&lt;/p&gt;

&lt;p&gt;此时此夜，且让我做一回，风雪夜归人。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/12/some-feelings/</link>
    <guid>https://yixuan.blog/cn/2009/12/some-feelings</guid>
    <pubDate>2009-12-05T21:46:33+00:00</pubDate>
  </item>

  <item>
    <title>做神曾经做过的事</title>
    <description>&lt;p&gt;第二届R会议马上就要开了，而我们这边的工作还有很多需要完善的。我们六七个人一起做都乱成这样，真的很难想像老大去年是怎么以一人之力就能把大会开展得井井有条的……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/11/do-what-god-has-done/</link>
    <guid>https://yixuan.blog/cn/2009/11/do-what-god-has-done</guid>
    <pubDate>2009-11-27T09:53:26+00:00</pubDate>
  </item>

  <item>
    <title>预测强度及其在聚类分析中的应用</title>
    <description>&lt;p&gt;打了一个草稿，先发到这里吧，等修改好了就往COS主站上发一份。:)&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;聚类分析的广泛应用想必不用多说，但大家在做聚类的时候可能经常会遇到一个比较头疼的问题，那就是聚类数的选择。聚类数的选择其实很有讲究：太少了，可能样本点不能很好地区分开来；太多了，结果又往往不好解释。因此聚类数的选择其实是一个很重要的问题。
但是在实际情形中，聚类数的选择又经常显得比较主观，我们可能会说，“依据这个问题的实际意义，应该聚成××类”，但是这样容易造成一个后果，就是甲说甲有理，乙说乙有理，孰是孰非难以分辨。
或者我们会采用层次聚类法，因为它不需要事先对聚类数进行指定，但面对那一棵繁茂倒长的树，我们手中的剪刀可能依然是无从下手。
而像k-means这样的聚类方法就更不用说了，k是必须要事先指定好的，否则连聚类的过程都完成不了。&lt;/p&gt;

&lt;p&gt;于是我们很自然地要提出这样一个问题：有没有这样一种方法，能为我们聚类数的确定提供一些理论上的依据？关于这一点，也许已经有很多人给出过他们的建议，而本文要为大家介绍的是Tibshirani等大牛们于2001年提出的一种基于“预测强度”的确定聚类数的方法。文章的原文请点击&lt;a href=&quot;http://www-stat.stanford.edu/~tibs/ftp/predstr.ps&quot;&gt;这里&lt;/a&gt;，注意这是ps格式的，可能需要用GSview之类的工具打开。本文所讲的大概只是文章的小部分内容，有兴趣的读者可以仔细研究一下那篇文章。&lt;/p&gt;

&lt;h3 id=&quot;一预测强度的思想&quot;&gt;一、预测强度的思想&lt;/h3&gt;

&lt;p&gt;预测强度的表达是一大串数学符号，为了直观地说明它的含义，我们先来思考一个问题：&lt;strong&gt;什么样的聚类是一个好的聚类&lt;/strong&gt;？如果我们回答了这个问题，而且可以把聚类的效果表达成聚类数k的函数，那么聚类数的选取其实就变成一个最优化的过程了。&lt;/p&gt;

&lt;p&gt;对于这个问题，不同的人会有不同的回答，但大体的想法应该差不多，就是希望聚类的结果能&lt;strong&gt;将样本点尽可能地分开&lt;/strong&gt;。事实上，聚类的过程就是围绕这个思想而来的。我们一般认为，类与类之间的距离越远，它们分得也就越开，于是我们就定义了点与点的距离，点与类的距离，类与类的距离，然后以此为依据将类分开。&lt;/p&gt;

&lt;p&gt;但是我们其实可以换一种思路，那就是用分类的思想去理解聚类。众所周知，分类和聚类一个是有监督一个是无监督，但它们并不是完全没有联系。试想，当一个数据集进行聚类之后，它就自然而然地具有了分类的能力，换句话说，拿一批新的样品过来，原来的数据就可以对新数据进行分类，于是新数据就可以得到一列记录了每个样品分类的编号（假定叫编号1）。另一方面，新的数据自身也可以进行聚类，也可以得到一个分类编号（假定叫编号2），那么编号1和编号2之间是否一致就决定了聚类的结果是否具有可预测性。我们之前说的要将样本点尽可能地分开，其实就是希望&lt;strong&gt;聚类结果能对新样本进行准确而良好的预测&lt;/strong&gt;，这正是预测强度方法最核心的思想，也是“预测强度”名称的由来。&lt;/p&gt;

&lt;h3 id=&quot;二预测强度的计算&quot;&gt;二、预测强度的计算&lt;/h3&gt;

&lt;p&gt;说到预测的能力，也许大家会很自然地想到分类器的误分率。不错，被正确预测的样品的比例是判断分类器预测能力的一个重要指标，但是如果出现下面的情形：有5个新样本点，自身聚类的编号分别是1，1，1，2，2，而用已有数据判别的结果是2，2，2，1，1，如果直接拿它们进行对比的话，判对率是0，但实际上，这两组编号应该是完全吻合的。其中的原因就在于，聚类的编号其实是无序的，是可以任意调换的，如果我们要测量两组编号的吻合性，就需要消除其中序的概念。&lt;/p&gt;

&lt;p&gt;关于这个问题的解决后面会提及，现在还是从预测强度的计算着手。拿到一批数据，首先将其随机划分为训练集和测试集，然后分别对这两组数进行聚类（按需要选定一种聚类方法，并先假设聚类数k是已知的），这样训练集和测试集就分别得到了一列类别编号（记为trainID和testID1）。接下来用训练集及其编号对测试集进行判别，于是测试集就获得了第二列编号（记为testID2）。下面的工作就是衡量testID1和testID2之间的一致性程度。&lt;/p&gt;

&lt;p&gt;预测强度的做法是，先考虑testID1里面的每一个类，比如编号为1的所有样品，如果它们一共有n_k1个（k是总的聚类数），那么它们的两两配对就有n_k1(n_k1-1)/2种；接下来就考察每一种配对下，这两个样品是否在testID2中也被分在同一个组；计算出那些在testID2中也被分在同一组的配对所占的比例，然后重复这个过程，对所有的k个类都计算出相应的值；最后对这k个数值取最小值，就是当前聚类数k下的预测强度。&lt;/p&gt;

&lt;p&gt;用一张流程图来说明，就是：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/11/ps-flowchart.png&quot; alt=&quot;flowchart&quot; /&gt;&lt;/p&gt;

&lt;p&gt;再举一个直观的例子。假设有一批新样品，自身聚类的编号是&lt;/p&gt;

&lt;table border=&quot;1&quot; align=&quot;center&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X3&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X4&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X5&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X6&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X7&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X8&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X9&lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;middle&quot;&gt;X10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;被判别的编号是&lt;/p&gt;

&lt;table border=&quot;1&quot; align=&quot;center&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X3&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X4&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X5&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X6&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X7&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X8&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X9&lt;/td&gt;
&lt;td colspan=&quot;2&quot; width=&quot;39&quot; valign=&quot;middle&quot;&gt;X10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;2&lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;middle&quot;&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;那么，对于自身聚类中的第1个类，可以作出下面的这个矩阵：&lt;/p&gt;

&lt;table border=&quot;1&quot; align=&quot;center&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X3&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X4&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X5&lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;middle&quot;&gt;X6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;X1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;X2&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;X3&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;X4&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;X5&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;X6&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;其中如果两个点在判别编号中也一样，就记为1，否则就记为0。于是很容易得出判对的比例是7/15。&lt;/p&gt;

&lt;p&gt;同理，另一个类别的矩阵是&lt;/p&gt;

&lt;table border=&quot;1&quot; align=&quot;center&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;36&quot; valign=&quot;middle&quot;&gt;X7&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X8&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;middle&quot;&gt;X9&lt;/td&gt;
&lt;td width=&quot;40&quot; valign=&quot;middle&quot;&gt;X10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;X7&lt;/td&gt;
&lt;td width=&quot;36&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;40&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;X8&lt;/td&gt;
&lt;td width=&quot;36&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt;1&lt;/td&gt;
&lt;td width=&quot;40&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;X9&lt;/td&gt;
&lt;td width=&quot;36&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;40&quot; valign=&quot;top&quot;&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td width=&quot;39&quot; valign=&quot;top&quot;&gt;X10&lt;/td&gt;
&lt;td width=&quot;36&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;37&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;td width=&quot;40&quot; valign=&quot;top&quot;&gt; &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;比例为3/6=1/2。于是，当前聚类数2下的预测强度就是7/15和1/2中的最小值，即7/15。&lt;/p&gt;

&lt;h3 id=&quot;三预测强度的应用&quot;&gt;三、预测强度的应用&lt;/h3&gt;

&lt;p&gt;有了上面的思想和计算方法，预测强度的应用就很直接了。就拿确定聚类数来说，我们只要算出各个聚类数下的预测强度，然后找出其中的最大值对应的k就可以了。
举一个原文中的例子，下图是作者做的三个模拟实验，其中左边的三幅图表示实际处理的三组数据，右边是对应的预测强度计算结果。在右边的图中，横轴表示尝试使用的聚类数，纵轴是每一个聚类数下的预测强度，将这些点连成图线，就可以直观地看出预测强度随聚类数的变化情况。
&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/11/ps-k.png&quot; alt=&quot;ps_k&quot; /&gt;
从右边第一幅图可以看出，预测强度在k&amp;gt;1时都比较小，尚不足70%，说明聚类数在大于1时表现都不太良好，这正好与左图反映的情况是一致的——数据本身就十分混杂，应该只有一类；
而右边第二幅和第三幅图分别在k=2和k=3时达到了高峰，这与左图反映的数据形态也是完全吻合的。&lt;/p&gt;

&lt;p&gt;在上述的例子中，预测强度为我们找到了“最正确”的聚类数，但这毕竟是模拟的结果。在实际应用的过程中，有以下几个问题还需要考虑一番：&lt;/p&gt;

&lt;h4 id=&quot;1交叉验证&quot;&gt;1、交叉验证&lt;/h4&gt;

&lt;p&gt;在计算预测强度的过程中，我们需要对数据进行随机划分，而这种带有随机性的算法我们最担心的就是结果的稳定性。如果我们手头上的数据量比较大的话，其实可以采用交叉验证的方式，比如把数据随机等分成5份，每一次从中取出一份作为测试集，其余的为训练集，然后计算出一个预测强度；重复这个步骤，直到每一份都轮了一遍，这样总共就算出了5个数值；对这5个数取平均，就是用5折交叉验证得出的平均预测强度。&lt;/p&gt;

&lt;h4 id=&quot;2k1时的预测强度&quot;&gt;2、k=1时的预测强度&lt;/h4&gt;

&lt;p&gt;这是一个值得思考的问题，因为事实上，当k=1，即数据只聚成一类时，预测强度是恒为1的。原因很简单，那就是因为，只要是聚成一类，那么所有的样本点都会落在同一个类中，因此没有任何一个配对会被错判。但是此时，我们并不能说聚类数为1时就一定是最好的——如果真是这样的话，聚类还有什么意义呢？&lt;/p&gt;

&lt;p&gt;所以这时候，我们就不能单纯地用最优化的思想去看待这个问题。我们应该首先看k&amp;gt;1时预测强度大致的取值，如果取值都比较小，那么我们也许可以认为这个数据本身就比较混杂，不适合做聚类，即承认k=1；而如果在某个聚类数k_0&amp;gt;1上预测强度达到了0.8、0.9这样的水平，那也许我们还是应该认为数据是有聚类的必要的。&lt;/p&gt;

&lt;p&gt;从之前的图中我们也可以得到一些启发：第一幅图的预测强度在k&amp;gt;1时就“一蹶不振”了，而后面两幅都在某个大于1的位置上有了一个接近于1的取值（在这个比较理想的模拟条件下其实就是等于1了），因此我们认为第一组数据应该聚成一类，而第二、第三组数据分别聚成两类和三类。&lt;/p&gt;

&lt;h4 id=&quot;3聚类和分类方法的选择&quot;&gt;3、聚类和分类方法的选择&lt;/h4&gt;

&lt;p&gt;在计算预测强度的过程中聚类算法和分类算法都有所涉及，但文章的原文中并没有太多地涉及相应方法的选择。我个人认为，选择什么样的聚类和分类方法主要取决于数据的特点。如果数据量比较大，可能层次聚类法算起来就比较困难，这时可以用k-means等方法；而至于分类，也可以用一些比较快速的基于距离的判别法，比如LDA、QDA等。但总的来说，方法的选取还是相对自由和宽松的。&lt;/p&gt;

&lt;h3 id=&quot;四一些思考&quot;&gt;四、一些思考&lt;/h3&gt;

&lt;p&gt;在文章的原文中，作者只是用预测强度来选择合适的聚类数，但是回到之前提出的那个问题，当我们把预测强度也作为衡量聚类效果好坏的一个标准的时候，被优化的因素按理就可以变得更多了。比如说，变量的选择也可以被纳入到优化的框架中，因为在分类的时候，并不是变量越多预测的效果就越好。于是，我们可以将聚类数和变量子集进行二维的优化，来得到各自最优的取值。&lt;/p&gt;

&lt;p&gt;最后总结一下，预测强度其实就是把分类的思想应用到聚类之中，以预测的能力作为聚类效果好坏的标准，进而以这个标准来选取最佳的聚类数或其它的影响因素。但还是那句话，任何一种统计方法都有其适用的范围，实际使用的过程中，可能并不总能得到很好的结果，这时就还需要对数据本身进行更深入的认识，才能得出可靠的结论。&lt;/p&gt;

&lt;p&gt;附：计算预测强度的R代码，供参考。&lt;/p&gt;

&lt;div class=&quot;language-r highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;psk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl.train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kmeans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cluster&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl.test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kmeans&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cluster&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl.test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;library&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MASS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cbind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as.data.frame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl.train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lda.model&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lda&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;~&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;train&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl.test.pred&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lda.model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;is.vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;as.matrix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ncol&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;in&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl.test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cl.test.pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pred&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;==&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;min&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
    <link>https://yixuan.blog/cn/2009/11/prediction-strength-and-its-application-in-clustering/</link>
    <guid>https://yixuan.blog/cn/2009/11/prediction-strength-and-its-application-in-clustering</guid>
    <pubDate>2009-11-08T21:16:06+00:00</pubDate>
  </item>

  <item>
    <title>别了，sinα</title>
    <description>&lt;p&gt;走之前用这里的电脑留个记号吧。&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;补记：回到寝室了，在sinα的实习也算是告一段落了，干脆就在这里总结一下这一段时间的经历和感受好了。&lt;/p&gt;

&lt;p&gt;首先要说的当然还是那些被无数人实践过的结果：&lt;/p&gt;

&lt;p&gt;企业和学校是不同的；&lt;br /&gt;
职员和学生是不同的；&lt;br /&gt;
报告和论文是不同的。&lt;/p&gt;

&lt;p&gt;不过虽然是陈词，但应该还不算是滥调，因为毕竟是自己亲身经历的，总好过凭空的想像。&lt;/p&gt;

&lt;p&gt;在那边的工作基本上划分为两块，老师和那边的合作项目，以及sinα日常的一些事务。合作项目就不用说了，主要是数据的处理，收获还是很大的，毕竟是真真正正的数据啊。在这个项目中学到了用Prediction Strength来确定聚类数的方法，我还在想是不是可以给主站投一稿呢。结果有可能不能公开，但至少可以介绍一下方法吧。&lt;/p&gt;

&lt;p&gt;其实真正想说的是日常的事务，尽管从“技术含量”上看远不如前者。当时几乎每天要做的工作就是定时更新几张Excel表格，里面是一些从网站上获取的数据，包括每天要更新的，每周要更新的，还有每月要更新的。而每天的操作，基本上就是Ctrl+C/Ctrl+V或者如某著名饼干产品广告说的，“框一框，拖一拖，填一填”。曾经有那么几个下午，由于实在太困，大脑几乎失去了意识，但手中的鼠标还在指定的位置进行着指定的操作，而正因为这个，我才体会到卓别林的《摩登时代》可能真的不只是一部电影。&lt;/p&gt;

&lt;p&gt;后来，十一假期的时候，为了能够摆脱这样的处境，能够进一步解放生产力、发展生产力，我再一次向Mr. R求救。由于其中的几个网站是公开的，所以可以通过读取源代码的方式来抓取需要的信息，用到的函数主要是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readLines(url())&lt;/code&gt;和一些正则表达式。等十一之后正式投入使用时，效果确实十分明显，原来要一个小时的工作，那时就只要十来分钟就行了。&lt;/p&gt;

&lt;p&gt;当然了，Mr. R肯定不能只在这种地方发挥作用。之前提到的那个合作项目，里面的结果基本上都是用R跑出来的。当时我跟我的主管说，这个方法太新，ABAA没有办法算，所以我就用R软件了。而他们也没有像传闻那样，听到是开源免费的就一副紧张的样子。事实上，在我用过的两台电脑里面，都已经装上了R软件，版本都是2.9以上的。这离不开PLF和暑假里面几位师兄的功劳啊。
今天要走的时候，主管还特意吩咐我，给后面来实习的人写一份自动获取数据程序的说明文档。看样子，Mr. R是要继续在那里工作一段时间了。&lt;/p&gt;

&lt;p&gt;所以呢，我有一些想法：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;商业公司不是坚决不用开源软件，关键是要有人推广；&lt;/li&gt;
  &lt;li&gt;要推广开源软件，就要找到它们相对于商业软件的优势；&lt;/li&gt;
  &lt;li&gt;我们其实每天都在享受开源作者的劳动果实，比如很多网站的后台，还有你吃到的“正宗宫保鸡丁”的配料。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;俗话说，一波未平，一波又起。好在现在sin这一波已经平息了，接下来，我得多花点心思到COS这一波吧……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/11/farewell-sina/</link>
    <guid>https://yixuan.blog/cn/2009/11/farewell-sina</guid>
    <pubDate>2009-11-06T19:55:51+00:00</pubDate>
  </item>

  <item>
    <title>十一月的雪</title>
    <description>&lt;p&gt;如果说昨天还是落叶萧萧的深秋，那么今天就算是风雪连天的寒冬了。十一月的第一天下这么大的雪，别说是在家里了，就算是在北京我之前也没遇到过啊……&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2009/11/snow-1.jpg&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/11/snow-1.jpg&quot; alt=&quot;IMG_3046&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2009/11/snow-2.jpg&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/11/snow-2.jpg&quot; alt=&quot;IMG_3048&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;补记：晚上妈妈发来短信，说到科学巨星陨落和北京骤降大雪是否有必然联系，我顿时一阵恍然。我们的后人如果读到这一段历史，他们的感觉或许就和我们读到古时历史人物那些奇特的经历一样，认为这不过是史家的夸张修饰而已。后人叹之而不信之，亦使后人而复叹后人也。可是这又有什么关系呢，我们的后人会知道，我们曾经因为这样的一批人物而自豪过，而感动过，是否是冥冥天意，又何妨把它作为对一代巨匠的深沉纪念呢？&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/11/snow-in-november/</link>
    <guid>https://yixuan.blog/cn/2009/11/snow-in-november</guid>
    <pubDate>2009-11-01T16:13:39+00:00</pubDate>
  </item>

  <item>
    <title>感冒了</title>
    <description>&lt;p&gt;不知道是感冒了还是被感冒了，反正总而言之是感冒了，而且感冒的还是一大片。恰巧最近又在做k-means，于是满脑子都在想最初的那几个点是谁，谁和谁离得比较近，谁被谁归成一类了——这样子什么时候能收敛到头啊。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/10/caught-a-cold/</link>
    <guid>https://yixuan.blog/cn/2009/10/caught-a-cold</guid>
    <pubDate>2009-10-24T22:53:19+00:00</pubDate>
  </item>

  <item>
    <title>有点累了</title>
    <description>&lt;p&gt;有点累了，而且这种累跟忙碌是不一样的感觉。&lt;/p&gt;

&lt;p&gt;最近事情很多很杂，但是完成度都是百分之七八十，没有一样是满意的，这种感觉非常让人崩溃。看上去好像过得很充实，其实只不过是在随机游走中一点一点地消磨时光。&lt;/p&gt;

&lt;p&gt;还是心态问题吧，一个人不可能同时做很多事情，没有舍弃就没有获得。就算是那些被我们称为神一样的人，我相信他们在自己的世界中，依然有着很多艰难的抉择。&lt;/p&gt;

&lt;p&gt;日常的课程和作业，精算考试，COS的例行事务，新浪网的报告，数据挖掘小组，学院网站，软件界面，学委的工作，帮师弟师妹解答R的问题。单独提出其中的任何一个都是我应该做的或我感兴趣做的，甚至我都有信心和耐心去做到最好，但是堆积在一起才发现这是多么恐怖的事情。我觉得如果我继续以这样的状态运行，也许每一件事情都做不好，那岂不是更加愧对这些人和事了？我需要放弃一些东西吗？&lt;/p&gt;

&lt;p&gt;可是想到这里，我又发现自己其实太过简单。或许这些事情只是某个高于我的存在给我的一种试炼？如果连这些事都应付不过来，那以后如何面对更加复杂的现实呢？想到这里，我不禁哑然失笑，当神人们在Keep on Fighting!的时候，我居然还妄图什么“怡然”，这是不是我自己对自己的一个最好的讽刺呢？
我更加迷惘了吗？&lt;/p&gt;

&lt;p&gt;或许，此时我需要神，或者像神一样的人，或者只是单纯的朋友的几句忠告吧。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/10/feeling-tired/</link>
    <guid>https://yixuan.blog/cn/2009/10/feeling-tired</guid>
    <pubDate>2009-10-20T10:56:56+00:00</pubDate>
  </item>

  <item>
    <title>被一道概率题玩弄了</title>
    <description>&lt;p&gt;今天彻底被一道题目玩弄了。题目是这样的：&lt;/p&gt;

&lt;p&gt;某国家为了限制人口增长而实行计划生育，即一对夫妻只能生一胎，但这个国家的人普遍重男轻女，于是经常会有“偷生”的行为，比如一对夫妻一定要生出一个男孩才满意，否则就一直生。假定这个国家原来的男女比例是1：1，问若干年后（还没有第三代），其性别比例会有什么变化。&lt;/p&gt;

&lt;p&gt;说实话吧，我看到这段陈述后我的第一反应就是，这不是赤果果地影射C国吗？当然是男的多了。但是计算一下才知道出问题了，最简单的办法就是用几何分布。如果认为生女孩是“失败”（嗯嗯，这里的失败是概率里面经常说的那个失败啊，我可不敢说是通常意义上的那个啊！！！），那么每家的孩子数就服从p=0.5的几何分布，均值为2，由于最后一胎一定是男孩，所以每家平均会有一个女孩，so……比例当然是不变了。&lt;/p&gt;

&lt;p&gt;今天丢人了，以后再也不说自己是统计学院出来的……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/10/probability-played-a-trick-on-me/</link>
    <guid>https://yixuan.blog/cn/2009/10/probability-played-a-trick-on-me</guid>
    <pubDate>2009-10-15T20:30:24+00:00</pubDate>
  </item>

  <item>
    <title>虚惊一场</title>
    <description>&lt;p&gt;今天练习了一下利用phpMyAdmin来导入导出WP的数据，结果执行之后发现中文全变成了问号，当时想肯定是编码的问题。检查了一下导出的sql文件，发现里面确实有很多乱码，但是GB2312、GBK、UTF-8都试了一遍，乱码依旧存在。仔细一看发现，原来导出的sql文件是用两种编码写出来的，phpMyAdmin的工作编码是GB2312，但WP存储的数据是UTF-8，所以不管用GB2312编码还是用UTF-8，总会有乱码存在。这种情况之前没遇到过，所以只好用傻办法，轮换更改编码，把显示正确的内容粘到另一个文本文件中，最后保存为UTF-8。&lt;/p&gt;

&lt;p&gt;然而问题依然存在，当我把显示正确的sql文件导入到phpMyAdmin中时，中文仍然会变成问号。在导入的时候可以选择导入文件的字符编码，但我试了很多个，问题还是没有解决。后来到网上搜了一下才知道，phpMyAdmin会把新建数据库的字符集设定为latin1_swedish_ci（估计程序的编写者是瑞典人），在之前导出的sql文件中，包含了新建数据库的命令，因此数据库也就成了latin1_swedish_ci编码，这时再读取中文，自然就变成问号了，并且之后再怎么修改编码也于事无补。换言之，在用phpMyAdmin导入sql文件时，界面中要你指定的字符集与数据库实际的字符集是两个完全独立的概念。找了一些资料后，目前惟一（感觉是）的解决办法是预先在phpMyAdmin中建好相应的数据库，设定好字符集，再用phpMyAdmin的导入功能将数据库进行填充。不过这时要注意的是在sql文件中需要把CREATE DATABASE命令删掉，否则导入时就会提示数据库已经存在而无法导入。&lt;/p&gt;

&lt;p&gt;总而言之，在用Ubuntu时，有两样东西我是很害怕的，那就是编码和字体……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/10/a-false-alarm/</link>
    <guid>https://yixuan.blog/cn/2009/10/a-false-alarm</guid>
    <pubDate>2009-10-08T00:14:22+00:00</pubDate>
  </item>

  <item>
    <title>原来我是山寨……</title>
    <description>&lt;p&gt;&lt;a href=&quot;http://www.yiranxuan.com/&quot;&gt;http://www.yiranxuan.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;看看人家这域名，人家这专业，唉，丢人喽……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/10/i-turn-out-to-be-shanzhai/</link>
    <guid>https://yixuan.blog/cn/2009/10/i-turn-out-to-be-shanzhai</guid>
    <pubDate>2009-10-07T00:22:59+00:00</pubDate>
  </item>

  <item>
    <title>我的中国心®</title>
    <description>&lt;p&gt;国庆那天ID为微微的朋友在&lt;a href=&quot;http://cos.name&quot;&gt;COS论坛&lt;/a&gt;的R版发了一个帖子，名叫“&lt;a href=&quot;http://cos.name/bbs/read.php?tid=16743&quot;&gt;灌灌水庆庆生&lt;/a&gt;”，里面是用随机点图描绘的一幅漂亮的中国心。当时突然想起来以前在百度贴吧看到的&lt;a href=&quot;http://tieba.baidu.com/f?ct=335675392&amp;amp;tn=baiduPostBrowser&amp;amp;sc=5137217861&amp;amp;z=511497934&amp;amp;pn=0&amp;amp;rn=30&amp;amp;lm=0&amp;amp;word=%CA%FD%D1%A7#5137217861&quot;&gt;一个帖子&lt;/a&gt;，是用一个曲面方程画出一颗立体的心形，于是乎，我就委托Mr. R照着方程的样子也画了一颗。&lt;/p&gt;

&lt;p&gt;方程的形式虽然简洁，但画起来可不容易，每画一个点就要解一个方程，而且经常出现斜率不存在的点，所以如果只从一个方向上来画，难免会得出NA解，使得原本光滑的曲面出现很多漏洞（如下图）。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2009/10/heart-1.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/10/heart-1.png&quot; alt=&quot;h01&quot; /&gt;&lt;/a&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2009/10/heart-2.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/10/heart-2.png&quot; alt=&quot;h02&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;就因为这样，暂时的解决办法就是从两个相互垂直的方向分别画出曲面，然后把它们重叠起来，将各自的漏洞都填补好。最后再利用rgl包的贴图功能，在心上“贴好”五星红旗，一颗中国心就完成了。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://upload.yixuan.blog/cn/2009/10/heart-3.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/10/heart-3.png&quot; alt=&quot;heart_snapshot01&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;https://upload.yixuan.blog/cn/2009/10/heart-4.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/10/heart-4.png&quot; alt=&quot;heart_snapshot02&quot; /&gt;&lt;/a&gt; &lt;a href=&quot;https://upload.yixuan.blog/cn/2009/10/heart-5.png&quot;&gt;&lt;img src=&quot;https://upload.yixuan.blog/cn/2009/10/heart-5.png&quot; alt=&quot;heart_snapshot03&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;附：R源文件和用来贴图的国旗（因为贴图的位置需要做了镜像变换）。点击&lt;a href=&quot;https://bitbucket.org/yixuan/cn/downloads/myChina.zip&quot;&gt;这里&lt;/a&gt;下载。&lt;/p&gt;

&lt;p&gt;运行程序时设好工作空间，可能会有些慢，想要加快些速度可以把程序中的n=100改小一些，当然图形的精细度会略微差些。&lt;/p&gt;

&lt;p&gt;另：老大说可以放到fun包里，那是不是得“普世”一点了？比如贴个R上去？:)&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/10/my-heart-for-china/</link>
    <guid>https://yixuan.blog/cn/2009/10/my-heart-for-china</guid>
    <pubDate>2009-10-06T18:29:19+00:00</pubDate>
  </item>

  <item>
    <title>平生第一个PHP程序</title>
    <description>&lt;p&gt;一波未平，一波又起。刚把图片的居中和背景搞定，突然发现文章的自动摘要又出问题了。山寨版学院网站用的是WP Kit CN的插件，但不知为什么，字数控制总是不好使，不管怎么样都会把第一段整个显示出来，好像只能控制段落数而不能控制字数。我看COS用的也是这个插件，怎么就可以精确做到250个字呢？&lt;/p&gt;

&lt;p&gt;后来想研究一下插件的代码，但这对于我这个PHP盲来说实在太困难了，光找到那个起作用的函数就花了好长时间。之后半猜半蒙查函数库，算是大概知道怎么回事了，不过得到的结果有点让我吃惊。那个函数里面貌似是通过换行符号“\n”来划分“单词”的，也就是说如果内容本身的分段不多，那么这个函数就基本上没有什么效果了。&lt;/p&gt;

&lt;p&gt;我目前对摘要的一个想法是，如果文章有多段，那么摘要就只显示第一段；如果第一段太长，那么再精简句子。有了这个想法后我就开始硬着头皮把WP Kit CN插件的wp-kit-cn.class.php文件（摘要函数所在的文件）进行改写，最后把里面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WKC_excerpt()&lt;/code&gt;函数改成了下面的语句：&lt;/p&gt;

&lt;div class=&quot;language-php highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WKC_excerpt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;global&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;remove_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;the_excerpt&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;mul_excerpt&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;remove_filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;the_excerpt_rss&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;mul_excerpt&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$post&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;post_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply_filters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;the_content&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str_replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;]]&amp;gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;]]&amp;gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;strip_tags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;trim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$wordnum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;excerpt_words_number&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$firstpara&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;explode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$firstpara&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cut_str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$wordnum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$words&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;explode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;。&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$output&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;$output&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;。&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$output&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;……&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中有些函数和变量是来自这个文件的其它地方，要查看全部的代码的话可以到&lt;a href=&quot;http://wordpress.org/extend/plugins/wp-kit-cn/&quot;&gt;http://wordpress.org/extend/plugins/wp-kit-cn/&lt;/a&gt;去下载插件。这段程序的意思大概就是先提取文本的第一段，然后截取前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$wordnum&lt;/code&gt;（通过WP Kit CN的设置获取）个字符，再判断这段文字中有多少个句号，如果没有句号就全部返回，如果有就返回最后一个句号前的所有文本，最后再加上一个意味深长的省略号，就算是完成了。&lt;/p&gt;

&lt;p&gt;这段程序其实非常粗糙，首先是没有加上“阅读全文”之类的超链接，我这里是因为模版会在另外一个函数中提供，所以就删了，其实插件原来的文件中是有的；然后就是分句的条件太过简单，没有考虑问号感叹号之类的，不过感觉学院网站的新闻不太可能出现这些符号，所以就偷懒没改。&lt;/p&gt;

&lt;p&gt;不管怎么说吧，好歹是自己的第一个PHP程序，就在此纪念一下吧。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/10/my-first-php-program/</link>
    <guid>https://yixuan.blog/cn/2009/10/my-first-php-program</guid>
    <pubDate>2009-10-02T21:29:22+00:00</pubDate>
  </item>

  <item>
    <title>让Windows XP实现所有类型文件的包含文字搜索</title>
    <description>&lt;p&gt;最近几天都在弄WP的模版，虽然我一直是怀着无比崇敬的心情去研究那些免费提供的模版，但再好的模版总会有一些细节不合你的心意——倒不是因为功能太少了，而是因为功能太多以至于有些东西你不想放在页面上。当然了，这对于我这个htmlxmljavascriptphpcss的初级未入门者来说是个好消息——改不过还注释不过吗？&lt;/p&gt;

&lt;p&gt;于是乎目前的工作基本上就是找到恰当的模版的恰当的页面然后恰当地注释掉那些显示我不想显示的东西的函数，而我感觉在一大堆PHP语句中找到那些函数的定义简直就跟炼狱一般。最后实在不行就准备用Windows XP的包含文字搜索功能，在文件夹中递归搜索文件。但是曾经有一段时间怎么也搜不出来，后来才知道，原来Windows XP默认的情况是只搜索特定的几种文件类型，比如&lt;em&gt;.txt，而&lt;/em&gt;.php却不在其中。&lt;/p&gt;

&lt;p&gt;接着就是百度Google一齐上，找到了一个解决方案：&lt;a href=&quot;http://www.phpx.com/happy/thread-109195-1-1.html&quot;&gt;http://www.phpx.com/happy/thread-109195-1-1.html&lt;/a&gt;。我用的是第二种方法，感觉直观些，就是找到注册表中的HKEY_LOCAL_MACHINE\SYSTEM\ControlSet\Control\ContentIndex，然后把FilterFilesWithUnknownExtensions子键的值改为1。之后就可以搜索到php文件里面的内容了。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/10/enable-winxp-to-search-content-of-all-types-of-files/</link>
    <guid>https://yixuan.blog/cn/2009/10/enable-winxp-to-search-content-of-all-types-of-files</guid>
    <pubDate>2009-10-01T23:09:13+00:00</pubDate>
  </item>

  <item>
    <title>骧</title>
    <description>&lt;blockquote&gt;
  &lt;p&gt;骧，后右蹄白色的马。&lt;/p&gt;

  &lt;p&gt;后右足白骧。——《尔雅·释畜》&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;这种字不选作中文GRE词汇真是可惜了……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/09/horse-with-the-right-hind-foot-in-whit/</link>
    <guid>https://yixuan.blog/cn/2009/09/horse-with-the-right-hind-foot-in-whit</guid>
    <pubDate>2009-09-29T10:59:59+00:00</pubDate>
  </item>

  <item>
    <title>这个可以点</title>
    <description>&lt;p&gt;右上角的AA&amp;lt;&amp;gt;和最底下的“^”，模版作者都设计好了。&lt;/p&gt;

&lt;p&gt;嗯，没了。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/09/you-can-click-on-them/</link>
    <guid>https://yixuan.blog/cn/2009/09/you-can-click-on-them</guid>
    <pubDate>2009-09-27T23:54:52+00:00</pubDate>
  </item>

  <item>
    <title>折腾网站的好方法——XAMPP</title>
    <description>&lt;p&gt;刚刚学习WordPress少不了一番折腾，不过经常遇到的一个麻烦是担心不当的修改会破坏原有的网页。一个可选的做法是用一个备份镜像做测试，不过在网络上操作毕竟有些不便，比如用FTP传输文件有时候还是挺麻烦的。&lt;/p&gt;

&lt;p&gt;前段时间在&lt;a href=&quot;http://fairyfish.net/2007/06/25/installing-wordpress-locally/&quot;&gt;一个网站&lt;/a&gt;上看到一种方法是在本地安装一个网页服务器，然后把网站的所有文件都放到本地机上，通过网页服务器软件就可以在浏览器中访问这些内容。这样的好处就是在本地修改文件，就能在浏览器中及时看到修改的结果，无需再通过FTP进行增删改操作。&lt;/p&gt;

&lt;p&gt;实现的办法就是下载一个名叫XAMPP的软件，网址是&lt;a href=&quot;http://www.apachefriends.org/zh_cn/xampp.html&quot;&gt;http://www.apachefriends.org/zh_cn/xampp.html&lt;/a&gt;，之前那个网站上也有很详细的说明。我是在Ubuntu下用的，软件的官网也有安装步骤，基本上就是直接解压就行，不需要编译安装，非常方便。唯一要注意的是，在Linux下按照提示提示运行程序可能会提示下面的错误：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Syntax error: &quot;(&quot; unexpected
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;出现这种情况时，只需要找到XAMPP安装的目录，打开lampp这个文件，把第一行的&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#!/bin/sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;改成&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#!/bin/bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;就可以了。&lt;/p&gt;

&lt;p&gt;之后把网站文件塞到/opt/lampp（或其它的安装目录）/htdocs中就行了，比如就是wordpress文件夹。运行启动服务的命令后，在浏览器中输入http://localhost/wordpress（对应于放进去的文件夹名）就可以在本地安装、测试和折腾WordPress了。&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/09/use-xampp-to-test-website/</link>
    <guid>https://yixuan.blog/cn/2009/09/use-xampp-to-test-website</guid>
    <pubDate>2009-09-26T20:40:55+00:00</pubDate>
  </item>

  <item>
    <title>前言</title>
    <description>&lt;p&gt;一千八百年前，有一个人对另一个人说，我现在没有立身之地，你把荆州借给我吧，等我取下了西蜀就还你。&lt;/p&gt;

&lt;p&gt;一千八百年后，有一个人对另一个人说，我现在没有网络空间，你借一块地给我吧，等我买了自己的就还你。&lt;/p&gt;

&lt;p&gt;于是乎，在浩瀚的COS版图上，这一个人抢占了一个山头，自立为寇。&lt;/p&gt;

&lt;p&gt;这个人会不会成为一千八百年前的那个人，我不知道……&lt;/p&gt;
</description>
    <link>https://yixuan.blog/cn/2009/09/preface/</link>
    <guid>https://yixuan.blog/cn/2009/09/preface</guid>
    <pubDate>2009-09-21T11:01:18+00:00</pubDate>
  </item>


</channel>
</rss>
