7. 条件概率模拟器

功能简介:通过模拟抽卡片、掷骰子等实验,展示条件概率的计算方法,验证贝叶斯定理。支持调整实验参数,实时显示概率结果和理论值对比,帮助学生理解条件概率的概念。
在这里插入图片描述
ArkTS代码

@Entry
@Component
struct ConditionalProbability {
  @State private experiment: string = 'cards'
  @State private trials: number = 1000
  @State private eventA: number = 0
  @State private eventB: number = 0
  @State private eventAB: number = 0
  @State private result: string = ''

  build() {
    Column() {
      Text('🎲 条件概率模拟器')
        .fontSize(24).fontWeight(FontWeight.Bold)

      Row() {
        Button('抽卡片').onClick(() => this.experiment = 'cards')
        Button('掷骰子').onClick(() => this.experiment = 'dice')
        Button('抛硬币').onClick(() => this.experiment = 'coin')
      }

      Row() {
        Text('实验次数: ')
        Slider({ value: this.trials, min: 100, max: 10000 })
          .width(200)
          .onChange((val: number) => this.trials = val)
        Text(this.trials.toString())
      }

      Button('开始实验')
        .onClick(() => this.runExperiment())

      Text(this.result)
        .fontSize(16).fontColor('#2196F3')

      Text(`P(A) = ${(this.eventA / this.trials).toFixed(3)}, P(B) = ${(this.eventB / this.trials).toFixed(3)}`)
        .fontSize(14).fontColor('#666')
      Text(`P(AB) = ${(this.eventAB / this.trials).toFixed(3)}, P(A|B) = ${(this.eventAB / this.eventB).toFixed(3)}`)
        .fontSize(14).fontColor('#666')
    }.padding(20)
  }

  private runExperiment() {
    this.eventA = 0
    this.eventB = 0
    this.eventAB = 0

    for (let i = 0; i < this.trials; i++) {
      if (this.experiment === 'cards') {
        const card = Math.floor(Math.random() * 52) + 1
        const isRed = card <= 26
        const isAce = card % 13 === 1
        if (isRed) this.eventA++
        if (isAce) this.eventB++
        if (isRed && isAce) this.eventAB++
      } else if (this.experiment === 'dice') {
        const dice = Math.floor(Math.random() * 6) + 1
        const isEven = dice % 2 === 0
        const isGreaterThan3 = dice > 3
        if (isEven) this.eventA++
        if (isGreaterThan3) this.eventB++
        if (isEven && isGreaterThan3) this.eventAB++
      } else if (this.experiment === 'coin') {
        const coin1 = Math.random() > 0.5
        const coin2 = Math.random() > 0.5
        if (coin1) this.eventA++
        if (coin2) this.eventB++
        if (coin1 && coin2) this.eventAB++
      }
    }

    const pA = this.eventA / this.trials
    const pB = this.eventB / this.trials
    const pAB = this.eventAB / this.trials
    const pAgivenB = this.eventB > 0 ? this.eventAB / this.eventB : 0
    const pBgivenA = this.eventA > 0 ? this.eventAB / this.eventA : 0

    // 贝叶斯定理验证: P(B|A) = P(A|B) * P(B) / P(A)
    const bayesResult = this.eventA > 0 && pA > 0 ? (pAgivenB * pB) / pA : 0

    this.result = `实验完成: A事件发生${this.eventA}次,B事件发生${this.eventB}次,AB同时发生${this.eventAB}次\n` +
                  `贝叶斯定理验证: P(B|A) = ${pBgivenA.toFixed(3)}, 计算值 = ${bayesResult.toFixed(3)}`
  }
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐